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.form;
35
36 import info.magnolia.ui.vaadin.gwt.client.form.VFormSection;
37
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Map;
43
44 import com.vaadin.terminal.ErrorMessage;
45 import com.vaadin.terminal.PaintException;
46 import com.vaadin.terminal.PaintTarget;
47 import com.vaadin.ui.AbstractComponent;
48 import com.vaadin.ui.AbstractLayout;
49 import com.vaadin.ui.ClientWidget;
50 import com.vaadin.ui.ClientWidget.LoadStyle;
51 import com.vaadin.ui.Component;
52
53
54
55
56 @ClientWidget(value = VFormSection.class, loadStyle = LoadStyle.EAGER)
57 public class FormSection extends AbstractLayout {
58
59 private boolean isValidationVisible = false;
60
61 private final List<Component> components = new LinkedList<Component>();
62
63 private final Map<Component, String> helpDescriptions = new HashMap<Component, String>();
64
65 public FormSection() {
66 addStyleName("v-form-layout");
67 }
68
69 @Override
70 public void paintContent(PaintTarget target) throws PaintException {
71 super.paintContent(target);
72 final Iterator<Component> it = getComponentIterator();
73 target.addAttribute("validationVisible", isValidationVisible);
74 while (it.hasNext()) {
75 final Component c = it.next();
76 target.startTag("component");
77 c.addStyleName("v-form-field");
78 c.setSizeUndefined();
79 c.paint(target);
80 if (helpDescriptions.containsKey(c)) {
81 target.addAttribute("helpDescription", helpDescriptions.get(c));
82 }
83 target.endTag("component");
84 }
85 }
86
87 public void setComponentHelpDescription(Component c, String description) {
88 if (components.contains(c)) {
89 helpDescriptions.put(c, description);
90 requestRepaint();
91 } else {
92 throw new IllegalArgumentException("Layout doesn't contain this component.");
93 }
94 }
95
96 @Override
97 public void addComponent(Component c) {
98 super.addComponent(c);
99 components.add(c);
100 requestRepaint();
101 }
102
103 @Override
104 public void replaceComponent(Component oldComponent, Component newComponent) {
105 }
106
107 @Override
108 public Iterator<Component> getComponentIterator() {
109 return components.iterator();
110 }
111
112 public void setValidationVisible(boolean isVisible) {
113 this.isValidationVisible = isVisible;
114 requestRepaint();
115 }
116
117 @Override
118 public ErrorMessage getErrorMessage() {
119 final Iterator<Component> it = getComponentIterator();
120 while (it.hasNext()) {
121 final Component c = it.next();
122 if (c instanceof AbstractComponent) {
123 final ErrorMessage errMsg = ((AbstractComponent) c).getErrorMessage();
124 if (errMsg != null) {
125 return errMsg;
126 }
127 }
128 }
129 return super.getErrorMessage();
130 }
131
132 public boolean hasError() {
133 return getErrorMessage() != null && isValidationVisible;
134 }
135
136 }