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 @ 2285
History | View | Annotate | Download (26.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* 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 |
*
|
11 |
* 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 |
*
|
16 |
* 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 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.tools.dynform.impl; |
24 |
|
25 |
import java.awt.Component; |
26 |
import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm; |
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import javax.swing.Action; |
34 |
import javax.swing.JComponent; |
35 |
import javax.swing.JTabbedPane; |
36 |
|
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dataTypes.CoercionException; |
40 |
import org.gvsig.tools.dataTypes.DataType; |
41 |
import org.gvsig.tools.dataTypes.DataTypes; |
42 |
import org.gvsig.tools.dynform.DynFormDefinition; |
43 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
44 |
import org.gvsig.tools.dynform.JDynFormField; |
45 |
import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener; |
46 |
import org.gvsig.tools.dynform.spi.dynformfield.SupportPopupMenu; |
47 |
import org.gvsig.tools.dynobject.DynClass; |
48 |
import org.gvsig.tools.dynobject.DynField; |
49 |
import org.gvsig.tools.dynobject.DynField_v2; |
50 |
import org.gvsig.tools.dynobject.DynObject; |
51 |
import org.gvsig.tools.service.ServiceException; |
52 |
|
53 |
import java.awt.GridBagConstraints; |
54 |
import java.awt.GridBagLayout; |
55 |
import java.awt.Insets; |
56 |
import java.util.Collection; |
57 |
import javax.swing.JLabel; |
58 |
import javax.swing.JPanel; |
59 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
60 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
61 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_CLEAR; |
62 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISMODIFIED; |
63 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISREADONLY; |
64 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDCHANGED; |
65 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDENTER; |
66 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDEXIT; |
67 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONLOAD; |
68 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONSETVALUES; |
69 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_VALIDATE; |
70 |
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
71 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
72 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
73 |
import org.gvsig.tools.i18n.I18nManager; |
74 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
75 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
76 |
|
77 |
@SuppressWarnings("UseSpecificCatch") |
78 |
public class DefaultJDynForm extends AbstractJDynForm implements JDynFormFieldListener { |
79 |
|
80 |
private Map components = null; |
81 |
private final ComponentsFactory componentsFactory; |
82 |
|
83 |
public DefaultJDynForm(
|
84 |
DynFormSPIManager manager, |
85 |
JDynFormFactory factory, |
86 |
DynFormDefinition definition, |
87 |
DynFormContext context |
88 |
) { |
89 |
super(manager, factory, definition, context);
|
90 |
this.components = new HashMap(); |
91 |
this.componentsFactory = manager.createDefaultComponentsFactory();
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
protected JComponent getFieldsContainer() { |
96 |
|
97 |
try {
|
98 |
JComponent component;
|
99 |
switch (this.getLayoutMode()) { |
100 |
case USE_PLAIN:
|
101 |
case USE_TREE:
|
102 |
default:
|
103 |
component = getFieldsContainerPlain(); |
104 |
break;
|
105 |
case USE_TABS:
|
106 |
component = getFieldsContainerUseTabs(); |
107 |
break;
|
108 |
case USE_SEPARATORS:
|
109 |
component = getFieldsContainerUseSeparators(); |
110 |
break;
|
111 |
} |
112 |
this.callUserEvent(USERCODE_FORM_ONLOAD, this); |
113 |
return component;
|
114 |
} catch (ServiceException ex) {
|
115 |
throw new RuntimeException(ex); |
116 |
} |
117 |
} |
118 |
|
119 |
private JComponent getFieldsContainerPlain() throws ServiceException { |
120 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
121 |
GridBagConstraints c = new GridBagConstraints(); |
122 |
c.insets = new Insets(2, 2, 2, 2); |
123 |
c.anchor = GridBagConstraints.PAGE_START;
|
124 |
JPanel fieldsPanel = new JPanel(); |
125 |
fieldsPanel.setLayout(new GridBagLayout()); |
126 |
int gridy = 0; |
127 |
List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(); |
128 |
for (DynFormFieldDefinition fieldDefinition : fields) {
|
129 |
if (fieldDefinition.isHidden()) {
|
130 |
continue;
|
131 |
} |
132 |
JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
133 |
this.getContext(),
|
134 |
fieldDefinition |
135 |
); |
136 |
JDynFormField jfield = factory.create( |
137 |
getServiceManager(), |
138 |
this.componentsFactory,
|
139 |
fieldDefinition, |
140 |
null
|
141 |
); |
142 |
if (jfield instanceof AbstractJDynFormField) { |
143 |
((AbstractJDynFormField) jfield).setForm(this);
|
144 |
} |
145 |
jfield.addListener(this);
|
146 |
if (this.isReadOnly()) { |
147 |
jfield.setReadOnly(this.isReadOnly());
|
148 |
} |
149 |
this.configurePopupMenu(jfield);
|
150 |
String sep = jfield.getSeparatorTitleToUseBefore();
|
151 |
if( !StringUtils.isBlank(sep) ) {
|
152 |
c.gridx = 0;
|
153 |
c.gridy = gridy; |
154 |
c.gridwidth = 2;
|
155 |
c.gridheight = 1;
|
156 |
c.fill = GridBagConstraints.HORIZONTAL;
|
157 |
c.weightx = 1;
|
158 |
c.weighty = 0;
|
159 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(sep), c); |
160 |
gridy++; |
161 |
} |
162 |
if( jfield.useEmptyLabel() ) {
|
163 |
c.gridx = 0;
|
164 |
c.gridy = gridy; |
165 |
c.gridwidth = 2;
|
166 |
c.gridheight = 1;
|
167 |
} else {
|
168 |
c.gridx = 0;
|
169 |
c.gridy = gridy; |
170 |
c.gridwidth = 1;
|
171 |
c.gridheight = 1;
|
172 |
c.fill = GridBagConstraints.HORIZONTAL;
|
173 |
c.weightx = 0;
|
174 |
c.weighty = 0;
|
175 |
jfield.getJLabel().setAlignmentY(Component.TOP_ALIGNMENT);
|
176 |
fieldsPanel.add(jfield.getJLabel(), c); |
177 |
c.gridx = 1;
|
178 |
c.gridy = gridy; |
179 |
c.gridwidth = 1;
|
180 |
c.gridheight = 1;
|
181 |
} |
182 |
double weighty = jfield.getResizeWeight();
|
183 |
if( weighty>0 ) { |
184 |
c.fill = GridBagConstraints.BOTH;
|
185 |
c.weightx = 1;
|
186 |
c.weighty = weighty; |
187 |
fieldsPanel.add(jfield.asJComponent(), c); |
188 |
} else {
|
189 |
c.fill = GridBagConstraints.HORIZONTAL;
|
190 |
c.weightx = 1;
|
191 |
c.weighty = 0;
|
192 |
fieldsPanel.add(jfield.asJComponent(), c); |
193 |
} |
194 |
this.components.put(jfield.getName(), jfield);
|
195 |
gridy++; |
196 |
|
197 |
} |
198 |
// Con esto forzamos a alinear los componentes arriba.
|
199 |
c.gridx = 0;
|
200 |
c.gridy = gridy; |
201 |
c.gridwidth = 1;
|
202 |
c.gridheight = 1;
|
203 |
c.fill = GridBagConstraints.VERTICAL;
|
204 |
c.weightx = 0;
|
205 |
c.weighty = 0.001;
|
206 |
fieldsPanel.add(new JLabel(), c); |
207 |
return fieldsPanel;
|
208 |
} |
209 |
|
210 |
private JComponent getFieldsContainerUseSeparators() throws ServiceException { |
211 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
212 |
List<String> groups = this.getDefinition().getGroups(); |
213 |
GridBagConstraints c = new GridBagConstraints(); |
214 |
c.insets = new Insets(2, 2, 2, 2); |
215 |
c.anchor = GridBagConstraints.PAGE_START;
|
216 |
JPanel fieldsPanel = new JPanel(); |
217 |
fieldsPanel.setLayout(new GridBagLayout()); |
218 |
int gridy = 0; |
219 |
for (String group : groups) { |
220 |
boolean firstField = true; |
221 |
List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(group); |
222 |
for (DynFormFieldDefinition fieldDefinition : fields) {
|
223 |
if (fieldDefinition.isHidden()) {
|
224 |
continue;
|
225 |
} |
226 |
JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
227 |
this.getContext(),
|
228 |
fieldDefinition |
229 |
); |
230 |
JDynFormField jfield = factory.create( |
231 |
getServiceManager(), |
232 |
this.componentsFactory,
|
233 |
fieldDefinition, |
234 |
null
|
235 |
); |
236 |
if (jfield instanceof AbstractJDynFormField) { |
237 |
((AbstractJDynFormField) jfield).setForm(this);
|
238 |
} |
239 |
jfield.addListener(this);
|
240 |
if (this.isReadOnly()) { |
241 |
jfield.setReadOnly(this.isReadOnly());
|
242 |
} |
243 |
|
244 |
this.configurePopupMenu(jfield);
|
245 |
if( firstField ) {
|
246 |
firstField = false;
|
247 |
c.gridx = 0;
|
248 |
c.gridy = gridy; |
249 |
c.gridwidth = 2;
|
250 |
c.gridheight = 1;
|
251 |
c.fill = GridBagConstraints.HORIZONTAL;
|
252 |
c.weightx = 1;
|
253 |
c.weighty = 0;
|
254 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(group), c); |
255 |
gridy++; |
256 |
} |
257 |
String sep = jfield.getSeparatorTitleToUseBefore();
|
258 |
if( !StringUtils.isBlank(sep) ) {
|
259 |
c.gridx = 0;
|
260 |
c.gridy = gridy; |
261 |
c.gridwidth = 2;
|
262 |
c.gridheight = 1;
|
263 |
c.fill = GridBagConstraints.HORIZONTAL;
|
264 |
c.weightx = 1;
|
265 |
c.weighty = 0;
|
266 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(sep), c); |
267 |
gridy++; |
268 |
} |
269 |
if( jfield.useEmptyLabel() ) {
|
270 |
c.gridx = 0;
|
271 |
c.gridy = gridy; |
272 |
c.gridwidth = 2;
|
273 |
c.gridheight = 1;
|
274 |
} else {
|
275 |
c.gridx = 0;
|
276 |
c.gridy = gridy; |
277 |
c.gridwidth = 1;
|
278 |
c.gridheight = 1;
|
279 |
c.fill = GridBagConstraints.HORIZONTAL;
|
280 |
c.weightx = 0;
|
281 |
c.weighty = 0;
|
282 |
fieldsPanel.add(jfield.getJLabel(), c); |
283 |
c.gridx = 1;
|
284 |
c.gridy = gridy; |
285 |
c.gridwidth = 1;
|
286 |
c.gridheight = 1;
|
287 |
} |
288 |
double weighty = jfield.getResizeWeight();
|
289 |
if( weighty>0 ) { |
290 |
c.fill = GridBagConstraints.BOTH;
|
291 |
c.weightx = 1;
|
292 |
c.weighty = weighty; |
293 |
fieldsPanel.add(jfield.asJComponent(), c); |
294 |
} else {
|
295 |
c.fill = GridBagConstraints.HORIZONTAL;
|
296 |
c.weightx = 1;
|
297 |
c.weighty = 0;
|
298 |
fieldsPanel.add(jfield.asJComponent(), c); |
299 |
} |
300 |
this.components.put(jfield.getName(), jfield);
|
301 |
gridy++; |
302 |
} |
303 |
} |
304 |
// Con esto forzamos a alinear los componentes arriba.
|
305 |
c.gridx = 0;
|
306 |
c.gridy = gridy; |
307 |
c.gridwidth = 1;
|
308 |
c.gridheight = 1;
|
309 |
c.fill = GridBagConstraints.VERTICAL;
|
310 |
c.weightx = 0;
|
311 |
c.weighty = 0.001;
|
312 |
fieldsPanel.add(new JLabel(), c); |
313 |
return fieldsPanel;
|
314 |
} |
315 |
|
316 |
private JComponent getFieldsContainerUseTabs() throws ServiceException { |
317 |
|
318 |
JTabbedPane tabbedPane = new JTabbedPane(); |
319 |
if (this.getDefinition().getTags().has("TabPlacement")) { |
320 |
try {
|
321 |
tabbedPane.setTabPlacement(this.getDefinition().getTags().getInt("TabPlacement")); |
322 |
} catch (Exception e) { |
323 |
// Ignorada
|
324 |
} |
325 |
} |
326 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
327 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
328 |
List<String> groups = this.getDefinition().getGroups(); |
329 |
GridBagConstraints c = new GridBagConstraints(); |
330 |
c.insets = new Insets(2, 2, 2, 2); |
331 |
c.anchor = GridBagConstraints.PAGE_START;
|
332 |
for (String group : groups) { |
333 |
JPanel fieldsPanel = new JPanel(); |
334 |
GridBagLayout gridbag = new GridBagLayout(); |
335 |
fieldsPanel.setLayout(gridbag); |
336 |
List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(group); |
337 |
int gridy = 0; |
338 |
for (DynFormFieldDefinition fieldDefinition : fields) {
|
339 |
if (fieldDefinition.isHidden()) {
|
340 |
continue;
|
341 |
} |
342 |
JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
343 |
this.getContext(),
|
344 |
fieldDefinition |
345 |
); |
346 |
JDynFormField jfield = factory.create( |
347 |
getServiceManager(), |
348 |
this.componentsFactory,
|
349 |
fieldDefinition, |
350 |
null
|
351 |
); |
352 |
if (jfield instanceof AbstractJDynFormField) { |
353 |
((AbstractJDynFormField) jfield).setForm(this);
|
354 |
} |
355 |
jfield.setReadOnly(this.isReadOnly());
|
356 |
jfield.addListener(this);
|
357 |
if (this.isReadOnly()) { |
358 |
jfield.setReadOnly(this.isReadOnly());
|
359 |
} |
360 |
this.configurePopupMenu(jfield);
|
361 |
String sep = jfield.getSeparatorTitleToUseBefore();
|
362 |
if( !StringUtils.isBlank(sep) ) {
|
363 |
c.gridx = 0;
|
364 |
c.gridy = gridy; |
365 |
c.gridwidth = 2;
|
366 |
c.gridheight = 1;
|
367 |
c.fill = GridBagConstraints.HORIZONTAL;
|
368 |
c.weightx = 1;
|
369 |
c.weighty = 0;
|
370 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(sep), c); |
371 |
gridy++; |
372 |
} |
373 |
if( jfield.useEmptyLabel() ) {
|
374 |
c.gridx = 0;
|
375 |
c.gridy = gridy; |
376 |
c.gridwidth = 2;
|
377 |
c.gridheight = 1;
|
378 |
} else {
|
379 |
c.gridx = 0;
|
380 |
c.gridy = gridy; |
381 |
c.gridwidth = 1;
|
382 |
c.gridheight = 1;
|
383 |
c.fill = GridBagConstraints.HORIZONTAL;
|
384 |
c.weightx = 0;
|
385 |
c.weighty = 0;
|
386 |
jfield.getJLabel().setAlignmentY(Component.TOP_ALIGNMENT);
|
387 |
fieldsPanel.add(jfield.getJLabel(), c); |
388 |
c.gridx = 1;
|
389 |
c.gridy = gridy; |
390 |
c.gridwidth = 1;
|
391 |
c.gridheight = 1;
|
392 |
} |
393 |
double weighty = jfield.getResizeWeight();
|
394 |
if( weighty>0 ) { |
395 |
c.fill = GridBagConstraints.BOTH;
|
396 |
c.weightx = 1;
|
397 |
c.weighty = weighty; |
398 |
fieldsPanel.add(jfield.asJComponent(), c); |
399 |
} else {
|
400 |
c.fill = GridBagConstraints.HORIZONTAL;
|
401 |
c.weightx = 1;
|
402 |
c.weighty = 0;
|
403 |
fieldsPanel.add(jfield.asJComponent(), c); |
404 |
} |
405 |
this.components.put(jfield.getName(), jfield);
|
406 |
gridy++; |
407 |
} |
408 |
if( gridy>0 ) { |
409 |
String tablabel = group;
|
410 |
if (StringUtils.isEmpty(tablabel)) {
|
411 |
tablabel = ToolsLocator.getI18nManager().getTranslation("General");
|
412 |
} |
413 |
tablabel = i18n.getTranslation(tablabel); |
414 |
|
415 |
// Con esto forzamos a alinear los componentes arriba.
|
416 |
c.gridx = 0;
|
417 |
c.gridy = gridy; |
418 |
c.gridwidth = 1;
|
419 |
c.gridheight = 1;
|
420 |
c.fill = GridBagConstraints.VERTICAL;
|
421 |
c.weightx = 0;
|
422 |
c.weighty = 0.001;
|
423 |
fieldsPanel.add(new JLabel(), c); |
424 |
|
425 |
tabbedPane.addTab(tablabel,fieldsPanel); |
426 |
} |
427 |
} |
428 |
|
429 |
try {
|
430 |
String defaultGroupName = (String) this.getDefinition().getTags().get("defaultGroup", DataTypes.STRING); |
431 |
List<String> groupsNames = this.getDefinition().getGroups(); |
432 |
int groupIndex = groupsNames.indexOf(defaultGroupName);
|
433 |
if (groupIndex >= 0) { |
434 |
tabbedPane.setSelectedIndex(groupIndex); |
435 |
} |
436 |
} catch (Exception ex) { |
437 |
// Ignore errors and don't set a tab by default
|
438 |
} |
439 |
|
440 |
return tabbedPane;
|
441 |
} |
442 |
|
443 |
@Override
|
444 |
public void setReadOnly(boolean readOnly) { |
445 |
if( this.isReadOnly() == readOnly ) { |
446 |
return;
|
447 |
} |
448 |
super.setReadOnly(readOnly);
|
449 |
if (this.isContentsInitialized()) { |
450 |
Iterator it = this.getFieldsIterator(); |
451 |
while (it.hasNext()) {
|
452 |
JDynFormField field = (JDynFormField) it.next(); |
453 |
if( readOnly ) {
|
454 |
field.setReadOnly(true);
|
455 |
} else if( field.getDefinition().isReadOnly() ) { |
456 |
field.setReadOnly(true);
|
457 |
} else {
|
458 |
field.setReadOnly(false);
|
459 |
} |
460 |
} |
461 |
} |
462 |
} |
463 |
|
464 |
@Override
|
465 |
public void setValues(DynObject values) { |
466 |
/*
|
467 |
* TODO: Probablemente fuese mas acertado recorrer los controles de
|
468 |
* components y a partir de ellos acceder a los valores del DynObject
|
469 |
* recibido. Eso permitiria trabajar mejor con herencia de DynObject.
|
470 |
* Habria que hacer lo mismo con el getValues.
|
471 |
*/
|
472 |
if (!this.isContentsInitialized()) { |
473 |
this.values = values;
|
474 |
return;
|
475 |
} |
476 |
DynClass def = values.getDynClass(); |
477 |
DynField[] fields = def.getDynFields();
|
478 |
for (int i = 0; i < fields.length; i++) { |
479 |
DynField field = fields[i]; |
480 |
if( field.isHidden() ) {
|
481 |
continue;
|
482 |
} |
483 |
String name = field.getName();
|
484 |
if (name == null || name.isEmpty()) { |
485 |
LOGGER.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty "); |
486 |
continue;
|
487 |
} |
488 |
JDynFormField jfield = (JDynFormField) this.getField(name);
|
489 |
if (jfield == null) { |
490 |
LOGGER.warn("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'."); |
491 |
continue;
|
492 |
} |
493 |
if (values.getDynValue(name) == null) { |
494 |
if (fields[i].getDataType().getType() == DataTypes.LIST) {
|
495 |
try {
|
496 |
if (((DynField_v2) fields[i]).getDynClassOfItems() != null) { |
497 |
values.setDynValue(name, new ArrayList<>()); |
498 |
} |
499 |
} catch (Exception e) { |
500 |
LOGGER.warn("Problems initializing the DynObject List", e);
|
501 |
} |
502 |
} |
503 |
} |
504 |
jfield.setValue(values.getDynValue(name)); |
505 |
} |
506 |
this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values); |
507 |
try {
|
508 |
if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this) ) { |
509 |
this.setReadOnly(true); |
510 |
} |
511 |
} catch (Exception ex) { |
512 |
} |
513 |
} |
514 |
|
515 |
@Override
|
516 |
public void getValues(DynObject values) { |
517 |
if (values == null) { |
518 |
return;
|
519 |
} |
520 |
DynField[] fields = values.getDynClass().getDynFields();
|
521 |
for (DynField field : fields) {
|
522 |
String name = field.getName();
|
523 |
JDynFormField jfield = (JDynFormField) this.getField(name);
|
524 |
if (jfield != null && !jfield.isReadOnly()) { |
525 |
try {
|
526 |
jfield.fetch(values); |
527 |
} catch (Exception ex) { |
528 |
LOGGER.warn("Can't get value of field '" + name + "'.", ex); |
529 |
} |
530 |
} |
531 |
} |
532 |
} |
533 |
|
534 |
@Override
|
535 |
public boolean hasValidValues() { |
536 |
Iterator it = this.getFieldsIterator(); |
537 |
while (it.hasNext()) {
|
538 |
JDynFormField jfield = (JDynFormField) it.next(); |
539 |
if (!jfield.hasValidValue()) {
|
540 |
return false; |
541 |
} |
542 |
} |
543 |
try {
|
544 |
return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this); |
545 |
} catch (Exception ex) { |
546 |
} |
547 |
return true; |
548 |
} |
549 |
|
550 |
@Override
|
551 |
public boolean hasValidValues(List<String> fieldsName) { |
552 |
if (fieldsName == null) { |
553 |
fieldsName = new ArrayList<>(); |
554 |
} |
555 |
Iterator it = this.getFieldsIterator(); |
556 |
while (it.hasNext()) {
|
557 |
JDynFormField jfield = (JDynFormField) it.next(); |
558 |
if (!jfield.hasValidValue()) {
|
559 |
fieldsName.add(jfield.getName()); |
560 |
} |
561 |
} |
562 |
return fieldsName.isEmpty();
|
563 |
} |
564 |
|
565 |
@Override
|
566 |
public Object getValue(String fieldName) { |
567 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
568 |
return field.getValue();
|
569 |
} |
570 |
|
571 |
@Override
|
572 |
public void setValue(String fieldName, Object value) { |
573 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
574 |
try {
|
575 |
value = field.getDefinition().getDataType().coerce(value); |
576 |
} catch (CoercionException e) {
|
577 |
String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'."; |
578 |
LOGGER.warn(msg, e); |
579 |
throw new RuntimeException(msg, e); |
580 |
} |
581 |
field.setValue(value); |
582 |
} |
583 |
|
584 |
@Override
|
585 |
public boolean isModified() { |
586 |
if( this.isReadOnly() ) { |
587 |
return false; |
588 |
} |
589 |
try {
|
590 |
if (this.getScript() !=null) { |
591 |
return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values); |
592 |
} |
593 |
} catch (NoSuchMethodException ex) { |
594 |
} catch (Exception ex) { |
595 |
LOGGER.warn("Error calling user function form_IsModified.",ex);
|
596 |
} |
597 |
Iterator it = this.getFieldsIterator(); |
598 |
while (it.hasNext()) {
|
599 |
JDynFormField jfield = (JDynFormField) it.next(); |
600 |
if (jfield.isModified()) {
|
601 |
return true; |
602 |
} |
603 |
} |
604 |
return false; |
605 |
} |
606 |
|
607 |
@Override
|
608 |
public void fieldEnter(JDynFormField field) { |
609 |
message(field.getDefinition().getDescription()); |
610 |
this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field); |
611 |
} |
612 |
|
613 |
@Override
|
614 |
public void fieldExit(JDynFormField field) { |
615 |
message(); |
616 |
this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field); |
617 |
} |
618 |
|
619 |
@Override
|
620 |
public void message(JDynFormField field, String message) { |
621 |
message(message); |
622 |
} |
623 |
|
624 |
@Override
|
625 |
public void fieldChanged(JDynFormField field) { |
626 |
fireFieldChangeEvent(field); |
627 |
this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field); |
628 |
} |
629 |
|
630 |
@Override
|
631 |
public JDynFormField getField(String fieldName) { |
632 |
JDynFormField field = (JDynFormField) this.components.get(fieldName);
|
633 |
return field;
|
634 |
} |
635 |
|
636 |
public Iterator getFieldsIterator() { |
637 |
if (!this.isContentsInitialized()) { |
638 |
this.initComponents();
|
639 |
} |
640 |
return this.components.values().iterator(); |
641 |
} |
642 |
|
643 |
public Collection getShowFields() { |
644 |
return this.components.values(); |
645 |
} |
646 |
|
647 |
@Override
|
648 |
public void clear() { |
649 |
Iterator it = this.components.entrySet().iterator(); |
650 |
while (it.hasNext()) {
|
651 |
Map.Entry entry = (Map.Entry) it.next(); |
652 |
((JDynFormField) (entry.getValue())).clear(); |
653 |
} |
654 |
this.callUserEvent(USERCODE_FORM_CLEAR, this); |
655 |
} |
656 |
|
657 |
@Override
|
658 |
public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
659 |
super.addActionToPopupMenu(tipo, name, action);
|
660 |
if ( this.isContentsInitialized() ) { |
661 |
Iterator it = this.components.keySet().iterator(); |
662 |
while (it.hasNext()) {
|
663 |
String key = (String) it.next(); |
664 |
Object obj = this.components.get(key); |
665 |
if (obj instanceof JDynFormField) { |
666 |
JDynFormField field = (JDynFormField) obj; |
667 |
if (tipo == field.getDefinition().getDataType()) {
|
668 |
if (field.asJComponent() instanceof SupportPopupMenu) { |
669 |
field.addActionToPopupMenu(name, action); |
670 |
} |
671 |
} |
672 |
} |
673 |
} |
674 |
} |
675 |
} |
676 |
|
677 |
@Override
|
678 |
public void addSeparatorToPopupMenu(DataType tipo) { |
679 |
super.addSeparatorToPopupMenu(tipo);
|
680 |
if ( this.isContentsInitialized() ) { |
681 |
Iterator it = this.components.keySet().iterator(); |
682 |
while (it.hasNext()) {
|
683 |
String key = (String) it.next(); |
684 |
Object obj = this.components.get(key); |
685 |
if (obj instanceof JDynFormField) { |
686 |
JDynFormField field = (JDynFormField) obj; |
687 |
if (tipo == field.getDefinition().getDataType()) {
|
688 |
if (field.asJComponent() instanceof SupportPopupMenu) { |
689 |
((SupportPopupMenu) field.asJComponent()).addSeparatorToPopupMenu(); |
690 |
} |
691 |
} |
692 |
} |
693 |
} |
694 |
} |
695 |
} |
696 |
|
697 |
@Override
|
698 |
public void setBorder(boolean border) { |
699 |
super.setBorder(border && this.getShowFields().size() >1); |
700 |
} |
701 |
|
702 |
} |