1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
206
207 public interface DialogHeaderCallback {
208
209 void onDescriptionVisibilityChanged(boolean isVisible);
210
211 void onCloseFired();
212
213 void onWideChanged(boolean isWide);
214 }
215 }