View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.vaadin.gwt.client.dialog.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.CloseButton;
37  
38  import com.google.gwt.event.dom.client.ClickEvent;
39  import com.google.gwt.event.dom.client.ClickHandler;
40  import com.google.gwt.user.client.DOM;
41  import com.google.gwt.user.client.Element;
42  import com.google.gwt.user.client.ui.Button;
43  import com.google.gwt.user.client.ui.FlowPanel;
44  import com.google.gwt.user.client.ui.Label;
45  import com.google.gwt.user.client.ui.Widget;
46  
47  /**
48   * DialogHeaderWidget.
49   */
50  public class DialogHeaderWidget extends FlowPanel {
51  
52      private static final String CLASSNAME_HEADER = "dialog-header";
53      private static final String ClASSNAME_DESCRIPTION = "dialog-description";
54      private static final String CLASSNAME_WIDEBUTTON = "btn-form-wide";
55      private static final String CLASSNAME_WIDEBUTTON_ICON = "icon-open-fullscreen-2";
56      private static final String CLASSNAME_WIDEBUTTON_ICON_CLOSE = "icon-close-fullscreen-2";
57      private static final String CLASSNAME_HELPBUTTON = "btn-form-help";
58      private static final String CLASSNAME_HEADER_TOOLBAR = "dialog-header-toolbar";
59  
60      protected CloseButtont/client/CloseButton.html#CloseButton">CloseButton closeButton = new CloseButton();
61  
62      protected DialogHeaderCallback callback = null;
63  
64      protected final FlowPanel descriptionPanel = new FlowPanel();
65  
66      protected final Element headerPanel = DOM.createDiv();
67  
68      protected final Element caption = DOM.createSpan();
69  
70      protected final Element toolbarEl = DOM.createSpan();
71  
72      protected Widget toolbar;
73  
74      protected static boolean isDescriptionVisible = false;
75  
76      protected boolean hasDescription = false;
77  
78      protected boolean isWide = false;
79  
80      protected final Button helpButton = new Button("", new ClickHandler() {
81          @Override
82          public void onClick(ClickEvent event) {
83              isDescriptionVisible = !isDescriptionVisible;
84              onDescriptionVisibility();
85          }
86      });
87  
88      protected final Button wideButton = new Button("", new ClickHandler() {
89          @Override
90          public void onClick(ClickEvent event) {
91              isWide = !isWide;
92              onWideChanged();
93          }
94      });
95  
96  
97      private void onWideChanged(){
98          callback.onWideChanged(isWide);
99          setWideIcon();
100     }
101 
102     private void setWideIcon(){
103         if (isWide){
104             wideButton.removeStyleName(CLASSNAME_WIDEBUTTON_ICON);
105             wideButton.addStyleName(CLASSNAME_WIDEBUTTON_ICON_CLOSE);
106         }else{
107             wideButton.removeStyleName(CLASSNAME_WIDEBUTTON_ICON_CLOSE);
108             wideButton.addStyleName(CLASSNAME_WIDEBUTTON_ICON);
109         }
110     }
111 
112     private void onDescriptionVisibility() {
113         if (hasDescription) {
114             descriptionPanel.setVisible(isDescriptionVisible);
115         }
116         callback.onDescriptionVisibilityChanged(isDescriptionVisible);
117     }
118 
119     @Override
120     protected void onLoad() {
121         super.onLoad();
122         onDescriptionVisibility();
123         if (hasDescription) {
124             if (this.getElement().getParentElement() != null) {
125                 this.getElement().getParentElement().setAttribute("role", "dialogDescriptionHeader");
126             }
127         }
128     }
129 
130     public DialogHeaderWidget(DialogHeaderCallback callback) {
131         this.callback = callback;
132         callback.onDescriptionVisibilityChanged(false);
133         construct();
134     }
135 
136     public void construct() {
137 
138         closeButton.addStyleDependentName("dialog");
139         closeButton.setVisible(false);
140         headerPanel.appendChild(closeButton.getElement());
141         addDomHandler(new ClickHandler() {
142             @Override
143             public void onClick(ClickEvent event) {
144                 Element target = event.getNativeEvent().getEventTarget().cast();
145                 if (closeButton.getElement().isOrHasChild(target)) {
146                     callback.onCloseFired();
147                 }
148             }
149         }, ClickEvent.getType());
150 
151         headerPanel.addClassName(CLASSNAME_HEADER);
152         descriptionPanel.addStyleName(ClASSNAME_DESCRIPTION);
153         wideButton.setStyleName(CLASSNAME_WIDEBUTTON);
154         setWideIcon();
155         helpButton.setStyleName(CLASSNAME_HELPBUTTON);
156         toolbarEl.addClassName(CLASSNAME_HEADER_TOOLBAR);
157 
158         getElement().appendChild(headerPanel);
159         caption.addClassName("title");
160         headerPanel.appendChild(caption);
161         add(wideButton, headerPanel);
162         add(helpButton, headerPanel);
163         headerPanel.appendChild(toolbarEl);
164 
165         descriptionPanel.setVisible(false);
166         add(descriptionPanel);
167 
168     }
169 
170     public void setDescription(String description) {
171         final Label content = new Label();
172         content.setText(description);
173         descriptionPanel.insert(content, 0);
174         hasDescription = !description.isEmpty();
175         if (hasDescription) {
176             descriptionPanel.setVisible(isDescriptionVisible);
177             if (this.getElement().getParentElement() != null) {
178                 this.getElement().getParentElement().setAttribute("role", "dialogDescriptionHeader");
179             }
180         }
181     }
182 
183     public void setWide(boolean isWide) {
184         this.isWide = isWide;
185         setWideIcon();
186     }
187 
188     public void setCaption(String caption) {
189         this.caption.setInnerText(caption);
190     }
191 
192     public void setToolbar(Widget toolbarWidget) {
193         if (toolbar != null) {
194             remove(toolbar);
195         }
196         toolbar = toolbarWidget;
197         add(toolbarWidget, toolbarEl);
198     }
199 
200     public void showCloseButton() {
201         closeButton.setVisible(true);
202     }
203 
204     /**
205      * Callback interface for the EditorLike header.
206      */
207     public interface DialogHeaderCallback {
208 
209         void onDescriptionVisibilityChanged(boolean isVisible);
210 
211         void onCloseFired();
212 
213         void onWideChanged(boolean isWide);
214     }
215 }