View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.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   * Custom field allowing for setting the period of rss feed data/statistics update.
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 }