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.Notification.DELAY_FOREVER;
38  import static com.vaadin.ui.themes.ValoTheme.BUTTON_LINK;
39  
40  import info.magnolia.icons.MagnoliaIcons;
41  import info.magnolia.ui.AlertBuilder;
42  import info.magnolia.ui.theme.ResurfaceTheme;
43  
44  import java.util.Arrays;
45  import java.util.stream.StreamSupport;
46  
47  import javax.servlet.annotation.WebServlet;
48  
49  import com.vaadin.annotations.Theme;
50  import com.vaadin.annotations.Title;
51  import com.vaadin.annotations.VaadinServletConfiguration;
52  import com.vaadin.annotations.Widgetset;
53  import com.vaadin.data.provider.ListDataProvider;
54  import com.vaadin.server.Page;
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.ComboBox;
60  import com.vaadin.ui.CssLayout;
61  import com.vaadin.ui.FormLayout;
62  import com.vaadin.ui.NativeButton;
63  import com.vaadin.ui.Notification;
64  import com.vaadin.ui.TextField;
65  import com.vaadin.ui.TwinColSelect;
66  import com.vaadin.ui.UI;
67  
68  @Theme("poctheme")
69  @Title("Buttons - Magnolia Resurface theme")
70  @Widgetset("info.magnolia.poc.Widgetset")
71  public class ButtonsUI extends UI {
72  
73      @Override
74      protected void init(VaadinRequest vaadinRequest) {
75          FormLayout layout = new FormLayout();
76          layout.setMargin(true);
77          layout.setWidth(926, PIXELS);
78  
79          TextField textField = new TextField("Text field");
80          textField.setValue("Foo bar");
81  
82          ComboBox<String> selectField = new ComboBox<>("Select field");
83          ListDataProvider<String> dataProvider = new ListDataProvider<>(Arrays.asList("Bullet list", "Inline list", "Ordered list"));
84          selectField.setEmptySelectionAllowed(false);
85          selectField.setPlaceholder("Select...");
86          selectField.setDataProvider(dataProvider);
87  
88          TwinColSelect<String> twinColSelect = new TwinColSelect<>("Twin-column field");
89          twinColSelect.setItems("Celebrities", "Holidays", "Local", " Pets", "Sports");
90          twinColSelect.setLeftColumnCaption("From");
91          twinColSelect.setRightColumnCaption("To");
92          twinColSelect.select("Sports");
93  
94          CssLayout regularButtons = new CssLayout(
95                  new Button("Deal with it"),
96                  new Button("Checkout", MagnoliaIcons.LOCK),
97                  styledButton("Edit profile", MagnoliaIcons.EDIT_WO, BUTTON_LINK),
98                  styledButton(MagnoliaIcons.EDIT_WO, ResurfaceTheme.BUTTON_ICON),
99                  new NativeButton("Native button")
100         );
101         regularButtons.addStyleName(ResurfaceTheme.CSSLAYOUT_PADDED);
102         CssLayout actionButtons = new CssLayout(
103                 styledButton("Primary button", ResurfaceTheme.BUTTON_PRIMARY),
104                 styledButton("Secondary button", ResurfaceTheme.BUTTON_SECONDARY)
105         );
106         actionButtons.addStyleName(ResurfaceTheme.CSSLAYOUT_PADDED);
107 
108         layout.addComponents(
109                 textField,
110                 selectField,
111                 twinColSelect,
112                 regularButtons,
113                 actionButtons,
114                 dialogButtons(Notification.Type.HUMANIZED_MESSAGE),
115                 dialogButtons(Notification.Type.WARNING_MESSAGE),
116                 dialogButtons(Notification.Type.ERROR_MESSAGE)
117         );
118 
119         StreamSupport.stream(layout.spliterator(), false)
120                 .filter(component -> !(component instanceof Button))
121                 .forEach(component -> component.setWidth(100, PERCENTAGE));
122 
123         setContent(layout);
124     }
125 
126     private CssLayout dialogButtons(Notification.Type level) {
127         Button yesButton = new Button("Yes", event -> {
128             AlertBuilder.confirmDialog("Dialog")
129                     .withBody(" \"What\" ain't no country I've ever heard of. They speak English in What?")
130                     .withOkButtonCaption("What?")
131                     .withDeclineButtonCaption("No")
132                     .withLevel(level)
133                     .buildAndOpen();
134         });
135         yesButton.addStyleNames(ResurfaceTheme.BUTTON_PRIMARY);
136         CssLayout dialogButts = new CssLayout(
137                 new Button("No", event -> {
138                     Notification notification = new Notification("Deal with it", level);
139                     notification.setDelayMsec(DELAY_FOREVER);
140                     notification.show(Page.getCurrent());
141                 }),
142                 yesButton);
143         String levelStyleName;
144         switch (level) {
145         case WARNING_MESSAGE:
146             levelStyleName = "warning";
147             break;
148         case ERROR_MESSAGE:
149             levelStyleName = "error";
150             break;
151         default:
152             levelStyleName = "humanized";
153         }
154         dialogButts.addStyleNames(ResurfaceTheme.CSSLAYOUT_PADDED, "alert", "inline-alert-sample", levelStyleName);
155         return dialogButts;
156     }
157 
158     private static Button styledButton(String caption, String... styleNames) {
159         Button button = new Button(caption);
160         button.addStyleNames(styleNames);
161         return button;
162     }
163 
164     private static Button styledButton(Resource icon, String... styleNames) {
165         Button button = new Button(icon);
166         button.addStyleNames(styleNames);
167         return button;
168     }
169 
170     private static Button styledButton(String caption, Resource icon, String... styleNames) {
171         Button button = new Button(caption, icon);
172         button.addStyleNames(styleNames);
173         return button;
174     }
175 
176     @WebServlet(value = "/buttons/*", displayName = "Buttons", asyncSupported = true)
177     @VaadinServletConfiguration(productionMode = false, ui = ButtonsUI.class)
178     public static class Servlet extends VaadinServlet {
179     }
180 }