View Javadoc

1   /**
2    * This file Copyright (c) 2013-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.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_HELPBUTTON = "btn-form-help";
55      private static final String CLASSNAME_HEADER_TOOLBAR = "dialog-header-toolbar";
56  
57      protected CloseButton closeButton = new CloseButton();
58  
59      protected DialogHeaderCallback callback = null;
60  
61      protected final FlowPanel descriptionPanel = new FlowPanel();
62  
63      protected final Element headerPanel = DOM.createDiv();
64  
65      protected final Element caption = DOM.createSpan();
66  
67      protected final Element toolbarEl = DOM.createSpan();
68  
69      protected Widget toolbar;
70  
71      protected static boolean isDescriptionVisible = false;
72  
73      protected boolean hasDescription = false;
74  
75      protected final Button helpButton = new Button("", new ClickHandler() {
76          @Override
77          public void onClick(ClickEvent event) {
78              isDescriptionVisible = !isDescriptionVisible;
79              onDescriptionVisibility();
80          }
81      });
82  
83      private void onDescriptionVisibility() {
84          if (hasDescription) {
85              descriptionPanel.setVisible(isDescriptionVisible);
86          }
87          callback.onDescriptionVisibilityChanged(isDescriptionVisible);
88      }
89  
90      @Override
91      protected void onLoad() {
92          super.onLoad();
93          onDescriptionVisibility();
94          if (hasDescription) {
95              if (this.getElement().getParentElement() != null) {
96                  this.getElement().getParentElement().setAttribute("role", "dialogDescriptionHeader");
97              }
98          }
99      }
100 
101     public DialogHeaderWidget(DialogHeaderCallback callback) {
102         this.callback = callback;
103         callback.onDescriptionVisibilityChanged(false);
104         construct();
105     }
106 
107     public void construct() {
108 
109         closeButton.addStyleDependentName("dialog");
110         closeButton.setVisible(false);
111         headerPanel.appendChild(closeButton.getElement());
112         addDomHandler(new ClickHandler() {
113             @Override
114             public void onClick(ClickEvent event) {
115                 Element target = event.getNativeEvent().getEventTarget().cast();
116                 if (closeButton.getElement().isOrHasChild(target)) {
117                     callback.onCloseFired();
118                 }
119             }
120         }, ClickEvent.getType());
121 
122         headerPanel.addClassName(CLASSNAME_HEADER);
123         descriptionPanel.addStyleName(ClASSNAME_DESCRIPTION);
124         helpButton.setStyleName(CLASSNAME_HELPBUTTON);
125         toolbarEl.addClassName(CLASSNAME_HEADER_TOOLBAR);
126 
127         getElement().appendChild(headerPanel);
128         caption.addClassName("title");
129         headerPanel.appendChild(caption);
130         add(helpButton, headerPanel);
131         headerPanel.appendChild(toolbarEl);
132 
133         descriptionPanel.setVisible(false);
134         add(descriptionPanel);
135 
136     }
137 
138     public void setDescription(String description) {
139         final Label content = new Label();
140         content.setText(description);
141         descriptionPanel.insert(content, 0);
142         hasDescription = !description.isEmpty();
143         if (hasDescription) {
144             descriptionPanel.setVisible(isDescriptionVisible);
145             if (this.getElement().getParentElement() != null) {
146                 this.getElement().getParentElement().setAttribute("role", "dialogDescriptionHeader");
147             }
148         }
149     }
150 
151     public void setCaption(String caption) {
152         this.caption.setInnerText(caption);
153     }
154 
155     public void setToolbar(Widget toolbarWidget) {
156         if (toolbar != null) {
157             remove(toolbar);
158         }
159         toolbar = toolbarWidget;
160         add(toolbarWidget, toolbarEl);
161     }
162 
163     public void showCloseButton() {
164         closeButton.setVisible(true);
165     }
166 
167     /**
168      * Callback interface for the EditorLike header.
169      */
170     public interface DialogHeaderCallback {
171 
172         void onDescriptionVisibilityChanged(boolean isVisible);
173 
174         void onCloseFired();
175     }
176 }