View Javadoc
1   /**
2    * This file Copyright (c) 2010-2014 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.dialog;
35  
36  import info.magnolia.ui.api.overlay.OverlayLayer.ModalityLevel;
37  import info.magnolia.ui.api.view.View;
38  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
39  import info.magnolia.ui.vaadin.dialog.BaseDialog;
40  import info.magnolia.ui.vaadin.dialog.BaseDialog.DialogCloseEvent;
41  import info.magnolia.ui.vaadin.dialog.BaseDialog.DialogCloseEvent.Handler;
42  import info.magnolia.ui.vaadin.dialog.BaseDialog.WideEvent;
43  
44  import java.util.HashSet;
45  import java.util.Set;
46  
47  import com.vaadin.event.ShortcutListener;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.Panel;
50  
51  /**
52   * Base implementation of {@link DialogView}.
53   */
54  public class BaseDialogViewImpl extends Panel implements DialogView {
55  
56      private BaseDialog dialog;
57  
58      private Set<DialogCloseHandler> dialogCloseHandlers = new HashSet<DialogCloseHandler>();
59  
60      private View contentView;
61  
62      private EditorActionAreaView actionAreaView;
63  
64      private ModalityLevel modalityLevel = ModalityLevel.STRONG;
65  
66      public static final int DIALOG_WIDTH = 720;
67  
68      public BaseDialogViewImpl() {
69          this(new BaseDialog());
70      }
71  
72      public BaseDialogViewImpl(BaseDialog dialog) {
73          this.dialog = dialog;
74  
75          setContent(this.dialog);
76          // We use Panel to keep keystroke events scoped within the currently focused component.
77          // Without it, if you have more than one dialog open,
78          // i.e. in different apps running at the same time, then all open
79          // dialogs would react to the keyboard event sent on the dialog currently having the focus.
80  
81          setWidth(DIALOG_WIDTH, Unit.PIXELS);
82          setHeight(100, Unit.PERCENTAGE); // Required for dynamic dialog shrinking upon window resize.
83  
84          this.dialog.addDialogCloseHandler(new Handler() {
85              @Override
86              public void onClose(DialogCloseEvent event) {
87                  close();
88              }
89          });
90  
91          this.dialog.addWideHandler(new BaseDialog.WideEvent.Handler() {
92  
93              @Override
94              public void onWideChanged(WideEvent event) {
95                  handleSetWide(event.isWide());
96              }
97          });
98  
99          this.dialog.setSizeFull();
100         this.dialog.setStyleName("dialog-panel");
101     }
102 
103     private void handleSetWide(boolean isWide){
104         if (isWide){
105             setWidth(100, Unit.PERCENTAGE);
106             addStyleName("wide");
107         }else{
108             setWidth(DIALOG_WIDTH, Unit.PIXELS);
109             removeStyleName("wide");
110         }
111     }
112 
113     @Override
114     public void setDescriptionVisible(boolean isDescriptionVisible) {
115         dialog.setDescriptionVisibility(isDescriptionVisible);
116     }
117 
118     @Override
119     public void setDescription(String description) {
120         dialog.setDialogDescription(description);
121     }
122 
123     @Override
124     public void setCaption(String caption) {
125         dialog.setCaption(caption);
126     }
127 
128     @Override
129     public void setContent(View content) {
130         this.contentView = content;
131         dialog.setContent(content.asVaadinComponent());
132     }
133 
134     @Override
135     public void close() {
136         DialogCloseHandler[] handlers = dialogCloseHandlers.toArray(new DialogCloseHandler[dialogCloseHandlers.size()]);
137         for (final DialogCloseHandler handler : handlers) {
138             handler.onDialogClose(BaseDialogViewImpl.this);
139         }
140         dialogCloseHandlers.clear();
141     }
142 
143     @Override
144     public void setClosable(boolean isClosable) {
145         if (isClosable) {
146             dialog.showCloseButton();
147         }
148     }
149 
150     @Override
151     public void addShortcut(ShortcutListener shortcut) {
152         addShortcutListener(shortcut);
153     }
154 
155     @Override
156     public void removeShortcut(ShortcutListener shortcut) {
157         removeShortcutListener(shortcut);
158     }
159 
160     @Override
161     public void addDialogCloseHandler(DialogCloseHandler handler) {
162         if (handler != null) {
163             dialogCloseHandlers.add(handler);
164         }
165     }
166 
167     @Override
168     public void removeDialogCloseHandler(DialogCloseHandler handler) {
169         dialogCloseHandlers.remove(handler);
170     }
171 
172     @Override
173     public void setActionAreaView(EditorActionAreaView actionAreaView) {
174         this.actionAreaView = actionAreaView;
175         dialog.setFooterToolbar(actionAreaView.asVaadinComponent());
176     }
177 
178     @Override
179     public void attach() {
180         super.attach();
181     }
182 
183     @Override
184     public Component asVaadinComponent() {
185         return this;
186     }
187 
188     @Override
189     public View getContentView() {
190         return contentView;
191     }
192 
193     @Override
194     public EditorActionAreaView getActionAreaView() {
195         return actionAreaView;
196     }
197 
198     protected BaseDialog getDialog() {
199         return this.dialog;
200     }
201 
202     /**
203      * @return the current modality or {@link ModalityLevel#STRONG} if not set.
204      */
205     @Override
206     public ModalityLevel getModalityLevel() {
207         return this.modalityLevel;
208     }
209 
210     @Override
211     public void setModalityLevel(ModalityLevel modalityLevel) {
212         this.modalityLevel = modalityLevel;
213         if (modalityLevel == ModalityLevel.LIGHT) {
214             dialog.removeStyleName("dialog-panel");
215             dialog.addStyleName("light");
216             setWidth("620px");
217         }
218         dialog.setModalityLevel(modalityLevel.getName());
219     }
220 
221     @Override
222     public void setWide(boolean isWide) {
223         handleSetWide(isWide);
224         dialog.setWideOnClient(isWide);
225     }
226 }