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