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 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 2035 jjdelcerro
import java.awt.Component;
26 1881 jjdelcerro
import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm;
27 919 jjdelcerro
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 1080 jbadia
import org.apache.commons.lang3.StringUtils;
38
import org.gvsig.tools.ToolsLocator;
39 931 jjdelcerro
import org.gvsig.tools.dataTypes.CoercionException;
40 919 jjdelcerro
import org.gvsig.tools.dataTypes.DataType;
41 1080 jbadia
import org.gvsig.tools.dataTypes.DataTypes;
42 933 jjdelcerro
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 967 jbadia
import org.gvsig.tools.dynform.spi.dynformfield.SupportPopupMenu;
47 937 jjdelcerro
import org.gvsig.tools.dynobject.DynClass;
48 919 jjdelcerro
import org.gvsig.tools.dynobject.DynField;
49 1080 jbadia
import org.gvsig.tools.dynobject.DynField_v2;
50 919 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
51
import org.gvsig.tools.service.ServiceException;
52
53 2035 jjdelcerro
import java.awt.GridBagConstraints;
54
import java.awt.GridBagLayout;
55
import java.awt.Insets;
56 1145 jjdelcerro
import java.util.Collection;
57 2035 jjdelcerro
import javax.swing.JLabel;
58
import javax.swing.JPanel;
59 1881 jjdelcerro
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
60
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
61 1947 jjdelcerro
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 1881 jjdelcerro
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory;
71
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
72 1149 jjdelcerro
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
73 1946 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
74 2035 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
75
import org.gvsig.tools.swing.api.ToolsSwingManager;
76 919 jjdelcerro
77 1881 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
78 1361 jjdelcerro
public class DefaultJDynForm extends AbstractJDynForm implements JDynFormFieldListener {
79 919 jjdelcerro
80 1145 jjdelcerro
    private Map components = null;
81 1983 jjdelcerro
    private final ComponentsFactory componentsFactory;
82 919 jjdelcerro
83 1735 jjdelcerro
    public DefaultJDynForm(
84 1881 jjdelcerro
            DynFormSPIManager manager,
85
            JDynFormFactory factory,
86 1735 jjdelcerro
            DynFormDefinition definition,
87
            DynFormContext context
88 1881 jjdelcerro
        ) {
89
        super(manager, factory, definition, context);
90 1145 jjdelcerro
        this.components = new HashMap();
91 1881 jjdelcerro
        this.componentsFactory = manager.createDefaultComponentsFactory();
92 1145 jjdelcerro
    }
93 919 jjdelcerro
94 1361 jjdelcerro
    @Override
95
    protected JComponent getFieldsContainer() {
96 1368 fdiaz
97 1361 jjdelcerro
        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 1145 jjdelcerro
            }
112 1947 jjdelcerro
            this.callUserEvent(USERCODE_FORM_ONLOAD, this);
113 1361 jjdelcerro
            return component;
114
        } catch (ServiceException ex) {
115
            throw new RuntimeException(ex);
116 1145 jjdelcerro
        }
117
    }
118
119 1361 jjdelcerro
    private JComponent getFieldsContainerPlain() throws ServiceException {
120 2035 jjdelcerro
        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 1145 jjdelcerro
            if (fieldDefinition.isHidden()) {
130
                continue;
131
            }
132 1886 jjdelcerro
            JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory(
133
                    this.getContext(),
134
                    fieldDefinition
135
            );
136 1881 jjdelcerro
            JDynFormField jfield = factory.create(
137
                    getServiceManager(),
138
                    this.componentsFactory,
139
                    fieldDefinition,
140
                    null
141
            );
142 1361 jjdelcerro
            if (jfield instanceof AbstractJDynFormField) {
143
                ((AbstractJDynFormField) jfield).setForm(this);
144 1149 jjdelcerro
            }
145 2035 jjdelcerro
            jfield.addListener(this);
146 1361 jjdelcerro
            if (this.isReadOnly()) {
147 2035 jjdelcerro
                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 1862 jjdelcerro
            } else {
177 2035 jjdelcerro
                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 1145 jjdelcerro
            }
191 2035 jjdelcerro
            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 2031 jjdelcerro
            } else {
198 2035 jjdelcerro
                c.fill = GridBagConstraints.HORIZONTAL;
199
                c.weightx = 1;
200
                c.weighty = 0;
201
                fieldsPanel.add(jfield.asJComponent(), c);
202 2031 jjdelcerro
            }
203 1145 jjdelcerro
            this.components.put(jfield.getName(), jfield);
204 2035 jjdelcerro
            gridy++;
205
206 1145 jjdelcerro
        }
207 2035 jjdelcerro
        return fieldsPanel;
208 1145 jjdelcerro
    }
209
210 1361 jjdelcerro
    private JComponent getFieldsContainerUseSeparators() throws ServiceException {
211 2035 jjdelcerro
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
212 1361 jjdelcerro
        List<String> groups = this.getDefinition().getGroups();
213 2035 jjdelcerro
        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 1361 jjdelcerro
        for (String group : groups) {
220 2035 jjdelcerro
            boolean firstField = true;
221
            List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(group);
222
            for (DynFormFieldDefinition fieldDefinition : fields) {
223 1145 jjdelcerro
                if (fieldDefinition.isHidden()) {
224
                    continue;
225
                }
226 1886 jjdelcerro
                JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory(
227
                        this.getContext(),
228
                        fieldDefinition
229
                );
230 1881 jjdelcerro
                JDynFormField jfield = factory.create(
231
                        getServiceManager(),
232
                        this.componentsFactory,
233
                        fieldDefinition,
234
                        null
235
                );
236 1361 jjdelcerro
                if (jfield instanceof AbstractJDynFormField) {
237
                    ((AbstractJDynFormField) jfield).setForm(this);
238 1149 jjdelcerro
                }
239 1145 jjdelcerro
                jfield.addListener(this);
240 1361 jjdelcerro
                if (this.isReadOnly()) {
241
                    jfield.setReadOnly(this.isReadOnly());
242 1145 jjdelcerro
                }
243
244 2035 jjdelcerro
                List<Action> customActions = getCustomActionsForDataType(fieldDefinition.getDataType());
245 1145 jjdelcerro
                if (customActions != null && !customActions.isEmpty()) {
246 2035 jjdelcerro
                    for (Action customAction : customActions) {
247
                        if (customAction != null) {
248
                            jfield.addActionToPopupMenu((String) customAction.getValue(Action.NAME), customAction);
249 1145 jjdelcerro
                        } else {
250
                            jfield.addSeparatorToPopupMenu();
251
                        }
252
                    }
253
                }
254 2035 jjdelcerro
                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 1509 jjdelcerro
                }
266 2035 jjdelcerro
                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 2031 jjdelcerro
                } else {
284 2035 jjdelcerro
                    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 2031 jjdelcerro
                }
297 2035 jjdelcerro
                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 1145 jjdelcerro
                this.components.put(jfield.getName(), jfield);
310 2035 jjdelcerro
                gridy++;
311 1145 jjdelcerro
            }
312
        }
313 2035 jjdelcerro
        return fieldsPanel;
314 1145 jjdelcerro
    }
315
316 1361 jjdelcerro
    private JComponent getFieldsContainerUseTabs() throws ServiceException {
317 1145 jjdelcerro
318 1033 jbadia
        JTabbedPane tabbedPane = new JTabbedPane();
319 1361 jjdelcerro
        if (this.getDefinition().getTags().has("TabPlacement")) {
320 1145 jjdelcerro
            try {
321 1361 jjdelcerro
                tabbedPane.setTabPlacement(this.getDefinition().getTags().getInt("TabPlacement"));
322 1145 jjdelcerro
            } catch (Exception e) {
323
                // Ignorada
324
            }
325
        }
326 2035 jjdelcerro
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
327 1946 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
328 1361 jjdelcerro
        List<String> groups = this.getDefinition().getGroups();
329 2035 jjdelcerro
        GridBagConstraints c = new GridBagConstraints();
330
        c.insets = new Insets(2, 2, 2, 2);
331
        c.anchor = GridBagConstraints.PAGE_START;
332 1361 jjdelcerro
        for (String group : groups) {
333 2035 jjdelcerro
            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 1145 jjdelcerro
                if (fieldDefinition.isHidden()) {
340
                    continue;
341 1022 jjdelcerro
                }
342 1886 jjdelcerro
                JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory(
343
                        this.getContext(),
344
                        fieldDefinition
345
                );
346 1881 jjdelcerro
                JDynFormField jfield = factory.create(
347
                        getServiceManager(),
348
                        this.componentsFactory,
349
                        fieldDefinition,
350
                        null
351
                );
352 1361 jjdelcerro
                if (jfield instanceof AbstractJDynFormField) {
353
                    ((AbstractJDynFormField) jfield).setForm(this);
354 1149 jjdelcerro
                }
355 1361 jjdelcerro
                jfield.setReadOnly(this.isReadOnly());
356 1145 jjdelcerro
                jfield.addListener(this);
357 1361 jjdelcerro
                if (this.isReadOnly()) {
358
                    jfield.setReadOnly(this.isReadOnly());
359 1145 jjdelcerro
                }
360 2035 jjdelcerro
                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 1947 jjdelcerro
                    }
369 2035 jjdelcerro
                }
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 1947 jjdelcerro
                }
382 2035 jjdelcerro
                if( jfield.useEmptyLabel() ) {
383
                    c.gridx = 0;
384
                    c.gridy = gridy;
385
                    c.gridwidth = 2;
386
                    c.gridheight = 1;
387 2031 jjdelcerro
                } else {
388 2035 jjdelcerro
                    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 2031 jjdelcerro
                }
402 2035 jjdelcerro
                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 1145 jjdelcerro
                this.components.put(jfield.getName(), jfield);
415 2035 jjdelcerro
                gridy++;
416 1145 jjdelcerro
            }
417 2035 jjdelcerro
            if( gridy>0 ) {
418 1509 jjdelcerro
                String tablabel = group;
419
                if (StringUtils.isEmpty(tablabel)) {
420
                    tablabel = ToolsLocator.getI18nManager().getTranslation("General");
421
                }
422 1946 jjdelcerro
                tablabel = i18n.getTranslation(tablabel);
423 2035 jjdelcerro
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 1145 jjdelcerro
            }
436
        }
437 1304 fdiaz
438 1154 jjdelcerro
        try {
439 1361 jjdelcerro
            String defaultGroupName = (String) this.getDefinition().getTags().get("defaultGroup", DataTypes.STRING);
440
            List<String> groupsNames = this.getDefinition().getGroups();
441 1154 jjdelcerro
            int groupIndex = groupsNames.indexOf(defaultGroupName);
442 1361 jjdelcerro
            if (groupIndex >= 0) {
443 1154 jjdelcerro
                tabbedPane.setSelectedIndex(groupIndex);
444
            }
445
        } catch (Exception ex) {
446
            // Ignore errors and don't set a tab by default
447
        }
448 1304 fdiaz
449 1361 jjdelcerro
        return tabbedPane;
450 1145 jjdelcerro
    }
451 919 jjdelcerro
452 1361 jjdelcerro
    @Override
453
    public void setReadOnly(boolean readOnly) {
454 1862 jjdelcerro
        if( this.isReadOnly() == readOnly ) {
455
            return;
456
        }
457 1361 jjdelcerro
        super.setReadOnly(readOnly);
458
        if (this.isContentsInitialized()) {
459
            Iterator it = this.getFieldsIterator();
460
            while (it.hasNext()) {
461
                JDynFormField field = (JDynFormField) it.next();
462 1852 jjdelcerro
                if( readOnly ) {
463
                    field.setReadOnly(true);
464
                } else if( field.getDefinition().isReadOnly() ) {
465
                    field.setReadOnly(true);
466
                } else {
467
                    field.setReadOnly(false);
468
                }
469 1361 jjdelcerro
            }
470 1145 jjdelcerro
        }
471
    }
472 919 jjdelcerro
473 1361 jjdelcerro
    @Override
474 1145 jjdelcerro
    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 1368 fdiaz
         * recibido. Eso permitiria trabajar mejor con herencia de DynObject.
479 1304 fdiaz
         * Habria que hacer lo mismo con el getValues.
480 1145 jjdelcerro
         */
481 1361 jjdelcerro
        if (!this.isContentsInitialized()) {
482 1145 jjdelcerro
            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 1947 jjdelcerro
            DynField field = fields[i];
489
            if( field.isHidden() ) {
490
                continue;
491
            }
492
            String name = field.getName();
493 1145 jjdelcerro
            if (name == null || name.isEmpty()) {
494 1881 jjdelcerro
                LOGGER.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty ");
495 1145 jjdelcerro
                continue;
496
            }
497
            JDynFormField jfield = (JDynFormField) this.getField(name);
498
            if (jfield == null) {
499 1881 jjdelcerro
                LOGGER.warn("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'.");
500 1145 jjdelcerro
                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 1862 jjdelcerro
                            values.setDynValue(name, new ArrayList<>());
507 1145 jjdelcerro
                        }
508
                    } catch (Exception e) {
509 1881 jjdelcerro
                        LOGGER.warn("Problems initializing the DynObject List", e);
510 1145 jjdelcerro
                    }
511
                }
512
            }
513
            jfield.setValue(values.getDynValue(name));
514
        }
515 1983 jjdelcerro
        this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values);
516 1948 jjdelcerro
        try {
517 1983 jjdelcerro
            if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this) ) {
518 1947 jjdelcerro
                this.setReadOnly(true);
519
            }
520 1948 jjdelcerro
        } catch (Exception ex) {
521 1947 jjdelcerro
        }
522 1145 jjdelcerro
    }
523 919 jjdelcerro
524 1361 jjdelcerro
    @Override
525 1145 jjdelcerro
    public void getValues(DynObject values) {
526
        if (values == null) {
527
            return;
528
        }
529
        DynField[] fields = values.getDynClass().getDynFields();
530 1361 jjdelcerro
        for (DynField field : fields) {
531
            String name = field.getName();
532 1145 jjdelcerro
            JDynFormField jfield = (JDynFormField) this.getField(name);
533 1852 jjdelcerro
            if (jfield != null && !jfield.isReadOnly()) {
534 1145 jjdelcerro
                try {
535 1405 jjdelcerro
                    jfield.fetch(values);
536 1145 jjdelcerro
                } catch (Exception ex) {
537 1881 jjdelcerro
                    LOGGER.warn("Can't get value of field '" + name + "'.", ex);
538 1145 jjdelcerro
                }
539
            }
540
        }
541
    }
542 919 jjdelcerro
543 1361 jjdelcerro
    @Override
544 1145 jjdelcerro
    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 1948 jjdelcerro
        try {
553
            return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this);
554
        } catch (Exception ex) {
555 1947 jjdelcerro
        }
556 1948 jjdelcerro
        return true;
557 1145 jjdelcerro
    }
558 919 jjdelcerro
559 1361 jjdelcerro
    @Override
560 1145 jjdelcerro
    public boolean hasValidValues(List<String> fieldsName) {
561
        if (fieldsName == null) {
562 1361 jjdelcerro
            fieldsName = new ArrayList<>();
563 1145 jjdelcerro
        }
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 1361 jjdelcerro
        return fieldsName.isEmpty();
572
    }
573 933 jjdelcerro
574 1361 jjdelcerro
    @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 1881 jjdelcerro
            LOGGER.warn(msg, e);
588 1361 jjdelcerro
            throw new RuntimeException(msg, e);
589 1145 jjdelcerro
        }
590 1361 jjdelcerro
        field.setValue(value);
591 1145 jjdelcerro
    }
592 933 jjdelcerro
593 1361 jjdelcerro
    @Override
594 1145 jjdelcerro
    public boolean isModified() {
595 1892 jjdelcerro
        if( this.isReadOnly() ) {
596
            return false;
597
        }
598 1948 jjdelcerro
        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 1947 jjdelcerro
        }
604 1145 jjdelcerro
        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 976 jbadia
614 1361 jjdelcerro
    @Override
615 1145 jjdelcerro
    public void fieldEnter(JDynFormField field) {
616
        message(field.getDefinition().getDescription());
617 1947 jjdelcerro
        this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field);
618 1145 jjdelcerro
    }
619 1031 jbadia
620 1361 jjdelcerro
    @Override
621 1145 jjdelcerro
    public void fieldExit(JDynFormField field) {
622
        message();
623 1947 jjdelcerro
        this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field);
624 1145 jjdelcerro
    }
625 1031 jbadia
626 1361 jjdelcerro
    @Override
627 1145 jjdelcerro
    public void message(JDynFormField field, String message) {
628
        message(message);
629
    }
630
631 1361 jjdelcerro
    @Override
632 1145 jjdelcerro
    public void fieldChanged(JDynFormField field) {
633
        fireFieldChangeEvent(field);
634 1947 jjdelcerro
        this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field);
635 1145 jjdelcerro
    }
636
637 1862 jjdelcerro
    @Override
638 1361 jjdelcerro
    public JDynFormField getField(String fieldName) {
639
        JDynFormField field = (JDynFormField) this.components.get(fieldName);
640
        return field;
641 1145 jjdelcerro
    }
642
643 1361 jjdelcerro
    public Iterator getFieldsIterator() {
644
        if (!this.isContentsInitialized()) {
645
            this.initComponents();
646 1145 jjdelcerro
        }
647 1361 jjdelcerro
        return this.components.values().iterator();
648 1145 jjdelcerro
    }
649
650 1361 jjdelcerro
    public Collection getShowFields() {
651
        return this.components.values();
652 1145 jjdelcerro
    }
653
654 1361 jjdelcerro
    @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 1145 jjdelcerro
        }
661 1947 jjdelcerro
        this.callUserEvent(USERCODE_FORM_CLEAR, this);
662 1145 jjdelcerro
    }
663
664 1361 jjdelcerro
    @Override
665 1145 jjdelcerro
    public void addActionToPopupMenu(DataType tipo, String name, Action action) {
666 1361 jjdelcerro
        super.addActionToPopupMenu(tipo, name, action);
667
        if ( this.isContentsInitialized() ) {
668 1145 jjdelcerro
            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 1361 jjdelcerro
                            field.addActionToPopupMenu(name, action);
677 1145 jjdelcerro
                        }
678
                    }
679
                }
680
            }
681
        }
682
    }
683
684 1361 jjdelcerro
    @Override
685 1145 jjdelcerro
    public void addSeparatorToPopupMenu(DataType tipo) {
686 1361 jjdelcerro
        super.addSeparatorToPopupMenu(tipo);
687
        if ( this.isContentsInitialized() ) {
688 1145 jjdelcerro
            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 1361 jjdelcerro
    @Override
705
    public void setBorder(boolean border) {
706
        super.setBorder(border && this.getShowFields().size() >1);
707 1145 jjdelcerro
    }
708
709 919 jjdelcerro
}