View Javadoc
1   /**
2    * This file Copyright (c) 2016 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.about.app.config;
35  
36  import info.magnolia.about.app.mapping.VirtualURIMappingViewImpl;
37  import info.magnolia.cms.beans.config.ServerConfiguration;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.init.MagnoliaConfigurationProperties;
40  import info.magnolia.ui.vaadin.layout.SmallAppLayout;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Comparator;
45  import java.util.List;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  import com.vaadin.annotations.StyleSheet;
52  import com.vaadin.data.util.AbstractBeanContainer;
53  import com.vaadin.data.util.BeanItemContainer;
54  import com.vaadin.data.util.filter.SimpleStringFilter;
55  import com.vaadin.event.FieldEvents;
56  import com.vaadin.ui.Component;
57  import com.vaadin.ui.Grid;
58  import com.vaadin.ui.Label;
59  import com.vaadin.ui.TextField;
60  import com.vaadin.ui.VerticalLayout;
61  import com.vaadin.ui.renderers.HtmlRenderer;
62  
63  /**
64   * View implementation.
65   */
66  @StyleSheet("vaadin://about-app.css")
67  public class ConfigInfoViewImpl extends SmallAppLayout implements ConfigInfoView {
68  
69      private static final String CONFIG = "config";
70      private static final String VALUE = "value";
71      private static final String CONFIG_HTML = "configHtml";
72      private static final String VALUE_HTML = "valueHtml";
73  
74      private final SimpleTranslator i18n;
75      private final ServerConfiguration serverConfiguration;
76      private final MagnoliaConfigurationProperties magnoliaConfigurationProperties;
77  
78      @Inject
79      public ConfigInfoViewImpl(SimpleTranslator i18n, ServerConfiguration serverConfiguration, MagnoliaConfigurationProperties magnoliaConfigurationProperties) {
80          this.i18n = i18n;
81          this.serverConfiguration = serverConfiguration;
82          this.magnoliaConfigurationProperties = magnoliaConfigurationProperties;
83  
84          addSection(setUpLayout());
85      }
86  
87      private VerticalLayout setUpLayout() {
88          VerticalLayout layout = new VerticalLayout();
89          layout.setStyleName("about-app");
90  
91          Label sectionTitle = new Label(i18n.translate("about.app.config.configinfo.title"));
92          sectionTitle.addStyleName("section-title");
93  
94          layout.addComponent(sectionTitle);
95  
96          Label serverDataTitle = new Label(i18n.translate("about.app.config.configinfo.serverdata.title"));
97          serverDataTitle.addStyleName("fieldset-title");
98          serverDataTitle.setHeight(20, Unit.PIXELS);
99  
100         layout.addComponent(serverDataTitle);
101 
102         String isAdminServer = String.valueOf(this.serverConfiguration.isAdmin());
103         Label isAdminServerLabel = new Label(i18n.translate("about.app.config.configinfo.serverdata.isadmin") + ": " + isAdminServer);
104         isAdminServerLabel.setHeight(40, Unit.PIXELS);
105 
106         layout.addComponent(isAdminServerLabel);
107 
108         Label systemDataTitle = new Label(i18n.translate("about.app.config.configinfo.systemdata.title"));
109         systemDataTitle.addStyleName("fieldset-title");
110         systemDataTitle.setHeight(30, Unit.PIXELS);
111         layout.addComponent(systemDataTitle);
112 
113         Grid systemDataGrid = setUpSystemDataGrid();
114 
115         layout.addComponent(systemDataGrid);
116         layout.setExpandRatio(systemDataGrid, 1f);
117 
118         layout.setSizeFull();
119 
120         return layout;
121     }
122 
123     private Grid setUpSystemDataGrid() {
124         List<ConfigInfoBean> configInfoBeans = new ArrayList<>();
125         for (String config : magnoliaConfigurationProperties.getKeys()) {
126             String value = magnoliaConfigurationProperties.getProperty(config);
127             String configHtml = String.format(VirtualURIMappingViewImpl.SPAN_TEMPLATE, config, config);
128             String valueHtml = String.format(VirtualURIMappingViewImpl.SPAN_TEMPLATE, value, value);
129             configInfoBeans.add(new ConfigInfoBean(config, value, configHtml, valueHtml));
130         }
131 
132         Collections.sort(configInfoBeans, new Comparator<ConfigInfoBean>() {
133             @Override
134             public int compare(ConfigInfoBean configInfoBean1, ConfigInfoBean configInfoBean2) {
135                 return configInfoBean1.getConfig().compareTo(configInfoBean2.getConfig());
136             }
137         });
138 
139         BeanItemContainer<ConfigInfoBean> container = new BeanItemContainer<>(ConfigInfoBean.class, configInfoBeans);
140 
141         Grid grid = new Grid(container);
142 
143         grid.getDefaultHeaderRow().setStyleName("about-app-header");
144         grid.setWidth(100, Unit.PERCENTAGE);
145         grid.setHeight(100, Unit.PERCENTAGE);
146 
147         grid.removeColumn(CONFIG);
148         grid.removeColumn(VALUE);
149 
150         grid.getColumn(CONFIG_HTML)
151                 .setWidth(250)
152                 .setHeaderCaption(i18n.translate("about.app.config.configinfo.systemdata.config"))
153                 .setRenderer(new HtmlRenderer());
154 
155         grid.getColumn(VALUE_HTML)
156                 .setWidth(570)
157                 .setHeaderCaption(i18n.translate("about.app.config.configinfo.systemdata.value"))
158                 .setRenderer(new HtmlRenderer());
159 
160         grid.setSelectionMode(Grid.SelectionMode.NONE);
161 
162         setGridFilter(grid);
163 
164         grid.setCellStyleGenerator(new Grid.CellStyleGenerator() {
165             @Override
166             public String getStyle(Grid.CellReference cellReference) {
167                 if (CONFIG_HTML.equals(cellReference.getPropertyId())) {
168                     return "about-app-grid-config-cell";
169                 }
170                 if (VALUE_HTML.equals(cellReference.getPropertyId())) {
171                     return "about-app-grid-value-cell";
172                 }
173                 return null;
174             }
175         });
176 
177         grid.setSizeFull();
178 
179         return grid;
180     }
181 
182     private void setGridFilter(final Grid grid) {
183         final AbstractBeanContainer container = (AbstractBeanContainer) grid.getContainerDataSource();
184 
185         // Create a header row to hold column filters
186         Grid.HeaderRow filterRow = grid.appendHeaderRow();
187         filterRow.setStyleName("about-app-grid-header");
188 
189         // Set up a filter for all columns
190         for (final Object pid : container.getContainerPropertyIds()) {
191             if (pid.equals(CONFIG) || pid.equals(VALUE)) {
192                 continue;
193             }
194             Grid.HeaderCell cell = filterRow.getCell(pid);
195 
196             // Have an input field to use for filter
197             TextField filterField = new TextField();
198             filterField.setWidth(100, Unit.PERCENTAGE);
199 
200             // Update filter when the filter input is changed
201             filterField.addTextChangeListener(new FieldEvents.TextChangeListener() {
202                 @Override
203                 public void textChange(FieldEvents.TextChangeEvent event) {
204                     String propertyId;
205                     if (pid.equals(CONFIG_HTML) || pid.equals(VALUE_HTML)) {
206                         propertyId = StringUtils.removeEnd((String) pid, "Html");
207                     } else {
208                         propertyId = (String) pid;
209                     }
210                     container.removeContainerFilters(propertyId);
211 
212                     // (Re)create the filter if necessary
213                     if (!event.getText().isEmpty()) {
214                         container.addContainerFilter(new SimpleStringFilter(propertyId, event.getText(), true, false));
215                     }
216                 }
217             });
218             cell.setComponent(filterField);
219         }
220     }
221 
222     @Override
223     public Component asVaadinComponent() {
224         return this;
225     }
226 
227     /**
228      * Config info bean class.
229      */
230     public static class ConfigInfoBean {
231         private final String config;
232         private final String value;
233         private final String configHtml;
234         private final String valueHtml;
235 
236         public ConfigInfoBean(String config, String value, String configHtml, String valueHtml) {
237             this.config = config;
238             this.value = value;
239             this.configHtml = configHtml;
240             this.valueHtml = valueHtml;
241         }
242 
243         public String getConfig() {
244             return config;
245         }
246 
247         public String getValue() {
248             return value;
249         }
250 
251         public String getConfigHtml() {
252             return configHtml;
253         }
254 
255         public String getValueHtml() {
256             return valueHtml;
257         }
258     }
259 }