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.formsection.connector.FormSectionState;
37 import info.magnolia.ui.vaadin.gwt.client.form.rpc.FormSectionClientRpc;
38
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
42
43 import com.vaadin.server.ErrorMessage;
44 import com.vaadin.shared.Connector;
45 import com.vaadin.ui.AbstractComponent;
46 import com.vaadin.ui.AbstractField;
47 import com.vaadin.ui.AbstractLayout;
48 import com.vaadin.ui.Component;
49
50
51
52
53 public class FormSection extends AbstractLayout {
54
55 private final List<Component> components = new LinkedList<Component>();
56
57 public FormSection() {
58 addStyleName("v-form-layout");
59 }
60
61 @Override
62 protected FormSectionState getState() {
63 return (FormSectionState) super.getState();
64 }
65
66 @Override
67 protected FormSectionState getState(boolean markAsDirty) {
68 return (FormSectionState) super.getState(markAsDirty);
69 }
70
71 public void setComponentHelpDescription(Component c, String description) {
72 if (components.contains(c)) {
73 getState().helpDescriptions.put(c, description);
74 } else {
75 throw new IllegalArgumentException("Layout doesn't contain this component.");
76 }
77 }
78
79 @Override
80 public void addComponent(Component c) {
81 super.addComponent(c);
82 components.add(c);
83 c.addStyleName("v-form-field");
84 markAsDirty();
85 }
86
87 @Override
88 public void removeComponent(Component c) {
89 super.removeComponent(c);
90 components.remove(c);
91 markAsDirty();
92 }
93
94 @Override
95 public void replaceComponent(Component oldComponent, Component newComponent) {
96 }
97
98 @Override
99 public Iterator<Component> iterator() {
100 return components.iterator();
101 }
102
103 public void setValidationVisible(boolean isVisible) {
104 getState().isValidationVisible = isVisible;
105 }
106
107 @Override
108 public ErrorMessage getErrorMessage() {
109 if (!getState(false).isValidationVisible) {
110 return null;
111 }
112 final Iterator<Component> it = getComponentIterator();
113 while (it.hasNext()) {
114 final Component c = it.next();
115 if (c instanceof AbstractComponent) {
116 final ErrorMessage errMsg = ((AbstractComponent) c).getErrorMessage();
117 if (errMsg != null) {
118 return errMsg;
119 }
120 }
121 }
122 return super.getErrorMessage();
123 }
124
125 @Override
126 public int getComponentCount() {
127 return components.size();
128 }
129
130 public Component getNextProblematicField(Connector currentFocused) {
131 int startIndex = components.indexOf(currentFocused) + 1;
132 if (startIndex < components.size() - 1) {
133 while (startIndex < components.size()) {
134 Component c = components.get(startIndex++);
135 if (c instanceof AbstractField && !((AbstractField<?>) c).isValid()) {
136 return c;
137 }
138 }
139 }
140 return null;
141 }
142
143 public void focusField(Component field) {
144 getRpcProxy(FormSectionClientRpc.class).focus(field);
145 }
146 }