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