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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  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.vaadin.terminal.gwt.client.ApplicationConnection;
  9. import com.vaadin.terminal.gwt.client.ClientExceptionHandler;
  10. import com.vaadin.terminal.gwt.client.DateTimeService;
  11. import com.vaadin.terminal.gwt.client.VTooltip;
  12. import com.vaadin.terminal.gwt.client.LocaleNotLoadedException;
  13. import com.vaadin.terminal.gwt.client.Paintable;
  14. import com.vaadin.terminal.gwt.client.UIDL;
  15. public class VDateField extends FlowPanel implements Paintable, Field {
  16. public static final String CLASSNAME = "i-datefield";
  17. protected String id;
  18. protected ApplicationConnection client;
  19. protected boolean immediate;
  20. public static final int RESOLUTION_YEAR = 0;
  21. public static final int RESOLUTION_MONTH = 1;
  22. public static final int RESOLUTION_DAY = 2;
  23. public static final int RESOLUTION_HOUR = 3;
  24. public static final int RESOLUTION_MIN = 4;
  25. public static final int RESOLUTION_SEC = 5;
  26. public static final int RESOLUTION_MSEC = 6;
  27. protected int currentResolution = RESOLUTION_YEAR;
  28. protected String currentLocale;
  29. protected boolean readonly;
  30. protected boolean enabled;
  31. protected Date date = null;
  32. // e.g when paging a calendar, before actually selecting
  33. protected Date showingDate = new Date();
  34. protected DateTimeService dts;
  35. public VDateField() {
  36. setStyleName(CLASSNAME);
  37. dts = new DateTimeService();
  38. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  39. }
  40. @Override
  41. public void onBrowserEvent(Event event) {
  42. super.onBrowserEvent(event);
  43. if (client != null) {
  44. client.handleTooltipEvent(event, this);
  45. }
  46. }
  47. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  48. // Ensure correct implementation and let layout manage caption
  49. if (client.updateComponent(this, uidl, true)) {
  50. return;
  51. }
  52. // Save details
  53. this.client = client;
  54. id = uidl.getId();
  55. immediate = uidl.getBooleanAttribute("immediate");
  56. readonly = uidl.getBooleanAttribute("readonly");
  57. enabled = !uidl.getBooleanAttribute("disabled");
  58. if (uidl.hasAttribute("locale")) {
  59. final String locale = uidl.getStringAttribute("locale");
  60. try {
  61. dts.setLocale(locale);
  62. currentLocale = locale;
  63. } catch (final LocaleNotLoadedException e) {
  64. currentLocale = dts.getLocale();
  65. ClientExceptionHandler.displayError(
  66. "Tried to use an unloaded locale \"" + locale
  67. + "\". Using default locale (" + currentLocale
  68. + ").", e);
  69. }
  70. }
  71. int newResolution;
  72. if (uidl.hasVariable("msec")) {
  73. newResolution = RESOLUTION_MSEC;
  74. } else if (uidl.hasVariable("sec")) {
  75. newResolution = RESOLUTION_SEC;
  76. } else if (uidl.hasVariable("min")) {
  77. newResolution = RESOLUTION_MIN;
  78. } else if (uidl.hasVariable("hour")) {
  79. newResolution = RESOLUTION_HOUR;
  80. } else if (uidl.hasVariable("day")) {
  81. newResolution = RESOLUTION_DAY;
  82. } else if (uidl.hasVariable("month")) {
  83. newResolution = RESOLUTION_MONTH;
  84. } else {
  85. newResolution = RESOLUTION_YEAR;
  86. }
  87. currentResolution = newResolution;
  88. final int year = uidl.getIntVariable("year");
  89. final int month = (currentResolution >= RESOLUTION_MONTH) ? uidl
  90. .getIntVariable("month") : -1;
  91. final int day = (currentResolution >= RESOLUTION_DAY) ? uidl
  92. .getIntVariable("day") : -1;
  93. final int hour = (currentResolution >= RESOLUTION_HOUR) ? uidl
  94. .getIntVariable("hour") : 0;
  95. final int min = (currentResolution >= RESOLUTION_MIN) ? uidl
  96. .getIntVariable("min") : 0;
  97. final int sec = (currentResolution >= RESOLUTION_SEC) ? uidl
  98. .getIntVariable("sec") : 0;
  99. final int msec = (currentResolution >= RESOLUTION_MSEC) ? uidl
  100. .getIntVariable("msec") : 0;
  101. // Construct new date for this datefield (only if not null)
  102. if (year > -1) {
  103. date = new Date((long) getTime(year, month, day, hour, min, sec,
  104. msec));
  105. showingDate.setTime(date.getTime());
  106. } else {
  107. date = null;
  108. showingDate = new Date();
  109. }
  110. }
  111. /*
  112. * We need this redundant native function because Java's Date object doesn't
  113. * have a setMilliseconds method.
  114. */
  115. private static native double getTime(int y, int m, int d, int h, int mi,
  116. int s, int ms)
  117. /*-{
  118. try {
  119. var date = new Date(2000,1,1,1); // don't use current date here
  120. if(y && y >= 0) date.setFullYear(y);
  121. if(m && m >= 1) date.setMonth(m-1);
  122. if(d && d >= 0) date.setDate(d);
  123. if(h >= 0) date.setHours(h);
  124. if(mi >= 0) date.setMinutes(mi);
  125. if(s >= 0) date.setSeconds(s);
  126. if(ms >= 0) date.setMilliseconds(ms);
  127. return date.getTime();
  128. } catch (e) {
  129. // TODO print some error message on the console
  130. //console.log(e);
  131. return (new Date()).getTime();
  132. }
  133. }-*/;
  134. public int getMilliseconds() {
  135. return (int) (date.getTime() - date.getTime() / 1000 * 1000);
  136. }
  137. public void setMilliseconds(int ms) {
  138. date.setTime(date.getTime() / 1000 * 1000 + ms);
  139. }
  140. public int getShowingMilliseconds() {
  141. return (int) (showingDate.getTime() - showingDate.getTime() / 1000 * 1000);
  142. }
  143. public void setShowingMilliseconds(int ms) {
  144. showingDate.setTime(showingDate.getTime() / 1000 * 1000 + ms);
  145. }
  146. public int getCurrentResolution() {
  147. return currentResolution;
  148. }
  149. public void setCurrentResolution(int currentResolution) {
  150. this.currentResolution = currentResolution;
  151. }
  152. public String getCurrentLocale() {
  153. return currentLocale;
  154. }
  155. public void setCurrentLocale(String currentLocale) {
  156. this.currentLocale = currentLocale;
  157. }
  158. public Date getCurrentDate() {
  159. return date;
  160. }
  161. public void setCurrentDate(Date date) {
  162. this.date = date;
  163. }
  164. public Date getShowingDate() {
  165. return showingDate;
  166. }
  167. public void setShowingDate(Date date) {
  168. showingDate = date;
  169. }
  170. public boolean isImmediate() {
  171. return immediate;
  172. }
  173. public boolean isReadonly() {
  174. return readonly;
  175. }
  176. public boolean isEnabled() {
  177. return enabled;
  178. }
  179. public DateTimeService getDateTimeService() {
  180. return dts;
  181. }
  182. public String getId() {
  183. return id;
  184. }
  185. public ApplicationConnection getClient() {
  186. return client;
  187. }
  188. }