View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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 com.vaadin.ui.AbstractComponent;
37  import com.vaadin.ui.AbstractSingleComponentContainer;
38  import com.vaadin.ui.Component;
39  import com.vaadin.ui.HasComponents;
40  import com.vaadin.ui.HorizontalLayout;
41  import com.vaadin.ui.VerticalLayout;
42  import info.magnolia.ui.vaadin.gwt.client.dialog.connector.BaseDialogState;
43  import info.magnolia.ui.vaadin.gwt.client.dialog.rpc.DialogServerRpc;
44  
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * Basic implementation of dialogs.
52   * Provides Action registration and callbacks to the view.
53   * Can be closed.
54   */
55  public class BaseDialog extends AbstractComponent implements HasComponents {
56  
57      public static final String CANCEL_ACTION_NAME = "cancel";
58  
59      public static final String COMMIT_ACTION_NAME = "commit";
60  
61      public BaseDialog() {
62          super();
63          setImmediate(true);
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 setDescriptionVisibility(boolean isVisible) {
74                  BaseDialog.this.setDescriptionVisibility(isVisible);
75              }
76          });
77      }
78  
79      @Override
80      protected BaseDialogState getState() {
81          return (BaseDialogState) super.getState();
82      }
83  
84      public void closeSelf() {
85          fireEvent(new DialogCloseEvent(this, this));
86      }
87  
88      public void setDescriptionVisibility(boolean isVisible) {
89          fireEvent(new DescriptionVisibilityEvent(this, isVisible));
90      }
91  
92      public void setDialogDescription(String description) {
93          getState().componentDescription = description;
94      }
95  
96      /* Basic component features ------------------------------------------ */
97  
98      @Override
99      public Iterator<Component> iterator() {
100         List<Component> components = new ArrayList<Component>() {
101             @Override
102             public boolean add(Component c) {
103                 if (c != null) {
104                     return super.add(c);
105                 }
106                 return false;
107             };
108         };
109 
110         Collections.addAll(components, (Component) getState().content, (Component) getState().headerToolbar, (Component) getState().footerToolbar);
111         return components.iterator();
112     }
113 
114     public void setContent(Component newContent) {
115         final Component actualContent = newContent == null ? createDefaultContent() : newContent;
116         if (getState().content != null) {
117             ((Component)getState().content).setParent(null);
118         }
119         getState().content = actualContent;
120         adoptComponent((Component) getState().content);
121     }
122 
123     public void setHeaderToolbar(Component newHeader) {
124         final Component actualHeader = newHeader == null ? createDefaultHeader() : newHeader;
125         if (getState().headerToolbar != null) {
126             ((Component)getState().headerToolbar).setParent(null);
127         }
128         getState().headerToolbar = actualHeader;
129         adoptComponent((Component) getState().headerToolbar);
130     }
131 
132     public void setFooterToolbar(Component newFooter) {
133         final Component actualFooter = newFooter == null ? createDefaultFooter() : newFooter;
134         if (getState().footerToolbar != null) {
135             ((Component)getState().footerToolbar).setParent(null);
136         }
137         getState().footerToolbar = actualFooter;
138         adoptComponent((Component) getState().footerToolbar);
139     }
140 
141     /**
142      * Sets a Component
143      * <p>
144      * The composition root must be set to non-null value before the component can be used. The composition root can only be set once.
145      * </p>
146      *
147      * @param newContent
148      * the root of the composition component tree.
149      */
150     protected void adoptComponent(Component newContent) {
151         if (newContent != null) {
152             // set new component
153             if (newContent.getParent() != null) {
154                 if (newContent.getParent() == this) {
155                     newContent.setParent(null);
156                 } else {
157                     // If the component already has a parent, try to remove it
158                     AbstractSingleComponentContainer
159                     .removeFromParent(newContent);
160                 }
161             }
162             newContent.setParent(this);
163         }
164         markAsDirty();
165 
166     }
167 
168     public Component getContent() {
169         return (Component) getState().content;
170     }
171 
172     @Override
173     public void setCaption(String caption) {
174         super.setCaption(caption);
175         getContent().setCaption(caption);
176     }
177 
178     protected Component createDefaultContent() {
179         return new VerticalLayout();
180     }
181 
182     protected Component createDefaultHeader() {
183         return new HorizontalLayout();
184     }
185 
186     protected Component createDefaultFooter() {
187         return new HorizontalLayout();
188     }
189 
190     public void showCloseButton() {
191         getState().hasCloseButton = true;
192     }
193 
194     public void addDescriptionVisibilityHandler(DescriptionVisibilityEvent.Handler handler) {
195         addListener("descriptionVisibilityEvent", DescriptionVisibilityEvent.class, handler, DescriptionVisibilityEvent.ON_DESCRIPTION_VISIBILITY_CHANGED);
196     }
197 
198     public void removeDescriptionVisibilityHandler(DescriptionVisibilityEvent.Handler handler) {
199         removeListener("descriptionVisibilityEvent", DescriptionVisibilityEvent.class, handler);
200     }
201 
202 
203     public void addDialogCloseHandler(DialogCloseEvent.Handler handler) {
204         addListener("dialogCloseEvent", DialogCloseEvent.class, handler, DialogCloseEvent.ON_DIALOG_CLOSE);
205     }
206 
207     public void removeDialogCloseHandler(DialogCloseEvent.Handler handler) {
208         removeListener("dialogCloseEvent", DialogCloseEvent.class, handler);
209     }
210 
211 
212     /**
213      * DialogCloseEvent.
214      */
215     public static class DialogCloseEvent extends com.vaadin.ui.Component.Event {
216 
217         /**
218          * Handler.
219          */
220         public interface Handler {
221             void onClose(DialogCloseEvent event);
222         }
223 
224         public static final java.lang.reflect.Method ON_DIALOG_CLOSE;
225 
226         public BaseDialog dialog;
227 
228         static {
229             try {
230                 ON_DIALOG_CLOSE = DialogCloseEvent.Handler.class.getDeclaredMethod("onClose", new Class[] { DialogCloseEvent.class });
231             } catch (final java.lang.NoSuchMethodException e) {
232                 throw new java.lang.RuntimeException(e);
233             }
234         }
235 
236         public DialogCloseEvent(Component source, BaseDialog dialog) {
237             super(source);
238             this.dialog = dialog;
239         }
240 
241         public BaseDialog getDialog() {
242             return dialog;
243         }
244     }
245 
246     /**
247      * DescriptionVisibilityEvent.
248      */
249     public static class DescriptionVisibilityEvent extends com.vaadin.ui.Component.Event {
250         /**
251          * Handler.
252          */
253         public interface Handler {
254             void onDescriptionVisibilityChanged(DescriptionVisibilityEvent event);
255         }
256 
257         public static final java.lang.reflect.Method ON_DESCRIPTION_VISIBILITY_CHANGED;
258 
259         private boolean isVisible;
260 
261         static {
262             try {
263                 ON_DESCRIPTION_VISIBILITY_CHANGED = DescriptionVisibilityEvent.Handler.class.getDeclaredMethod("onDescriptionVisibilityChanged", new Class[] { DescriptionVisibilityEvent.class });
264             } catch (final java.lang.NoSuchMethodException e) {
265                 throw new java.lang.RuntimeException(e);
266             }
267         }
268 
269         public DescriptionVisibilityEvent(Component source, boolean isVisible) {
270             super(source);
271             this.isVisible = isVisible;
272         }
273 
274         public boolean isVisible() {
275             return isVisible;
276         }
277     }
278 
279 }