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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Date;
  6. import com.google.gwt.user.client.Event;
  7. import com.google.gwt.user.client.ui.FlowPanel;
  8. import com.google.gwt.user.client.ui.Widget;
  9. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  10. import com.vaadin.terminal.gwt.client.DateTimeService;
  11. import com.vaadin.terminal.gwt.client.LocaleNotLoadedException;
  12. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  13. import com.vaadin.terminal.gwt.client.UIDL;
  14. import com.vaadin.terminal.gwt.client.VConsole;
  15. import com.vaadin.terminal.gwt.client.VTooltip;
  16. public class VDateField extends FlowPanel implements VPaintableWidget, Field {
  17. public static final String CLASSNAME = "v-datefield";
  18. private String id;
  19. private ApplicationConnection client;
  20. protected boolean immediate;
  21. public static final int RESOLUTION_YEAR = 1;
  22. public static final int RESOLUTION_MONTH = 2;
  23. public static final int RESOLUTION_DAY = 4;
  24. public static final int RESOLUTION_HOUR = 8;
  25. public static final int RESOLUTION_MIN = 16;
  26. public static final int RESOLUTION_SEC = 32;
  27. public static final String WEEK_NUMBERS = "wn";
  28. static String resolutionToString(int res) {
  29. if (res > RESOLUTION_DAY) {
  30. return "full";
  31. }
  32. if (res == RESOLUTION_DAY) {
  33. return "day";
  34. }
  35. if (res == RESOLUTION_MONTH) {
  36. return "month";
  37. }
  38. return "year";
  39. }
  40. protected int currentResolution = RESOLUTION_YEAR;
  41. protected String currentLocale;
  42. protected boolean readonly;
  43. protected boolean enabled;
  44. /**
  45. * The date that is selected in the date field. Null if an invalid date is
  46. * specified.
  47. */
  48. private Date date = null;
  49. protected DateTimeService dts;
  50. private boolean showISOWeekNumbers = false;
  51. public VDateField() {
  52. setStyleName(CLASSNAME);
  53. dts = new DateTimeService();
  54. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  55. }
  56. @Override
  57. public void onBrowserEvent(Event event) {
  58. super.onBrowserEvent(event);
  59. if (client != null) {
  60. client.handleTooltipEvent(event, this);
  61. }
  62. }
  63. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  64. // Ensure correct implementation and let layout manage caption
  65. if (client.updateComponent(this, uidl, true)) {
  66. return;
  67. }
  68. // Save details
  69. this.client = client;
  70. id = uidl.getId();
  71. immediate = uidl.getBooleanAttribute("immediate");
  72. readonly = uidl.getBooleanAttribute("readonly");
  73. enabled = !uidl.getBooleanAttribute("disabled");
  74. if (uidl.hasAttribute("locale")) {
  75. final String locale = uidl.getStringAttribute("locale");
  76. try {
  77. dts.setLocale(locale);
  78. currentLocale = locale;
  79. } catch (final LocaleNotLoadedException e) {
  80. currentLocale = dts.getLocale();
  81. VConsole.error("Tried to use an unloaded locale \"" + locale
  82. + "\". Using default locale (" + currentLocale + ").");
  83. VConsole.error(e);
  84. }
  85. }
  86. // We show week numbers only if the week starts with Monday, as ISO 8601
  87. // specifies
  88. showISOWeekNumbers = uidl.getBooleanAttribute(WEEK_NUMBERS)
  89. && dts.getFirstDayOfWeek() == 1;
  90. int newResolution;
  91. if (uidl.hasVariable("sec")) {
  92. newResolution = RESOLUTION_SEC;
  93. } else if (uidl.hasVariable("min")) {
  94. newResolution = RESOLUTION_MIN;
  95. } else if (uidl.hasVariable("hour")) {
  96. newResolution = RESOLUTION_HOUR;
  97. } else if (uidl.hasVariable("day")) {
  98. newResolution = RESOLUTION_DAY;
  99. } else if (uidl.hasVariable("month")) {
  100. newResolution = RESOLUTION_MONTH;
  101. } else {
  102. newResolution = RESOLUTION_YEAR;
  103. }
  104. currentResolution = newResolution;
  105. // Add stylename that indicates current resolution
  106. addStyleName(CLASSNAME + "-" + resolutionToString(currentResolution));
  107. final int year = uidl.getIntVariable("year");
  108. final int month = (currentResolution >= RESOLUTION_MONTH) ? uidl
  109. .getIntVariable("month") : -1;
  110. final int day = (currentResolution >= RESOLUTION_DAY) ? uidl
  111. .getIntVariable("day") : -1;
  112. final int hour = (currentResolution >= RESOLUTION_HOUR) ? uidl
  113. .getIntVariable("hour") : 0;
  114. final int min = (currentResolution >= RESOLUTION_MIN) ? uidl
  115. .getIntVariable("min") : 0;
  116. final int sec = (currentResolution >= RESOLUTION_SEC) ? uidl
  117. .getIntVariable("sec") : 0;
  118. // Construct new date for this datefield (only if not null)
  119. if (year > -1) {
  120. setCurrentDate(new Date((long) getTime(year, month, day, hour, min,
  121. sec, 0)));
  122. } else {
  123. setCurrentDate(null);
  124. }
  125. }
  126. /*
  127. * We need this redundant native function because Java's Date object doesn't
  128. * have a setMilliseconds method.
  129. */
  130. private static native double getTime(int y, int m, int d, int h, int mi,
  131. int s, int ms)
  132. /*-{
  133. try {
  134. var date = new Date(2000,1,1,1); // don't use current date here
  135. if(y && y >= 0) date.setFullYear(y);
  136. if(m && m >= 1) date.setMonth(m-1);
  137. if(d && d >= 0) date.setDate(d);
  138. if(h >= 0) date.setHours(h);
  139. if(mi >= 0) date.setMinutes(mi);
  140. if(s >= 0) date.setSeconds(s);
  141. if(ms >= 0) date.setMilliseconds(ms);
  142. return date.getTime();
  143. } catch (e) {
  144. // TODO print some error message on the console
  145. //console.log(e);
  146. return (new Date()).getTime();
  147. }
  148. }-*/;
  149. public int getMilliseconds() {
  150. return DateTimeService.getMilliseconds(date);
  151. }
  152. public void setMilliseconds(int ms) {
  153. DateTimeService.setMilliseconds(date, ms);
  154. }
  155. public int getCurrentResolution() {
  156. return currentResolution;
  157. }
  158. public void setCurrentResolution(int currentResolution) {
  159. this.currentResolution = currentResolution;
  160. }
  161. public String getCurrentLocale() {
  162. return currentLocale;
  163. }
  164. public void setCurrentLocale(String currentLocale) {
  165. this.currentLocale = currentLocale;
  166. }
  167. public Date getCurrentDate() {
  168. return date;
  169. }
  170. public void setCurrentDate(Date date) {
  171. this.date = date;
  172. }
  173. public boolean isImmediate() {
  174. return immediate;
  175. }
  176. public boolean isReadonly() {
  177. return readonly;
  178. }
  179. public boolean isEnabled() {
  180. return enabled;
  181. }
  182. public DateTimeService getDateTimeService() {
  183. return dts;
  184. }
  185. public String getId() {
  186. return id;
  187. }
  188. public ApplicationConnection getClient() {
  189. return client;
  190. }
  191. /**
  192. * Returns whether ISO 8601 week numbers should be shown in the date
  193. * selector or not. ISO 8601 defines that a week always starts with a Monday
  194. * so the week numbers are only shown if this is the case.
  195. *
  196. * @return true if week number should be shown, false otherwise
  197. */
  198. public boolean isShowISOWeekNumbers() {
  199. return showISOWeekNumbers;
  200. }
  201. /**
  202. * Returns a copy of the current date. Modifying the returned date will not
  203. * modify the value of this VDateField. Use {@link #setDate(Date)} to change
  204. * the current date.
  205. *
  206. * @return A copy of the current date
  207. */
  208. protected Date getDate() {
  209. Date current = getCurrentDate();
  210. if (current == null) {
  211. return null;
  212. } else {
  213. return (Date) getCurrentDate().clone();
  214. }
  215. }
  216. /**
  217. * Sets the current date for this VDateField.
  218. *
  219. * @param date
  220. * The new date to use
  221. */
  222. protected void setDate(Date date) {
  223. this.date = date;
  224. }
  225. public Widget getWidgetForPaintable() {
  226. return this;
  227. }
  228. }