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_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
169
170 public interface DialogHeaderCallback {
171
172 void onDescriptionVisibilityChanged(boolean isVisible);
173
174 void onCloseFired();
175 }
176 }