您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VPopupTimeCalendar.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2000-2018 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 static com.vaadin.shared.ui.datefield.DateTimeResolution.DAY;
  18. import static com.vaadin.shared.ui.datefield.DateTimeResolution.HOUR;
  19. import static com.vaadin.shared.ui.datefield.DateTimeResolution.MINUTE;
  20. import static com.vaadin.shared.ui.datefield.DateTimeResolution.MONTH;
  21. import static com.vaadin.shared.ui.datefield.DateTimeResolution.SECOND;
  22. import static com.vaadin.shared.ui.datefield.DateTimeResolution.YEAR;
  23. import java.util.Date;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.google.gwt.core.client.GWT;
  28. import com.vaadin.client.LocaleNotLoadedException;
  29. import com.vaadin.client.LocaleService;
  30. import com.vaadin.shared.ui.datefield.DateTimeResolution;
  31. /**
  32. * Represents a date-time selection component with a text field and a popup date
  33. * selector.
  34. *
  35. * @author Vaadin Ltd
  36. *
  37. * @since 8.0
  38. */
  39. public class VPopupTimeCalendar extends
  40. VAbstractPopupCalendar<VDateTimeCalendarPanel, DateTimeResolution> {
  41. public VPopupTimeCalendar() {
  42. super(GWT.create(VDateTimeCalendarPanel.class), MINUTE);
  43. }
  44. @Override
  45. protected DateTimeResolution[] doGetResolutions() {
  46. return DateTimeResolution.values();
  47. }
  48. @Override
  49. public String resolutionAsString() {
  50. if (getCurrentResolution().compareTo(DAY) >= 0) {
  51. return getResolutionVariable(getCurrentResolution());
  52. }
  53. return "full";
  54. }
  55. @Override
  56. public void setCurrentResolution(DateTimeResolution resolution) {
  57. super.setCurrentResolution(resolution == null ? MINUTE : resolution);
  58. }
  59. public static Date makeDate(Map<DateTimeResolution, Integer> dateValues) {
  60. if (dateValues.get(YEAR) == null) {
  61. return null;
  62. }
  63. Date date = new Date(2000 - 1900, 0, 1);
  64. Integer year = dateValues.get(YEAR);
  65. if (year != null) {
  66. date.setYear(year - 1900);
  67. }
  68. Integer month = dateValues.get(MONTH);
  69. if (month != null) {
  70. date.setMonth(month - 1);
  71. }
  72. Integer day = dateValues.get(DAY);
  73. if (day != null) {
  74. date.setDate(day);
  75. }
  76. Integer hour = dateValues.get(HOUR);
  77. if (hour != null) {
  78. date.setHours(hour);
  79. }
  80. Integer minute = dateValues.get(MINUTE);
  81. if (minute != null) {
  82. date.setMinutes(minute);
  83. }
  84. Integer second = dateValues.get(SECOND);
  85. if (second != null) {
  86. date.setSeconds(second);
  87. }
  88. return date;
  89. }
  90. @Override
  91. public boolean isYear(DateTimeResolution resolution) {
  92. return YEAR.equals(resolution);
  93. }
  94. @Override
  95. protected Date getDate(Map<DateTimeResolution, Integer> dateValues) {
  96. return makeDate(dateValues);
  97. }
  98. @Override
  99. protected void updateBufferedResolutions() {
  100. super.updateBufferedResolutions();
  101. Date currentDate = getDate();
  102. if (currentDate != null) {
  103. DateTimeResolution resolution = getCurrentResolution();
  104. if (resolution.compareTo(MONTH) <= 0) {
  105. bufferedResolutions.put(MONTH, currentDate.getMonth() + 1);
  106. }
  107. if (resolution.compareTo(DAY) <= 0) {
  108. bufferedResolutions.put(DAY, currentDate.getDate());
  109. }
  110. if (resolution.compareTo(HOUR) <= 0) {
  111. bufferedResolutions.put(HOUR, currentDate.getHours());
  112. }
  113. if (resolution.compareTo(MINUTE) <= 0) {
  114. bufferedResolutions.put(MINUTE, currentDate.getMinutes());
  115. }
  116. if (resolution.compareTo(SECOND) <= 0) {
  117. bufferedResolutions.put(SECOND, currentDate.getSeconds());
  118. }
  119. }
  120. }
  121. @Override
  122. @SuppressWarnings("deprecation")
  123. public void updateValue(Date newDate) {
  124. Date currentDate = getCurrentDate();
  125. super.updateValue(newDate);
  126. DateTimeResolution resolution = getCurrentResolution();
  127. if (currentDate == null || newDate.getTime() != currentDate.getTime()) {
  128. if (resolution.compareTo(DAY) < 0) {
  129. bufferedResolutions.put(HOUR, newDate.getHours());
  130. if (resolution.compareTo(HOUR) < 0) {
  131. bufferedResolutions.put(MINUTE, newDate.getMinutes());
  132. if (resolution.compareTo(MINUTE) < 0) {
  133. bufferedResolutions.put(SECOND, newDate.getSeconds());
  134. }
  135. }
  136. }
  137. }
  138. }
  139. @Override
  140. protected String createFormatString() {
  141. if (isYear(getCurrentResolution())) {
  142. return "yyyy"; // force full year
  143. }
  144. try {
  145. String frmString = LocaleService.getDateFormat(currentLocale);
  146. frmString = cleanFormat(frmString);
  147. // String delim = LocaleService
  148. // .getClockDelimiter(currentLocale);
  149. if (getCurrentResolution().compareTo(HOUR) <= 0) {
  150. if (dts.isTwelveHourClock()) {
  151. frmString += " hh";
  152. } else {
  153. frmString += " HH";
  154. }
  155. if (getCurrentResolution().compareTo(MINUTE) <= 0) {
  156. frmString += ":mm";
  157. if (getCurrentResolution().compareTo(SECOND) <= 0) {
  158. frmString += ":ss";
  159. }
  160. }
  161. if (dts.isTwelveHourClock()) {
  162. frmString += " aaa";
  163. }
  164. }
  165. return frmString;
  166. } catch (LocaleNotLoadedException e) {
  167. // TODO should die instead? Can the component survive
  168. // without format string?
  169. getLogger().log(Level.SEVERE,
  170. e.getMessage() == null ? "" : e.getMessage(), e);
  171. return null;
  172. }
  173. }
  174. @Override
  175. protected String cleanFormat(String format) {
  176. // Remove unnecessary d & M if resolution is too low
  177. if (getCurrentResolution().compareTo(DAY) > 0) {
  178. format = format.replaceAll("d", "");
  179. }
  180. if (getCurrentResolution().compareTo(MONTH) > 0) {
  181. format = format.replaceAll("M", "");
  182. }
  183. return super.cleanFormat(format);
  184. }
  185. @Override
  186. protected boolean supportsTime() {
  187. return true;
  188. }
  189. private static Logger getLogger() {
  190. return Logger.getLogger(VPopupTimeCalendar.class.getName());
  191. }
  192. }