View Javadoc
1   /**
2    * This file Copyright (c) 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.poc;
35  
36  import static com.vaadin.server.Sizeable.Unit.*;
37  import static com.vaadin.ui.themes.ValoTheme.BUTTON_LINK;
38  import static info.magnolia.ui.theme.ResurfaceTheme.BUTTON_ICON;
39  
40  import info.magnolia.icons.MagnoliaIcons;
41  import info.magnolia.ui.theme.ResurfaceTheme;
42  
43  import java.util.Arrays;
44  import java.util.stream.StreamSupport;
45  
46  import javax.servlet.annotation.WebServlet;
47  
48  import com.vaadin.annotations.Theme;
49  import com.vaadin.annotations.Title;
50  import com.vaadin.annotations.VaadinServletConfiguration;
51  import com.vaadin.annotations.Widgetset;
52  import com.vaadin.data.Binder;
53  import com.vaadin.data.ValidationResult;
54  import com.vaadin.data.provider.ListDataProvider;
55  import com.vaadin.server.Resource;
56  import com.vaadin.server.VaadinRequest;
57  import com.vaadin.server.VaadinServlet;
58  import com.vaadin.ui.Button;
59  import com.vaadin.ui.CheckBoxGroup;
60  import com.vaadin.ui.ComboBox;
61  import com.vaadin.ui.CssLayout;
62  import com.vaadin.ui.FormLayout;
63  import com.vaadin.ui.Label;
64  import com.vaadin.ui.NativeButton;
65  import com.vaadin.ui.RadioButtonGroup;
66  import com.vaadin.ui.TabSheet;
67  import com.vaadin.ui.TextArea;
68  import com.vaadin.ui.TextField;
69  import com.vaadin.ui.TwinColSelect;
70  import com.vaadin.ui.UI;
71  
72  @Theme("poctheme")
73  @Title("Select fields & Option groups - Magnolia Resurface theme")
74  @Widgetset("info.magnolia.poc.Widgetset")
75  public class OptionFieldsComponentUI extends UI {
76  
77      @Override
78      protected void init(VaadinRequest vaadinRequest) {
79  
80          FormLayout layout = new FormLayout();
81          layout.setMargin(true);
82          layout.setWidth(926, PIXELS);
83  
84          Binder<String> binder = new Binder<>();
85  
86          TextField textField = new TextField("Text field");
87          textField.setValue("Foo bar");
88          binder.forField(textField)
89                  .asRequired()
90                  .withValidator(s -> s.length() > 1, "Must be longer than 1 char")
91                  .bind(source -> "Foo bar",
92                          (bean, value) -> System.out.println(value));
93  
94          ComboBox<String> selectWithoutField = new ComboBox<>("Select field");
95          selectWithoutField.setPlaceholder("Select...");
96          selectWithoutField.setEmptySelectionAllowed(false);
97  
98          ComboBox<String> selectField = new ComboBox<>("Select field");
99          ListDataProvider<String> dataProvider = new ListDataProvider<>(Arrays.asList("Bullet list", "Inline list", "Ordered list"));
100         selectField.setEmptySelectionAllowed(false);
101         selectField.setPlaceholder("Select...");
102         selectField.setDataProvider(dataProvider);
103         binder.forField(selectField)
104                 .asRequired()
105                 .withValidator((value, valueContext) -> value.equals("Bullet list") ?
106                         ValidationResult.ok() :
107                         ValidationResult.error("Not a bullet list!"))
108                 .bind(source -> null,
109                         (bean, value) -> System.out.println(value));
110 
111         CheckBoxGroup<String> checkBoxGroup = new CheckBoxGroup<>("CheckBox field");
112         checkBoxGroup.setItems("Select to display title");
113 
114         CheckBoxGroup<String> optionGroupFieldCheck = new CheckBoxGroup<>("Option group field check");
115         optionGroupFieldCheck.setItems("Navigation Title", "None", "Page title");
116         optionGroupFieldCheck.select("Page title");
117 
118         RadioButtonGroup<String> optionGroupFieldRadio = new RadioButtonGroup<>("Option group field radio");
119         optionGroupFieldRadio.setItems("Navigation Title", "None", "Page");
120         optionGroupFieldRadio.setSelectedItem("Page");
121 
122         TwinColSelect<String> twinColSelect = new TwinColSelect<>("Twin-column field");
123         twinColSelect.setItems("Celebrities", "Holidays", "Local", " Pets", "Sports");
124         twinColSelect.setLeftColumnCaption("From");
125         twinColSelect.setRightColumnCaption("To");
126         twinColSelect.select("Sports");
127 
128         TabSheet tab = new TabSheet();
129         tab.setCaption("Switchable field");
130         tab.addStyleName("switchable-field");
131         final TextArea plainText = new TextArea();
132         plainText.setWidth("100%");
133         plainText.setRows(10);
134         plainText.setValue("Soccis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. " +
135                 "Vestibulum id ligula porta felis euismod semper. " +
136                 "Nullam quis risus eget urna mollis ornare vel eu leo. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.");
137         tab.addTab(plainText, "Plain text");
138 
139         final TextField richText = new TextField();
140         richText.setWidth("100%");
141         tab.addTab(richText, "Rich text");
142 
143         Label dummy = new Label("");
144 
145         CssLayout buttons = new CssLayout(
146                 new Button("Deal with it"),
147                 new Button("Checkout", MagnoliaIcons.LOCK),
148                 styledButton("Primary button", ResurfaceTheme.BUTTON_PRIMARY),
149                 styledButton("Secondary button", ResurfaceTheme.BUTTON_SECONDARY),
150                 styledButton(MagnoliaIcons.EDIT_WO, BUTTON_ICON),
151                 styledButton("Edit profile", MagnoliaIcons.EDIT_WO, BUTTON_LINK),
152                 new NativeButton("Native button")
153         );
154         buttons.addStyleName(ResurfaceTheme.CSSLAYOUT_PADDED);
155 
156         layout.addComponents(
157                 textField,
158                 selectWithoutField,
159                 selectField,
160                 buttons,
161                 checkBoxGroup,
162                 optionGroupFieldCheck,
163                 optionGroupFieldRadio,
164                 twinColSelect,
165                 tab,
166                 dummy,
167                 new Button("Submit", event -> binder.validate()));
168 
169         StreamSupport.stream(layout.spliterator(), false)
170                 .filter(component -> !(component instanceof Button))
171                 .forEach(component -> component.setWidth(100, PERCENTAGE));
172 
173         setContent(layout);
174         textField.focus();
175     }
176 
177     private static Button styledButton(String caption, String... styleNames) {
178         Button button = new Button(caption);
179         button.addStyleNames(styleNames);
180         return button;
181     }
182 
183     private static Button styledButton(Resource icon, String... styleNames) {
184         Button button = new Button(icon);
185         button.addStyleNames(styleNames);
186         return button;
187     }
188 
189     private static Button styledButton(String caption, Resource icon, String... styleNames) {
190         Button button = new Button(caption, icon);
191         button.addStyleNames(styleNames);
192         return button;
193     }
194 
195     @WebServlet(value = "/select/*", displayName = "Select fields & Option groups", asyncSupported = true)
196     @VaadinServletConfiguration(productionMode = false, ui = OptionFieldsComponentUI.class)
197     public static class Servlet extends VaadinServlet {
198     }
199 }