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.form.widget;
35
36 import info.magnolia.ui.vaadin.gwt.client.form.formsection.widget.InlineMessageWidget;
37
38 import com.google.gwt.event.dom.client.BlurEvent;
39 import com.google.gwt.event.dom.client.BlurHandler;
40 import com.google.gwt.event.dom.client.ClickEvent;
41 import com.google.gwt.event.dom.client.ClickHandler;
42 import com.google.gwt.event.dom.client.FocusEvent;
43 import com.google.gwt.event.dom.client.FocusHandler;
44 import com.google.gwt.event.dom.client.HasBlurHandlers;
45 import com.google.gwt.event.dom.client.HasFocusHandlers;
46 import com.google.gwt.event.shared.HandlerRegistration;
47 import com.google.gwt.user.client.DOM;
48 import com.google.gwt.user.client.Element;
49 import com.google.gwt.user.client.ui.Button;
50 import com.google.gwt.user.client.ui.FlowPanel;
51 import com.google.gwt.user.client.ui.Widget;
52 import com.google.gwt.user.client.ui.impl.FocusImpl;
53 import com.vaadin.client.ui.aria.AriaHelper;
54
55
56
57
58 public class FormFieldWrapper extends FlowPanel implements HasFocusHandlers, HasBlurHandlers {
59
60 private Element label = DOM.createDiv();
61
62 private Element fieldWrapper = DOM.createDiv();
63
64 private Element root;
65
66 private final HelpIconWidget helpButton = new HelpIconWidget();
67
68 private Button errorAction = new Button();
69
70 private InlineMessageWidget errorSection = null;
71
72 private InlineMessageWidget helpSection = null;
73
74 private String helpDescription = null;
75
76 private Widget field = null;
77
78 public FormFieldWrapper() {
79 super();
80 addStyleName("v-form-field-section");
81 root = super.getElement();
82 construct();
83 setHelpEnabled(false);
84
85 helpButton.addDomHandler(new ClickHandler() {
86 @Override
87 public void onClick(ClickEvent event) {
88 if (helpSection == null) {
89 showHelp();
90 } else {
91 hideHelp();
92 }
93 }
94 }, ClickEvent.getType());
95
96 }
97
98 public void hideHelp() {
99 if (helpSection != null) {
100 remove(helpSection);
101 }
102 helpSection = null;
103 helpButton.setHighlighted(false);
104 }
105
106 public void showHelp() {
107 if (helpDescription == null || "".equals(helpDescription)) {
108 return;
109 }
110 helpSection = InlineMessageWidget.createHelpMessage();
111 helpSection.setMessage(helpDescription);
112 add(helpSection, root);
113 helpButton.setHighlighted(true);
114 }
115
116 private void construct() {
117 label.addClassName("v-form-field-label");
118 fieldWrapper.addClassName("v-form-field-container");
119 errorAction.addStyleName("action-validation");
120
121 root.appendChild(label);
122 root.appendChild(fieldWrapper);
123 add(helpButton, fieldWrapper);
124 add(errorAction, fieldWrapper);
125 }
126
127 public void showError(final String errorDescription) {
128 helpButton.setVisible(false);
129 errorAction.setVisible(true);
130 fieldWrapper.addClassName("validation-highlight");
131 if (errorSection == null) {
132 errorSection = InlineMessageWidget.createErrorMessage();
133 }
134 errorSection.setMessage(errorDescription);
135 add(errorSection, root);
136 }
137
138 public void setCaption(String caption) {
139 label.setInnerHTML(caption);
140 if (caption != null) {
141
142 String toolTip = caption.replaceAll("\\<.*?\\>", "");
143 label.setTitle(toolTip);
144 }
145 }
146
147 @Override
148 public void add(Widget child) {
149 add(child, fieldWrapper);
150 }
151
152 public void setField(Widget child) {
153 if (this.field != null) {
154 remove(field);
155 }
156 this.field = child;
157 if (child != null) {
158 child.removeFromParent();
159 AriaHelper.bindCaption(child, label);
160 getChildren().add(child);
161 fieldWrapper.insertBefore(child.getElement(), helpButton.getElement());
162 adopt(child);
163 }
164 }
165
166 public boolean isDisplayingHelpSection() {
167 return helpSection != null;
168 }
169
170 public void clearError() {
171 if (errorSection != null) {
172 remove(errorSection);
173 errorSection = null;
174 }
175 fieldWrapper.removeClassName("validation-hilight");
176 errorAction.setVisible(false);
177 if (helpDescription != null && !"".equals(helpDescription)) {
178 helpButton.setVisible(true);
179 }
180 }
181
182 public void setHelpEnabled(boolean isHelpEnabled) {
183 helpButton.setVisible(helpDescription != null && !"".equals(helpDescription) && !errorAction.isVisible());
184 if (!isHelpEnabled && helpSection != null) {
185 hideHelp();
186 return;
187 }
188 if (isHelpEnabled && helpButton.isVisible() && helpSection == null) {
189 showHelp();
190 }
191 }
192
193 public void setHelpDescription(String description) {
194 this.helpDescription = description;
195 if (helpSection != null && getWidgetIndex(helpSection) >= 0) {
196 helpSection.setMessage(helpDescription);
197 }
198 if (description != null && !"".equals(description)) {
199 helpButton.setVisible(true);
200 } else {
201 helpButton.setVisible(false);
202 }
203 }
204
205 public void focusField() {
206 if (field != null) {
207 FocusImpl.getFocusImplForWidget().focus(field.getElement());
208 }
209 }
210
211 public Widget getField() {
212 return field;
213 }
214
215 @Override
216 public HandlerRegistration addFocusHandler(FocusHandler handler) {
217 return field.addDomHandler(handler, FocusEvent.getType());
218 }
219
220 @Override
221 public HandlerRegistration addBlurHandler(BlurHandler handler) {
222 return field.addDomHandler(handler, BlurEvent.getType());
223 }
224 }