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.form.tab.MagnoliaFormTab;
37 import info.magnolia.ui.vaadin.gwt.client.form.connector.FormState;
38 import info.magnolia.ui.vaadin.gwt.client.form.rpc.FormServerRpc;
39 import info.magnolia.ui.vaadin.tabsheet.MagnoliaTab;
40 import info.magnolia.ui.vaadin.tabsheet.MagnoliaTabSheet;
41
42 import java.util.Collection;
43 import java.util.Iterator;
44 import java.util.LinkedList;
45 import java.util.List;
46
47 import com.vaadin.data.Item;
48 import com.vaadin.data.Property;
49 import com.vaadin.shared.Connector;
50 import com.vaadin.ui.AbstractSingleComponentContainer;
51 import com.vaadin.ui.Component;
52 import com.vaadin.ui.Field;
53
54
55
56
57 public class Form extends AbstractSingleComponentContainer implements FormViewReduced {
58
59 private List<Field<?>> fields = new LinkedList<Field<?>>();
60
61 private Item itemDataSource;
62
63 private boolean isValidationVisible = false;
64
65 private final MagnoliaTabSheet tabSheet = new MagnoliaTabSheet() {
66 @Override
67 public MagnoliaFormTab addTab(final String caption, final Component c) {
68 if (c instanceof FormSection) {
69 final FormSection section = (FormSection) c;
70 final MagnoliaFormTab tab = new MagnoliaFormTab(caption, section);
71 tab.setClosable(false);
72 doAddTab(tab);
73 return tab;
74 }
75 throw new IllegalArgumentException("TabSheet inside a Form should only receive the FormSection objects as tab content.");
76 }
77 };
78
79 public Form() {
80 super();
81 setStyleName("v-magnolia-form");
82 tabSheet.setSizeFull();
83 tabSheet.showAllTab(true, "");
84 setContent(tabSheet);
85 registerRpc(new FormServerRpc() {
86 @Override
87 public void focusNextProblematicField(Connector currentFocused) {
88 doFocusNextProblematicField(currentFocused);
89 }
90 });
91 }
92
93 public void focusFirstField() {
94 if (fields.isEmpty()) {
95 return;
96 }
97 fields.get(0).focus();
98 }
99
100 private void doFocusNextProblematicField(Connector currentFocused) {
101
102
103
104
105
106 int tabsToIterate = tabSheet.getComponentCount() + 1;
107 MagnoliaTab tab = tabSheet.getActiveTab();
108 FormSection section;
109 Component nextProblematic;
110 do {
111 section = (FormSection) tab.getContent();
112 nextProblematic = section.getNextProblematicField(currentFocused);
113 if (nextProblematic == null) {
114 tab = tabSheet.getNextTab(tab);
115 tabsToIterate--;
116
117 currentFocused = null;
118 }
119
120 } while (nextProblematic == null && tabsToIterate > 0);
121
122
123 if (nextProblematic != null) {
124 tabSheet.setActiveTab(tab);
125 section.focusField(nextProblematic);
126 }
127 }
128
129 @Override
130 public void setDescriptionVisibility(boolean isVisible) {
131 getState().descriptionsVisible = isVisible;
132 }
133
134 @Override
135 public void setItemDataSource(Item newDataSource) {
136 this.itemDataSource = newDataSource;
137 }
138
139 @Override
140 public Item getItemDataSource() {
141 return itemDataSource;
142 }
143
144 @Override
145 public void addFormSection(String tabName, FormSection inputFields) {
146 tabSheet.addTab(tabName, inputFields);
147 }
148
149 @Override
150 public void addField(Field<?> field) {
151 fields.add(field);
152 field.addValueChangeListener(new Property.ValueChangeListener() {
153 @Override
154 public void valueChange(Property.ValueChangeEvent event) {
155 invalidateErrorAmount();
156 }
157 });
158 }
159
160 public void setErrorLabels(String errorsLabel, String nextErrorLabel){
161 getState().errorsLabel = errorsLabel;
162 getState().nextErrorLabel = nextErrorLabel;
163 }
164
165 private void invalidateErrorAmount() {
166 getState().errorAmount = -1;
167 }
168
169 @Override
170 public Collection<Field<?>> getFields() {
171 return fields;
172 }
173
174 @Override
175 public boolean isValid() {
176 boolean res = true;
177 getState().errorAmount = 0;
178 for (Field<?> field : getFields()) {
179 boolean isFieldValid = field.isValid();
180 if(!isFieldValid){
181 ++getState().errorAmount;
182 }
183 res &= isFieldValid;
184 }
185 return res;
186 }
187
188 @Override
189 public void showValidation(boolean isVisible) {
190 isValidationVisible = isVisible;
191
192 if (isVisible) {
193 invalidateErrorAmount();
194 }
195
196 final Iterator<Component> it = tabSheet.iterator();
197 while (it.hasNext()) {
198 final Component c = it.next();
199 if (c instanceof MagnoliaFormTab) {
200 ((MagnoliaFormTab) c).setValidationVisible(isVisible);
201 }
202 }
203 }
204
205
206
207
208 @Override
209 @Deprecated
210 public void setShowAllEnabled(boolean enabled) {
211 tabSheet.showAllTab(enabled, "");
212 }
213
214 @Override
215 protected FormState getState() {
216 return (FormState) super.getState();
217 }
218
219 @Override
220 protected FormState getState(boolean markAsDirty) {
221 return (FormState) super.getState(markAsDirty);
222 }
223
224 @Override
225 public MagnoliaTabSheet getContent() {
226 return (MagnoliaTabSheet) super.getContent();
227 }
228
229 @Override
230 public Form asVaadinComponent() {
231 return this;
232 }
233 }