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.ui.dialog;
35  
36  import static java.util.stream.Collectors.toMap;
37  
38  import info.magnolia.ui.vaadin.extension.ResurfacedWindow;
39  
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  import com.vaadin.server.Sizeable;
46  import com.vaadin.ui.Component;
47  import com.vaadin.ui.UI;
48  import com.vaadin.ui.Window;
49  
50  /**
51   * Builder for Magnolia 6.2+ dialogs.
52   * They come in two different flavors: <strong>light</strong> and <strong>strong</strong>.
53   * Dialogs created with this builder are modal and strong by default.
54   * <p>When to use light or strong dialogs:
55   * <ul>
56   *     <li>light dialogs ask for quick input but do not severely interrupt the user. They have a transparent background. </li>
57   *     <li>strong dialogs ask for data collection. They have a partially opaque background</li>
58   * </ul>
59   *
60   * Here is an usage example:
61   * <pre>
62   * DialogBuilder.dialog()
63   *         .withTitle("Edit object")
64   *         .withContent(view)
65   *         .light(myDialogDefinition.isLight())
66   *         .withActions(new Button("Commit"))
67   *         .buildAndOpen();
68   * </pre>
69   *
70   * @see info.magnolia.ui.dialog.DialogDefinition
71   */
72  public class DialogBuilder {
73  
74      private String title;
75      private Component content;
76      private List<Component> actions = new ArrayList<>();
77      private boolean modal = true;
78      private Component footer;
79      private Map<Integer, Runnable> shortcuts = new HashMap<>();
80      private boolean light = false;
81      private List<Window.CloseListener> closeListeners = new ArrayList<>();
82      private String styles;
83      private boolean wide = false;
84  
85      public static DialogBuilder dialog() {
86          return new DialogBuilder();
87      }
88  
89      public DialogBuilder withTitle(String title) {
90          this.title = title;
91          return this;
92      }
93  
94      public DialogBuilder withContent(Component content) {
95          this.content = content;
96          return this;
97      }
98  
99      public DialogBuilder withStyles(String styles) {
100         this.styles = styles;
101         return this;
102     }
103 
104     public DialogBuilder withActions(List<Component> actions) {
105         this.actions.addAll(actions);
106         return this;
107     }
108 
109     public DialogBuilder modal() {
110         this.modal = true;
111         return this;
112     }
113 
114     public DialogBuilder light(boolean isLight) {
115         this.light = isLight;
116         return this;
117     }
118 
119     public DialogBuilder withFooter(Component footer) {
120         this.footer = footer;
121         return this;
122     }
123 
124     public <T> DialogBuilder withShortcuts(List<ActionExecution<T>> shortcuts) {
125         this.shortcuts.putAll(shortcuts.stream().collect(toMap(
126                 execution -> execution.getDefinition().getShortcut(),
127                 actionExecution -> (Runnable) actionExecution::execute,
128                 (initialShortcut, overridingShortcut) -> overridingShortcut)));
129         return this;
130     }
131 
132     public DialogBuilder withShortcuts(Map<Integer, Runnable> shortcuts) {
133         this.shortcuts.putAll(shortcuts);
134         return this;
135     }
136 
137     public DialogBuilder withCloseListener(Window.CloseListener closeListener) {
138         this.closeListeners.add(closeListener);
139         return this;
140     }
141 
142     public DialogBuilder wide(boolean isWide) {
143         this.wide = isWide;
144         return this;
145     }
146 
147     public DialogComponent build() {
148         DialogComponentnt.html#DialogComponent">DialogComponent dialog = new DialogComponent(this.content, getFooter());
149         dialog.setShortcuts(this.shortcuts);
150         return dialog;
151     }
152 
153     private Component getFooter() {
154         if (this.footer == null) {
155             final EditorActionLayoutorActionLayout">EditorActionLayout editorActionLayout = new EditorActionLayout();
156             this.actions.forEach(editorActionLayout::addPrimaryAction);
157             this.footer = editorActionLayout;
158         }
159         return this.footer;
160     }
161 
162     public Window buildAndOpen() {
163         Window window = new Window();
164         window.setCaption(this.title);
165         window.setContent(this.build());
166         window.center();
167         window.setModal(this.modal);
168         String modalityStyleName = this.light ? "light" : "";
169         String wideStyleName = this.wide ? "wide" : "";
170         window.addStyleNames("dialog", this.content.getStyleName(), modalityStyleName, wideStyleName);
171         this.content.addStyleName("content");
172         this.content.addStyleName(this.styles);
173         window.setDraggable(false);
174         window.setResizable(false);
175 
176         closeListeners.forEach(window::addCloseListener);
177         ResurfacedWindow.extend(window);
178         UI.getCurrent().addWindow(window);
179         return window;
180     }
181 }