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