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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.shared.ui.datefield.DateResolution;
  21. /**
  22. * Represents a date selection component with a text field and a popup date
  23. * selector.
  24. *
  25. * @author Vaadin Ltd
  26. *
  27. */
  28. public class VPopupCalendar extends VAbstractPopupCalendar<DateResolution> {
  29. public VPopupCalendar() {
  30. super(GWT.create(VDateCalendarPanel.class), DateResolution.YEAR);
  31. }
  32. @Override
  33. protected DateResolution[] doGetResolutions() {
  34. return DateResolution.values();
  35. }
  36. @Override
  37. public String resolutionAsString() {
  38. return getResolutionVariable(getCurrentResolution());
  39. }
  40. @Override
  41. public void setCurrentResolution(DateResolution resolution) {
  42. super.setCurrentResolution(
  43. resolution == null ? DateResolution.YEAR : resolution);
  44. }
  45. public static Date makeDate(Map<DateResolution, Integer> dateVaules) {
  46. if (dateVaules.get(DateResolution.YEAR) == -1) {
  47. return null;
  48. }
  49. Date date = new Date(2000 - 1900, 0, 1);
  50. int year = dateVaules.get(DateResolution.YEAR);
  51. if (year >= 0) {
  52. date.setYear(year - 1900);
  53. }
  54. int month = dateVaules.get(DateResolution.MONTH);
  55. if (month >= 0) {
  56. date.setMonth(month - 1);
  57. }
  58. int day = dateVaules.get(DateResolution.DAY);
  59. if (day >= 0) {
  60. date.setDate(day);
  61. }
  62. return date;
  63. }
  64. @Override
  65. public boolean isYear(DateResolution resolution) {
  66. return DateResolution.YEAR.equals(resolution);
  67. }
  68. @Override
  69. protected Date getDate(Map<DateResolution, Integer> dateValues) {
  70. return makeDate(dateValues);
  71. }
  72. @Override
  73. protected void updateDateVariables() {
  74. super.updateDateVariables();
  75. // Update variables
  76. // (only the smallest defining resolution needs to be
  77. // immediate)
  78. Date currentDate = getDate();
  79. if (getCurrentResolution().compareTo(DateResolution.MONTH) <= 0) {
  80. getClient().updateVariable(getId(),
  81. getResolutionVariable(DateResolution.MONTH),
  82. currentDate != null ? currentDate.getMonth() + 1 : -1,
  83. getCurrentResolution() == DateResolution.MONTH);
  84. }
  85. if (getCurrentResolution().compareTo(DateResolution.DAY) <= 0) {
  86. getClient().updateVariable(getId(),
  87. getResolutionVariable(DateResolution.DAY),
  88. currentDate != null ? currentDate.getDate() : -1,
  89. getCurrentResolution() == DateResolution.DAY);
  90. }
  91. }
  92. }