1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61 public class TimeZoneFieldFactory extends SelectFieldFactory<TimeZoneFieldFactory.Definition> {
62
63
64
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
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
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
132
133 public static class Definition extends SelectFieldDefinition {
134 public Definition() {
135 setSortOptions(false);
136 setFilteringMode(FilteringMode.CONTAINS);
137 setTextInputAllowed(true);
138 }
139 }
140 }