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
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 }