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  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.vaadin.form.FormSection;
39  import info.magnolia.ui.vaadin.form.FormViewReduced;
40  import info.magnolia.ui.vaadin.layout.SmallAppLayout;
41  
42  import java.util.HashMap;
43  import java.util.Map;
44  
45  import javax.inject.Inject;
46  
47  import com.vaadin.shared.ui.MarginInfo;
48  import com.vaadin.ui.Button;
49  import com.vaadin.ui.Component;
50  import com.vaadin.ui.ComponentContainer;
51  import com.vaadin.ui.HorizontalLayout;
52  import com.vaadin.ui.Label;
53  import com.vaadin.ui.NativeButton;
54  import com.vaadin.ui.VerticalLayout;
55  
56  /**
57   * Implementation of {@link RSSAggregatorConfigurationView}.
58   */
59  public class RSSAggregatorConfigurationViewImpl implements RSSAggregatorConfigurationView {
60      public static final String APPLY = "rssAggregator.config.apply.label";
61  
62      public static final String MANUAL_PLANET_UPDATE_BUTTON = "rssaggregator.planet.update";
63      public static final String MANUAL_STATS_UPDATE_BUTTON = "rssaggregator.planet.statistics.update";
64      public static final String MANUAL_UPDATE_BUTTON= "rssaggregator.planet.update";
65  
66      public static final String RSSAGGREGATOR_CONFIG = "rssaggregator.title";
67      public static final String PLANET_RSSAGGREGATOR_CONFIG = "rssaggregator.planet.title";
68  
69      public static final String PLANET_AUTO_UPDATE = "rssaggregator.planet.automatic_update";
70      public static final String STATS_AUTO_UPDATE = "rssaggregator.planet.statistics.automatic_update";
71      public static final String RSS_AUTO_UPDATE = "rssaggregator.automatic_update";
72  
73      private SimpleTranslator translator;
74  
75      private ComponentProvider provider;
76  
77      private PeriodSelectorComponent automaticFeedDataUpdate;
78  
79      private PeriodSelectorComponent planetFeedDataPeriodicUpdate;
80  
81      private PeriodSelectorComponent planetStatsDataUpdate;
82  
83      private SmallAppLayout subAppLayout = new SmallAppLayout();
84  
85      private Listener listener;
86  
87      private Map<String, Component> sectionMap = new HashMap<String, Component>();
88  
89      @Inject
90      public RSSAggregatorConfigurationViewImpl(SimpleTranslator translator, ComponentProvider provider) {
91          this.translator = translator;
92          this.provider = provider;
93  
94          subAppLayout.setSizeFull();
95  
96          this.automaticFeedDataUpdate = new PeriodSelectorComponent(translator);
97          this.planetFeedDataPeriodicUpdate = new PeriodSelectorComponent(translator);
98          this.planetStatsDataUpdate = new PeriodSelectorComponent(translator);
99  
100         this.automaticFeedDataUpdate.setCaption(translator.translate(RSS_AUTO_UPDATE));
101         this.planetStatsDataUpdate.setCaption(translator.translate(STATS_AUTO_UPDATE));
102         this.planetFeedDataPeriodicUpdate.setCaption(translator.translate(PLANET_AUTO_UPDATE));
103 
104         populateRSSAggregatorConfig();
105         populatePlanetRSSAggregatorConfig();
106     }
107 
108     private void populatePlanetRSSAggregatorConfig() {
109         ComponentContainer section = prepareConfigSection(PLANET_RSSAGGREGATOR_CONFIG);
110         section.addComponent(planetFeedDataPeriodicUpdate);
111         section.addComponent(planetStatsDataUpdate);
112 
113         final HorizontalLayout buttons = createButtonsLayout();
114         Component applyButton = createButtonControl(APPLY, new Button.ClickListener() {
115             @Override
116             public void buttonClick(Button.ClickEvent event) {
117                 listener.onAutomaticPlanetUpdateChanged(planetFeedDataPeriodicUpdate.getValue());
118                 listener.onPlanetStatisticsAutomaticUpdatePeriodChanged(planetStatsDataUpdate.getValue());
119             }
120         });
121 
122         applyButton.addStyleName("commit");
123         buttons.addComponent(applyButton);
124         buttons.addComponent(createButtonControl(MANUAL_PLANET_UPDATE_BUTTON, new Button.ClickListener() {
125             @Override
126             public void buttonClick(Button.ClickEvent event) {
127                 listener.onPlanetDataManualUpdate();
128             }
129         }));
130 
131         buttons.addComponent(createButtonControl(MANUAL_STATS_UPDATE_BUTTON, new Button.ClickListener() {
132             @Override
133             public void buttonClick(Button.ClickEvent event) {
134                 listener.onPlanetStatisticsManualUpdate();
135             }
136         }));
137 
138         section.addComponent(buttons);
139     }
140 
141     private void populateRSSAggregatorConfig() {
142         final ComponentContainer formSection = prepareConfigSection(RSSAGGREGATOR_CONFIG);
143         final HorizontalLayout buttons = createButtonsLayout();
144 
145         Component applyButton = createButtonControl(APPLY, new Button.ClickListener() {
146             @Override
147             public void buttonClick(Button.ClickEvent event) {
148                 listener.onAutomaticImportChanged(automaticFeedDataUpdate.getValue());
149             }
150         });
151 
152         applyButton.addStyleName("commit");
153         buttons.addComponent(applyButton);
154 
155         buttons.addComponent(createButtonControl(MANUAL_UPDATE_BUTTON, new Button.ClickListener() {
156             @Override
157             public void buttonClick(Button.ClickEvent event) {
158                 listener.onManualImport();
159             }
160         }));
161 
162         formSection.addComponent(automaticFeedDataUpdate);
163         formSection.addComponent(buttons);
164 
165     }
166 
167     @Override
168     public Component asVaadinComponent() {
169         return subAppLayout;
170     }
171 
172     @Override
173     public void setListener(Listener listener) {
174         this.listener = listener;
175     }
176 
177     @Override
178     public void setPlanetConfigVisible(boolean isPlanetConfigVisible) {
179         sectionMap.get(PLANET_RSSAGGREGATOR_CONFIG).setVisible(isPlanetConfigVisible);
180     }
181 
182     @Override
183     public void setDataImportPeriod(int periodMinutes) {
184         automaticFeedDataUpdate.setValue(periodMinutes);
185     }
186 
187     @Override
188     public void setPlanetStatsImportPeriod(int periodMinutes) {
189         planetFeedDataPeriodicUpdate.setValue(periodMinutes);
190     }
191 
192     @Override
193     public void setPlanetDataImportPeriod(int periodMinutes) {
194         planetStatsDataUpdate.setValue(periodMinutes);
195     }
196 
197     private Component createButtonControl(String caption, Button.ClickListener clickListener) {
198         NativeButton button = new NativeButton(translator.translate(caption), clickListener);
199         button.addStyleName("btn-dialog");
200         return button;
201     }
202 
203     private HorizontalLayout createButtonsLayout() {
204         final HorizontalLayout buttons = new HorizontalLayout();
205         buttons.setSpacing(true);
206         buttons.setMargin(new MarginInfo(true, false, false, false));
207         return buttons;
208     }
209 
210     private ComponentContainer prepareConfigSection(String sectionCaptionKey) {
211         final VerticalLayout sectionRoot = new VerticalLayout();
212         final Label title = new Label(translator.translate(sectionCaptionKey));
213         title.addStyleName("section-title");
214         sectionRoot.addComponent(title);
215         sectionMap.put(sectionCaptionKey, sectionRoot);
216 
217         final FormViewReduced formView = provider.getComponent(FormViewReduced.class);
218         final FormSection formSection = new FormSection();
219         formView.addFormSection("", formSection);
220         sectionRoot.addComponent(formView.asVaadinComponent());
221         subAppLayout.addSection(sectionRoot);
222         sectionRoot.setSpacing(true);
223 
224         return formSection;
225     }
226 
227 }