You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VDateField.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.Date;
  18. import com.google.gwt.user.client.ui.FlowPanel;
  19. import com.google.gwt.user.client.ui.HasEnabled;
  20. import com.vaadin.client.ApplicationConnection;
  21. import com.vaadin.client.DateTimeService;
  22. import com.vaadin.shared.ui.datefield.Resolution;
  23. public class VDateField extends FlowPanel implements Field, HasEnabled {
  24. public static final String CLASSNAME = "v-datefield";
  25. /** For internal use only. May be removed or replaced in the future. */
  26. public String paintableId;
  27. /** For internal use only. May be removed or replaced in the future. */
  28. public ApplicationConnection client;
  29. /** For internal use only. May be removed or replaced in the future. */
  30. public boolean immediate;
  31. @Deprecated
  32. public static final Resolution RESOLUTION_YEAR = Resolution.YEAR;
  33. @Deprecated
  34. public static final Resolution RESOLUTION_MONTH = Resolution.MONTH;
  35. @Deprecated
  36. public static final Resolution RESOLUTION_DAY = Resolution.DAY;
  37. @Deprecated
  38. public static final Resolution RESOLUTION_HOUR = Resolution.HOUR;
  39. @Deprecated
  40. public static final Resolution RESOLUTION_MIN = Resolution.MINUTE;
  41. @Deprecated
  42. public static final Resolution RESOLUTION_SEC = Resolution.SECOND;
  43. /** For internal use only. May be removed or replaced in the future. */
  44. public static String resolutionToString(Resolution res) {
  45. if (res.getCalendarField() > Resolution.DAY.getCalendarField()) {
  46. return "full";
  47. }
  48. if (res == Resolution.DAY) {
  49. return "day";
  50. }
  51. if (res == Resolution.MONTH) {
  52. return "month";
  53. }
  54. return "year";
  55. }
  56. protected Resolution currentResolution = Resolution.YEAR;
  57. protected String currentLocale;
  58. protected boolean readonly;
  59. protected boolean enabled;
  60. /**
  61. * The date that is selected in the date field. Null if an invalid date is
  62. * specified.
  63. */
  64. private Date date = null;
  65. /** For internal use only. May be removed or replaced in the future. */
  66. public DateTimeService dts;
  67. protected boolean showISOWeekNumbers = false;
  68. public VDateField() {
  69. setStyleName(CLASSNAME);
  70. dts = new DateTimeService();
  71. }
  72. /**
  73. * We need this redundant native function because Java's Date object doesn't
  74. * have a setMilliseconds method.
  75. * <p>
  76. * For internal use only. May be removed or replaced in the future.
  77. */
  78. public static native double getTime(int y, int m, int d, int h, int mi,
  79. int s, int ms)
  80. /*-{
  81. try {
  82. var date = new Date(2000,1,1,1); // don't use current date here
  83. if(y && y >= 0) date.setFullYear(y);
  84. if(m && m >= 1) date.setMonth(m-1);
  85. if(d && d >= 0) date.setDate(d);
  86. if(h >= 0) date.setHours(h);
  87. if(mi >= 0) date.setMinutes(mi);
  88. if(s >= 0) date.setSeconds(s);
  89. if(ms >= 0) date.setMilliseconds(ms);
  90. return date.getTime();
  91. } catch (e) {
  92. // TODO print some error message on the console
  93. //console.log(e);
  94. return (new Date()).getTime();
  95. }
  96. }-*/;
  97. public int getMilliseconds() {
  98. return DateTimeService.getMilliseconds(date);
  99. }
  100. public void setMilliseconds(int ms) {
  101. DateTimeService.setMilliseconds(date, ms);
  102. }
  103. public Resolution getCurrentResolution() {
  104. return currentResolution;
  105. }
  106. public void setCurrentResolution(Resolution currentResolution) {
  107. this.currentResolution = currentResolution;
  108. }
  109. public String getCurrentLocale() {
  110. return currentLocale;
  111. }
  112. public void setCurrentLocale(String currentLocale) {
  113. this.currentLocale = currentLocale;
  114. }
  115. public Date getCurrentDate() {
  116. return date;
  117. }
  118. public void setCurrentDate(Date date) {
  119. this.date = date;
  120. }
  121. public boolean isImmediate() {
  122. return immediate;
  123. }
  124. public void setImmediate(boolean immediate) {
  125. this.immediate = immediate;
  126. }
  127. public boolean isReadonly() {
  128. return readonly;
  129. }
  130. public void setReadonly(boolean readonly) {
  131. this.readonly = readonly;
  132. }
  133. @Override
  134. public boolean isEnabled() {
  135. return enabled;
  136. }
  137. @Override
  138. public void setEnabled(boolean enabled) {
  139. this.enabled = enabled;
  140. }
  141. public DateTimeService getDateTimeService() {
  142. return dts;
  143. }
  144. public String getId() {
  145. return paintableId;
  146. }
  147. public ApplicationConnection getClient() {
  148. return client;
  149. }
  150. /**
  151. * Returns whether ISO 8601 week numbers should be shown in the date
  152. * selector or not. ISO 8601 defines that a week always starts with a Monday
  153. * so the week numbers are only shown if this is the case.
  154. *
  155. * @return true if week number should be shown, false otherwise
  156. */
  157. public boolean isShowISOWeekNumbers() {
  158. return showISOWeekNumbers;
  159. }
  160. public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
  161. this.showISOWeekNumbers = showISOWeekNumbers;
  162. }
  163. /**
  164. * Returns a copy of the current date. Modifying the returned date will not
  165. * modify the value of this VDateField. Use {@link #setDate(Date)} to change
  166. * the current date.
  167. * <p>
  168. * For internal use only. May be removed or replaced in the future.
  169. *
  170. * @return A copy of the current date
  171. */
  172. public Date getDate() {
  173. Date current = getCurrentDate();
  174. if (current == null) {
  175. return null;
  176. } else {
  177. return (Date) getCurrentDate().clone();
  178. }
  179. }
  180. /**
  181. * Sets the current date for this VDateField.
  182. *
  183. * @param date
  184. * The new date to use
  185. */
  186. protected void setDate(Date date) {
  187. this.date = date;
  188. }
  189. }