View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.form.field.factory;
35  
36  import static info.magnolia.ui.form.field.definition.DateFieldDefinition.NOW;
37  
38  import info.magnolia.cms.security.MgnlUserManager;
39  import info.magnolia.context.Context;
40  import info.magnolia.i18nsystem.SimpleTranslator;
41  import info.magnolia.objectfactory.Components;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44  import info.magnolia.ui.form.field.definition.DateFieldDefinition;
45  
46  import java.text.ParseException;
47  import java.text.SimpleDateFormat;
48  import java.util.Date;
49  import java.util.TimeZone;
50  
51  import javax.inject.Inject;
52  
53  import org.apache.commons.lang3.StringUtils;
54  
55  import com.vaadin.v7.data.Item;
56  import com.vaadin.v7.shared.ui.datefield.Resolution;
57  import com.vaadin.v7.ui.Field;
58  import com.vaadin.v7.ui.PopupDateField;
59  
60  /**
61   * Creates and initializes a date field based on a field definition.
62   */
63  public class DateFieldFactory extends AbstractFieldFactory<DateFieldDefinition, Date> {
64  
65      /**
66       * @deprecated not used anymore in timezone options, though some user profiles might still have it.
67       */
68      private static final String BROWSER_TIMEZONE = "browser";
69  
70      private final SimpleTranslator i18n;
71      private final Context context;
72  
73      @Inject
74      public DateFieldFactory(DateFieldDefinition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18NAuthoringSupport, SimpleTranslator i18n, Context context) {
75          super(definition, relatedFieldItem, uiContext, i18NAuthoringSupport);
76          this.i18n = i18n;
77          this.context = context;
78      }
79  
80      /**
81       * @deprecated since 5.4.7. Use {@link #DateFieldFactory(info.magnolia.ui.form.field.definition.DateFieldDefinition, com.vaadin.v7.data.Item, info.magnolia.ui.api.context.UiContext, info.magnolia.ui.api.i18n.I18NAuthoringSupport, info.magnolia.i18nsystem.SimpleTranslator, Context)} instead.
82       */
83      @Deprecated
84      public DateFieldFactory(DateFieldDefinition definition, Item relatedFieldItem) {
85          this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), Components.getComponent(SimpleTranslator.class), Components.getComponent(Context.class));
86      }
87  
88      @Override
89      public Field<Date> createField() {
90          Field<Date> field = super.createField();
91          field.setWidthUndefined();
92          return field;
93      }
94  
95      @Override
96      protected Field<Date> createFieldComponent() {
97          DateFieldDefinition definition = getFieldDefinition();
98          PopupDateField popupDateField = new PopupDateField();
99          setTimeZone(popupDateField);
100         String dateFormat;
101 
102         // set Resolution
103         if (definition.isTime()) {
104             popupDateField.setResolution(Resolution.MINUTE);
105             dateFormat = definition.getDateFormat() + " " + definition.getTimeFormat();
106         } else {
107             popupDateField.setResolution(Resolution.DAY);
108             dateFormat = definition.getDateFormat();
109         }
110         popupDateField.setDateFormat(dateFormat);
111         return popupDateField;
112     }
113 
114     @Override
115     protected Class<?> getDefaultFieldType() {
116         return Date.class;
117     }
118 
119     /**
120      * Sets the field's time zone according to user preference if set, or to system default otherwise.
121      * User is informed of the input field's expected time zone through the field description and input prompt.
122      *
123      * Do mind however, that when value is null, the client-side time selector always shows in "local" time,
124      * thus the default selected time might be off from the current instant. TODO: we might patch that from Vaadin/GWT
125      */
126     protected void setTimeZone(PopupDateField popupDateField) {
127 
128         final String timeZoneId = context.getUser().getProperty(MgnlUserManager.PROPERTY_TIMEZONE);
129         final TimeZone timeZone;
130 
131         if (timeZoneId == null || timeZoneId.isEmpty() || timeZoneId.equals(BROWSER_TIMEZONE)) {
132             timeZone = TimeZone.getDefault();
133         } else {
134             timeZone = TimeZone.getTimeZone(timeZoneId);
135         }
136         if (timeZone != null) {
137             popupDateField.setTimeZone(timeZone);
138 
139             // Show resolved TZ info in the description tooltip & input-prompt
140             popupDateField.setDescription(i18n.translate("ui-admincentral.dateField.timeZone.description", timeZone.getDisplayName(false, TimeZone.LONG, context.getLocale()), timeZone.getRawOffset() / 3600000));
141             popupDateField.setInputPrompt(i18n.translate("ui-admincentral.dateField.timeZone.description", timeZone.getDisplayName(false, TimeZone.SHORT, context.getLocale()), timeZone.getRawOffset() / 3600000));
142         }
143     }
144 
145     @Override
146     protected Object getConfiguredDefaultValue() {
147         if (NOW.equalsIgnoreCase(definition.getDefaultValue())) {
148             return new Date();
149         }
150         if (definition.isTime() && StringUtils.isNotEmpty(definition.getDefaultValue())) {
151             SimpleDateFormat dateFormatter = new SimpleDateFormat(definition.getDateFormat() + " " + definition.getTimeFormat());
152             try {
153                 return dateFormatter.parse(definition.getDefaultValue());
154             } catch (ParseException e) {
155             }
156         }
157         return super.getConfiguredDefaultValue();
158     }
159 }