Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VPopupTimeCalendar.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.Date;
  18. import java.util.Map;
  19. import com.google.gwt.core.client.GWT;
  20. import com.vaadin.client.LocaleNotLoadedException;
  21. import com.vaadin.client.LocaleService;
  22. import com.vaadin.client.VConsole;
  23. import com.vaadin.shared.ui.datefield.DateTimeResolution;
  24. /**
  25. * Represents a date-time selection component with a text field and a popup date
  26. * selector.
  27. *
  28. * @author Vaadin Ltd
  29. *
  30. * @since 8.0
  31. */
  32. public class VPopupTimeCalendar extends
  33. VAbstractPopupCalendar<VDateTimeCalendarPanel, DateTimeResolution> {
  34. public VPopupTimeCalendar() {
  35. super(GWT.create(VDateTimeCalendarPanel.class),
  36. DateTimeResolution.MINUTE);
  37. }
  38. @Override
  39. protected DateTimeResolution[] doGetResolutions() {
  40. return DateTimeResolution.values();
  41. }
  42. @Override
  43. public String resolutionAsString() {
  44. if (getCurrentResolution().compareTo(DateTimeResolution.DAY) >= 0) {
  45. return getResolutionVariable(getCurrentResolution());
  46. } else {
  47. return "full";
  48. }
  49. }
  50. @Override
  51. public void setCurrentResolution(DateTimeResolution resolution) {
  52. super.setCurrentResolution(
  53. resolution == null ? DateTimeResolution.MINUTE : resolution);
  54. }
  55. public static Date makeDate(Map<DateTimeResolution, Integer> dateValues) {
  56. if (dateValues.get(DateTimeResolution.YEAR) == null) {
  57. return null;
  58. }
  59. Date date = new Date(2000 - 1900, 0, 1);
  60. Integer year = dateValues.get(DateTimeResolution.YEAR);
  61. if (year != null) {
  62. date.setYear(year - 1900);
  63. }
  64. Integer month = dateValues.get(DateTimeResolution.MONTH);
  65. if (month != null) {
  66. date.setMonth(month - 1);
  67. }
  68. Integer day = dateValues.get(DateTimeResolution.DAY);
  69. if (day != null) {
  70. date.setDate(day);
  71. }
  72. Integer hour = dateValues.get(DateTimeResolution.HOUR);
  73. if (hour != null) {
  74. date.setHours(hour);
  75. }
  76. Integer minute = dateValues.get(DateTimeResolution.MINUTE);
  77. if (minute != null) {
  78. date.setMinutes(minute);
  79. }
  80. Integer second = dateValues.get(DateTimeResolution.SECOND);
  81. if (second != null) {
  82. date.setSeconds(second);
  83. }
  84. return date;
  85. }
  86. @Override
  87. public boolean isYear(DateTimeResolution resolution) {
  88. return DateTimeResolution.YEAR.equals(resolution);
  89. }
  90. @Override
  91. protected Date getDate(Map<DateTimeResolution, Integer> dateValues) {
  92. return makeDate(dateValues);
  93. }
  94. @Override
  95. protected void updateDateVariables() {
  96. super.updateDateVariables();
  97. DateTimeResolution resolution = getCurrentResolution();
  98. // (only the smallest defining resolution needs to be
  99. // immediate)
  100. Date currentDate = getDate();
  101. if (resolution.compareTo(DateTimeResolution.MONTH) <= 0) {
  102. addBufferedResolution(DateTimeResolution.MONTH,
  103. currentDate != null ? currentDate.getMonth() + 1 : null);
  104. }
  105. if (resolution.compareTo(DateTimeResolution.DAY) <= 0) {
  106. addBufferedResolution(DateTimeResolution.DAY,
  107. currentDate != null ? currentDate.getDate() : null);
  108. }
  109. if (resolution.compareTo(DateTimeResolution.HOUR) <= 0) {
  110. addBufferedResolution(DateTimeResolution.HOUR,
  111. currentDate != null ? currentDate.getHours() : null);
  112. }
  113. if (resolution.compareTo(DateTimeResolution.MINUTE) <= 0) {
  114. addBufferedResolution(DateTimeResolution.MINUTE,
  115. currentDate != null ? currentDate.getMinutes() : null);
  116. }
  117. if (resolution.compareTo(DateTimeResolution.SECOND) <= 0) {
  118. addBufferedResolution(DateTimeResolution.SECOND,
  119. currentDate != null ? currentDate.getSeconds() : null);
  120. }
  121. sendBufferedValues();
  122. }
  123. private void addBufferedResolution(DateTimeResolution resolutionToAdd,
  124. Integer value) {
  125. bufferedResolutions.put(resolutionToAdd.name(), value);
  126. }
  127. @Override
  128. @SuppressWarnings("deprecation")
  129. public void updateValue(Date newDate) {
  130. Date currentDate = getCurrentDate();
  131. super.updateValue(newDate);
  132. DateTimeResolution resolution = getCurrentResolution();
  133. if (currentDate == null || newDate.getTime() != currentDate.getTime()) {
  134. if (resolution.compareTo(DateTimeResolution.DAY) < 0) {
  135. bufferedResolutions.put(DateTimeResolution.HOUR.name(),
  136. newDate.getHours());
  137. if (resolution.compareTo(DateTimeResolution.HOUR) < 0) {
  138. bufferedResolutions.put(DateTimeResolution.MINUTE.name(),
  139. newDate.getMinutes());
  140. if (resolution.compareTo(DateTimeResolution.MINUTE) < 0) {
  141. bufferedResolutions.put(
  142. DateTimeResolution.SECOND.name(),
  143. newDate.getSeconds());
  144. }
  145. }
  146. }
  147. }
  148. }
  149. @Override
  150. protected String createFormatString() {
  151. if (isYear(getCurrentResolution())) {
  152. return "yyyy"; // force full year
  153. } else {
  154. try {
  155. String frmString = LocaleService.getDateFormat(currentLocale);
  156. frmString = cleanFormat(frmString);
  157. // String delim = LocaleService
  158. // .getClockDelimiter(currentLocale);
  159. if (getCurrentResolution()
  160. .compareTo(DateTimeResolution.HOUR) <= 0) {
  161. if (dts.isTwelveHourClock()) {
  162. frmString += " hh";
  163. } else {
  164. frmString += " HH";
  165. }
  166. if (getCurrentResolution()
  167. .compareTo(DateTimeResolution.MINUTE) <= 0) {
  168. frmString += ":mm";
  169. if (getCurrentResolution()
  170. .compareTo(DateTimeResolution.SECOND) <= 0) {
  171. frmString += ":ss";
  172. }
  173. }
  174. if (dts.isTwelveHourClock()) {
  175. frmString += " aaa";
  176. }
  177. }
  178. return frmString;
  179. } catch (LocaleNotLoadedException e) {
  180. // TODO should die instead? Can the component survive
  181. // without format string?
  182. VConsole.error(e);
  183. return null;
  184. }
  185. }
  186. }
  187. @Override
  188. protected String cleanFormat(String format) {
  189. // Remove unnecessary d & M if resolution is too low
  190. if (getCurrentResolution().compareTo(DateTimeResolution.DAY) > 0) {
  191. format = format.replaceAll("d", "");
  192. }
  193. if (getCurrentResolution().compareTo(DateTimeResolution.MONTH) > 0) {
  194. format = format.replaceAll("M", "");
  195. }
  196. return super.cleanFormat(format);
  197. }
  198. @Override
  199. protected boolean supportsTime() {
  200. return true;
  201. }
  202. }