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.4KB

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