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  
40  import org.apache.commons.lang3.StringUtils;
41  
42  import com.vaadin.shared.ui.ContentMode;
43  import com.vaadin.ui.Component;
44  import com.vaadin.ui.CssLayout;
45  import com.vaadin.ui.Label;
46  import com.vaadin.ui.VerticalLayout;
47  
48  /**
49   * The SmallAppLayout, offering space for a header component and multiple sections stacked vertically.
50   */
51  public class SmallAppLayout extends VerticalLayout {
52  
53      private final CssLayout sectionsLayout = new CssLayout();
54  
55      private final CssLayout descriptionLayout = new CssLayout();
56      private final Label description = new Label();
57  
58      public SmallAppLayout() {
59          addStyleName("smallapp");
60          setSizeFull();
61          setSpacing(false);
62          setMargin(false);
63  
64          description.addStyleName("smallapp-description");
65          description.setContentMode(ContentMode.HTML);
66          descriptionLayout.addStyleName("smallapp-description-layout");
67          descriptionLayout.setVisible(false);
68          descriptionLayout.addComponent(description);
69  
70          sectionsLayout.addStyleName("smallapp-sections");
71          sectionsLayout.setSizeFull();
72  
73          addComponents(descriptionLayout, sectionsLayout);
74          setExpandRatio(descriptionLayout, 0f);
75          setExpandRatio(sectionsLayout, 1f);
76      }
77  
78      @Override
79      public String getDescription() {
80          return description.getValue();
81      }
82  
83      @Override
84      public void setDescription(String description) {
85          this.description.setValue(description);
86          this.descriptionLayout.setVisible(StringUtils.isNoneBlank(description));
87      }
88  
89      public List<Component> getSections() {
90          Iterator<Component> it = sectionsLayout.iterator();
91          List<Component> sections = new ArrayList<>();
92          while (it.hasNext()) {
93              sections.add(it.next());
94          }
95          return sections;
96      }
97  
98      /**
99       * Add new section with title into small app layout.
100      */
101     public void addSection(Component section, String sectionTitle) {
102         section.setCaption(sectionTitle);
103         addSection(section);
104     }
105 
106     /**
107      * Add new section without a title into small app layout.
108      */
109     public void addSection(Component section) {
110         section.addStyleName("smallapp-section");
111         sectionsLayout.addComponent(section);
112     }
113 
114     public void removeSection(Component section) {
115         sectionsLayout.removeComponent(section);
116     }
117 }