Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractDateFieldConnector.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.v7.client.ui.datefield;
  17. import java.util.Date;
  18. import com.vaadin.client.ApplicationConnection;
  19. import com.vaadin.client.LocaleNotLoadedException;
  20. import com.vaadin.client.Paintable;
  21. import com.vaadin.client.UIDL;
  22. import com.vaadin.client.VConsole;
  23. import com.vaadin.v7.client.ui.AbstractFieldConnector;
  24. import com.vaadin.v7.client.ui.VDateField;
  25. import com.vaadin.v7.shared.ui.datefield.DateFieldConstants;
  26. import com.vaadin.v7.shared.ui.datefield.Resolution;
  27. public class AbstractDateFieldConnector extends AbstractFieldConnector
  28. implements Paintable {
  29. @Override
  30. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  31. if (!isRealUpdate(uidl)) {
  32. return;
  33. }
  34. // Save details
  35. getWidget().client = client;
  36. getWidget().paintableId = uidl.getId();
  37. getWidget().immediate = getState().immediate;
  38. getWidget().setReadonly(isReadOnly());
  39. getWidget().setEnabled(isEnabled());
  40. if (uidl.hasAttribute("locale")) {
  41. final String locale = uidl.getStringAttribute("locale");
  42. try {
  43. getWidget().dts.setLocale(locale);
  44. getWidget().setCurrentLocale(locale);
  45. } catch (final LocaleNotLoadedException e) {
  46. getWidget().setCurrentLocale(getWidget().dts.getLocale());
  47. VConsole.error("Tried to use an unloaded locale \"" + locale
  48. + "\". Using default locale ("
  49. + getWidget().getCurrentLocale() + ").");
  50. VConsole.error(e);
  51. }
  52. }
  53. // We show week numbers only if the week starts with Monday, as ISO 8601
  54. // specifies
  55. getWidget().setShowISOWeekNumbers(
  56. uidl.getBooleanAttribute(DateFieldConstants.ATTR_WEEK_NUMBERS)
  57. && getWidget().dts.getFirstDayOfWeek() == 1);
  58. Resolution newResolution;
  59. if (uidl.hasVariable("sec")) {
  60. newResolution = Resolution.SECOND;
  61. } else if (uidl.hasVariable("min")) {
  62. newResolution = Resolution.MINUTE;
  63. } else if (uidl.hasVariable("hour")) {
  64. newResolution = Resolution.HOUR;
  65. } else if (uidl.hasVariable("day")) {
  66. newResolution = Resolution.DAY;
  67. } else if (uidl.hasVariable("month")) {
  68. newResolution = Resolution.MONTH;
  69. } else {
  70. newResolution = Resolution.YEAR;
  71. }
  72. // Remove old stylename that indicates current resolution
  73. setWidgetStyleName(
  74. getWidget().getStylePrimaryName() + "-" + VDateField
  75. .resolutionToString(getWidget().getCurrentResolution()),
  76. false);
  77. getWidget().setCurrentResolution(newResolution);
  78. // Add stylename that indicates current resolution
  79. setWidgetStyleName(
  80. getWidget().getStylePrimaryName() + "-" + VDateField
  81. .resolutionToString(getWidget().getCurrentResolution()),
  82. true);
  83. final Resolution resolution = getWidget().getCurrentResolution();
  84. final int year = uidl.getIntVariable("year");
  85. final int month = (resolution.getCalendarField() >= Resolution.MONTH
  86. .getCalendarField()) ? uidl.getIntVariable("month") : -1;
  87. final int day = (resolution.getCalendarField() >= Resolution.DAY
  88. .getCalendarField()) ? uidl.getIntVariable("day") : -1;
  89. final int hour = (resolution.getCalendarField() >= Resolution.HOUR
  90. .getCalendarField()) ? uidl.getIntVariable("hour") : 0;
  91. final int min = (resolution.getCalendarField() >= Resolution.MINUTE
  92. .getCalendarField()) ? uidl.getIntVariable("min") : 0;
  93. final int sec = (resolution.getCalendarField() >= Resolution.SECOND
  94. .getCalendarField()) ? uidl.getIntVariable("sec") : 0;
  95. // Construct new date for this datefield (only if not null)
  96. if (year > -1) {
  97. getWidget().setCurrentDate(new Date((long) getWidget().getTime(year,
  98. month, day, hour, min, sec, 0)));
  99. } else {
  100. getWidget().setCurrentDate(null);
  101. }
  102. }
  103. @Override
  104. public VDateField getWidget() {
  105. return (VDateField) super.getWidget();
  106. }
  107. }