1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 private String styles;
82 private boolean wide = false;
83 private Width width = Width.medium;
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
143
144
145
146
147 @Deprecated
148 public DialogBuilder wide(boolean isWide) {
149 this.wide = isWide;
150 return this;
151 }
152
153 public DialogComponent build() {
154 DialogComponentnt.html#DialogComponent">DialogComponent dialog = new DialogComponent(this.content, getFooter());
155 dialog.setShortcuts(this.shortcuts);
156 return dialog;
157 }
158
159 private Component getFooter() {
160 if (this.footer == null) {
161 final EditorActionLayoutorActionLayout">EditorActionLayout editorActionLayout = new EditorActionLayout();
162 this.actions.forEach(editorActionLayout::addPrimaryAction);
163 this.footer = editorActionLayout;
164 }
165 return this.footer;
166 }
167
168 public DialogBuilder width(Width width) {
169 this.width = width;
170 return this;
171 }
172
173 public Window buildAndOpen() {
174 Window window = new Window();
175 window.setCaption(this.title);
176 window.setContent(this.build());
177 window.center();
178 window.setModal(this.modal);
179 String modalityStyleName = this.light ? "light" : "";
180 String widthStyleName = this.wide ? Width.wide.name() : width.name();
181 window.addStyleNames("dialog", this.content.getStyleName(), modalityStyleName, widthStyleName);
182 this.content.addStyleName("content");
183 this.content.addStyleName(this.styles);
184 window.setDraggable(false);
185 window.setResizable(false);
186
187
188 closeListeners.forEach(window::addCloseListener);
189 ResurfacedWindow.extend(window);
190 UI currentUI = UI.getCurrent();
191 currentUI.addWindow(window);
192 window.setErrorHandler(currentUI.getErrorHandler());
193 return window;
194 }
195
196 public enum Width {
197 small, medium, wide
198 }
199 }