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.

VPopupCalendar.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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.client.GWT;
  23. import com.vaadin.shared.ui.datefield.DateResolution;
  24. /**
  25. * Represents a date selection component with a text field and a popup date
  26. * selector.
  27. *
  28. * @author Vaadin Ltd
  29. *
  30. */
  31. public class VPopupCalendar
  32. extends VAbstractPopupCalendar<VDateCalendarPanel, DateResolution> {
  33. public VPopupCalendar() {
  34. super(GWT.create(VDateCalendarPanel.class), YEAR);
  35. }
  36. @Override
  37. protected DateResolution[] doGetResolutions() {
  38. return DateResolution.values();
  39. }
  40. @Override
  41. public String resolutionAsString() {
  42. return getResolutionVariable(getCurrentResolution());
  43. }
  44. @Override
  45. public void setCurrentResolution(DateResolution resolution) {
  46. super.setCurrentResolution(resolution == null ? YEAR : resolution);
  47. }
  48. public static Date makeDate(Map<DateResolution, Integer> dateValues) {
  49. if (dateValues.get(YEAR) == null) {
  50. return null;
  51. }
  52. Date date = new Date(2000 - 1900, 0, 1);
  53. Integer year = dateValues.get(YEAR);
  54. if (year != null) {
  55. date.setYear(year - 1900);
  56. }
  57. Integer month = dateValues.get(MONTH);
  58. if (month != null) {
  59. date.setMonth(month - 1);
  60. }
  61. Integer day = dateValues.get(DAY);
  62. if (day != null) {
  63. date.setDate(day);
  64. }
  65. return date;
  66. }
  67. @Override
  68. public boolean isYear(DateResolution resolution) {
  69. return YEAR.equals(resolution);
  70. }
  71. @Override
  72. protected Date getDate(Map<DateResolution, Integer> dateValues) {
  73. return makeDate(dateValues);
  74. }
  75. @Override
  76. protected void updateBufferedResolutions() {
  77. super.updateBufferedResolutions();
  78. Date currentDate = getDate();
  79. if (currentDate != null) {
  80. DateResolution resolution = getCurrentResolution();
  81. if (resolution.compareTo(MONTH) <= 0) {
  82. bufferedResolutions.put(MONTH, currentDate.getMonth() + 1);
  83. }
  84. if (resolution.compareTo(DAY) <= 0) {
  85. bufferedResolutions.put(DAY, currentDate.getDate());
  86. }
  87. }
  88. }
  89. @Override
  90. protected String cleanFormat(String format) {
  91. // Remove unnecessary d & M if resolution is too low
  92. if (getCurrentResolution().compareTo(DAY) > 0) {
  93. format = format.replaceAll("d", "");
  94. }
  95. if (getCurrentResolution().compareTo(MONTH) > 0) {
  96. format = format.replaceAll("M", "");
  97. }
  98. return super.cleanFormat(format);
  99. }
  100. @Override
  101. protected boolean supportsTime() {
  102. return false;
  103. }
  104. }