2 * Copyright 2000-2018 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.client.ui.datefield;
18 import java.util.Date;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
22 import com.vaadin.client.ApplicationConnection;
23 import com.vaadin.client.LocaleNotLoadedException;
24 import com.vaadin.client.Paintable;
25 import com.vaadin.client.UIDL;
26 import com.vaadin.v7.client.ui.AbstractFieldConnector;
27 import com.vaadin.v7.client.ui.VDateField;
28 import com.vaadin.v7.shared.ui.datefield.DateFieldConstants;
29 import com.vaadin.v7.shared.ui.datefield.Resolution;
31 public class AbstractDateFieldConnector extends AbstractFieldConnector
32 implements Paintable {
35 public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
36 if (!isRealUpdate(uidl)) {
41 getWidget().client = client;
42 getWidget().paintableId = uidl.getId();
43 getWidget().immediate = getState().immediate;
45 getWidget().setReadonly(isReadOnly());
46 getWidget().setEnabled(isEnabled());
48 if (uidl.hasAttribute("locale")) {
49 final String locale = uidl.getStringAttribute("locale");
51 getWidget().dts.setLocale(locale);
52 getWidget().setCurrentLocale(locale);
53 } catch (final LocaleNotLoadedException e) {
54 getWidget().setCurrentLocale(getWidget().dts.getLocale());
55 getLogger().severe("Tried to use an unloaded locale \"" + locale
56 + "\". Using default locale ("
57 + getWidget().getCurrentLocale() + ").");
58 getLogger().log(Level.SEVERE,
59 e.getMessage() == null ? "" : e.getMessage(), e);
63 // We show week numbers only if the week starts with Monday, as ISO 8601
65 getWidget().setShowISOWeekNumbers(
66 uidl.getBooleanAttribute(DateFieldConstants.ATTR_WEEK_NUMBERS)
67 && getWidget().dts.getFirstDayOfWeek() == 1);
69 Resolution newResolution;
70 if (uidl.hasVariable("sec")) {
71 newResolution = Resolution.SECOND;
72 } else if (uidl.hasVariable("min")) {
73 newResolution = Resolution.MINUTE;
74 } else if (uidl.hasVariable("hour")) {
75 newResolution = Resolution.HOUR;
76 } else if (uidl.hasVariable("day")) {
77 newResolution = Resolution.DAY;
78 } else if (uidl.hasVariable("month")) {
79 newResolution = Resolution.MONTH;
81 newResolution = Resolution.YEAR;
84 // Remove old stylename that indicates current resolution
86 getWidget().getStylePrimaryName() + "-" + VDateField
87 .resolutionToString(getWidget().getCurrentResolution()),
90 getWidget().setCurrentResolution(newResolution);
92 // Add stylename that indicates current resolution
94 getWidget().getStylePrimaryName() + "-" + VDateField
95 .resolutionToString(getWidget().getCurrentResolution()),
98 final Resolution resolution = getWidget().getCurrentResolution();
99 final int year = uidl.getIntVariable("year");
100 final int month = (resolution.getCalendarField() >= Resolution.MONTH
101 .getCalendarField()) ? uidl.getIntVariable("month") : -1;
102 final int day = (resolution.getCalendarField() >= Resolution.DAY
103 .getCalendarField()) ? uidl.getIntVariable("day") : -1;
104 final int hour = (resolution.getCalendarField() >= Resolution.HOUR
105 .getCalendarField()) ? uidl.getIntVariable("hour") : 0;
106 final int min = (resolution.getCalendarField() >= Resolution.MINUTE
107 .getCalendarField()) ? uidl.getIntVariable("min") : 0;
108 final int sec = (resolution.getCalendarField() >= Resolution.SECOND
109 .getCalendarField()) ? uidl.getIntVariable("sec") : 0;
111 // Construct new date for this datefield (only if not null)
113 getWidget().setCurrentDate(new Date((long) VDateField.getTime(year,
114 month, day, hour, min, sec, 0)));
116 getWidget().setCurrentDate(null);
121 public VDateField getWidget() {
122 return (VDateField) super.getWidget();
125 private static Logger getLogger() {
126 return Logger.getLogger(AbstractDateFieldConnector.class.getName());