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.Alignment.TOP_RIGHT;
38  
39  import info.magnolia.icons.MagnoliaIcons;
40  
41  import java.util.Arrays;
42  import java.util.List;
43  
44  import javax.servlet.annotation.WebServlet;
45  
46  import com.vaadin.annotations.Theme;
47  import com.vaadin.annotations.Title;
48  import com.vaadin.annotations.VaadinServletConfiguration;
49  import com.vaadin.annotations.Widgetset;
50  import com.vaadin.server.VaadinRequest;
51  import com.vaadin.server.VaadinServlet;
52  import com.vaadin.ui.Button;
53  import com.vaadin.ui.ComboBox;
54  import com.vaadin.ui.Component;
55  import com.vaadin.ui.HorizontalLayout;
56  import com.vaadin.ui.Layout;
57  import com.vaadin.ui.Notification;
58  import com.vaadin.ui.UI;
59  
60  @Theme("poctheme")
61  @Title("Magnolia 6 Resurface - Text Components")
62  @Widgetset("info.magnolia.poc.Widgetset")
63  public class DialogFooterUI extends UI {
64      private static final String TEXT_SHORT = "Nullam quis risus eget urna mollis ornare vel eu leo";
65      private static final String TEXT_ONELINE = TEXT_SHORT + ". Sed posuere consec tetur est at lobortis.";
66      private static final String TEXT_2_LINES = TEXT_ONELINE + " Cras justo odio, dapibus ac facilisis in, egestas eget quam.";
67      private static final String TEXT_3_LINES = TEXT_2_LINES + " Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.";
68      private static final String TEXT_FULL = TEXT_3_LINES + " Etiam porta sem malesuada magna mollis euismod.";
69  
70      @Override
71      protected void init(VaadinRequest vaadinRequest) {
72          // mandatory buttons
73          Button commit = new Button("Commit", DialogFooterUI::clickityClick);
74          commit.addStyleName("commit primary-button primary-actions");
75  
76          Button cancel = new Button("Cancel", DialogFooterUI::clickityClick);
77          cancel.addStyleName("cancel secondary-button primary-actions");
78  
79          final List<Component> primaryActions = Arrays.asList(commit, cancel);
80          final List<Component> secondaryActions = Arrays.asList(
81                  new ComboBox<>(null, Arrays.asList("One", "Two", "Three")),
82                  new Button("Download", MagnoliaIcons.DOWNLOAD));
83  
84          Layout footer = buildHorizontalFooter(primaryActions, secondaryActions);
85  
86          setContent(footer);
87      }
88  
89      private Layout buildHorizontalFooter(List<Component> primaryActions, List<Component> secondaryActions) {
90          HorizontalLayout footer = new HorizontalLayout();
91          footer.addStyleNames("actions");
92          footer.setWidth(100, PERCENTAGE);
93          footer.setHeight(75, PIXELS);
94          footer.setSpacing(true);
95          footer.setMargin(false);
96  
97          primaryActions.forEach(component -> {
98              // add primary actions in reverse order since they'll be right-aligned
99              footer.addComponentAsFirst(component);
100             footer.setExpandRatio(component, 0);
101         });
102 
103         // use the last primary action as a spacer
104         Component lastPrimaryAction = primaryActions.get(primaryActions.size() - 1);
105         footer.setExpandRatio(lastPrimaryAction, 1);
106         footer.setComponentAlignment(lastPrimaryAction, TOP_RIGHT);
107 
108         secondaryActions.forEach(component -> {
109             // add secondary actions one after the other, but prior to the last primary action
110             footer.addComponent(component, footer.getComponentIndex(lastPrimaryAction));
111             footer.setExpandRatio(component, 0);
112         });
113 
114         return footer;
115     }
116 
117     private static void clickityClick(Button.ClickEvent event) {
118         Notification.show(event.getButton().getCaption());
119     }
120 
121     @WebServlet(value = "/footer/*", displayName = "Dialog Footer", asyncSupported = true)
122     @VaadinServletConfiguration(productionMode = false, ui = DialogFooterUI.class)
123     public static class Servlet extends VaadinServlet {
124     }
125 }