gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / impl / DefaultJDynForm.java @ 1948
History | View | Annotate | Download (21.3 KB)
1 | 954 | jbadia | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 1145 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 954 | jbadia | *
|
11 | 1145 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 954 | jbadia | *
|
16 | 1145 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 954 | jbadia | *
|
20 | 1145 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 954 | jbadia | */
|
23 | 919 | jjdelcerro | package org.gvsig.tools.dynform.impl; |
24 | |||
25 | 1881 | jjdelcerro | import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm; |
26 | 919 | jjdelcerro | import java.util.ArrayList; |
27 | import java.util.HashMap; |
||
28 | import java.util.Iterator; |
||
29 | import java.util.List; |
||
30 | import java.util.Map; |
||
31 | |||
32 | import javax.swing.Action; |
||
33 | import javax.swing.JComponent; |
||
34 | import javax.swing.JTabbedPane; |
||
35 | |||
36 | 1080 | jbadia | import org.apache.commons.lang3.StringUtils; |
37 | import org.gvsig.tools.ToolsLocator; |
||
38 | 931 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
39 | 919 | jjdelcerro | import org.gvsig.tools.dataTypes.DataType; |
40 | 1080 | jbadia | import org.gvsig.tools.dataTypes.DataTypes; |
41 | 933 | jjdelcerro | import org.gvsig.tools.dynform.DynFormDefinition; |
42 | import org.gvsig.tools.dynform.DynFormFieldDefinition; |
||
43 | import org.gvsig.tools.dynform.JDynFormField; |
||
44 | import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener; |
||
45 | 967 | jbadia | import org.gvsig.tools.dynform.spi.dynformfield.SupportPopupMenu; |
46 | 937 | jjdelcerro | import org.gvsig.tools.dynobject.DynClass; |
47 | 919 | jjdelcerro | import org.gvsig.tools.dynobject.DynField; |
48 | 1080 | jbadia | import org.gvsig.tools.dynobject.DynField_v2; |
49 | 919 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
50 | import org.gvsig.tools.service.ServiceException; |
||
51 | |||
52 | import com.jgoodies.forms.builder.DefaultFormBuilder; |
||
53 | import com.jgoodies.forms.layout.FormLayout; |
||
54 | import com.jgoodies.forms.layout.RowSpec; |
||
55 | 1145 | jjdelcerro | import java.util.Collection; |
56 | 1881 | jjdelcerro | import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
57 | import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
||
58 | 1947 | jjdelcerro | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_SEPARATOR; |
59 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_CLEAR; |
||
60 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISMODIFIED; |
||
61 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISREADONLY; |
||
62 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDCHANGED; |
||
63 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDENTER; |
||
64 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDEXIT; |
||
65 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONLOAD; |
||
66 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONSETVALUES; |
||
67 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_VALIDATE; |
||
68 | 1881 | jjdelcerro | import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
69 | import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
||
70 | 1149 | jjdelcerro | import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
71 | 1947 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
72 | 1946 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
73 | 919 | jjdelcerro | |
74 | 1881 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
75 | 1361 | jjdelcerro | public class DefaultJDynForm extends AbstractJDynForm implements JDynFormFieldListener { |
76 | 919 | jjdelcerro | |
77 | 1145 | jjdelcerro | private Map components = null; |
78 | 1881 | jjdelcerro | private ComponentsFactory componentsFactory;
|
79 | 919 | jjdelcerro | |
80 | 1735 | jjdelcerro | public DefaultJDynForm(
|
81 | 1881 | jjdelcerro | DynFormSPIManager manager, |
82 | JDynFormFactory factory, |
||
83 | 1735 | jjdelcerro | DynFormDefinition definition, |
84 | DynFormContext context |
||
85 | 1881 | jjdelcerro | ) { |
86 | super(manager, factory, definition, context);
|
||
87 | 1145 | jjdelcerro | this.components = new HashMap(); |
88 | 1881 | jjdelcerro | this.componentsFactory = manager.createDefaultComponentsFactory();
|
89 | 1145 | jjdelcerro | } |
90 | 919 | jjdelcerro | |
91 | 1361 | jjdelcerro | @Override
|
92 | protected JComponent getFieldsContainer() { |
||
93 | 1368 | fdiaz | |
94 | 1361 | jjdelcerro | try {
|
95 | JComponent component;
|
||
96 | switch (this.getLayoutMode()) { |
||
97 | case USE_PLAIN:
|
||
98 | case USE_TREE:
|
||
99 | default:
|
||
100 | component = getFieldsContainerPlain(); |
||
101 | break;
|
||
102 | case USE_TABS:
|
||
103 | component = getFieldsContainerUseTabs(); |
||
104 | break;
|
||
105 | case USE_SEPARATORS:
|
||
106 | component = getFieldsContainerUseSeparators(); |
||
107 | break;
|
||
108 | 1145 | jjdelcerro | } |
109 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONLOAD, this); |
110 | 1361 | jjdelcerro | return component;
|
111 | } catch (ServiceException ex) {
|
||
112 | throw new RuntimeException(ex); |
||
113 | 1145 | jjdelcerro | } |
114 | } |
||
115 | |||
116 | 1361 | jjdelcerro | private JComponent getFieldsContainerPlain() throws ServiceException { |
117 | 1145 | jjdelcerro | FormLayout layout = new FormLayout(
|
118 | "left:pref, 8px, fill:80dlu:grow", "pref"); |
||
119 | |||
120 | DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
||
121 | builder.setDefaultRowSpec(new RowSpec(
|
||
122 | RowSpec.TOP, |
||
123 | builder.getLayout().getRowSpec(1).getSize(),
|
||
124 | builder.getLayout().getRowSpec(1).getResizeWeight()));
|
||
125 | |||
126 | 1361 | jjdelcerro | List fields = this.getDefinition().getDefinitions(); |
127 | 1145 | jjdelcerro | Iterator it = fields.iterator();
|
128 | while (it.hasNext()) {
|
||
129 | DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
||
130 | if (fieldDefinition.isHidden()) {
|
||
131 | continue;
|
||
132 | } |
||
133 | 1886 | jjdelcerro | JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
134 | this.getContext(),
|
||
135 | fieldDefinition |
||
136 | ); |
||
137 | 1881 | jjdelcerro | JDynFormField jfield = factory.create( |
138 | getServiceManager(), |
||
139 | this.componentsFactory,
|
||
140 | fieldDefinition, |
||
141 | null
|
||
142 | ); |
||
143 | 1361 | jjdelcerro | if (jfield instanceof AbstractJDynFormField) { |
144 | ((AbstractJDynFormField) jfield).setForm(this);
|
||
145 | 1149 | jjdelcerro | } |
146 | 1361 | jjdelcerro | if (this.isReadOnly()) { |
147 | 1862 | jjdelcerro | jfield.setReadOnly(true);
|
148 | } else {
|
||
149 | jfield.setReadOnly(fieldDefinition.isReadOnly()); |
||
150 | 1145 | jjdelcerro | } |
151 | 1862 | jjdelcerro | jfield.addListener(this);
|
152 | 1145 | jjdelcerro | builder.append(jfield.getJLabel(), jfield.asJComponent()); |
153 | |||
154 | this.components.put(jfield.getName(), jfield);
|
||
155 | } |
||
156 | 1361 | jjdelcerro | return builder.getPanel();
|
157 | 1145 | jjdelcerro | } |
158 | |||
159 | 1361 | jjdelcerro | private JComponent getFieldsContainerUseSeparators() throws ServiceException { |
160 | List<String> groups = this.getDefinition().getGroups(); |
||
161 | 1145 | jjdelcerro | |
162 | FormLayout layout = new FormLayout(
|
||
163 | "left:pref, 8px, fill:80dlu:grow", "pref"); |
||
164 | |||
165 | DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
||
166 | builder.setDefaultRowSpec(new RowSpec(
|
||
167 | RowSpec.TOP, |
||
168 | builder.getLayout().getRowSpec(1).getSize(),
|
||
169 | builder.getLayout().getRowSpec(1).getResizeWeight()));
|
||
170 | |||
171 | 1361 | jjdelcerro | for (String group : groups) { |
172 | 1509 | jjdelcerro | boolean firstfield = true; |
173 | 1361 | jjdelcerro | List fields = this.getDefinition().getDefinitions(group); |
174 | 1145 | jjdelcerro | Iterator it = fields.iterator();
|
175 | while (it.hasNext()) {
|
||
176 | DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
||
177 | if (fieldDefinition.isHidden()) {
|
||
178 | continue;
|
||
179 | } |
||
180 | 1886 | jjdelcerro | JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
181 | this.getContext(),
|
||
182 | fieldDefinition |
||
183 | ); |
||
184 | 1881 | jjdelcerro | JDynFormField jfield = factory.create( |
185 | getServiceManager(), |
||
186 | this.componentsFactory,
|
||
187 | fieldDefinition, |
||
188 | null
|
||
189 | ); |
||
190 | 1361 | jjdelcerro | if (jfield instanceof AbstractJDynFormField) { |
191 | ((AbstractJDynFormField) jfield).setForm(this);
|
||
192 | 1149 | jjdelcerro | } |
193 | 1145 | jjdelcerro | jfield.addListener(this);
|
194 | 1361 | jjdelcerro | if (this.isReadOnly()) { |
195 | jfield.setReadOnly(this.isReadOnly());
|
||
196 | 1145 | jjdelcerro | } |
197 | |||
198 | List<Action> customActions = getCustomFields(fieldDefinition.getDataType()); |
||
199 | |||
200 | if (customActions != null && !customActions.isEmpty()) { |
||
201 | Iterator it2 = customActions.iterator();
|
||
202 | while (it2.hasNext()) {
|
||
203 | Object obj = it2.next();
|
||
204 | if (obj != null) { |
||
205 | Action act = (Action) obj; |
||
206 | jfield.addActionToPopupMenu((String) act.getValue(Action.NAME), act); |
||
207 | } else {
|
||
208 | jfield.addSeparatorToPopupMenu(); |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | 1509 | jjdelcerro | if( firstfield ) {
|
213 | firstfield = false;
|
||
214 | builder.appendSeparator(group); |
||
215 | } |
||
216 | 1145 | jjdelcerro | builder.append(jfield.getJLabel(), jfield.asJComponent()); |
217 | |||
218 | this.components.put(jfield.getName(), jfield);
|
||
219 | } |
||
220 | } |
||
221 | 1361 | jjdelcerro | return builder.getPanel();
|
222 | 1145 | jjdelcerro | } |
223 | |||
224 | 1361 | jjdelcerro | private JComponent getFieldsContainerUseTabs() throws ServiceException { |
225 | 1145 | jjdelcerro | |
226 | 1033 | jbadia | JTabbedPane tabbedPane = new JTabbedPane(); |
227 | 1881 | jjdelcerro | tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
|
228 | 1361 | jjdelcerro | if (this.getDefinition().getTags().has("TabPlacement")) { |
229 | 1145 | jjdelcerro | try {
|
230 | 1361 | jjdelcerro | tabbedPane.setTabPlacement(this.getDefinition().getTags().getInt("TabPlacement")); |
231 | 1145 | jjdelcerro | } catch (Exception e) { |
232 | // Ignorada
|
||
233 | } |
||
234 | } |
||
235 | 1946 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
236 | 1361 | jjdelcerro | List<String> groups = this.getDefinition().getGroups(); |
237 | for (String group : groups) { |
||
238 | 1145 | jjdelcerro | FormLayout layout = new FormLayout(
|
239 | "left:pref, 8px, fill:80dlu:grow", "pref"); |
||
240 | DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
||
241 | 1304 | fdiaz | builder.defaultRowSpec(new RowSpec(
|
242 | 1145 | jjdelcerro | RowSpec.TOP, |
243 | builder.getLayout().getRowSpec(1).getSize(),
|
||
244 | builder.getLayout().getRowSpec(1).getResizeWeight()));
|
||
245 | 1361 | jjdelcerro | List fields = this.getDefinition().getDefinitions(group); |
246 | 1509 | jjdelcerro | boolean hasFields = false; |
247 | 1145 | jjdelcerro | Iterator it = fields.iterator();
|
248 | while (it.hasNext()) {
|
||
249 | DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
||
250 | if (fieldDefinition.isHidden()) {
|
||
251 | continue;
|
||
252 | 1022 | jjdelcerro | } |
253 | 1886 | jjdelcerro | JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
254 | this.getContext(),
|
||
255 | fieldDefinition |
||
256 | ); |
||
257 | 1881 | jjdelcerro | JDynFormField jfield = factory.create( |
258 | getServiceManager(), |
||
259 | this.componentsFactory,
|
||
260 | fieldDefinition, |
||
261 | null
|
||
262 | ); |
||
263 | 1361 | jjdelcerro | if (jfield instanceof AbstractJDynFormField) { |
264 | ((AbstractJDynFormField) jfield).setForm(this);
|
||
265 | 1149 | jjdelcerro | } |
266 | 1361 | jjdelcerro | jfield.setReadOnly(this.isReadOnly());
|
267 | 1145 | jjdelcerro | jfield.addListener(this);
|
268 | 1361 | jjdelcerro | if (this.isReadOnly()) { |
269 | jfield.setReadOnly(this.isReadOnly());
|
||
270 | 1145 | jjdelcerro | } |
271 | 1509 | jjdelcerro | hasFields = true;
|
272 | 1947 | jjdelcerro | Tags tags = jfield.getDefinition().getTags(); |
273 | if( tags!=null ) { |
||
274 | String sep = tags.getString(TAG_DYNFORM_SEPARATOR, null); |
||
275 | if( !StringUtils.isBlank(sep) ) {
|
||
276 | builder.appendSeparator(i18n.getTranslation(sep)); |
||
277 | } |
||
278 | } |
||
279 | 1145 | jjdelcerro | builder.append(jfield.getJLabel(), jfield.asJComponent()); |
280 | 942 | jjdelcerro | |
281 | 1145 | jjdelcerro | this.components.put(jfield.getName(), jfield);
|
282 | } |
||
283 | 1509 | jjdelcerro | if( hasFields ) {
|
284 | String tablabel = group;
|
||
285 | if (StringUtils.isEmpty(tablabel)) {
|
||
286 | tablabel = ToolsLocator.getI18nManager().getTranslation("General");
|
||
287 | } |
||
288 | 1946 | jjdelcerro | tablabel = i18n.getTranslation(tablabel); |
289 | 1509 | jjdelcerro | tabbedPane.addTab(tablabel, builder.getPanel()); |
290 | 1145 | jjdelcerro | } |
291 | } |
||
292 | 1304 | fdiaz | |
293 | 1154 | jjdelcerro | try {
|
294 | 1361 | jjdelcerro | String defaultGroupName = (String) this.getDefinition().getTags().get("defaultGroup", DataTypes.STRING); |
295 | List<String> groupsNames = this.getDefinition().getGroups(); |
||
296 | 1154 | jjdelcerro | int groupIndex = groupsNames.indexOf(defaultGroupName);
|
297 | 1361 | jjdelcerro | if (groupIndex >= 0) { |
298 | 1154 | jjdelcerro | tabbedPane.setSelectedIndex(groupIndex); |
299 | } |
||
300 | } catch (Exception ex) { |
||
301 | // Ignore errors and don't set a tab by default
|
||
302 | } |
||
303 | 1304 | fdiaz | |
304 | 1361 | jjdelcerro | return tabbedPane;
|
305 | 1145 | jjdelcerro | } |
306 | 919 | jjdelcerro | |
307 | 1361 | jjdelcerro | @Override
|
308 | public void setReadOnly(boolean readOnly) { |
||
309 | 1862 | jjdelcerro | if( this.isReadOnly() == readOnly ) { |
310 | return;
|
||
311 | } |
||
312 | 1361 | jjdelcerro | super.setReadOnly(readOnly);
|
313 | if (this.isContentsInitialized()) { |
||
314 | Iterator it = this.getFieldsIterator(); |
||
315 | while (it.hasNext()) {
|
||
316 | JDynFormField field = (JDynFormField) it.next(); |
||
317 | 1852 | jjdelcerro | if( readOnly ) {
|
318 | field.setReadOnly(true);
|
||
319 | } else if( field.getDefinition().isReadOnly() ) { |
||
320 | field.setReadOnly(true);
|
||
321 | } else {
|
||
322 | field.setReadOnly(false);
|
||
323 | } |
||
324 | 1361 | jjdelcerro | } |
325 | 1145 | jjdelcerro | } |
326 | } |
||
327 | 919 | jjdelcerro | |
328 | 1361 | jjdelcerro | @Override
|
329 | 1145 | jjdelcerro | public void setValues(DynObject values) { |
330 | /*
|
||
331 | * TODO: Probablemente fuese mas acertado recorrer los controles de
|
||
332 | * components y a partir de ellos acceder a los valores del DynObject
|
||
333 | 1368 | fdiaz | * recibido. Eso permitiria trabajar mejor con herencia de DynObject.
|
334 | 1304 | fdiaz | * Habria que hacer lo mismo con el getValues.
|
335 | 1145 | jjdelcerro | */
|
336 | 1361 | jjdelcerro | if (!this.isContentsInitialized()) { |
337 | 1145 | jjdelcerro | this.values = values;
|
338 | return;
|
||
339 | } |
||
340 | DynClass def = values.getDynClass(); |
||
341 | DynField[] fields = def.getDynFields();
|
||
342 | for (int i = 0; i < fields.length; i++) { |
||
343 | 1947 | jjdelcerro | DynField field = fields[i]; |
344 | if( field.isHidden() ) {
|
||
345 | continue;
|
||
346 | } |
||
347 | String name = field.getName();
|
||
348 | 1145 | jjdelcerro | if (name == null || name.isEmpty()) { |
349 | 1881 | jjdelcerro | LOGGER.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty "); |
350 | 1145 | jjdelcerro | continue;
|
351 | } |
||
352 | JDynFormField jfield = (JDynFormField) this.getField(name);
|
||
353 | if (jfield == null) { |
||
354 | 1881 | jjdelcerro | LOGGER.warn("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'."); |
355 | 1145 | jjdelcerro | continue;
|
356 | } |
||
357 | if (values.getDynValue(name) == null) { |
||
358 | if (fields[i].getDataType().getType() == DataTypes.LIST) {
|
||
359 | try {
|
||
360 | if (((DynField_v2) fields[i]).getDynClassOfItems() != null) { |
||
361 | 1862 | jjdelcerro | values.setDynValue(name, new ArrayList<>()); |
362 | 1145 | jjdelcerro | } |
363 | } catch (Exception e) { |
||
364 | 1881 | jjdelcerro | LOGGER.warn("Problems initializing the DynObject List", e);
|
365 | 1145 | jjdelcerro | } |
366 | } |
||
367 | } |
||
368 | jfield.setValue(values.getDynValue(name)); |
||
369 | } |
||
370 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONSETVALUES, this); |
371 | 1948 | jjdelcerro | try {
|
372 | if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this, values) ) { |
||
373 | 1947 | jjdelcerro | this.setReadOnly(true); |
374 | } |
||
375 | 1948 | jjdelcerro | } catch (Exception ex) { |
376 | 1947 | jjdelcerro | } |
377 | 1145 | jjdelcerro | } |
378 | 919 | jjdelcerro | |
379 | 1361 | jjdelcerro | @Override
|
380 | 1145 | jjdelcerro | public void getValues(DynObject values) { |
381 | if (values == null) { |
||
382 | return;
|
||
383 | } |
||
384 | DynField[] fields = values.getDynClass().getDynFields();
|
||
385 | 1361 | jjdelcerro | for (DynField field : fields) {
|
386 | String name = field.getName();
|
||
387 | 1145 | jjdelcerro | JDynFormField jfield = (JDynFormField) this.getField(name);
|
388 | 1852 | jjdelcerro | if (jfield != null && !jfield.isReadOnly()) { |
389 | 1145 | jjdelcerro | try {
|
390 | 1405 | jjdelcerro | jfield.fetch(values); |
391 | 1145 | jjdelcerro | } catch (Exception ex) { |
392 | 1881 | jjdelcerro | LOGGER.warn("Can't get value of field '" + name + "'.", ex); |
393 | 1145 | jjdelcerro | } |
394 | } |
||
395 | } |
||
396 | } |
||
397 | 919 | jjdelcerro | |
398 | 1361 | jjdelcerro | @Override
|
399 | 1145 | jjdelcerro | public boolean hasValidValues() { |
400 | Iterator it = this.getFieldsIterator(); |
||
401 | while (it.hasNext()) {
|
||
402 | JDynFormField jfield = (JDynFormField) it.next(); |
||
403 | if (!jfield.hasValidValue()) {
|
||
404 | return false; |
||
405 | } |
||
406 | } |
||
407 | 1948 | jjdelcerro | try {
|
408 | return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this); |
||
409 | } catch (Exception ex) { |
||
410 | 1947 | jjdelcerro | } |
411 | 1948 | jjdelcerro | return true; |
412 | 1145 | jjdelcerro | } |
413 | 919 | jjdelcerro | |
414 | 1361 | jjdelcerro | @Override
|
415 | 1145 | jjdelcerro | public boolean hasValidValues(List<String> fieldsName) { |
416 | if (fieldsName == null) { |
||
417 | 1361 | jjdelcerro | fieldsName = new ArrayList<>(); |
418 | 1145 | jjdelcerro | } |
419 | Iterator it = this.getFieldsIterator(); |
||
420 | while (it.hasNext()) {
|
||
421 | JDynFormField jfield = (JDynFormField) it.next(); |
||
422 | if (!jfield.hasValidValue()) {
|
||
423 | fieldsName.add(jfield.getName()); |
||
424 | } |
||
425 | } |
||
426 | 1361 | jjdelcerro | return fieldsName.isEmpty();
|
427 | } |
||
428 | 933 | jjdelcerro | |
429 | 1361 | jjdelcerro | @Override
|
430 | public Object getValue(String fieldName) { |
||
431 | JDynFormField field = (JDynFormField) this.getField(fieldName);
|
||
432 | return field.getValue();
|
||
433 | } |
||
434 | |||
435 | @Override
|
||
436 | public void setValue(String fieldName, Object value) { |
||
437 | JDynFormField field = (JDynFormField) this.getField(fieldName);
|
||
438 | try {
|
||
439 | value = field.getDefinition().getDataType().coerce(value); |
||
440 | } catch (CoercionException e) {
|
||
441 | String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'."; |
||
442 | 1881 | jjdelcerro | LOGGER.warn(msg, e); |
443 | 1361 | jjdelcerro | throw new RuntimeException(msg, e); |
444 | 1145 | jjdelcerro | } |
445 | 1361 | jjdelcerro | field.setValue(value); |
446 | 1145 | jjdelcerro | } |
447 | 933 | jjdelcerro | |
448 | 1361 | jjdelcerro | @Override
|
449 | 1145 | jjdelcerro | public boolean isModified() { |
450 | 1892 | jjdelcerro | if( this.isReadOnly() ) { |
451 | return false; |
||
452 | } |
||
453 | 1948 | jjdelcerro | try {
|
454 | return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values); |
||
455 | } catch (NoSuchMethodException ex) { |
||
456 | } catch (Exception ex) { |
||
457 | LOGGER.warn("Error calling user function form_IsModified.",ex);
|
||
458 | 1947 | jjdelcerro | } |
459 | 1145 | jjdelcerro | Iterator it = this.getFieldsIterator(); |
460 | while (it.hasNext()) {
|
||
461 | JDynFormField jfield = (JDynFormField) it.next(); |
||
462 | if (jfield.isModified()) {
|
||
463 | return true; |
||
464 | } |
||
465 | } |
||
466 | return false; |
||
467 | } |
||
468 | 976 | jbadia | |
469 | 1361 | jjdelcerro | @Override
|
470 | 1145 | jjdelcerro | public void fieldEnter(JDynFormField field) { |
471 | message(field.getDefinition().getDescription()); |
||
472 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field); |
473 | 1145 | jjdelcerro | } |
474 | 1031 | jbadia | |
475 | 1361 | jjdelcerro | @Override
|
476 | 1145 | jjdelcerro | public void fieldExit(JDynFormField field) { |
477 | message(); |
||
478 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field); |
479 | 1145 | jjdelcerro | } |
480 | 1031 | jbadia | |
481 | 1361 | jjdelcerro | @Override
|
482 | 1145 | jjdelcerro | public void message(JDynFormField field, String message) { |
483 | message(message); |
||
484 | } |
||
485 | |||
486 | 1361 | jjdelcerro | @Override
|
487 | 1145 | jjdelcerro | public void fieldChanged(JDynFormField field) { |
488 | fireFieldChangeEvent(field); |
||
489 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field); |
490 | 1145 | jjdelcerro | } |
491 | |||
492 | 1862 | jjdelcerro | @Override
|
493 | 1361 | jjdelcerro | public JDynFormField getField(String fieldName) { |
494 | JDynFormField field = (JDynFormField) this.components.get(fieldName);
|
||
495 | return field;
|
||
496 | 1145 | jjdelcerro | } |
497 | |||
498 | 1361 | jjdelcerro | public Iterator getFieldsIterator() { |
499 | if (!this.isContentsInitialized()) { |
||
500 | this.initComponents();
|
||
501 | 1145 | jjdelcerro | } |
502 | 1361 | jjdelcerro | return this.components.values().iterator(); |
503 | 1145 | jjdelcerro | } |
504 | |||
505 | 1361 | jjdelcerro | public Collection getShowFields() { |
506 | return this.components.values(); |
||
507 | 1145 | jjdelcerro | } |
508 | |||
509 | 1361 | jjdelcerro | @Override
|
510 | public void clear() { |
||
511 | Iterator it = this.components.entrySet().iterator(); |
||
512 | while (it.hasNext()) {
|
||
513 | Map.Entry entry = (Map.Entry) it.next(); |
||
514 | ((JDynFormField) (entry.getValue())).clear(); |
||
515 | 1145 | jjdelcerro | } |
516 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_CLEAR, this); |
517 | 1145 | jjdelcerro | } |
518 | |||
519 | 1361 | jjdelcerro | @Override
|
520 | 1145 | jjdelcerro | public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
521 | 1361 | jjdelcerro | super.addActionToPopupMenu(tipo, name, action);
|
522 | if ( this.isContentsInitialized() ) { |
||
523 | 1145 | jjdelcerro | Iterator it = this.components.keySet().iterator(); |
524 | while (it.hasNext()) {
|
||
525 | String key = (String) it.next(); |
||
526 | Object obj = this.components.get(key); |
||
527 | if (obj instanceof JDynFormField) { |
||
528 | JDynFormField field = (JDynFormField) obj; |
||
529 | if (tipo == field.getDefinition().getDataType()) {
|
||
530 | if (field.asJComponent() instanceof SupportPopupMenu) { |
||
531 | 1361 | jjdelcerro | field.addActionToPopupMenu(name, action); |
532 | 1145 | jjdelcerro | } |
533 | } |
||
534 | } |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | |||
539 | 1361 | jjdelcerro | @Override
|
540 | 1145 | jjdelcerro | public void addSeparatorToPopupMenu(DataType tipo) { |
541 | 1361 | jjdelcerro | super.addSeparatorToPopupMenu(tipo);
|
542 | if ( this.isContentsInitialized() ) { |
||
543 | 1145 | jjdelcerro | Iterator it = this.components.keySet().iterator(); |
544 | while (it.hasNext()) {
|
||
545 | String key = (String) it.next(); |
||
546 | Object obj = this.components.get(key); |
||
547 | if (obj instanceof JDynFormField) { |
||
548 | JDynFormField field = (JDynFormField) obj; |
||
549 | if (tipo == field.getDefinition().getDataType()) {
|
||
550 | if (field.asJComponent() instanceof SupportPopupMenu) { |
||
551 | ((SupportPopupMenu) field.asJComponent()).addSeparatorToPopupMenu(); |
||
552 | } |
||
553 | } |
||
554 | } |
||
555 | } |
||
556 | } |
||
557 | } |
||
558 | |||
559 | 1361 | jjdelcerro | @Override
|
560 | public void setBorder(boolean border) { |
||
561 | super.setBorder(border && this.getShowFields().size() >1); |
||
562 | 1145 | jjdelcerro | } |
563 | |||
564 | 919 | jjdelcerro | } |