Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

IDateField.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import java.util.Date;
  3. import com.google.gwt.user.client.ui.FlowPanel;
  4. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  5. import com.itmill.toolkit.terminal.gwt.client.DateTimeService;
  6. import com.itmill.toolkit.terminal.gwt.client.LocaleNotLoadedException;
  7. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  8. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  9. public class IDateField extends FlowPanel implements Paintable {
  10. public static final String CLASSNAME = "i-datefield";
  11. String id;
  12. ApplicationConnection client;
  13. protected boolean immediate;
  14. public static int RESOLUTION_YEAR = 0;
  15. public static int RESOLUTION_MONTH = 1;
  16. public static int RESOLUTION_DAY = 2;
  17. public static int RESOLUTION_HOUR = 3;
  18. public static int RESOLUTION_MIN = 4;
  19. public static int RESOLUTION_SEC = 5;
  20. public static int RESOLUTION_MSEC = 6;
  21. protected int currentResolution = RESOLUTION_YEAR;
  22. protected String currentLocale;
  23. protected boolean readonly;
  24. protected boolean enabled;
  25. protected Date date = null;
  26. protected DateTimeService dts;
  27. public IDateField() {
  28. setStyleName(CLASSNAME);
  29. dts = new DateTimeService();
  30. }
  31. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  32. // Ensure correct implementation and let layout manage caption
  33. if (client.updateComponent(this, uidl, true))
  34. return;
  35. // Save details
  36. this.client = client;
  37. this.id = uidl.getId();
  38. this.immediate = uidl.getBooleanAttribute("immediate");
  39. readonly = uidl.getBooleanAttribute("readonly");
  40. enabled = !uidl.getBooleanAttribute("disabled");
  41. if (uidl.hasAttribute("locale")) {
  42. String locale = uidl.getStringAttribute("locale");
  43. try {
  44. dts.setLocale(locale);
  45. currentLocale = locale;
  46. } catch (LocaleNotLoadedException e) {
  47. currentLocale = dts.getLocale();
  48. // TODO redirect this to console
  49. System.out.println("Tried to use an unloaded locale \""
  50. + locale + "\". Using default locale (" + currentLocale
  51. + ").");
  52. }
  53. }
  54. int newResolution;
  55. if (uidl.hasVariable("msec"))
  56. newResolution = RESOLUTION_MSEC;
  57. else if (uidl.hasVariable("sec"))
  58. newResolution = RESOLUTION_SEC;
  59. else if (uidl.hasVariable("min"))
  60. newResolution = RESOLUTION_MIN;
  61. else if (uidl.hasVariable("hour"))
  62. newResolution = RESOLUTION_HOUR;
  63. else if (uidl.hasVariable("day"))
  64. newResolution = RESOLUTION_DAY;
  65. else if (uidl.hasVariable("month"))
  66. newResolution = RESOLUTION_MONTH;
  67. else
  68. newResolution = RESOLUTION_YEAR;
  69. currentResolution = newResolution;
  70. int year = uidl.getIntVariable("year");
  71. int month = (currentResolution >= RESOLUTION_MONTH) ? uidl
  72. .getIntVariable("month") : -1;
  73. int day = (currentResolution >= RESOLUTION_DAY) ? uidl
  74. .getIntVariable("day") : -1;
  75. int hour = (currentResolution >= RESOLUTION_HOUR) ? uidl
  76. .getIntVariable("hour") : -1;
  77. int min = (currentResolution >= RESOLUTION_MIN) ? uidl
  78. .getIntVariable("min") : -1;
  79. int sec = (currentResolution >= RESOLUTION_SEC) ? uidl
  80. .getIntVariable("sec") : -1;
  81. int msec = (currentResolution >= RESOLUTION_MSEC) ? uidl
  82. .getIntVariable("msec") : -1;
  83. // Construct new date for this datefield (only if not null)
  84. if (year > -1)
  85. date = new Date((long) getTime(year, month, day, hour, min, sec,
  86. msec));
  87. }
  88. /*
  89. * We need this redundant native function because Java's Date object doesn't
  90. * have a setMilliseconds method.
  91. */
  92. private static native double getTime(int y, int m, int d, int h, int mi,
  93. int s, int ms) /*-{
  94. try {
  95. var date = new Date();
  96. if(y && y >= 0) date.setFullYear(y);
  97. if(m && m >= 1) date.setMonth(m-1);
  98. if(d && d >= 0) date.setDate(d);
  99. if(h && h >= 0) date.setHours(h);
  100. if(mi && mi >= 0) date.setMinutes(mi);
  101. if(s && s >= 0) date.setSeconds(s);
  102. if(ms && ms >= 0) date.setMilliseconds(ms);
  103. return date.getTime();
  104. } catch (e) {
  105. // TODO print some error message on the console
  106. //console.log(e);
  107. return (new Date()).getTime();
  108. }
  109. }-*/;
  110. public int getMilliseconds() {
  111. return (int) (date.getTime() - date.getTime() / 1000 * 1000);
  112. }
  113. public void setMilliseconds(int ms) {
  114. date.setTime(date.getTime() / 1000 * 1000 + ms);
  115. }
  116. public int getCurrentResolution() {
  117. return currentResolution;
  118. }
  119. public void setCurrentResolution(int currentResolution) {
  120. this.currentResolution = currentResolution;
  121. }
  122. public String getCurrentLocale() {
  123. return currentLocale;
  124. }
  125. public void setCurrentLocale(String currentLocale) {
  126. this.currentLocale = currentLocale;
  127. }
  128. public Date getCurrentDate() {
  129. return date;
  130. }
  131. public void setCurrentDate(Date date) {
  132. this.date = date;
  133. }
  134. public boolean isImmediate() {
  135. return immediate;
  136. }
  137. public boolean isReadonly() {
  138. return readonly;
  139. }
  140. public boolean isEnabled() {
  141. return enabled;
  142. }
  143. public DateTimeService getDateTimeService() {
  144. return dts;
  145. }
  146. public String getId() {
  147. return id;
  148. }
  149. public ApplicationConnection getClient() {
  150. return client;
  151. }
  152. }