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

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