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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2000-2014 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.DomEvent;
  19. import com.vaadin.client.DateTimeService;
  20. import com.vaadin.client.ui.VCalendarPanel.FocusOutListener;
  21. import com.vaadin.client.ui.VCalendarPanel.SubmitListener;
  22. import com.vaadin.shared.ui.datefield.Resolution;
  23. /**
  24. * A client side implementation for InlineDateField
  25. */
  26. public class VDateFieldCalendar extends VDateField {
  27. /** For internal use only. May be removed or replaced in the future. */
  28. public final VCalendarPanel calendarPanel;
  29. public VDateFieldCalendar() {
  30. super();
  31. calendarPanel = new VCalendarPanel();
  32. calendarPanel.setParentField(this);
  33. add(calendarPanel);
  34. calendarPanel.setSubmitListener(new SubmitListener() {
  35. @Override
  36. public void onSubmit() {
  37. updateValueFromPanel();
  38. }
  39. @Override
  40. public void onCancel() {
  41. // TODO Auto-generated method stub
  42. }
  43. });
  44. calendarPanel.setFocusOutListener(new FocusOutListener() {
  45. @Override
  46. public boolean onFocusOut(DomEvent<?> event) {
  47. updateValueFromPanel();
  48. return false;
  49. }
  50. });
  51. }
  52. /**
  53. * TODO refactor: almost same method as in VPopupCalendar.updateValue
  54. * <p>
  55. * For internal use only. May be removed or replaced in the future.
  56. */
  57. @SuppressWarnings("deprecation")
  58. public void updateValueFromPanel() {
  59. // If field is invisible at the beginning, client can still be null when
  60. // this function is called.
  61. if (getClient() == null) {
  62. return;
  63. }
  64. Date date2 = calendarPanel.getDate();
  65. Date currentDate = getCurrentDate();
  66. if (currentDate == null || date2.getTime() != currentDate.getTime()) {
  67. setCurrentDate((Date) date2.clone());
  68. getClient().updateVariable(getId(), "year", date2.getYear() + 1900,
  69. false);
  70. if (getCurrentResolution().getCalendarField() > Resolution.YEAR
  71. .getCalendarField()) {
  72. getClient().updateVariable(getId(), "month",
  73. date2.getMonth() + 1, false);
  74. if (getCurrentResolution().getCalendarField() > Resolution.MONTH
  75. .getCalendarField()) {
  76. getClient().updateVariable(getId(), "day", date2.getDate(),
  77. false);
  78. if (getCurrentResolution().getCalendarField() > Resolution.DAY
  79. .getCalendarField()) {
  80. getClient().updateVariable(getId(), "hour",
  81. date2.getHours(), false);
  82. if (getCurrentResolution().getCalendarField() > Resolution.HOUR
  83. .getCalendarField()) {
  84. getClient().updateVariable(getId(), "min",
  85. date2.getMinutes(), false);
  86. if (getCurrentResolution().getCalendarField() > Resolution.MINUTE
  87. .getCalendarField()) {
  88. getClient().updateVariable(getId(), "sec",
  89. date2.getSeconds(), false);
  90. if (getCurrentResolution().getCalendarField() > Resolution.SECOND
  91. .getCalendarField()) {
  92. getClient().updateVariable(
  93. getId(),
  94. "msec",
  95. DateTimeService
  96. .getMilliseconds(date2),
  97. false);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if (isImmediate()) {
  105. getClient().sendPendingVariableChanges();
  106. }
  107. }
  108. }
  109. public void setTabIndex(int tabIndex) {
  110. calendarPanel.getElement().setTabIndex(tabIndex);
  111. }
  112. public int getTabIndex() {
  113. return calendarPanel.getElement().getTabIndex();
  114. }
  115. }