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 @ 2031
History | View | Annotate | Download (23.8 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 | 2031 | jjdelcerro | import com.jgoodies.forms.layout.CellConstraints; |
54 | 919 | jjdelcerro | import com.jgoodies.forms.layout.FormLayout; |
55 | import com.jgoodies.forms.layout.RowSpec; |
||
56 | 1145 | jjdelcerro | import java.util.Collection; |
57 | 1881 | jjdelcerro | import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
58 | import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
||
59 | 1947 | jjdelcerro | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_SEPARATOR; |
60 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_CLEAR; |
||
61 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISMODIFIED; |
||
62 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISREADONLY; |
||
63 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDCHANGED; |
||
64 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDENTER; |
||
65 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDEXIT; |
||
66 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONLOAD; |
||
67 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONSETVALUES; |
||
68 | import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_VALIDATE; |
||
69 | 1881 | jjdelcerro | import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
70 | import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
||
71 | 1149 | jjdelcerro | import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
72 | 1947 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
73 | 1946 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
74 | 919 | jjdelcerro | |
75 | 1881 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
76 | 1361 | jjdelcerro | public class DefaultJDynForm extends AbstractJDynForm implements JDynFormFieldListener { |
77 | 919 | jjdelcerro | |
78 | 1145 | jjdelcerro | private Map components = null; |
79 | 1983 | jjdelcerro | private final ComponentsFactory componentsFactory; |
80 | 919 | jjdelcerro | |
81 | 1735 | jjdelcerro | public DefaultJDynForm(
|
82 | 1881 | jjdelcerro | DynFormSPIManager manager, |
83 | JDynFormFactory factory, |
||
84 | 1735 | jjdelcerro | DynFormDefinition definition, |
85 | DynFormContext context |
||
86 | 1881 | jjdelcerro | ) { |
87 | super(manager, factory, definition, context);
|
||
88 | 1145 | jjdelcerro | this.components = new HashMap(); |
89 | 1881 | jjdelcerro | this.componentsFactory = manager.createDefaultComponentsFactory();
|
90 | 1145 | jjdelcerro | } |
91 | 919 | jjdelcerro | |
92 | 1361 | jjdelcerro | @Override
|
93 | protected JComponent getFieldsContainer() { |
||
94 | 1368 | fdiaz | |
95 | 1361 | jjdelcerro | try {
|
96 | JComponent component;
|
||
97 | switch (this.getLayoutMode()) { |
||
98 | case USE_PLAIN:
|
||
99 | case USE_TREE:
|
||
100 | default:
|
||
101 | component = getFieldsContainerPlain(); |
||
102 | break;
|
||
103 | case USE_TABS:
|
||
104 | component = getFieldsContainerUseTabs(); |
||
105 | break;
|
||
106 | case USE_SEPARATORS:
|
||
107 | component = getFieldsContainerUseSeparators(); |
||
108 | break;
|
||
109 | 1145 | jjdelcerro | } |
110 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONLOAD, this); |
111 | 1361 | jjdelcerro | return component;
|
112 | } catch (ServiceException ex) {
|
||
113 | throw new RuntimeException(ex); |
||
114 | 1145 | jjdelcerro | } |
115 | } |
||
116 | |||
117 | 1361 | jjdelcerro | private JComponent getFieldsContainerPlain() throws ServiceException { |
118 | 1145 | jjdelcerro | FormLayout layout = new FormLayout(
|
119 | "left:pref, 8px, fill:80dlu:grow", "pref"); |
||
120 | |||
121 | DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
||
122 | 1975 | jjdelcerro | builder.defaultRowSpec(new RowSpec(
|
123 | 1145 | jjdelcerro | RowSpec.TOP, |
124 | builder.getLayout().getRowSpec(1).getSize(),
|
||
125 | builder.getLayout().getRowSpec(1).getResizeWeight()));
|
||
126 | |||
127 | 1361 | jjdelcerro | List fields = this.getDefinition().getDefinitions(); |
128 | 1145 | jjdelcerro | Iterator it = fields.iterator();
|
129 | while (it.hasNext()) {
|
||
130 | DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
||
131 | if (fieldDefinition.isHidden()) {
|
||
132 | continue;
|
||
133 | } |
||
134 | 1886 | jjdelcerro | JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
135 | this.getContext(),
|
||
136 | fieldDefinition |
||
137 | ); |
||
138 | 1881 | jjdelcerro | JDynFormField jfield = factory.create( |
139 | getServiceManager(), |
||
140 | this.componentsFactory,
|
||
141 | fieldDefinition, |
||
142 | null
|
||
143 | ); |
||
144 | 1361 | jjdelcerro | if (jfield instanceof AbstractJDynFormField) { |
145 | ((AbstractJDynFormField) jfield).setForm(this);
|
||
146 | 1149 | jjdelcerro | } |
147 | 1361 | jjdelcerro | if (this.isReadOnly()) { |
148 | 1862 | jjdelcerro | jfield.setReadOnly(true);
|
149 | } else {
|
||
150 | jfield.setReadOnly(fieldDefinition.isReadOnly()); |
||
151 | 1145 | jjdelcerro | } |
152 | 1862 | jjdelcerro | jfield.addListener(this);
|
153 | 2031 | jjdelcerro | double resizeWeight = jfield.getResizeWeight();
|
154 | if( resizeWeight>0 ) { |
||
155 | builder.appendRow(new RowSpec(
|
||
156 | RowSpec.FILL, |
||
157 | builder.getLayout().getRowSpec(1).getSize(),
|
||
158 | resizeWeight) |
||
159 | ); |
||
160 | builder.append(jfield.getJLabel()); |
||
161 | builder.add( |
||
162 | jfield.asJComponent(), |
||
163 | new CellConstraints(
|
||
164 | builder.getColumn(), |
||
165 | builder.getRow(), |
||
166 | CellConstraints.FILL, |
||
167 | CellConstraints.FILL |
||
168 | ) |
||
169 | ); |
||
170 | builder.nextColumn(); |
||
171 | } else {
|
||
172 | builder.append(jfield.getJLabel(), jfield.asJComponent()); |
||
173 | } |
||
174 | 1145 | jjdelcerro | this.components.put(jfield.getName(), jfield);
|
175 | } |
||
176 | 1361 | jjdelcerro | return builder.getPanel();
|
177 | 1145 | jjdelcerro | } |
178 | |||
179 | 1361 | jjdelcerro | private JComponent getFieldsContainerUseSeparators() throws ServiceException { |
180 | List<String> groups = this.getDefinition().getGroups(); |
||
181 | 1145 | jjdelcerro | |
182 | FormLayout layout = new FormLayout(
|
||
183 | "left:pref, 8px, fill:80dlu:grow", "pref"); |
||
184 | |||
185 | DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
||
186 | 1975 | jjdelcerro | builder.defaultRowSpec(new RowSpec(
|
187 | 1145 | jjdelcerro | RowSpec.TOP, |
188 | builder.getLayout().getRowSpec(1).getSize(),
|
||
189 | builder.getLayout().getRowSpec(1).getResizeWeight()));
|
||
190 | |||
191 | 1361 | jjdelcerro | for (String group : groups) { |
192 | 1509 | jjdelcerro | boolean firstfield = true; |
193 | 1361 | jjdelcerro | List fields = this.getDefinition().getDefinitions(group); |
194 | 1145 | jjdelcerro | Iterator it = fields.iterator();
|
195 | while (it.hasNext()) {
|
||
196 | DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
||
197 | if (fieldDefinition.isHidden()) {
|
||
198 | continue;
|
||
199 | } |
||
200 | 1886 | jjdelcerro | JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
201 | this.getContext(),
|
||
202 | fieldDefinition |
||
203 | ); |
||
204 | 1881 | jjdelcerro | JDynFormField jfield = factory.create( |
205 | getServiceManager(), |
||
206 | this.componentsFactory,
|
||
207 | fieldDefinition, |
||
208 | null
|
||
209 | ); |
||
210 | 1361 | jjdelcerro | if (jfield instanceof AbstractJDynFormField) { |
211 | ((AbstractJDynFormField) jfield).setForm(this);
|
||
212 | 1149 | jjdelcerro | } |
213 | 1145 | jjdelcerro | jfield.addListener(this);
|
214 | 1361 | jjdelcerro | if (this.isReadOnly()) { |
215 | jfield.setReadOnly(this.isReadOnly());
|
||
216 | 1145 | jjdelcerro | } |
217 | |||
218 | List<Action> customActions = getCustomFields(fieldDefinition.getDataType()); |
||
219 | |||
220 | if (customActions != null && !customActions.isEmpty()) { |
||
221 | Iterator it2 = customActions.iterator();
|
||
222 | while (it2.hasNext()) {
|
||
223 | Object obj = it2.next();
|
||
224 | if (obj != null) { |
||
225 | Action act = (Action) obj; |
||
226 | jfield.addActionToPopupMenu((String) act.getValue(Action.NAME), act); |
||
227 | } else {
|
||
228 | jfield.addSeparatorToPopupMenu(); |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | 1509 | jjdelcerro | if( firstfield ) {
|
233 | firstfield = false;
|
||
234 | builder.appendSeparator(group); |
||
235 | } |
||
236 | 2031 | jjdelcerro | double resizeWeight = jfield.getResizeWeight();
|
237 | if( resizeWeight>0 ) { |
||
238 | builder.appendRow(new RowSpec(
|
||
239 | RowSpec.FILL, |
||
240 | builder.getLayout().getRowSpec(1).getSize(),
|
||
241 | resizeWeight) |
||
242 | ); |
||
243 | builder.append(jfield.getJLabel()); |
||
244 | builder.add( |
||
245 | jfield.asJComponent(), |
||
246 | new CellConstraints(
|
||
247 | builder.getColumn(), |
||
248 | builder.getRow(), |
||
249 | CellConstraints.FILL, |
||
250 | CellConstraints.FILL |
||
251 | ) |
||
252 | ); |
||
253 | builder.nextColumn(); |
||
254 | } else {
|
||
255 | builder.append(jfield.getJLabel(), jfield.asJComponent()); |
||
256 | } |
||
257 | 1145 | jjdelcerro | this.components.put(jfield.getName(), jfield);
|
258 | } |
||
259 | } |
||
260 | 1361 | jjdelcerro | return builder.getPanel();
|
261 | 1145 | jjdelcerro | } |
262 | |||
263 | 1361 | jjdelcerro | private JComponent getFieldsContainerUseTabs() throws ServiceException { |
264 | 1145 | jjdelcerro | |
265 | 1033 | jbadia | JTabbedPane tabbedPane = new JTabbedPane(); |
266 | 1881 | jjdelcerro | tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
|
267 | 1361 | jjdelcerro | if (this.getDefinition().getTags().has("TabPlacement")) { |
268 | 1145 | jjdelcerro | try {
|
269 | 1361 | jjdelcerro | tabbedPane.setTabPlacement(this.getDefinition().getTags().getInt("TabPlacement")); |
270 | 1145 | jjdelcerro | } catch (Exception e) { |
271 | // Ignorada
|
||
272 | } |
||
273 | } |
||
274 | 1946 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
275 | 1361 | jjdelcerro | List<String> groups = this.getDefinition().getGroups(); |
276 | for (String group : groups) { |
||
277 | 1145 | jjdelcerro | FormLayout layout = new FormLayout(
|
278 | "left:pref, 8px, fill:80dlu:grow", "pref"); |
||
279 | DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
||
280 | 1304 | fdiaz | builder.defaultRowSpec(new RowSpec(
|
281 | 1145 | jjdelcerro | RowSpec.TOP, |
282 | builder.getLayout().getRowSpec(1).getSize(),
|
||
283 | builder.getLayout().getRowSpec(1).getResizeWeight()));
|
||
284 | 1361 | jjdelcerro | List fields = this.getDefinition().getDefinitions(group); |
285 | 1509 | jjdelcerro | boolean hasFields = false; |
286 | 1145 | jjdelcerro | Iterator it = fields.iterator();
|
287 | while (it.hasNext()) {
|
||
288 | DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
||
289 | if (fieldDefinition.isHidden()) {
|
||
290 | continue;
|
||
291 | 1022 | jjdelcerro | } |
292 | 1886 | jjdelcerro | JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
293 | this.getContext(),
|
||
294 | fieldDefinition |
||
295 | ); |
||
296 | 1881 | jjdelcerro | JDynFormField jfield = factory.create( |
297 | getServiceManager(), |
||
298 | this.componentsFactory,
|
||
299 | fieldDefinition, |
||
300 | null
|
||
301 | ); |
||
302 | 1361 | jjdelcerro | if (jfield instanceof AbstractJDynFormField) { |
303 | ((AbstractJDynFormField) jfield).setForm(this);
|
||
304 | 1149 | jjdelcerro | } |
305 | 1361 | jjdelcerro | jfield.setReadOnly(this.isReadOnly());
|
306 | 1145 | jjdelcerro | jfield.addListener(this);
|
307 | 1361 | jjdelcerro | if (this.isReadOnly()) { |
308 | jfield.setReadOnly(this.isReadOnly());
|
||
309 | 1145 | jjdelcerro | } |
310 | 1509 | jjdelcerro | hasFields = true;
|
311 | 1947 | jjdelcerro | Tags tags = jfield.getDefinition().getTags(); |
312 | if( tags!=null ) { |
||
313 | String sep = tags.getString(TAG_DYNFORM_SEPARATOR, null); |
||
314 | if( !StringUtils.isBlank(sep) ) {
|
||
315 | builder.appendSeparator(i18n.getTranslation(sep)); |
||
316 | } |
||
317 | } |
||
318 | 2031 | jjdelcerro | double resizeWeight = jfield.getResizeWeight();
|
319 | if( resizeWeight>0 ) { |
||
320 | builder.appendRow(new RowSpec(
|
||
321 | RowSpec.FILL, |
||
322 | builder.getLayout().getRowSpec(1).getSize(),
|
||
323 | resizeWeight) |
||
324 | ); |
||
325 | builder.append(jfield.getJLabel()); |
||
326 | builder.add( |
||
327 | jfield.asJComponent(), |
||
328 | new CellConstraints(
|
||
329 | builder.getColumn(), |
||
330 | builder.getRow(), |
||
331 | CellConstraints.FILL, |
||
332 | CellConstraints.FILL |
||
333 | ) |
||
334 | ); |
||
335 | builder.nextColumn(); |
||
336 | } else {
|
||
337 | builder.append(jfield.getJLabel(), jfield.asJComponent()); |
||
338 | } |
||
339 | 942 | jjdelcerro | |
340 | 1145 | jjdelcerro | this.components.put(jfield.getName(), jfield);
|
341 | } |
||
342 | 1509 | jjdelcerro | if( hasFields ) {
|
343 | String tablabel = group;
|
||
344 | if (StringUtils.isEmpty(tablabel)) {
|
||
345 | tablabel = ToolsLocator.getI18nManager().getTranslation("General");
|
||
346 | } |
||
347 | 1946 | jjdelcerro | tablabel = i18n.getTranslation(tablabel); |
348 | 1509 | jjdelcerro | tabbedPane.addTab(tablabel, builder.getPanel()); |
349 | 1145 | jjdelcerro | } |
350 | } |
||
351 | 1304 | fdiaz | |
352 | 1154 | jjdelcerro | try {
|
353 | 1361 | jjdelcerro | String defaultGroupName = (String) this.getDefinition().getTags().get("defaultGroup", DataTypes.STRING); |
354 | List<String> groupsNames = this.getDefinition().getGroups(); |
||
355 | 1154 | jjdelcerro | int groupIndex = groupsNames.indexOf(defaultGroupName);
|
356 | 1361 | jjdelcerro | if (groupIndex >= 0) { |
357 | 1154 | jjdelcerro | tabbedPane.setSelectedIndex(groupIndex); |
358 | } |
||
359 | } catch (Exception ex) { |
||
360 | // Ignore errors and don't set a tab by default
|
||
361 | } |
||
362 | 1304 | fdiaz | |
363 | 1361 | jjdelcerro | return tabbedPane;
|
364 | 1145 | jjdelcerro | } |
365 | 919 | jjdelcerro | |
366 | 1361 | jjdelcerro | @Override
|
367 | public void setReadOnly(boolean readOnly) { |
||
368 | 1862 | jjdelcerro | if( this.isReadOnly() == readOnly ) { |
369 | return;
|
||
370 | } |
||
371 | 1361 | jjdelcerro | super.setReadOnly(readOnly);
|
372 | if (this.isContentsInitialized()) { |
||
373 | Iterator it = this.getFieldsIterator(); |
||
374 | while (it.hasNext()) {
|
||
375 | JDynFormField field = (JDynFormField) it.next(); |
||
376 | 1852 | jjdelcerro | if( readOnly ) {
|
377 | field.setReadOnly(true);
|
||
378 | } else if( field.getDefinition().isReadOnly() ) { |
||
379 | field.setReadOnly(true);
|
||
380 | } else {
|
||
381 | field.setReadOnly(false);
|
||
382 | } |
||
383 | 1361 | jjdelcerro | } |
384 | 1145 | jjdelcerro | } |
385 | } |
||
386 | 919 | jjdelcerro | |
387 | 1361 | jjdelcerro | @Override
|
388 | 1145 | jjdelcerro | public void setValues(DynObject values) { |
389 | /*
|
||
390 | * TODO: Probablemente fuese mas acertado recorrer los controles de
|
||
391 | * components y a partir de ellos acceder a los valores del DynObject
|
||
392 | 1368 | fdiaz | * recibido. Eso permitiria trabajar mejor con herencia de DynObject.
|
393 | 1304 | fdiaz | * Habria que hacer lo mismo con el getValues.
|
394 | 1145 | jjdelcerro | */
|
395 | 1361 | jjdelcerro | if (!this.isContentsInitialized()) { |
396 | 1145 | jjdelcerro | this.values = values;
|
397 | return;
|
||
398 | } |
||
399 | DynClass def = values.getDynClass(); |
||
400 | DynField[] fields = def.getDynFields();
|
||
401 | for (int i = 0; i < fields.length; i++) { |
||
402 | 1947 | jjdelcerro | DynField field = fields[i]; |
403 | if( field.isHidden() ) {
|
||
404 | continue;
|
||
405 | } |
||
406 | String name = field.getName();
|
||
407 | 1145 | jjdelcerro | if (name == null || name.isEmpty()) { |
408 | 1881 | jjdelcerro | LOGGER.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty "); |
409 | 1145 | jjdelcerro | continue;
|
410 | } |
||
411 | JDynFormField jfield = (JDynFormField) this.getField(name);
|
||
412 | if (jfield == null) { |
||
413 | 1881 | jjdelcerro | LOGGER.warn("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'."); |
414 | 1145 | jjdelcerro | continue;
|
415 | } |
||
416 | if (values.getDynValue(name) == null) { |
||
417 | if (fields[i].getDataType().getType() == DataTypes.LIST) {
|
||
418 | try {
|
||
419 | if (((DynField_v2) fields[i]).getDynClassOfItems() != null) { |
||
420 | 1862 | jjdelcerro | values.setDynValue(name, new ArrayList<>()); |
421 | 1145 | jjdelcerro | } |
422 | } catch (Exception e) { |
||
423 | 1881 | jjdelcerro | LOGGER.warn("Problems initializing the DynObject List", e);
|
424 | 1145 | jjdelcerro | } |
425 | } |
||
426 | } |
||
427 | jfield.setValue(values.getDynValue(name)); |
||
428 | } |
||
429 | 1983 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values); |
430 | 1948 | jjdelcerro | try {
|
431 | 1983 | jjdelcerro | if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this) ) { |
432 | 1947 | jjdelcerro | this.setReadOnly(true); |
433 | } |
||
434 | 1948 | jjdelcerro | } catch (Exception ex) { |
435 | 1947 | jjdelcerro | } |
436 | 1145 | jjdelcerro | } |
437 | 919 | jjdelcerro | |
438 | 1361 | jjdelcerro | @Override
|
439 | 1145 | jjdelcerro | public void getValues(DynObject values) { |
440 | if (values == null) { |
||
441 | return;
|
||
442 | } |
||
443 | DynField[] fields = values.getDynClass().getDynFields();
|
||
444 | 1361 | jjdelcerro | for (DynField field : fields) {
|
445 | String name = field.getName();
|
||
446 | 1145 | jjdelcerro | JDynFormField jfield = (JDynFormField) this.getField(name);
|
447 | 1852 | jjdelcerro | if (jfield != null && !jfield.isReadOnly()) { |
448 | 1145 | jjdelcerro | try {
|
449 | 1405 | jjdelcerro | jfield.fetch(values); |
450 | 1145 | jjdelcerro | } catch (Exception ex) { |
451 | 1881 | jjdelcerro | LOGGER.warn("Can't get value of field '" + name + "'.", ex); |
452 | 1145 | jjdelcerro | } |
453 | } |
||
454 | } |
||
455 | } |
||
456 | 919 | jjdelcerro | |
457 | 1361 | jjdelcerro | @Override
|
458 | 1145 | jjdelcerro | public boolean hasValidValues() { |
459 | Iterator it = this.getFieldsIterator(); |
||
460 | while (it.hasNext()) {
|
||
461 | JDynFormField jfield = (JDynFormField) it.next(); |
||
462 | if (!jfield.hasValidValue()) {
|
||
463 | return false; |
||
464 | } |
||
465 | } |
||
466 | 1948 | jjdelcerro | try {
|
467 | return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this); |
||
468 | } catch (Exception ex) { |
||
469 | 1947 | jjdelcerro | } |
470 | 1948 | jjdelcerro | return true; |
471 | 1145 | jjdelcerro | } |
472 | 919 | jjdelcerro | |
473 | 1361 | jjdelcerro | @Override
|
474 | 1145 | jjdelcerro | public boolean hasValidValues(List<String> fieldsName) { |
475 | if (fieldsName == null) { |
||
476 | 1361 | jjdelcerro | fieldsName = new ArrayList<>(); |
477 | 1145 | jjdelcerro | } |
478 | Iterator it = this.getFieldsIterator(); |
||
479 | while (it.hasNext()) {
|
||
480 | JDynFormField jfield = (JDynFormField) it.next(); |
||
481 | if (!jfield.hasValidValue()) {
|
||
482 | fieldsName.add(jfield.getName()); |
||
483 | } |
||
484 | } |
||
485 | 1361 | jjdelcerro | return fieldsName.isEmpty();
|
486 | } |
||
487 | 933 | jjdelcerro | |
488 | 1361 | jjdelcerro | @Override
|
489 | public Object getValue(String fieldName) { |
||
490 | JDynFormField field = (JDynFormField) this.getField(fieldName);
|
||
491 | return field.getValue();
|
||
492 | } |
||
493 | |||
494 | @Override
|
||
495 | public void setValue(String fieldName, Object value) { |
||
496 | JDynFormField field = (JDynFormField) this.getField(fieldName);
|
||
497 | try {
|
||
498 | value = field.getDefinition().getDataType().coerce(value); |
||
499 | } catch (CoercionException e) {
|
||
500 | String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'."; |
||
501 | 1881 | jjdelcerro | LOGGER.warn(msg, e); |
502 | 1361 | jjdelcerro | throw new RuntimeException(msg, e); |
503 | 1145 | jjdelcerro | } |
504 | 1361 | jjdelcerro | field.setValue(value); |
505 | 1145 | jjdelcerro | } |
506 | 933 | jjdelcerro | |
507 | 1361 | jjdelcerro | @Override
|
508 | 1145 | jjdelcerro | public boolean isModified() { |
509 | 1892 | jjdelcerro | if( this.isReadOnly() ) { |
510 | return false; |
||
511 | } |
||
512 | 1948 | jjdelcerro | try {
|
513 | return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values); |
||
514 | } catch (NoSuchMethodException ex) { |
||
515 | } catch (Exception ex) { |
||
516 | LOGGER.warn("Error calling user function form_IsModified.",ex);
|
||
517 | 1947 | jjdelcerro | } |
518 | 1145 | jjdelcerro | Iterator it = this.getFieldsIterator(); |
519 | while (it.hasNext()) {
|
||
520 | JDynFormField jfield = (JDynFormField) it.next(); |
||
521 | if (jfield.isModified()) {
|
||
522 | return true; |
||
523 | } |
||
524 | } |
||
525 | return false; |
||
526 | } |
||
527 | 976 | jbadia | |
528 | 1361 | jjdelcerro | @Override
|
529 | 1145 | jjdelcerro | public void fieldEnter(JDynFormField field) { |
530 | message(field.getDefinition().getDescription()); |
||
531 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field); |
532 | 1145 | jjdelcerro | } |
533 | 1031 | jbadia | |
534 | 1361 | jjdelcerro | @Override
|
535 | 1145 | jjdelcerro | public void fieldExit(JDynFormField field) { |
536 | message(); |
||
537 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field); |
538 | 1145 | jjdelcerro | } |
539 | 1031 | jbadia | |
540 | 1361 | jjdelcerro | @Override
|
541 | 1145 | jjdelcerro | public void message(JDynFormField field, String message) { |
542 | message(message); |
||
543 | } |
||
544 | |||
545 | 1361 | jjdelcerro | @Override
|
546 | 1145 | jjdelcerro | public void fieldChanged(JDynFormField field) { |
547 | fireFieldChangeEvent(field); |
||
548 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field); |
549 | 1145 | jjdelcerro | } |
550 | |||
551 | 1862 | jjdelcerro | @Override
|
552 | 1361 | jjdelcerro | public JDynFormField getField(String fieldName) { |
553 | JDynFormField field = (JDynFormField) this.components.get(fieldName);
|
||
554 | return field;
|
||
555 | 1145 | jjdelcerro | } |
556 | |||
557 | 1361 | jjdelcerro | public Iterator getFieldsIterator() { |
558 | if (!this.isContentsInitialized()) { |
||
559 | this.initComponents();
|
||
560 | 1145 | jjdelcerro | } |
561 | 1361 | jjdelcerro | return this.components.values().iterator(); |
562 | 1145 | jjdelcerro | } |
563 | |||
564 | 1361 | jjdelcerro | public Collection getShowFields() { |
565 | return this.components.values(); |
||
566 | 1145 | jjdelcerro | } |
567 | |||
568 | 1361 | jjdelcerro | @Override
|
569 | public void clear() { |
||
570 | Iterator it = this.components.entrySet().iterator(); |
||
571 | while (it.hasNext()) {
|
||
572 | Map.Entry entry = (Map.Entry) it.next(); |
||
573 | ((JDynFormField) (entry.getValue())).clear(); |
||
574 | 1145 | jjdelcerro | } |
575 | 1947 | jjdelcerro | this.callUserEvent(USERCODE_FORM_CLEAR, this); |
576 | 1145 | jjdelcerro | } |
577 | |||
578 | 1361 | jjdelcerro | @Override
|
579 | 1145 | jjdelcerro | public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
580 | 1361 | jjdelcerro | super.addActionToPopupMenu(tipo, name, action);
|
581 | if ( this.isContentsInitialized() ) { |
||
582 | 1145 | jjdelcerro | Iterator it = this.components.keySet().iterator(); |
583 | while (it.hasNext()) {
|
||
584 | String key = (String) it.next(); |
||
585 | Object obj = this.components.get(key); |
||
586 | if (obj instanceof JDynFormField) { |
||
587 | JDynFormField field = (JDynFormField) obj; |
||
588 | if (tipo == field.getDefinition().getDataType()) {
|
||
589 | if (field.asJComponent() instanceof SupportPopupMenu) { |
||
590 | 1361 | jjdelcerro | field.addActionToPopupMenu(name, action); |
591 | 1145 | jjdelcerro | } |
592 | } |
||
593 | } |
||
594 | } |
||
595 | } |
||
596 | } |
||
597 | |||
598 | 1361 | jjdelcerro | @Override
|
599 | 1145 | jjdelcerro | public void addSeparatorToPopupMenu(DataType tipo) { |
600 | 1361 | jjdelcerro | super.addSeparatorToPopupMenu(tipo);
|
601 | if ( this.isContentsInitialized() ) { |
||
602 | 1145 | jjdelcerro | Iterator it = this.components.keySet().iterator(); |
603 | while (it.hasNext()) {
|
||
604 | String key = (String) it.next(); |
||
605 | Object obj = this.components.get(key); |
||
606 | if (obj instanceof JDynFormField) { |
||
607 | JDynFormField field = (JDynFormField) obj; |
||
608 | if (tipo == field.getDefinition().getDataType()) {
|
||
609 | if (field.asJComponent() instanceof SupportPopupMenu) { |
||
610 | ((SupportPopupMenu) field.asJComponent()).addSeparatorToPopupMenu(); |
||
611 | } |
||
612 | } |
||
613 | } |
||
614 | } |
||
615 | } |
||
616 | } |
||
617 | |||
618 | 1361 | jjdelcerro | @Override
|
619 | public void setBorder(boolean border) { |
||
620 | super.setBorder(border && this.getShowFields().size() >1); |
||
621 | 1145 | jjdelcerro | } |
622 | |||
623 | 919 | jjdelcerro | } |