View Javadoc
1   /**
2    * This file Copyright (c) 2016-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.admincentral.usermenu.field.factory;
35  
36  import static org.apache.commons.lang3.StringUtils.EMPTY;
37  
38  import info.magnolia.context.Context;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.ui.api.context.UiContext;
42  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
43  import info.magnolia.ui.form.field.definition.SelectFieldDefinition;
44  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
45  import info.magnolia.ui.form.field.factory.SelectFieldFactory;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.TimeZone;
50  
51  import javax.inject.Inject;
52  
53  import com.vaadin.v7.data.Item;
54  import com.vaadin.v7.data.Property;
55  import com.vaadin.v7.shared.ui.combobox.FilteringMode;
56  import com.vaadin.v7.ui.AbstractSelect;
57  
58  /**
59   * Creates and initializes a timezone field.
60   */
61  public class TimeZoneFieldFactory extends SelectFieldFactory<TimeZoneFieldFactory.Definition> {
62  
63      /**
64       * @deprecated not used anymore in timezone options, though some user profiles might still have it.
65       */
66      @Deprecated
67      static final String BROWSER_TIMEZONE = "browser";
68  
69      private final Context context;
70      private final SimpleTranslator i18n;
71  
72      @Inject
73      public TimeZoneFieldFactory(Definition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18nAuthoringSupport,
74                                  Context context, SimpleTranslator i18n) {
75          super(definition, relatedFieldItem, uiContext, i18nAuthoringSupport);
76          this.context = context;
77          this.i18n = i18n;
78      }
79  
80      /**
81       * @deprecated since 5.4.11, use {@link #TimeZoneFieldFactory(Definition, Item, UiContext, I18NAuthoringSupport, Context, SimpleTranslator)} instead
82       */
83      @Deprecated
84      public TimeZoneFieldFactory(Definition definition, Item relatedFieldItem, Context context, SimpleTranslator i18n) {
85          this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), context, i18n);
86      }
87  
88      @Override
89      protected AbstractSelect createFieldComponent() {
90          AbstractSelect select = super.createFieldComponent();
91          select.setNullSelectionAllowed(true);
92          select.setNullSelectionItemId(EMPTY);
93          return select;
94      }
95  
96      @Override
97      public void setPropertyDataSourceAndDefaultValue(Property property) {
98          super.setPropertyDataSourceAndDefaultValue(property);
99  
100         if (property.getValue() == null || property.getValue().equals(BROWSER_TIMEZONE)) {
101             // sanity check — make sure there's a first value up for grabs
102             if (select.getItemIds() != null && !select.getItemIds().isEmpty()) {
103                 property.setValue(select.getItemIds().iterator().next());
104             }
105         }
106     }
107 
108     @Override
109     public List<SelectFieldOptionDefinition> getOptions() {
110         final List<SelectFieldOptionDefinition> options = new ArrayList<>();
111 
112         TimeZone defaultTimeZone = TimeZone.getDefault();
113         String defaultLabel = i18n.translate("ui-admincentral.editUserProfile.preferences.timezone.default", defaultTimeZone.getDisplayName(context.getLocale()));
114         SelectFieldOptionDefinition defaultOption = new SelectFieldOptionDefinition();
115         defaultOption.setName("default");
116         defaultOption.setValue(EMPTY);
117         defaultOption.setLabel(defaultLabel);
118         options.add(defaultOption);
119 
120         for (String timeZoneStr : TimeZone.getAvailableIDs()) {
121             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
122             TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr);
123             option.setValue(timeZoneStr);
124             option.setLabel(timeZoneStr + " (" + timeZone.getDisplayName(false, TimeZone.LONG, context.getLocale()) + ")");
125             options.add(option);
126         }
127         return options;
128     }
129 
130     /**
131      * Definition for {@link TimeZoneFieldFactory}.
132      */
133     public static class Definition extends SelectFieldDefinition {
134         public Definition() {
135             setSortOptions(false); // We want to have the default timezone at top
136             setFilteringMode(FilteringMode.CONTAINS);
137             setTextInputAllowed(true);
138         }
139     }
140 }