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.module.rssaggregator.app.subapps.aggregationconfig.view;
35
36 import info.magnolia.i18nsystem.SimpleTranslator;
37
38 import com.vaadin.data.Property;
39 import com.vaadin.ui.Alignment;
40 import com.vaadin.ui.Component;
41 import com.vaadin.ui.CssLayout;
42 import com.vaadin.ui.CustomField;
43 import com.vaadin.ui.HorizontalLayout;
44 import com.vaadin.ui.Label;
45 import com.vaadin.ui.NativeSelect;
46 import com.vaadin.ui.OptionGroup;
47 import com.vaadin.ui.VerticalLayout;
48
49
50
51
52 class PeriodSelectorComponent extends CustomField<Integer> {
53
54 public static final String REPEAT_EVERY = "rssaggregator.repeat";
55
56 public static final String MINUTES = "rssaggregator.repeat.minutes";
57
58 public static final String DISABLE_PERIODIC_UPDATE = "rssaggregator.auto_update.disable";
59
60 public static final int[] PERIOD_VARIANTS = new int[]{5, 10, 15, 30, 45};
61
62 private VerticalLayout root = new VerticalLayout();
63
64 private OptionGroup disableSelection = new OptionGroup() {
65 @Override
66 public String getItemCaption(Object itemId) {
67 return "";
68 }
69 };
70
71 private OptionGroup periodSelection = new OptionGroup() {
72 @Override
73 public String getItemCaption(Object itemId) {
74 return "";
75 }
76 };
77
78 private NativeSelect combo = createPeriodicComboControl();
79
80 private SimpleTranslator translator;
81
82 PeriodSelectorComponent(SimpleTranslator translator) {
83 this.translator = translator;
84 disableSelection.setImmediate(true);
85 periodSelection.setImmediate(true);
86 disableSelection.addItem(true);
87 periodSelection.addItem(true);
88
89 HorizontalLayout optionEntry = new HorizontalLayout();
90 optionEntry.addComponent(disableSelection);
91 Label label = new Label(translator.translate(DISABLE_PERIODIC_UPDATE));
92 optionEntry.addComponent(label);
93 optionEntry.setSpacing(true);
94 optionEntry.setComponentAlignment(disableSelection, Alignment.MIDDLE_RIGHT);
95 optionEntry.setComponentAlignment(label, Alignment.MIDDLE_RIGHT);
96
97 root.addComponent(optionEntry);
98
99 optionEntry = new HorizontalLayout();
100 optionEntry.addComponent(periodSelection);
101 Component automaticUpdatePeriodControl = createAutomaticUpdatePeriodControl(combo);
102 optionEntry.addComponent(automaticUpdatePeriodControl);
103 optionEntry.setSpacing(true);
104 optionEntry.setComponentAlignment(periodSelection, Alignment.MIDDLE_RIGHT);
105 optionEntry.setComponentAlignment(automaticUpdatePeriodControl, Alignment.MIDDLE_RIGHT);
106 root.addComponent(optionEntry);
107
108 disableSelection.addValueChangeListener(new ValueChangeListener() {
109 @Override
110 public void valueChange(Property.ValueChangeEvent event) {
111 if (event.getProperty().getValue() != null) {
112 periodSelection.setValue(null);
113 }
114 }
115 });
116
117 periodSelection.addValueChangeListener(new ValueChangeListener() {
118 @Override
119 public void valueChange(Property.ValueChangeEvent event) {
120 if (event.getProperty().getValue() != null) {
121 disableSelection.setValue(null);
122 }
123 }
124 });
125
126 combo.addValueChangeListener(new ValueChangeListener() {
127 @Override
128 public void valueChange(Property.ValueChangeEvent event) {
129 setInternalValue((Integer) event.getProperty().getValue());
130 }
131 });
132 }
133
134 @Override
135 protected void setInternalValue(Integer newValue) {
136 super.setInternalValue(newValue);
137 if (newValue == null || newValue <= 0) {
138 disableSelection.setValue(true);
139 periodSelection.setValue(null);
140 combo.setValue(null);
141 } else {
142 disableSelection.setValue(null);
143 periodSelection.setValue(true);
144 if (combo.getItem(newValue) == null) {
145 combo.addItem(newValue);
146 }
147 combo.setValue(newValue);
148 }
149 }
150
151 @Override
152 protected Integer getInternalValue() {
153 if (disableSelection.getValue() != null && (Boolean)disableSelection.getValue()) {
154 return 0;
155 } else {
156 return (Integer)combo.getValue();
157 }
158 }
159
160 @Override
161 protected Component initContent() {
162 return root;
163 }
164
165 @Override
166 public Class<? extends Integer> getType() {
167 return Integer.class;
168 }
169
170 private NativeSelect createPeriodicComboControl() {
171 final NativeSelect combo = new NativeSelect();
172 combo.setImmediate(true);
173 combo.setNullSelectionAllowed(false);
174 combo.setWidth("50px");
175 for (int period : PERIOD_VARIANTS) {
176 combo.addItem(period);
177 }
178 return combo;
179 }
180
181 private Component createAutomaticUpdatePeriodControl(NativeSelect combo) {
182 final CssLayout wrapper = new CssLayout() {
183 @Override
184 protected String getCss(Component c) {
185 StringBuilder sb = new StringBuilder("display: inline-block; min-width: 50px;");
186 if (getComponentIndex(c) > 0) {
187 sb.append("margin-left: 5px");
188 }
189 return sb.toString();
190 }
191 };
192 wrapper.setWidth("100%");
193 Label captionLabel = new Label(translator.translate(REPEAT_EVERY));
194 captionLabel.setSizeUndefined();
195 wrapper.addComponent(captionLabel);
196 wrapper.addComponent(combo);
197
198 Label minutesLabel = new Label(translator.translate(MINUTES));
199 minutesLabel.setSizeUndefined();
200 wrapper.addComponent(minutesLabel);
201
202 return wrapper;
203 }
204 }