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