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

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