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