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.dialog;
35  
36  import info.magnolia.ui.api.view.View;
37  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
38  import info.magnolia.ui.vaadin.dialog.BaseDialog;
39  import info.magnolia.ui.vaadin.dialog.BaseDialog.DialogCloseEvent;
40  import info.magnolia.ui.vaadin.dialog.BaseDialog.DialogCloseEvent.Handler;
41  
42  import java.util.HashSet;
43  import java.util.Set;
44  
45  import com.vaadin.event.ShortcutListener;
46  import com.vaadin.ui.Component;
47  import com.vaadin.ui.Panel;
48  
49  /**
50   * Base implementation of {@link DialogView}.
51   */
52  public class BaseDialogViewImpl extends Panel implements DialogView {
53  
54      private BaseDialog dialog;
55  
56      private Set<DialogCloseHandler> dialogCloseHandlers = new HashSet<DialogCloseHandler>();
57  
58      private View contentView;
59  
60      private EditorActionAreaView actionAreaView;
61  
62      public BaseDialogViewImpl() {
63          this(new BaseDialog());
64      }
65  
66      public BaseDialogViewImpl(BaseDialog dialog) {
67          this.dialog = dialog;
68  
69          setContent(this.dialog);
70          // We use Panel to keep keystroke events scoped within the currently focused component.
71          // Without it, if you have more than one dialog open,
72          // i.e. in different apps running at the same time, then all open
73          // dialogs would react to the keyboard event sent on the dialog currently having the focus.
74          //setWidth(Sizeable.SIZE_UNDEFINED, Unit.PIXELS);
75          setWidth("720px");
76          setHeight(100, Unit.PERCENTAGE); // Required for dynamic dialog shrinking upon window resize.
77          this.dialog.addDialogCloseHandler(new Handler() {
78              @Override
79              public void onClose(DialogCloseEvent event) {
80                  close();
81              }
82          });
83          this.dialog.setSizeFull();
84          this.dialog.setStyleName("dialog-panel");
85      }
86  
87      @Override
88      public void setDescriptionVisible(boolean isDescriptionVisible) {
89          dialog.setDescriptionVisibility(isDescriptionVisible);
90      }
91  
92      @Override
93      public void setDescription(String description) {
94          dialog.setDialogDescription(description);
95      }
96  
97      @Override
98      public void setCaption(String caption) {
99          dialog.setCaption(caption);
100     }
101 
102     @Override
103     public void setContent(View content) {
104         this.contentView = content;
105         dialog.setContent(content.asVaadinComponent());
106     }
107 
108     @Override
109     public void close() {
110         DialogCloseHandler[] handlers = dialogCloseHandlers.toArray(new DialogCloseHandler[dialogCloseHandlers.size()]);
111         for (final DialogCloseHandler handler : handlers) {
112             handler.onDialogClose(BaseDialogViewImpl.this);
113         }
114         dialogCloseHandlers.clear();
115     }
116 
117     @Override
118     public void setClosable(boolean isClosable) {
119         if (isClosable) {
120             dialog.showCloseButton();
121         }
122     }
123 
124     @Override
125     public void addShortcut(ShortcutListener shortcut) {
126         addShortcutListener(shortcut);
127     }
128 
129     @Override
130     public void removeShortcut(ShortcutListener shortcut) {
131         removeShortcutListener(shortcut);
132     }
133 
134     @Override
135     public void addDialogCloseHandler(DialogCloseHandler handler) {
136         if (handler != null) {
137             dialogCloseHandlers.add(handler);
138         }
139     }
140 
141     @Override
142     public void removeDialogCloseHandler(DialogCloseHandler handler) {
143         dialogCloseHandlers.remove(handler);
144     }
145 
146     @Override
147     public void setActionAreaView(EditorActionAreaView actionAreaView) {
148         this.actionAreaView = actionAreaView;
149         dialog.setFooterToolbar(actionAreaView.asVaadinComponent());
150     }
151 
152     @Override
153     public void attach() {
154         super.attach();
155     }
156 
157     @Override
158     public Component asVaadinComponent() {
159         return this;
160     }
161 
162     @Override
163     public View getContentView() {
164         return contentView;
165     }
166 
167     @Override
168     public EditorActionAreaView getActionAreaView() {
169         return actionAreaView;
170     }
171 
172     protected BaseDialog getDialog() {
173         return this.dialog;
174     }
175 }