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.

VDateTimeCalendarPanel.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright 2000-2021 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 com.google.gwt.event.dom.client.ChangeEvent;
  19. import com.google.gwt.event.dom.client.ChangeHandler;
  20. import com.google.gwt.event.dom.client.KeyCodes;
  21. import com.google.gwt.user.client.ui.FlowPanel;
  22. import com.google.gwt.user.client.ui.ListBox;
  23. import com.google.gwt.user.client.ui.Widget;
  24. import com.vaadin.client.DateTimeService;
  25. import com.vaadin.shared.ui.datefield.DateTimeResolution;
  26. /**
  27. * A calendar panel widget to show and select a date and a time.
  28. *
  29. * @author Vaadin Ltd
  30. * @since 8.0
  31. */
  32. public class VDateTimeCalendarPanel
  33. extends VAbstractCalendarPanel<DateTimeResolution> {
  34. private static final String SUBPART_HOUR_SELECT = "h";
  35. private static final String SUBPART_MINUTE_SELECT = "m";
  36. private static final String SUBPART_SECS_SELECT = "s";
  37. private static final String SUBPART_AMPM_SELECT = "ampm";
  38. private TimeChangeListener timeChangeListener;
  39. private VTime time;
  40. /**
  41. * TimeSelector is a widget consisting of list boxes that modify the Date
  42. * object that is given for.
  43. *
  44. */
  45. public class VTime extends FlowPanel implements ChangeHandler {
  46. private ListBox hours;
  47. private ListBox mins;
  48. private ListBox sec;
  49. private ListBox ampm;
  50. /**
  51. * Constructor.
  52. */
  53. public VTime() {
  54. super();
  55. setStyleName(VDateField.CLASSNAME + "-time");
  56. buildTime();
  57. }
  58. private ListBox createListBox() {
  59. ListBox lb = new ListBox();
  60. lb.setStyleName("v-select");
  61. lb.addChangeHandler(this);
  62. lb.addBlurHandler(VDateTimeCalendarPanel.this);
  63. lb.addFocusHandler(VDateTimeCalendarPanel.this);
  64. return lb;
  65. }
  66. /**
  67. * Constructs the ListBoxes and updates their value
  68. *
  69. */
  70. @SuppressWarnings("deprecation")
  71. private void buildTime() {
  72. clear();
  73. hours = createListBox();
  74. if (getDateTimeService().isTwelveHourClock()) {
  75. hours.addItem("12");
  76. for (int i = 1; i < 12; i++) {
  77. hours.addItem(DateTimeService.asTwoDigits(i));
  78. }
  79. } else {
  80. for (int i = 0; i < 24; i++) {
  81. hours.addItem(DateTimeService.asTwoDigits(i));
  82. }
  83. }
  84. hours.addChangeHandler(this);
  85. if (getDateTimeService().isTwelveHourClock()) {
  86. ampm = createListBox();
  87. final String[] ampmText = getDateTimeService().getAmPmStrings();
  88. ampm.addItem(ampmText[0]);
  89. ampm.addItem(ampmText[1]);
  90. ampm.addChangeHandler(this);
  91. }
  92. if (getResolution().compareTo(DateTimeResolution.MINUTE) <= 0) {
  93. mins = createListBox();
  94. for (int i = 0; i < 60; i++) {
  95. mins.addItem(DateTimeService.asTwoDigits(i));
  96. }
  97. mins.addChangeHandler(this);
  98. }
  99. if (getResolution().compareTo(DateTimeResolution.SECOND) <= 0) {
  100. sec = createListBox();
  101. for (int i = 0; i < 60; i++) {
  102. sec.addItem(DateTimeService.asTwoDigits(i));
  103. }
  104. sec.addChangeHandler(this);
  105. }
  106. // Update times
  107. updateTimes();
  108. final String delimiter = getDateTimeService().getClockDelimeter();
  109. if (isReadonly()) {
  110. int h = 0;
  111. if (getDate() != null) {
  112. h = getDate().getHours();
  113. }
  114. if (getDateTimeService().isTwelveHourClock()) {
  115. h -= h < 12 ? 0 : 12;
  116. }
  117. add(new VLabel(DateTimeService.asTwoDigits(h)));
  118. } else {
  119. add(hours);
  120. }
  121. if (getResolution().compareTo(DateTimeResolution.MINUTE) <= 0) {
  122. add(new VLabel(delimiter));
  123. if (isReadonly()) {
  124. final int m = mins.getSelectedIndex();
  125. add(new VLabel(DateTimeService.asTwoDigits(m)));
  126. } else {
  127. add(mins);
  128. }
  129. }
  130. if (getResolution().compareTo(DateTimeResolution.SECOND) <= 0) {
  131. add(new VLabel(delimiter));
  132. if (isReadonly()) {
  133. final int s = sec.getSelectedIndex();
  134. add(new VLabel(DateTimeService.asTwoDigits(s)));
  135. } else {
  136. add(sec);
  137. }
  138. }
  139. if (getResolution() == DateTimeResolution.HOUR) {
  140. add(new VLabel(delimiter + "00")); // o'clock
  141. }
  142. if (getDateTimeService().isTwelveHourClock()) {
  143. add(new VLabel("&nbsp;"));
  144. if (isReadonly()) {
  145. int i = 0;
  146. if (getDate() != null) {
  147. i = (getDate().getHours() < 12) ? 0 : 1;
  148. }
  149. add(new VLabel(ampm.getItemText(i)));
  150. } else {
  151. add(ampm);
  152. }
  153. }
  154. if (isReadonly()) {
  155. return;
  156. }
  157. ListBox lastDropDown = getLastDropDown();
  158. lastDropDown.addKeyDownHandler(event -> {
  159. boolean shiftKey = event.getNativeEvent().getShiftKey();
  160. if (!shiftKey) {
  161. int nativeKeyCode = event.getNativeKeyCode();
  162. if (nativeKeyCode == KeyCodes.KEY_TAB) {
  163. onTabOut(event);
  164. }
  165. }
  166. });
  167. }
  168. private ListBox getLastDropDown() {
  169. int i = getWidgetCount() - 1;
  170. while (i >= 0) {
  171. Widget widget = getWidget(i);
  172. if (widget instanceof ListBox) {
  173. return (ListBox) widget;
  174. }
  175. i--;
  176. }
  177. return null;
  178. }
  179. /**
  180. * Updates the value to correspond to the values in value.
  181. */
  182. @SuppressWarnings("deprecation")
  183. public void updateTimes() {
  184. if (getDate() == null) {
  185. setDate(new Date());
  186. }
  187. if (getDateTimeService().isTwelveHourClock()) {
  188. int h = getDate().getHours();
  189. ampm.setSelectedIndex(h < 12 ? 0 : 1);
  190. h -= ampm.getSelectedIndex() * 12;
  191. hours.setSelectedIndex(h);
  192. } else {
  193. hours.setSelectedIndex(getDate().getHours());
  194. }
  195. if (getResolution().compareTo(DateTimeResolution.MINUTE) <= 0) {
  196. mins.setSelectedIndex(getDate().getMinutes());
  197. }
  198. if (getResolution().compareTo(DateTimeResolution.SECOND) <= 0) {
  199. sec.setSelectedIndex(getDate().getSeconds());
  200. }
  201. if (getDateTimeService().isTwelveHourClock()) {
  202. ampm.setSelectedIndex(getDate().getHours() < 12 ? 0 : 1);
  203. }
  204. hours.setEnabled(isEnabled());
  205. if (mins != null) {
  206. mins.setEnabled(isEnabled());
  207. }
  208. if (sec != null) {
  209. sec.setEnabled(isEnabled());
  210. }
  211. if (ampm != null) {
  212. ampm.setEnabled(isEnabled());
  213. }
  214. }
  215. private DateTimeService getDateTimeService() {
  216. DateTimeService dts = VDateTimeCalendarPanel.this
  217. .getDateTimeService();
  218. if (dts == null) {
  219. dts = new DateTimeService();
  220. setDateTimeService(dts);
  221. }
  222. return dts;
  223. }
  224. /*
  225. * (non-Javadoc) VT
  226. *
  227. * @see
  228. * com.google.gwt.event.dom.client.ChangeHandler#onChange(com.google.gwt
  229. * .event.dom.client.ChangeEvent)
  230. */
  231. @Override
  232. @SuppressWarnings("deprecation")
  233. public void onChange(ChangeEvent event) {
  234. /*
  235. * Value from dropdowns gets always set for the value. Like year and
  236. * month when resolution is month or year.
  237. */
  238. if (event.getSource() == hours) {
  239. int h = hours.getSelectedIndex();
  240. if (getDateTimeService().isTwelveHourClock()) {
  241. h = h + ampm.getSelectedIndex() * 12;
  242. }
  243. getDate().setHours(h);
  244. if (timeChangeListener != null) {
  245. timeChangeListener.changed(h, getDate().getMinutes(),
  246. getDate().getSeconds(),
  247. DateTimeService.getMilliseconds(getDate()));
  248. }
  249. event.preventDefault();
  250. event.stopPropagation();
  251. } else if (event.getSource() == mins) {
  252. final int m = mins.getSelectedIndex();
  253. getDate().setMinutes(m);
  254. if (timeChangeListener != null) {
  255. timeChangeListener.changed(getDate().getHours(), m,
  256. getDate().getSeconds(),
  257. DateTimeService.getMilliseconds(getDate()));
  258. }
  259. event.preventDefault();
  260. event.stopPropagation();
  261. } else if (event.getSource() == sec) {
  262. final int s = sec.getSelectedIndex();
  263. getDate().setSeconds(s);
  264. if (timeChangeListener != null) {
  265. timeChangeListener.changed(getDate().getHours(),
  266. getDate().getMinutes(), s,
  267. DateTimeService.getMilliseconds(getDate()));
  268. }
  269. event.preventDefault();
  270. event.stopPropagation();
  271. } else if (event.getSource() == ampm) {
  272. final int h = hours.getSelectedIndex()
  273. + (ampm.getSelectedIndex() * 12);
  274. getDate().setHours(h);
  275. if (timeChangeListener != null) {
  276. timeChangeListener.changed(h, getDate().getMinutes(),
  277. getDate().getSeconds(),
  278. DateTimeService.getMilliseconds(getDate()));
  279. }
  280. event.preventDefault();
  281. event.stopPropagation();
  282. }
  283. }
  284. }
  285. /**
  286. * Dispatches an event when the panel when time is changed.
  287. */
  288. public interface TimeChangeListener {
  289. /**
  290. * Handle time change.
  291. *
  292. * @param hour
  293. * the new hour value
  294. * @param min
  295. * the new minute value
  296. * @param sec
  297. * the new second value
  298. * @param msec
  299. * the new millisecond value
  300. */
  301. void changed(int hour, int min, int sec, int msec);
  302. }
  303. /**
  304. * The time change listener is triggered when the user changes the time.
  305. *
  306. * @param listener
  307. * the listener to use
  308. */
  309. public void setTimeChangeListener(TimeChangeListener listener) {
  310. timeChangeListener = listener;
  311. }
  312. @Override
  313. public void setDate(Date currentDate) {
  314. doSetDate(currentDate, isTimeSelectorNeeded() && time == null, () -> {
  315. if (isTimeSelectorNeeded()) {
  316. time.updateTimes();
  317. }
  318. });
  319. }
  320. @Override
  321. public void setResolution(DateTimeResolution resolution) {
  322. super.setResolution(resolution);
  323. if (isTimeSelectorNeeded() && time != null) {
  324. // resolution has changed => rebuild time UI
  325. time.buildTime();
  326. }
  327. }
  328. @Override
  329. protected boolean acceptDayFocus() {
  330. return getResolution().compareTo(DateTimeResolution.MONTH) < 0;
  331. }
  332. @Override
  333. protected boolean isDay(DateTimeResolution resolution) {
  334. return DateTimeResolution.DAY.equals(resolution);
  335. }
  336. @Override
  337. protected boolean isMonth(DateTimeResolution resolution) {
  338. return DateTimeResolution.MONTH.equals(resolution);
  339. }
  340. @Override
  341. protected boolean isBelowMonth(DateTimeResolution resolution) {
  342. return resolution.compareTo(DateTimeResolution.MONTH) < 0;
  343. }
  344. @Override
  345. protected void doRenderCalendar(boolean updateDate) {
  346. super.doRenderCalendar(updateDate);
  347. if (isTimeSelectorNeeded()) {
  348. time = new VTime();
  349. setWidget(2, 0, time);
  350. getFlexCellFormatter().setColSpan(2, 0, 5);
  351. getFlexCellFormatter().setStyleName(2, 0,
  352. getDateField().getStylePrimaryName()
  353. + "-calendarpanel-time");
  354. } else if (time != null) {
  355. remove(time);
  356. }
  357. }
  358. @Override
  359. @SuppressWarnings("deprecation")
  360. public String getSubPartName(
  361. com.google.gwt.user.client.Element subElement) {
  362. if (time != null) {
  363. if (contains(time.hours, subElement)) {
  364. return SUBPART_HOUR_SELECT;
  365. } else if (contains(time.mins, subElement)) {
  366. return SUBPART_MINUTE_SELECT;
  367. } else if (contains(time.sec, subElement)) {
  368. return SUBPART_SECS_SELECT;
  369. } else if (contains(time.ampm, subElement)) {
  370. return SUBPART_AMPM_SELECT;
  371. }
  372. }
  373. return super.getSubPartName(subElement);
  374. }
  375. @Override
  376. @SuppressWarnings("deprecation")
  377. public com.google.gwt.user.client.Element getSubPartElement(
  378. String subPart) {
  379. if (SUBPART_HOUR_SELECT.equals(subPart)) {
  380. return time.hours.getElement();
  381. }
  382. if (SUBPART_MINUTE_SELECT.equals(subPart)) {
  383. return time.mins.getElement();
  384. }
  385. if (SUBPART_SECS_SELECT.equals(subPart)) {
  386. return time.sec.getElement();
  387. }
  388. if (SUBPART_AMPM_SELECT.equals(subPart)) {
  389. return time.ampm.getElement();
  390. }
  391. return super.getSubPartElement(subPart);
  392. }
  393. /**
  394. * Do we need the time selector
  395. *
  396. * @return True if it is required
  397. */
  398. private boolean isTimeSelectorNeeded() {
  399. return getResolution().compareTo(DateTimeResolution.DAY) < 0;
  400. }
  401. }