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;
35
36 import com.google.gwt.dom.client.Style.Display;
37 import com.google.gwt.dom.client.Style.Unit;
38 import com.google.gwt.user.client.DOM;
39 import com.google.gwt.user.client.Element;
40 import com.google.gwt.user.client.ui.FlowPanel;
41 import com.google.gwt.user.client.ui.Widget;
42 import com.vaadin.terminal.gwt.client.ApplicationConnection;
43 import com.vaadin.terminal.gwt.client.ComputedStyle;
44 import com.vaadin.terminal.gwt.client.Container;
45 import com.vaadin.terminal.gwt.client.Paintable;
46 import com.vaadin.terminal.gwt.client.RenderSpace;
47 import com.vaadin.terminal.gwt.client.UIDL;
48
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.Iterator;
52 import java.util.LinkedHashMap;
53 import java.util.LinkedList;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.Set;
57
58
59
60
61 public class VFormSection extends FlowPanel implements Container {
62
63 private final List<Widget> children = new LinkedList<Widget>();
64
65 private final Map<Widget, FormFieldWrapper> sections = new LinkedHashMap<Widget, FormFieldWrapper>();
66
67 private final List<FormFieldWrapper> problematicSections = new ArrayList<FormFieldWrapper>();
68
69 private final Element fieldSet = DOM.createElement("fieldset");
70
71 private final Element legend = DOM.createElement("legend");
72
73 private final Element horizontalRule = DOM.createElement("hr");
74
75 private boolean isValidationVisible = false;
76
77 public VFormSection() {
78 super();
79 getElement().appendChild(fieldSet);
80
81 horizontalRule.getStyle().setDisplay(Display.NONE);
82 legend.getStyle().setDisplay(Display.NONE);
83 }
84
85 @Override
86 public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
87 if (client.updateComponent(this, uidl, true)) {
88 return;
89 }
90
91 isValidationVisible = uidl.getBooleanAttribute("validationVisible");
92
93 if(uidl.hasAttribute("caption")) {
94 String caption = uidl.getStringAttribute("caption");
95 legend.setInnerText(caption);
96 fieldSet.appendChild(legend);
97 }
98
99 final Iterator<?> it = uidl.getChildIterator();
100 while (it.hasNext()) {
101 final UIDL childUIdl = (UIDL) it.next();
102 final Paintable p = client.getPaintable(childUIdl.getChildUIDL(0));
103 final Widget w = (Widget) p;
104 if (!hasChildComponent(w)) {
105 FormFieldWrapper fieldSection = new FormFieldWrapper();
106 sections.put(w, fieldSection);
107 children.add(w);
108 fieldSection.setField(w);
109 add(fieldSection, fieldSet);
110 }
111 if (childUIdl.hasAttribute("helpDescription")) {
112 String description = childUIdl.getStringAttribute("helpDescription");
113 FormFieldWrapper fieldSection = sections.get(w);
114 fieldSection.setHelpDescription(description);
115 }
116
117 p.updateFromUIDL(childUIdl.getChildUIDL(0), client);
118
119
120
121 }
122
123 fieldSet.appendChild(horizontalRule);
124 }
125
126 @Override
127 public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
128 }
129
130 @Override
131 public boolean hasChildComponent(Widget component) {
132 return children.contains(component);
133 }
134
135 @Override
136 public void updateCaption(Paintable component, UIDL uidl) {
137 FormFieldWrapper fs = sections.get(component);
138 if (fs != null) {
139 boolean errorsOccured = uidl.hasAttribute("error");
140 if (errorsOccured && isValidationVisible) {
141 fs.resetErrorMessage();
142 for (final Iterator<?> it = uidl.getErrors().getChildIterator(); it.hasNext();) {
143 final Object child = it.next();
144 if (child instanceof String) {
145 final String errorMessage = (String) child;
146 fs.showError(errorMessage);
147 if (!problematicSections.contains(fs)) {
148 problematicSections.add(fs);
149 }
150 }
151 }
152 } else {
153 problematicSections.remove(fs);
154 fs.clearErrors();
155 }
156 getParent().setHasError(!problematicSections.isEmpty());
157 if (uidl.hasAttribute("caption")) {
158 fs.setCaption(uidl.getStringAttribute("caption"));
159 }
160 }
161 }
162
163 @Override
164 public VFormTab getParent() {
165 final Widget parent = super.getParent();
166 if (parent == null) {
167 return null;
168 }
169 if (!(super.getParent() instanceof VFormTab)) {
170 throw new RuntimeException("Parent of VFormSection must be of type VFormTab, you have used: " + super.getParent().getClass());
171 }
172 return (VFormTab)super.getParent();
173 }
174
175 @Override
176 public RenderSpace getAllocatedSpace(Widget child) {
177 final FormFieldWrapper fs = sections.get(child);
178 if (fs != null) {
179 return new RenderSpace(getOffsetWidth(), getOffsetHeight());
180 }
181 return new RenderSpace();
182 }
183
184 public void setDescriptionVisible(boolean isAccessible) {
185 for (final FormFieldWrapper fs : sections.values()) {
186 fs.setHelpEnabled(isAccessible);
187 }
188 }
189
190 @Override
191 public boolean requestLayout(Set<Paintable> children) {
192 return false;
193 }
194
195 @Override
196 public void setHeight(String height) {
197 super.setHeight(height);
198 final Integer heightPx = ComputedStyle.parseInt(height);
199 if (heightPx != null) {
200 fieldSet.getStyle().setHeight(heightPx, Unit.PX);
201 }
202 }
203
204 public void setValidationVisible(boolean isVisible) {
205 this.isValidationVisible = isVisible;
206 }
207
208 public int getErrorAmount() {
209 return problematicSections.size();
210 }
211
212 public List<FormFieldWrapper> getProblematicFields() {
213 return Collections.unmodifiableList(problematicSections);
214 }
215
216 public List<FormFieldWrapper> getFields() {
217 return new ArrayList<FormFieldWrapper>(sections.values());
218 }
219 }