View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.ui.vaadin.layout;
35  
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Locale;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import com.vaadin.data.Property;
44  import com.vaadin.data.Property.ValueChangeEvent;
45  import com.vaadin.data.Property.ValueChangeListener;
46  import com.vaadin.data.util.ObjectProperty;
47  import com.vaadin.data.util.converter.Converter;
48  import com.vaadin.shared.ui.MarginInfo;
49  import com.vaadin.shared.ui.label.ContentMode;
50  import com.vaadin.ui.Component;
51  import com.vaadin.ui.CssLayout;
52  import com.vaadin.ui.Label;
53  import com.vaadin.ui.VerticalLayout;
54  
55  /**
56   * The SmallAppLayout, offering space for a header component and multiple sections stacked vertically.
57   */
58  public class SmallAppLayout extends VerticalLayout {
59  
60      private Property<String> description = new ObjectProperty<String>("");
61  
62      private VerticalLayout root = this;
63  
64      private CssLayout sectionsLayout = new CssLayout();
65  
66      private Label descriptionLabel = new Label();
67  
68      public SmallAppLayout() {
69          root.addStyleName("smallapp");
70          root.setSizeFull();
71          root.setMargin(new MarginInfo(true, false, false, false));
72  
73          descriptionLabel.addStyleName("description");
74          descriptionLabel.setContentMode(ContentMode.HTML);
75          descriptionLabel.setConverter(new StringToHtmlConverter());
76          descriptionLabel.setPropertyDataSource(description);
77          descriptionLabel.addValueChangeListener(new ValueChangeListener() {
78  
79              @Override
80              public void valueChange(ValueChangeEvent event) {
81                  if (StringUtils.isNotBlank((String) event.getProperty().getValue())) {
82                      descriptionLabel.setVisible(true);
83                  } else {
84                      descriptionLabel.setVisible(false);
85                  }
86              }
87          });
88          root.addComponent(descriptionLabel);
89  
90          sectionsLayout.addStyleName("smallapp-sections");
91          sectionsLayout.setSizeFull();
92          root.addComponent(sectionsLayout);
93          root.setExpandRatio(sectionsLayout, 1);
94      }
95  
96      @Override
97      public String getDescription() {
98          return description.getValue();
99      }
100 
101     @Override
102     public void setDescription(String description) {
103         this.description.setValue(description);
104     }
105 
106     public List<Component> getSections() {
107         Iterator<Component> it = sectionsLayout.iterator();
108         List<Component> sections = new ArrayList<Component>();
109         while (it.hasNext()) {
110             sections.add(it.next());
111         }
112         return sections;
113     }
114 
115     public void addSection(Component section) {
116         section.addStyleName("smallapp-section");
117         sectionsLayout.addComponent(section);
118     }
119 
120     public void removeSection(Component section) {
121         sectionsLayout.removeComponent(section);
122     }
123 
124     /**
125      * The StringToHtmlConverter, currently only turns carriage-returns into HTML br elements.
126      */
127     private class StringToHtmlConverter implements Converter<String, String> {
128 
129         @Override
130         public String convertToModel(String value, Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
131             return value.replaceAll("<br/>", "\n");
132         }
133 
134         @Override
135         public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
136             if (value != null) {
137                 return value.replaceAll("\n", "<br/>");
138             }
139             return null;
140         }
141 
142         @Override
143         public Class<String> getModelType() {
144             return String.class;
145         }
146 
147         @Override
148         public Class<String> getPresentationType() {
149             return String.class;
150         }
151 
152     }
153 
154 }