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.vaadin.dialog;
35
36 import info.magnolia.ui.vaadin.gwt.client.dialog.connector.BaseDialogState;
37 import info.magnolia.ui.vaadin.gwt.client.dialog.rpc.DialogServerRpc;
38
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.List;
43
44 import com.vaadin.ui.AbstractComponent;
45 import com.vaadin.ui.AbstractSingleComponentContainer;
46 import com.vaadin.ui.Component;
47 import com.vaadin.ui.HasComponents;
48 import com.vaadin.v7.ui.HorizontalLayout;
49 import com.vaadin.v7.ui.VerticalLayout;
50
51
52
53
54
55
56 public class BaseDialog extends AbstractComponent implements HasComponents {
57
58 public static final String CANCEL_ACTION_NAME = "cancel";
59
60 public static final String COMMIT_ACTION_NAME = "commit";
61
62 public BaseDialog() {
63 super();
64 setContent(createDefaultContent());
65 registerRpc(new DialogServerRpc() {
66
67 @Override
68 public void closeSelf() {
69 BaseDialog.this.closeSelf();
70 }
71
72 @Override
73 public void setWide(boolean isWide) {
74 BaseDialog.this.setWide(isWide);
75 }
76
77 @Override
78 public void setDescriptionVisibility(boolean isVisible) {
79 BaseDialog.this.setDescriptionVisibility(isVisible);
80 }
81 });
82 }
83
84 @Override
85 protected BaseDialogState getState() {
86 return (BaseDialogState) super.getState();
87 }
88
89 public void closeSelf() {
90 fireEvent(new DialogCloseEvent(this, this));
91 }
92
93 public void setWide(boolean isWide) {
94 fireEvent(new WideEvent(this, isWide));
95 }
96
97 public void setDescriptionVisibility(boolean isVisible) {
98 fireEvent(new DescriptionVisibilityEvent(this, isVisible));
99 }
100
101 public void setDialogDescription(String description) {
102 getState().componentDescription = description;
103 }
104
105
106
107 @Override
108 public Iterator<Component> iterator() {
109 List<Component> components = new ArrayList<Component>() {
110 @Override
111 public boolean add(Component c) {
112 if (c != null) {
113 return super.add(c);
114 }
115 return false;
116 };
117 };
118
119 Collections.addAll(components, (Component) getState().content, (Component) getState().headerToolbar, (Component) getState().footerToolbar);
120 return components.iterator();
121 }
122
123 public void setContent(Component newContent) {
124 final Component actualContent = newContent == null ? createDefaultContent() : newContent;
125 if (getState().content != null) {
126 ((Component)getState().content).setParent(null);
127 }
128 getState().content = actualContent;
129 adoptComponent((Component) getState().content);
130 }
131
132 public void setHeaderToolbar(Component newHeader) {
133 final Component actualHeader = newHeader == null ? createDefaultHeader() : newHeader;
134 if (getState().headerToolbar != null) {
135 ((Component)getState().headerToolbar).setParent(null);
136 }
137 getState().headerToolbar = actualHeader;
138 adoptComponent((Component) getState().headerToolbar);
139 }
140
141 public void setFooterToolbar(Component newFooter) {
142 final Component actualFooter = newFooter == null ? createDefaultFooter() : newFooter;
143 if (getState().footerToolbar != null) {
144 ((Component)getState().footerToolbar).setParent(null);
145 }
146 getState().footerToolbar = actualFooter;
147 adoptComponent((Component) getState().footerToolbar);
148 }
149
150
151
152
153
154
155
156
157
158
159 protected void adoptComponent(Component newContent) {
160 if (newContent != null) {
161
162 if (newContent.getParent() != null) {
163 if (newContent.getParent() == this) {
164 newContent.setParent(null);
165 } else {
166
167 AbstractSingleComponentContainer
168 .removeFromParent(newContent);
169 }
170 }
171 newContent.setParent(this);
172 }
173 markAsDirty();
174
175 }
176
177 public Component getContent() {
178 return (Component) getState().content;
179 }
180
181 @Override
182 public void setCaption(String caption) {
183 super.setCaption(caption);
184 getContent().setCaption(caption);
185 }
186
187 protected Component createDefaultContent() {
188 return new VerticalLayout();
189 }
190
191 protected Component createDefaultHeader() {
192 return new HorizontalLayout();
193 }
194
195 protected Component createDefaultFooter() {
196 return new HorizontalLayout();
197 }
198
199 public void showCloseButton() {
200 getState().hasCloseButton = true;
201 }
202
203 public void setModalityLevel(String modalityLevel){
204 getState().modalityLevel = modalityLevel;
205 }
206
207 public void setWideOnClient(boolean isWide){
208 getState().isWide = isWide;
209 }
210
211 public void addDescriptionVisibilityHandler(DescriptionVisibilityEvent.Handler handler) {
212 addListener("descriptionVisibilityEvent", DescriptionVisibilityEvent.class, handler, DescriptionVisibilityEvent.ON_DESCRIPTION_VISIBILITY_CHANGED);
213 }
214
215 public void removeDescriptionVisibilityHandler(DescriptionVisibilityEvent.Handler handler) {
216 removeListener("descriptionVisibilityEvent", DescriptionVisibilityEvent.class, handler);
217 }
218
219 public void addWideHandler(WideEvent.Handler handler) {
220 addListener("wideEvent", WideEvent.class, handler, WideEvent.ON_WIDE_CHANGED);
221 }
222
223 public void removeWideHandler(DialogCloseEvent.Handler handler) {
224 removeListener("wideEvent", WideEvent.class, handler);
225 }
226
227 public void addDialogCloseHandler(DialogCloseEvent.Handler handler) {
228 addListener("dialogCloseEvent", DialogCloseEvent.class, handler, DialogCloseEvent.ON_DIALOG_CLOSE);
229 }
230
231 public void removeDialogCloseHandler(DialogCloseEvent.Handler handler) {
232 removeListener("dialogCloseEvent", DialogCloseEvent.class, handler);
233 }
234
235
236
237
238
239 public static class DialogCloseEvent extends com.vaadin.ui.Component.Event {
240
241
242
243
244 public interface Handler {
245 void onClose(DialogCloseEvent event);
246 }
247
248 public static final java.lang.reflect.Method ON_DIALOG_CLOSE;
249
250 public BaseDialog dialog;
251
252 static {
253 try {
254 ON_DIALOG_CLOSE = DialogCloseEvent.Handler.class.getDeclaredMethod("onClose", new Class[] { DialogCloseEvent.class });
255 } catch (final java.lang.NoSuchMethodException e) {
256 throw new java.lang.RuntimeException(e);
257 }
258 }
259
260 public DialogCloseEvent(Component source, BaseDialog dialog) {
261 super(source);
262 this.dialog = dialog;
263 }
264
265 public BaseDialog getDialog() {
266 return dialog;
267 }
268 }
269
270
271
272
273 public static class WideEvent extends com.vaadin.ui.Component.Event {
274
275
276
277 public interface Handler {
278 void onWideChanged(WideEvent event);
279 }
280
281 public static final java.lang.reflect.Method ON_WIDE_CHANGED;
282
283 private boolean isWide;
284
285 static {
286 try {
287 ON_WIDE_CHANGED = WideEvent.Handler.class.getDeclaredMethod("onWideChanged", new Class[] { WideEvent.class });
288 } catch (final java.lang.NoSuchMethodException e) {
289 throw new java.lang.RuntimeException(e);
290 }
291 }
292
293 public WideEvent(Component source, boolean isWide) {
294 super(source);
295 this.isWide = isWide;
296 }
297
298 public boolean isWide() {
299 return isWide;
300 }
301 }
302
303
304
305
306 public static class DescriptionVisibilityEvent extends com.vaadin.ui.Component.Event {
307
308
309
310 public interface Handler {
311 void onDescriptionVisibilityChanged(DescriptionVisibilityEvent event);
312 }
313
314 public static final java.lang.reflect.Method ON_DESCRIPTION_VISIBILITY_CHANGED;
315
316 private boolean isVisible;
317
318 static {
319 try {
320 ON_DESCRIPTION_VISIBILITY_CHANGED = DescriptionVisibilityEvent.Handler.class.getDeclaredMethod("onDescriptionVisibilityChanged", new Class[] { DescriptionVisibilityEvent.class });
321 } catch (final java.lang.NoSuchMethodException e) {
322 throw new java.lang.RuntimeException(e);
323 }
324 }
325
326 public DescriptionVisibilityEvent(Component source, boolean isVisible) {
327 super(source);
328 this.isVisible = isVisible;
329 }
330
331 public boolean isVisible() {
332 return isVisible;
333 }
334 }
335
336 }