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.

ITextualDate.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import com.google.gwt.user.client.Timer;
  3. import com.google.gwt.user.client.ui.ChangeListener;
  4. import com.google.gwt.user.client.ui.Widget;
  5. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  6. import com.itmill.toolkit.terminal.gwt.client.DateLocale;
  7. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  8. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  9. import com.itmill.toolkit.terminal.gwt.client.util.SimpleDateFormat;
  10. public class ITextualDate extends IDateField implements Paintable,
  11. ChangeListener {
  12. private ITextField text;
  13. private SimpleDateFormat format;
  14. private DateLocale dl;
  15. public ITextualDate() {
  16. super();
  17. text = new ITextField();
  18. text.addChangeListener(this);
  19. add(text);
  20. }
  21. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  22. super.updateFromUIDL(uidl, client);
  23. buildDate();
  24. }
  25. public void buildDate() {
  26. dl = new DateLocale();
  27. DateLocale.setLocale(currentLocale);
  28. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_YEAR;
  29. if (currentResolution == IDateField.RESOLUTION_MONTH)
  30. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_MONTH;
  31. else if (currentResolution >= IDateField.RESOLUTION_DAY)
  32. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_DAY;
  33. format = new SimpleDateFormat(cleanFormat(dts.getDateFormat()));
  34. format.setLocale(dl);
  35. // Size the textfield a bit smaller if no clock time is needed
  36. if (currentResolution <= IDateField.RESOLUTION_DAY)
  37. text.setColumns(12);
  38. // Create the initial text for the textfield
  39. String dateText = "";
  40. if (date != null) {
  41. dateText = format.format(date);
  42. if (currentResolution >= IDateField.RESOLUTION_HOUR) {
  43. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_ALL;
  44. int h = date.getHours();
  45. if (h > 11 && dts.isTwelveHourClock())
  46. h -= 12;
  47. int m = currentResolution > IDateField.RESOLUTION_HOUR ? date
  48. .getMinutes() : 0;
  49. dateText += " " + (h < 10 ? "0" + h : "" + h)
  50. + dts.getClockDelimeter() + (m < 10 ? "0" + m : "" + m);
  51. }
  52. if (currentResolution >= IDateField.RESOLUTION_SEC) {
  53. int s = date.getSeconds();
  54. dateText += dts.getClockDelimeter()
  55. + (s < 10 ? "0" + s : "" + s);
  56. }
  57. if (currentResolution == IDateField.RESOLUTION_MSEC) {
  58. int ms = getMilliseconds();
  59. String text = "" + ms;
  60. if (ms < 10)
  61. text = "00" + text;
  62. else if (ms < 100)
  63. text = "0" + text;
  64. dateText += "." + text;
  65. }
  66. if (currentResolution >= IDateField.RESOLUTION_HOUR
  67. && dts.isTwelveHourClock())
  68. dateText += " "
  69. + (date.getHours() < 12 ? dts.getAmPmStrings()[0] : dts
  70. .getAmPmStrings()[1]);
  71. }
  72. text.setText(dateText);
  73. text.setEnabled(enabled && !readonly);
  74. if (readonly)
  75. text.addStyleName("i-readonly");
  76. else
  77. text.removeStyleName("i-readonly");
  78. }
  79. public void onChange(Widget sender) {
  80. if (sender == text) {
  81. if (!text.getText().equals("")) {
  82. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_ALL;
  83. if (currentResolution == IDateField.RESOLUTION_YEAR)
  84. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_YEAR;
  85. else if (currentResolution == IDateField.RESOLUTION_MONTH)
  86. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_MONTH;
  87. else if (currentResolution == IDateField.RESOLUTION_DAY)
  88. DateLocale.SUPPORTED_DF_TOKENS = DateLocale.TOKENS_RESOLUTION_DAY;
  89. String f = cleanFormat(dts.getDateFormat());
  90. if (currentResolution >= IDateField.RESOLUTION_HOUR)
  91. f += " "
  92. + (dts.isTwelveHourClock() ? DateLocale.TOKEN_HOUR_12
  93. + DateLocale.TOKEN_HOUR_12
  94. : DateLocale.TOKEN_HOUR_24
  95. + DateLocale.TOKEN_HOUR_24)
  96. + dts.getClockDelimeter() + DateLocale.TOKEN_MINUTE
  97. + DateLocale.TOKEN_MINUTE;
  98. if (currentResolution >= IDateField.RESOLUTION_SEC)
  99. f += dts.getClockDelimeter() + DateLocale.TOKEN_SECOND
  100. + DateLocale.TOKEN_SECOND;
  101. if (currentResolution == IDateField.RESOLUTION_MSEC)
  102. f += "." + DateLocale.TOKEN_MILLISECOND
  103. + DateLocale.TOKEN_MILLISECOND
  104. + DateLocale.TOKEN_MILLISECOND;
  105. if (currentResolution >= IDateField.RESOLUTION_HOUR
  106. && dts.isTwelveHourClock())
  107. f += " " + DateLocale.TOKEN_AM_PM;
  108. format = new SimpleDateFormat(f);
  109. DateLocale.setLocale(currentLocale);
  110. format.setLocale(dl);
  111. try {
  112. date = format.parse(text.getText());
  113. } catch (Exception e) {
  114. ApplicationConnection.getConsole().log(e.getMessage());
  115. text.addStyleName(ITextField.CLASSNAME + "-error");
  116. Timer t = new Timer() {
  117. public void run() {
  118. text.removeStyleName(ITextField.CLASSNAME
  119. + "-error");
  120. }
  121. };
  122. t.schedule(2000);
  123. return;
  124. }
  125. } else
  126. date = null;
  127. // Update variables
  128. // (only the smallest defining resolution needs to be immediate)
  129. client.updateVariable(id, "year",
  130. date != null ? date.getYear() + 1900 : -1,
  131. currentResolution == IDateField.RESOLUTION_YEAR
  132. && immediate);
  133. if (currentResolution >= IDateField.RESOLUTION_MONTH)
  134. client.updateVariable(id, "month", date != null ? date
  135. .getMonth() + 1 : -1,
  136. currentResolution == IDateField.RESOLUTION_MONTH
  137. && immediate);
  138. if (currentResolution >= IDateField.RESOLUTION_DAY)
  139. client.updateVariable(id, "day", date != null ? date.getDate()
  140. : -1, currentResolution == IDateField.RESOLUTION_DAY
  141. && immediate);
  142. if (currentResolution >= IDateField.RESOLUTION_HOUR)
  143. client.updateVariable(id, "hour", date != null ? date
  144. .getHours() : -1,
  145. currentResolution == IDateField.RESOLUTION_HOUR
  146. && immediate);
  147. if (currentResolution >= IDateField.RESOLUTION_MIN)
  148. client.updateVariable(id, "min", date != null ? date
  149. .getMinutes() : -1,
  150. currentResolution == IDateField.RESOLUTION_MIN
  151. && immediate);
  152. if (currentResolution >= IDateField.RESOLUTION_SEC)
  153. client.updateVariable(id, "sec", date != null ? date
  154. .getSeconds() : -1,
  155. currentResolution == IDateField.RESOLUTION_SEC
  156. && immediate);
  157. if (currentResolution == IDateField.RESOLUTION_MSEC)
  158. client.updateVariable(id, "msec",
  159. date != null ? getMilliseconds() : -1, immediate);
  160. buildDate();
  161. }
  162. }
  163. private String cleanFormat(String format) {
  164. // Remove unnecessary d & M if resolution is too low
  165. if (currentResolution < IDateField.RESOLUTION_DAY)
  166. format = format.replaceAll("d", "");
  167. if (currentResolution < IDateField.RESOLUTION_MONTH)
  168. format = format.replaceAll("M", "");
  169. // Remove unsupported patterns
  170. // TODO support for 'G', era designator (used at least in Japan)
  171. format = format.replaceAll("[GzZwWkK]", "");
  172. // Remove extra delimiters ('/' and '.')
  173. while (format.startsWith("/") || format.startsWith(".")
  174. || format.startsWith("-"))
  175. format = format.substring(1);
  176. while (format.endsWith("/") || format.endsWith(".")
  177. || format.endsWith("-"))
  178. format = format.substring(0, format.length() - 1);
  179. // Remove duplicate delimiters
  180. format = format.replaceAll("//", "/");
  181. format = format.replaceAll("\\.\\.", ".");
  182. format = format.replaceAll("--", "-");
  183. return format.trim();
  184. }
  185. }