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