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
81 public static DialogBuilder dialog() {
82 return new DialogBuilder();
83 }
84
85 public DialogBuilder withTitle(String title) {
86 this.title = title;
87 return this;
88 }
89
90 public DialogBuilder withContent(Component content) {
91 this.content = content;
92 return this;
93 }
94
95 public DialogBuilder withActions(List<Component> actions) {
96 this.actions.addAll(actions);
97 return this;
98 }
99
100 public DialogBuilder modal() {
101 this.modal = true;
102 return this;
103 }
104
105 public DialogBuilder light(boolean isLight) {
106 this.light = isLight;
107 return this;
108 }
109
110 public DialogBuilder withFooter(Component footer) {
111 this.footer = footer;
112 return this;
113 }
114
115 public <T> DialogBuilder withShortcuts(List<ActionExecution<T>> shortcuts) {
116 this.shortcuts.putAll(shortcuts.stream().collect(toMap(
117 execution -> execution.getDefinition().getShortcut(),
118 actionExecution -> (Runnable) actionExecution::execute,
119 (initialShortcut, overridingShortcut) -> overridingShortcut)));
120 return this;
121 }
122
123 public DialogBuilder withShortcuts(Map<Integer, Runnable> shortcuts) {
124 this.shortcuts.putAll(shortcuts);
125 return this;
126 }
127
128 public DialogComponent build() {
129 DialogComponentnt.html#DialogComponent">DialogComponent dialog = new DialogComponent(this.content, getFooter());
130 dialog.setShortcuts(this.shortcuts);
131 return dialog;
132 }
133
134 private Component getFooter() {
135 if (this.footer == null) {
136 final EditorActionLayoutorActionLayout">EditorActionLayout editorActionLayout = new EditorActionLayout();
137 this.actions.forEach(editorActionLayout::addPrimaryAction);
138 this.footer = editorActionLayout;
139 }
140 return this.footer;
141 }
142
143 public Window buildAndOpen() {
144 Window window = new Window();
145 window.setCaption(this.title);
146 window.setContent(this.build());
147 window.center();
148 window.setModal(this.modal);
149 String modalityStyleName = this.light ? "light" : "";
150 window.addStyleNames("dialog", this.content.getStyleName(), modalityStyleName);
151 this.content.addStyleName("content");
152 window.setDraggable(false);
153 window.setResizable(false);
154
155 ResurfacedWindow.extend(window);
156 UI.getCurrent().addWindow(window);
157 return window;
158 }
159 }