View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.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   * Basic implementation of dialogs.
53   * Provides Action registration and callbacks to the view.
54   * Can be closed.
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     /* Basic component features ------------------------------------------ */
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      * Sets a Component
152      * <p>
153      * The composition root must be set to non-null value before the component can be used. The composition root can only be set once.
154      * </p>
155      *
156      * @param newContent
157      * the root of the composition component tree.
158      */
159     protected void adoptComponent(Component newContent) {
160         if (newContent != null) {
161             // set new component
162             if (newContent.getParent() != null) {
163                 if (newContent.getParent() == this) {
164                     newContent.setParent(null);
165                 } else {
166                     // If the component already has a parent, try to remove it
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      * DialogCloseEvent.
238      */
239     public static class DialogCloseEvent extends com.vaadin.ui.Component.Event {
240 
241         /**
242          * Handler.
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      * DescriptionVisibilityEvent.
272      */
273     public static class WideEvent extends com.vaadin.ui.Component.Event {
274         /**
275          * Handler.
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      * DescriptionVisibilityEvent.
305      */
306     public static class DescriptionVisibilityEvent extends com.vaadin.ui.Component.Event {
307         /**
308          * Handler.
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 }