View Javadoc
1   /**
2    * This file Copyright (c) 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;
37  import info.magnolia.ui.api.view.View;
38  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
39  
40  import java.util.LinkedHashSet;
41  import java.util.Set;
42  
43  import com.vaadin.event.ShortcutListener;
44  import com.vaadin.ui.AbstractComponent;
45  import com.vaadin.ui.CustomComponent;
46  
47  /**
48   *.
49   */
50  public class ResurfaceDialogViewImpl extends CustomComponent implements DialogView {
51  
52      private String title;
53      private boolean isClosable;
54      private Set<DialogCloseHandler> dialogCloseHandlers = new LinkedHashSet<>();
55      private Set<ShortcutListener> shortcutListeners = new LinkedHashSet<>();
56      private EditorActionAreaView actionView;
57      private View contentView;
58      private DialogComponent dialogComponent;
59      private boolean isClosed = false;
60  
61      public ResurfaceDialogViewImpl() {
62          this.dialogComponent = new DialogComponent();
63      }
64  
65      public void setCaption(String caption) {
66          this.title = caption;
67      }
68  
69      @Override
70      public String getTitle() {
71          return title;
72      }
73  
74      @Override
75      public void setContent(View content) {
76          this.contentView = content;
77          this.dialogComponent.setContent(content.asVaadinComponent());
78      }
79  
80      @Override
81      public void setClosable(boolean isClosable) {
82          this.isClosable = isClosable;
83      }
84  
85      @Override
86      public boolean isClosable() {
87          return this.isClosable;
88      }
89  
90      @Override
91      public void close() {
92          // Some handler could call #close() method once again, but the handlers should be called only once -
93          // all the subsequent calls should be avoided.
94          if (!isClosed) {
95              isClosed = true;
96              this.dialogCloseHandlers.forEach(handler -> handler.onDialogClose(this));
97          }
98      }
99  
100     @Override
101     public void addDialogCloseHandler(DialogCloseHandler handler) {
102         dialogCloseHandlers.add(handler);
103     }
104 
105     @Override
106     public void removeDialogCloseHandler(DialogCloseHandler handler) {
107         dialogCloseHandlers.remove(handler);
108     }
109 
110     @Override
111     public void addShortcut(ShortcutListener shortcut) {
112         this.shortcutListeners.add(shortcut);
113         this.dialogComponent.addShortcutListener(shortcut);
114     }
115 
116     @Override
117     public void removeShortcut(ShortcutListener shortcut) {
118         this.shortcutListeners.remove(shortcut);
119         this.dialogComponent.removeShortcutListener(shortcut);
120     }
121 
122     @Override
123     public void setActionAreaView(EditorActionAreaView actionView) {
124         this.actionView = actionView;
125         this.dialogComponent.setFooter(actionView.asVaadinComponent());
126     }
127 
128     @Override
129     public View getContentView() {
130         return this.contentView;
131     }
132 
133     @Override
134     public EditorActionAreaView getActionAreaView() {
135         return this.actionView;
136     }
137 
138     @Override
139     public OverlayLayer.ModalityLevel getModalityLevel() {
140         return OverlayLayer.ModalityLevel.STRONG;
141     }
142 
143     @Override
144     public AbstractComponent asVaadinComponent() {
145         return this.dialogComponent;
146     }
147 
148     @Override
149     public void setModalityLevel(OverlayLayer.ModalityLevel modalityLevel) {
150         // NOOP
151     }
152 
153     @Override
154     public void setDescriptionVisible(boolean isDescriptionVisible) {
155         // NOOP
156     }
157 
158     @Override
159     public void setDescription(String description) {
160         // NOOP
161     }
162 }