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.

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