Statistics
| Revision:

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 @ 2035

History | View | Annotate | Download (27.1 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
            List<Action> customActions = getCustomActionsForDataType(fieldDefinition.getDataType());
150
            if (customActions != null && !customActions.isEmpty()) {
151
                for (Action customAction : customActions) {
152
                    if (customAction != null) {
153
                        jfield.addActionToPopupMenu((String) customAction.getValue(Action.NAME), customAction);
154
                    } else {
155
                        jfield.addSeparatorToPopupMenu();
156
                    }
157
                }
158
            }
159
            String sep = jfield.getSeparatorTitleToUseBefore();
160
            if( !StringUtils.isBlank(sep) ) {
161
                c.gridx = 0;
162
                c.gridy = gridy;
163
                c.gridwidth = 2;
164
                c.gridheight = 1;
165
                c.fill = GridBagConstraints.HORIZONTAL;
166
                c.weightx = 1;
167
                c.weighty = 0;
168
                fieldsPanel.add(toolsSwingManager.createTitledSeparator(sep), c);
169
                gridy++;
170
            }
171
            if( jfield.useEmptyLabel() ) {
172
                c.gridx = 0;
173
                c.gridy = gridy;
174
                c.gridwidth = 2;
175
                c.gridheight = 1;
176
            } else {
177
                c.gridx = 0;
178
                c.gridy = gridy;
179
                c.gridwidth = 1;
180
                c.gridheight = 1;
181
                c.fill = GridBagConstraints.HORIZONTAL;
182
                c.weightx = 0;
183
                c.weighty = 0;
184
                jfield.getJLabel().setAlignmentY(Component.TOP_ALIGNMENT);
185
                fieldsPanel.add(jfield.getJLabel(), c);
186
                c.gridx = 1;
187
                c.gridy = gridy;
188
                c.gridwidth = 1;
189
                c.gridheight = 1;
190
            }
191
            double weighty = jfield.getResizeWeight();
192
            if( weighty>0 ) {
193
                c.fill = GridBagConstraints.BOTH;
194
                c.weightx = 1;
195
                c.weighty = weighty;
196
                fieldsPanel.add(jfield.asJComponent(), c);
197
            } else {
198
                c.fill = GridBagConstraints.HORIZONTAL;
199
                c.weightx = 1;
200
                c.weighty = 0;
201
                fieldsPanel.add(jfield.asJComponent(), c);
202
            }
203
            this.components.put(jfield.getName(), jfield);
204
            gridy++;
205

    
206
        }
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
                List<Action> customActions = getCustomActionsForDataType(fieldDefinition.getDataType());
245
                if (customActions != null && !customActions.isEmpty()) {
246
                    for (Action customAction : customActions) {
247
                        if (customAction != null) {
248
                            jfield.addActionToPopupMenu((String) customAction.getValue(Action.NAME), customAction);
249
                        } else {
250
                            jfield.addSeparatorToPopupMenu();
251
                        }
252
                    }
253
                }
254
                if( firstField ) {
255
                    firstField = false;
256
                    c.gridx = 0;
257
                    c.gridy = gridy;
258
                    c.gridwidth = 2;
259
                    c.gridheight = 1;
260
                    c.fill = GridBagConstraints.HORIZONTAL;
261
                    c.weightx = 1;
262
                    c.weighty = 0;
263
                    fieldsPanel.add(toolsSwingManager.createTitledSeparator(group), c);
264
                    gridy++;
265
                }
266
                String sep = jfield.getSeparatorTitleToUseBefore();
267
                if( !StringUtils.isBlank(sep) ) {
268
                    c.gridx = 0;
269
                    c.gridy = gridy;
270
                    c.gridwidth = 2;
271
                    c.gridheight = 1;
272
                    c.fill = GridBagConstraints.HORIZONTAL;
273
                    c.weightx = 1;
274
                    c.weighty = 0;
275
                    fieldsPanel.add(toolsSwingManager.createTitledSeparator(sep), c);
276
                    gridy++;
277
                }
278
                if( jfield.useEmptyLabel() ) {
279
                    c.gridx = 0;
280
                    c.gridy = gridy;
281
                    c.gridwidth = 2;
282
                    c.gridheight = 1;
283
                } else {
284
                    c.gridx = 0;
285
                    c.gridy = gridy;
286
                    c.gridwidth = 1;
287
                    c.gridheight = 1;
288
                    c.fill = GridBagConstraints.HORIZONTAL;
289
                    c.weightx = 0;
290
                    c.weighty = 0;
291
                    fieldsPanel.add(jfield.getJLabel(), c);
292
                    c.gridx = 1;
293
                    c.gridy = gridy;
294
                    c.gridwidth = 1;
295
                    c.gridheight = 1;
296
                }
297
                double weighty = jfield.getResizeWeight();
298
                if( weighty>0 ) {
299
                    c.fill = GridBagConstraints.BOTH;
300
                    c.weightx = 1;
301
                    c.weighty = weighty;
302
                    fieldsPanel.add(jfield.asJComponent(), c);
303
                } else {
304
                    c.fill = GridBagConstraints.HORIZONTAL;
305
                    c.weightx = 1;
306
                    c.weighty = 0;
307
                    fieldsPanel.add(jfield.asJComponent(), c);
308
                }
309
                this.components.put(jfield.getName(), jfield);
310
                gridy++;
311
            }
312
        }
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
                List<Action> customActions = getCustomActionsForDataType(fieldDefinition.getDataType());
361
                if (customActions != null && !customActions.isEmpty()) {
362
                    for (Action customAction : customActions) {
363
                        if (customAction != null) {
364
                            jfield.addActionToPopupMenu((String) customAction.getValue(Action.NAME), customAction);
365
                        } else {
366
                            jfield.addSeparatorToPopupMenu();
367
                        }
368
                    }
369
                }
370
                String sep = jfield.getSeparatorTitleToUseBefore();
371
                if( !StringUtils.isBlank(sep) ) {
372
                    c.gridx = 0;
373
                    c.gridy = gridy;
374
                    c.gridwidth = 2;
375
                    c.gridheight = 1;
376
                    c.fill = GridBagConstraints.HORIZONTAL;
377
                    c.weightx = 1;
378
                    c.weighty = 0;
379
                    fieldsPanel.add(toolsSwingManager.createTitledSeparator(sep), c);
380
                    gridy++;
381
                }                
382
                if( jfield.useEmptyLabel() ) {
383
                    c.gridx = 0;
384
                    c.gridy = gridy;
385
                    c.gridwidth = 2;
386
                    c.gridheight = 1;
387
                } else {
388
                    c.gridx = 0;
389
                    c.gridy = gridy;
390
                    c.gridwidth = 1;
391
                    c.gridheight = 1;
392
                    c.fill = GridBagConstraints.HORIZONTAL;
393
                    c.weightx = 0;
394
                    c.weighty = 0;
395
                    jfield.getJLabel().setAlignmentY(Component.TOP_ALIGNMENT);
396
                    fieldsPanel.add(jfield.getJLabel(), c);
397
                    c.gridx = 1;
398
                    c.gridy = gridy;
399
                    c.gridwidth = 1;
400
                    c.gridheight = 1;
401
                }
402
                double weighty = jfield.getResizeWeight();
403
                if( weighty>0 ) {
404
                    c.fill = GridBagConstraints.BOTH;
405
                    c.weightx = 1;
406
                    c.weighty = weighty;
407
                    fieldsPanel.add(jfield.asJComponent(), c);
408
                } else {
409
                    c.fill = GridBagConstraints.HORIZONTAL;
410
                    c.weightx = 1;
411
                    c.weighty = 0;
412
                    fieldsPanel.add(jfield.asJComponent(), c);
413
                }
414
                this.components.put(jfield.getName(), jfield);
415
                gridy++;
416
            }
417
            if( gridy>0 ) {
418
                String tablabel = group;
419
                if (StringUtils.isEmpty(tablabel)) {
420
                    tablabel = ToolsLocator.getI18nManager().getTranslation("General");
421
                }
422
                tablabel = i18n.getTranslation(tablabel);
423
                
424
                // Con esto forzamos a alinear los componentes arriba.
425
                c.gridx = 0;
426
                c.gridy = gridy;
427
                c.gridwidth = 1;
428
                c.gridheight = 1;
429
                c.fill = GridBagConstraints.VERTICAL;
430
                c.weightx = 0;
431
                c.weighty = 0.001;
432
                fieldsPanel.add(new JLabel(), c);
433
                
434
                tabbedPane.addTab(tablabel,fieldsPanel);
435
            }
436
        }
437

    
438
        try {
439
            String defaultGroupName = (String) this.getDefinition().getTags().get("defaultGroup", DataTypes.STRING);
440
            List<String> groupsNames = this.getDefinition().getGroups();
441
            int groupIndex = groupsNames.indexOf(defaultGroupName);
442
            if (groupIndex >= 0) {
443
                tabbedPane.setSelectedIndex(groupIndex);
444
            }
445
        } catch (Exception ex) {
446
            // Ignore errors and don't set a tab by default
447
        }
448

    
449
        return tabbedPane;
450
    }
451

    
452
    @Override
453
    public void setReadOnly(boolean readOnly) {
454
        if( this.isReadOnly() == readOnly ) {
455
            return;
456
        }
457
        super.setReadOnly(readOnly);
458
        if (this.isContentsInitialized()) {
459
            Iterator it = this.getFieldsIterator();
460
            while (it.hasNext()) {
461
                JDynFormField field = (JDynFormField) it.next();
462
                if( readOnly ) {
463
                    field.setReadOnly(true);
464
                } else if( field.getDefinition().isReadOnly() ) {
465
                    field.setReadOnly(true);
466
                } else {
467
                    field.setReadOnly(false);
468
                }
469
            }
470
        }
471
    }
472

    
473
    @Override
474
    public void setValues(DynObject values) {
475
        /*
476
         * TODO: Probablemente fuese mas acertado recorrer los controles de
477
         * components y a partir de ellos acceder a los valores del DynObject
478
         * recibido. Eso permitiria trabajar mejor con herencia de DynObject.
479
         * Habria que hacer lo mismo con el getValues.
480
         */
481
        if (!this.isContentsInitialized()) {
482
            this.values = values;
483
            return;
484
        }
485
        DynClass def = values.getDynClass();
486
        DynField[] fields = def.getDynFields();
487
        for (int i = 0; i < fields.length; i++) {
488
            DynField field = fields[i];
489
            if( field.isHidden() ) {
490
                continue;
491
            }
492
            String name = field.getName();
493
            if (name == null || name.isEmpty()) {
494
                LOGGER.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty ");
495
                continue;
496
            }
497
            JDynFormField jfield = (JDynFormField) this.getField(name);
498
            if (jfield == null) {
499
                LOGGER.warn("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'.");
500
                continue;
501
            }
502
            if (values.getDynValue(name) == null) {
503
                if (fields[i].getDataType().getType() == DataTypes.LIST) {
504
                    try {
505
                        if (((DynField_v2) fields[i]).getDynClassOfItems() != null) {
506
                            values.setDynValue(name, new ArrayList<>());
507
                        }
508
                    } catch (Exception e) {
509
                        LOGGER.warn("Problems initializing the DynObject List", e);
510
                    }
511
                }
512
            }
513
            jfield.setValue(values.getDynValue(name));
514
        }
515
        this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values);
516
        try {
517
            if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this) ) {
518
                this.setReadOnly(true);
519
            }
520
        } catch (Exception ex) {
521
        }
522
    }
523

    
524
    @Override
525
    public void getValues(DynObject values) {
526
        if (values == null) {
527
            return;
528
        }
529
        DynField[] fields = values.getDynClass().getDynFields();
530
        for (DynField field : fields) {
531
            String name = field.getName();
532
            JDynFormField jfield = (JDynFormField) this.getField(name);
533
            if (jfield != null && !jfield.isReadOnly()) {
534
                try {
535
                    jfield.fetch(values);
536
                } catch (Exception ex) {
537
                    LOGGER.warn("Can't get value of field '" + name + "'.", ex);
538
                }
539
            }
540
        }
541
    }
542

    
543
    @Override
544
    public boolean hasValidValues() {
545
        Iterator it = this.getFieldsIterator();
546
        while (it.hasNext()) {
547
            JDynFormField jfield = (JDynFormField) it.next();
548
            if (!jfield.hasValidValue()) {
549
                return false;
550
            }
551
        }
552
        try {
553
            return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this);
554
        } catch (Exception ex) {
555
        }
556
        return true;
557
    }
558

    
559
    @Override
560
    public boolean hasValidValues(List<String> fieldsName) {
561
        if (fieldsName == null) {
562
            fieldsName = new ArrayList<>();
563
        }
564
        Iterator it = this.getFieldsIterator();
565
        while (it.hasNext()) {
566
            JDynFormField jfield = (JDynFormField) it.next();
567
            if (!jfield.hasValidValue()) {
568
                fieldsName.add(jfield.getName());
569
            }
570
        }
571
        return fieldsName.isEmpty();
572
    }
573

    
574
    @Override
575
    public Object getValue(String fieldName) {
576
        JDynFormField field = (JDynFormField) this.getField(fieldName);
577
        return field.getValue();
578
    }
579

    
580
    @Override
581
    public void setValue(String fieldName, Object value) {
582
        JDynFormField field = (JDynFormField) this.getField(fieldName);
583
        try {
584
            value = field.getDefinition().getDataType().coerce(value);
585
        } catch (CoercionException e) {
586
            String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'.";
587
            LOGGER.warn(msg, e);
588
            throw new RuntimeException(msg, e);
589
        }
590
        field.setValue(value);
591
    }
592

    
593
    @Override
594
    public boolean isModified() {
595
        if( this.isReadOnly() ) {
596
            return false;
597
        }
598
        try {
599
            return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values);
600
        } catch (NoSuchMethodException ex) {
601
        } catch (Exception ex) {
602
            LOGGER.warn("Error calling user function form_IsModified.",ex);
603
        }
604
        Iterator it = this.getFieldsIterator();
605
        while (it.hasNext()) {
606
            JDynFormField jfield = (JDynFormField) it.next();
607
            if (jfield.isModified()) {
608
                return true;
609
            }
610
        }
611
        return false;
612
    }
613

    
614
    @Override
615
    public void fieldEnter(JDynFormField field) {
616
        message(field.getDefinition().getDescription());
617
        this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field);
618
    }
619

    
620
    @Override
621
    public void fieldExit(JDynFormField field) {
622
        message();
623
        this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field);
624
    }
625

    
626
    @Override
627
    public void message(JDynFormField field, String message) {
628
        message(message);
629
    }
630

    
631
    @Override
632
    public void fieldChanged(JDynFormField field) {
633
        fireFieldChangeEvent(field);
634
        this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field);
635
    }
636

    
637
    @Override
638
    public JDynFormField getField(String fieldName) {
639
        JDynFormField field = (JDynFormField) this.components.get(fieldName);
640
        return field;
641
    }
642

    
643
    public Iterator getFieldsIterator() {
644
        if (!this.isContentsInitialized()) {
645
            this.initComponents();
646
        }
647
        return this.components.values().iterator();
648
    }
649

    
650
    public Collection getShowFields() {
651
        return this.components.values();
652
    }
653

    
654
    @Override
655
    public void clear() {
656
        Iterator it = this.components.entrySet().iterator();
657
        while (it.hasNext()) {
658
            Map.Entry entry = (Map.Entry) it.next();
659
            ((JDynFormField) (entry.getValue())).clear();
660
        }
661
        this.callUserEvent(USERCODE_FORM_CLEAR, this);
662
    }
663

    
664
    @Override
665
    public void addActionToPopupMenu(DataType tipo, String name, Action action) {
666
        super.addActionToPopupMenu(tipo, name, action);
667
        if ( this.isContentsInitialized() ) {
668
            Iterator it = this.components.keySet().iterator();
669
            while (it.hasNext()) {
670
                String key = (String) it.next();
671
                Object obj = this.components.get(key);
672
                if (obj instanceof JDynFormField) {
673
                    JDynFormField field = (JDynFormField) obj;
674
                    if (tipo == field.getDefinition().getDataType()) {
675
                        if (field.asJComponent() instanceof SupportPopupMenu) {
676
                            field.addActionToPopupMenu(name, action);
677
                        }
678
                    }
679
                }
680
            }
681
        }
682
    }
683

    
684
    @Override
685
    public void addSeparatorToPopupMenu(DataType tipo) {
686
        super.addSeparatorToPopupMenu(tipo);
687
        if ( this.isContentsInitialized() ) {
688
            Iterator it = this.components.keySet().iterator();
689
            while (it.hasNext()) {
690
                String key = (String) it.next();
691
                Object obj = this.components.get(key);
692
                if (obj instanceof JDynFormField) {
693
                    JDynFormField field = (JDynFormField) obj;
694
                    if (tipo == field.getDefinition().getDataType()) {
695
                        if (field.asJComponent() instanceof SupportPopupMenu) {
696
                            ((SupportPopupMenu) field.asJComponent()).addSeparatorToPopupMenu();
697
                        }
698
                    }
699
                }
700
            }
701
        }
702
    }
703

    
704
    @Override
705
    public void setBorder(boolean border) {
706
        super.setBorder(border && this.getShowFields().size() >1);
707
    }
708

    
709
}