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.

VDateFieldCalendar.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.DateResolution.DAY;
  18. import static com.vaadin.shared.ui.datefield.DateResolution.MONTH;
  19. import static com.vaadin.shared.ui.datefield.DateResolution.YEAR;
  20. import java.util.Date;
  21. import java.util.Map;
  22. import com.google.gwt.core.shared.GWT;
  23. import com.vaadin.shared.ui.datefield.DateResolution;
  24. /**
  25. * A client side implementation for InlineDateField.
  26. *
  27. * @author Vaadin Ltd
  28. *
  29. */
  30. public class VDateFieldCalendar
  31. extends VAbstractDateFieldCalendar<VDateCalendarPanel, DateResolution> {
  32. public VDateFieldCalendar() {
  33. super(GWT.create(VDateCalendarPanel.class), YEAR);
  34. }
  35. @Override
  36. public void updateBufferedValues() {
  37. // If field is invisible at the beginning, client can still be null when
  38. // this function is called.
  39. if (getClient() == null) {
  40. return;
  41. }
  42. Date date2 = calendarPanel.getDate();
  43. Date currentDate = getCurrentDate();
  44. DateResolution resolution = getCurrentResolution();
  45. if (currentDate == null || date2.getTime() != currentDate.getTime()) {
  46. setCurrentDate((Date) date2.clone());
  47. bufferedResolutions.put(YEAR,
  48. // Java Date uses the year aligned to 1900 (no to zero).
  49. // So we should add 1900 to get a correct year aligned to 0.
  50. date2.getYear() + 1900);
  51. if (resolution.compareTo(YEAR) < 0) {
  52. bufferedResolutions.put(MONTH, date2.getMonth() + 1);
  53. if (resolution.compareTo(MONTH) < 0) {
  54. bufferedResolutions.put(DAY, date2.getDate());
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * TODO refactor: almost same method as in VPopupCalendar.updateValue
  61. * <p>
  62. * For internal use only. May be removed or replaced in the future.
  63. */
  64. @Override
  65. public void updateValueFromPanel() {
  66. updateBufferedValues();
  67. if (bufferedResolutions != null) {
  68. sendBufferedValues();
  69. }
  70. }
  71. @Override
  72. public void setCurrentResolution(DateResolution resolution) {
  73. super.setCurrentResolution(resolution == null ? YEAR : resolution);
  74. }
  75. @Override
  76. public String resolutionAsString() {
  77. return getResolutionVariable(getCurrentResolution());
  78. }
  79. @Override
  80. public boolean isYear(DateResolution resolution) {
  81. return YEAR.equals(resolution);
  82. }
  83. @Override
  84. protected DateResolution[] doGetResolutions() {
  85. return DateResolution.values();
  86. }
  87. @Override
  88. protected Date getDate(Map<DateResolution, Integer> dateVaules) {
  89. return VPopupCalendar.makeDate(dateVaules);
  90. }
  91. @Override
  92. protected boolean supportsTime() {
  93. return false;
  94. }
  95. }