Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynform / AbeilleJDynForm.java @ 2039

History | View | Annotate | Download (20.6 KB)

1
package org.gvsig.tools.dynform.services.dynform;
2

    
3
import java.awt.Component;
4
import java.io.FileInputStream;
5
import java.io.InputStream;
6
import java.util.HashMap;
7
import java.util.Map;
8

    
9
import javax.swing.JCheckBox;
10
import javax.swing.JComboBox;
11
import javax.swing.JComponent;
12
import javax.swing.JSpinner;
13
import javax.swing.text.JTextComponent;
14

    
15
import org.gvsig.tools.dynform.DynFormDefinition;
16
import org.gvsig.tools.dynform.DynFormFieldDefinition;
17
import org.gvsig.tools.dynform.JDynFormField;
18
import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener;
19
import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm;
20
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
21

    
22
import com.jeta.forms.components.panel.FormPanel;
23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
25
import java.io.File;
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29
import javax.swing.JButton;
30
import javax.swing.JLabel;
31
import javax.swing.JList;
32
import javax.swing.JScrollPane;
33
import javax.swing.JTable;
34
import javax.swing.JTextArea;
35
import javax.swing.JViewport;
36
import org.apache.commons.io.FilenameUtils;
37
import org.apache.commons.io.IOUtils;
38
import org.apache.commons.lang3.StringUtils;
39
import org.gvsig.tools.dataTypes.CoercionException;
40
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
41
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM;
42
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_CLEAR;
43
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISMODIFIED;
44
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISREADONLY;
45
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDCHANGED;
46
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDENTER;
47
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDEXIT;
48
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONLOAD;
49
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONSETVALUES;
50
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_VALIDATE;
51
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory;
52
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
53
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
54
import org.gvsig.tools.dynobject.DynObject;
55
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
56
import org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource;
57
import org.gvsig.tools.script.Script;
58

    
59
@SuppressWarnings("UseSpecificCatch")
60
public class AbeilleJDynForm extends AbstractJDynForm implements JDynFormFieldListener {
61

    
62
    private class AbeilleComponentsFactory implements ComponentsFactory {
63

    
64
        private class DefaultScrolledComponent<T> implements ScrolledComponent<T> {
65
            private final T component;
66
            
67
            public DefaultScrolledComponent(T component) {
68
                this.component = component;
69
            }
70

    
71
            @Override
72
            public JScrollPane getScrollPane() {
73
                try {
74
                    Object p1 = ((JComponent) component).getParent();
75
                    if( p1 instanceof JViewport ) {
76
                        p1 = ((JComponent) component).getParent();
77
                    }
78
                    if( p1 instanceof JScrollPane ) {
79
                        return (JScrollPane) p1;
80
                    }
81
                } catch(Exception ex) {
82

    
83
                }
84
                return null;
85
            }
86

    
87
            @Override
88
            public T getComponent() {
89
                return this.component;
90
            }
91
        }
92

    
93
        private static final String PREFIX_JCHECKBOX = "chk";
94
        private static final String PREFIX_JLABEL = "lbl";
95
        private static final String PREFIX_DROPDOWN = "lblDdn";
96
        private static final String PREFIX_JCOMBOBOX = "cbo";
97
        private static final String PREFIX_JSPINNER = "spn";
98
        private static final String PREFIX_JTEXTFIELD = "txt";
99
        private static final String PREFIX_JTEXTAREA = "txt";
100
        private static final String PREFIX_JBUTTON = "btn";
101
        private static final String PREFIX_JLIST = "lst";
102
        private static final String PREFIX_JTABLE = "tbl";
103
        
104
        private final FormPanel form;
105

    
106
        public AbeilleComponentsFactory(FormPanel form) {
107
            this.form = form;
108
        }
109
        
110
        @Override
111
        public boolean containsComponents(DynFormFieldDefinition definition) {
112
            String[] prefixes = new String[] {
113
                PREFIX_JCHECKBOX,
114
                PREFIX_JLABEL,
115
                PREFIX_DROPDOWN,
116
                PREFIX_JCOMBOBOX,
117
                PREFIX_JSPINNER,
118
                PREFIX_JTEXTFIELD,
119
                PREFIX_JTEXTAREA,
120
                PREFIX_JBUTTON,
121
                PREFIX_JLIST,
122
                PREFIX_JTABLE
123
            };
124
            for (String prefix : prefixes) {
125
                Component component = this.form.getComponentByName(prefix + definition.getName());
126
                if( component!=null ) {
127
                    return true;
128
                }
129
            }
130
            return false;
131
        }
132

    
133
        @Override
134
        public boolean containsJComboBox(DynFormFieldDefinition definition, String prefix) {
135
            Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
136
            return component!=null;
137
        }
138

    
139
        @Override
140
        public boolean containsJButton(DynFormFieldDefinition definition, String prefix) {
141
            Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
142
            return component!=null;
143
        }
144

    
145
        @Override
146
        public boolean containsJLabel(DynFormFieldDefinition definition, String prefix) {
147
            Component component = this.form.getComponentByName(PREFIX_JLABEL+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
148
            return component!=null;
149
        }
150

    
151
        @Override
152
        public boolean containsJSpinner(DynFormFieldDefinition definition, String prefix) {
153
            Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
154
            return component!=null;
155
        }
156

    
157
        @Override
158
        public boolean containsJTextField(DynFormFieldDefinition definition, String prefix) {
159
            Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
160
            return component!=null;
161
        }
162

    
163
        @Override
164
        public JCheckBox getJCheckBox(DynFormFieldDefinition definition, String prefix) {
165
            try {
166
                Component component = this.form.getComponentByName(PREFIX_JCHECKBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
167
                return (JCheckBox) component;
168
            } catch(Throwable th) {
169
                return null;
170
            }
171
        }
172

    
173
        @Override
174
        public JLabel getJLabel(DynFormFieldDefinition definition, String prefix) {
175
            try {
176
                Component component = this.form.getComponentByName(PREFIX_JLABEL+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
177
                return (JLabel) component;
178
            } catch(Throwable th) {
179
                return null;
180
            }
181
        }
182

    
183
        @Override
184
        public JComboBox getJComboBox(DynFormFieldDefinition definition, String prefix) {
185
            try {
186
                Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
187
                return (JComboBox) component;
188
            } catch(Throwable th) {
189
                return null;
190
            }
191
        }
192

    
193
        @Override
194
        public JTextComponent getJTextField(DynFormFieldDefinition definition, String prefix) {
195
            try {
196
                Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
197
                return (JTextComponent) component;
198
            } catch(Throwable th) {
199
                return null;
200
            }
201
        }
202

    
203
        @Override
204
        public ScrolledComponent<JTextArea> getJTextArea(DynFormFieldDefinition definition, String prefix) {
205
            try {
206
                JTextArea component = (JTextArea) this.form.getComponentByName(
207
                        PREFIX_JTEXTAREA + StringUtils.defaultIfBlank(prefix, "") + definition.getName()
208
                );
209
                return new DefaultScrolledComponent<>(component);
210
            } catch(Throwable th) {
211
                return null;
212
            }
213
        }
214

    
215
        @Override
216
        public ScrolledComponent<JList> getJList(DynFormFieldDefinition definition, String prefix) {
217
            try {
218
                JList component = (JList) this.form.getComponentByName(
219
                        PREFIX_JLIST+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
220
                );
221
                return new DefaultScrolledComponent<>(component);
222
            } catch(Throwable th) {
223
                return null;
224
            }
225
        }
226

    
227
        @Override
228
        public ScrolledComponent<JTable> getJTable(DynFormFieldDefinition definition, String prefix) {
229
            try {
230
                JTable component = (JTable) this.form.getComponentByName(
231
                        PREFIX_JTABLE+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
232
                );
233
                return new DefaultScrolledComponent<>(component);
234
            } catch(Throwable th) {
235
                return null;
236
            }
237
        }
238

    
239
        
240
        @Override
241
        public JSpinner getJSpinner(DynFormFieldDefinition definition, String prefix) {
242
            try {
243
                Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
244
                return (JSpinner) component;
245
            } catch(Throwable th) {
246
                return null;
247
            }
248
        }
249
        
250
        @Override
251
        public JButton getJButton(DynFormFieldDefinition definition, String prefix) {
252
            try {
253
                Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
254
                return (JButton) component;
255
            } catch(Throwable th) {
256
                return null;
257
            }
258
        }
259
        
260
        
261
    }
262
    private Map<String,JDynFormField> components = null;
263
    private String resourceName = null;
264
    private AbeilleComponentsFactory componentsFactory;
265

    
266

    
267
    public AbeilleJDynForm(
268
            DynFormSPIManager manager,
269
            JDynFormFactory factory,
270
            DynFormContext context,
271
            DynFormDefinition definition
272
    ) {
273
        super(manager, factory, definition, context);
274
        this.components = new HashMap();
275
        resourceName = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM);
276
        if (resourceName == null) {
277
            ResourcesStorage storage = context.getResourcesStorage();
278
            if( storage.exists("jetaform") ) {
279
                this.resourceName = "jetaform";
280
            } else if( storage.exists("jfrm") ) {
281
                this.resourceName = "jfrm";
282
            } else {
283
                throw new IllegalArgumentException("Can't locate form resource. Need tag '"+TAG_DYNFORM_ABEILLE_FORM+"' or resource 'jetaform'.");
284
            }
285
        }
286
        this.componentsFactory = null;
287
    }
288

    
289
    @Override
290
    protected JComponent getFieldsContainer() {
291
        InputStream is = null;
292
        FormPanel form = null;
293
        Resource resource = null;
294
        try {
295
            File f = new File(resourceName);
296
            if( f.isAbsolute() && f.exists() ) {
297
                is = new FileInputStream(f);
298
            } else {
299
                ResourcesStorage storage = this.getContext().getResourcesStorage();
300
                resource = storage.getResource(FilenameUtils.getName(resourceName));
301
                if( resource == null || !resource.exists() ) {
302
                    throw new IllegalArgumentException("Can't locate abeille form '"+resourceName+"'.");
303
                }
304
                is = resource.asInputStream();
305
            }
306
            form = new FormPanel(is);
307
            this.componentsFactory = new AbeilleComponentsFactory(form);
308
            for (DynFormFieldDefinition definition : this.getDefinition().getDefinitions()) {
309
                if( definition==null ) {
310
                    continue;
311
                }
312
                if (definition.isHidden()) {
313
                    continue;
314
                }
315
                if( !this.componentsFactory.containsComponents(definition) ) {
316
                    continue;
317
                }
318
                try {
319
                    JDynFormFieldFactory factory = this.getServiceManager()
320
                            .getJDynFormFieldFactory(this.getContext(),definition);
321
                    JDynFormField jfield = factory.create(
322
                            this.getServiceManager(), 
323
                            this.componentsFactory, 
324
                            definition, 
325
                            null
326
                    );
327
                    if (jfield instanceof AbstractJDynFormField) {
328
                        ((AbstractJDynFormField) jfield).setForm(this);
329
                    }
330
                    if (this.isReadOnly()) {
331
                        jfield.setReadOnly(true);
332
                    } else {
333
                        jfield.setReadOnly(definition.isReadOnly());
334
                    }
335
                    jfield.addListener(this);
336

    
337
                    this.components.put(jfield.getName(), jfield);
338
                    jfield.asJComponent(); // Forzamos que se inicialize
339
                    jfield.getJLabel();  // Forzamos que se inicialize 
340
                } catch(Throwable th1) {
341
                    LOGGER.warn("Can't load field '"+definition.getName()+"' for abeille form '"+resourceName+"'.", th1);
342
                }
343
            }
344
            this.bindUserCode(form);
345
            this.callUserEvent(USERCODE_FORM_ONLOAD, this);
346
        } catch (Throwable th) {
347
            LOGGER.warn("Can't load abeille form '"+resourceName+"'.", th);
348
        } finally {
349
            IOUtils.closeQuietly(is);
350
            IOUtils.closeQuietly(resource);
351
        }
352

    
353
        return form;
354
    }
355
    
356
    public Iterator getFieldsIterator() {
357
        if (!this.isContentsInitialized()) {
358
            this.initComponents();
359
        }
360
        return this.components.values().iterator();
361
    }
362

    
363
    @Override
364
    public void setReadOnly(boolean readOnly) {
365
        if( this.isReadOnly() == readOnly ) {
366
            return;
367
        }
368
        super.setReadOnly(readOnly);
369
        if (this.isContentsInitialized()) {
370
            Iterator it = this.getFieldsIterator();
371
            while (it.hasNext()) {
372
                JDynFormField field = (JDynFormField) it.next();
373
                if( readOnly ) {
374
                    field.setReadOnly(true);
375
                } else if( field.getDefinition().isReadOnly() ) {
376
                    field.setReadOnly(true);
377
                } else {
378
                    field.setReadOnly(false);
379
                }
380
            }
381
        }
382
    }
383

    
384
    private void bindUserCode(FormPanel form) {
385
        Script theScript = this.getScript();
386
        if( theScript==null ) {
387
            return;
388
        }        
389
        List<String> names = theScript.getNames();
390
        if( names == null ) {
391
            return;
392
        }
393
        for(String name : names ) {
394
            if( name.endsWith("_click") ) {
395
                String componentName = name.substring(0, name.length()-6);
396
                Component c = form.getComponentByName(componentName);
397
                if( c instanceof JButton ) {
398
                    JButton button = (JButton) c;
399
                    final String fnName = name;
400
                    button.addActionListener(new ActionListener() {
401
                        @Override
402
                        public void actionPerformed(ActionEvent e) {
403
                            callUserEvent(fnName, e);
404
                        }
405
                    });
406
                }
407
            }
408
        }
409
    }
410

    
411
    @Override
412
    public void setValues(DynObject values) {
413
        if (!this.isContentsInitialized()) {
414
            this.values = values;
415
            return;
416
        }
417
        for (JDynFormField jfield : this.getFields()) {
418
            String name = "unknown";
419
            try {
420
                name = jfield.getName();
421
                jfield.setValue(values.getDynValue(jfield.getName()));
422
            } catch(Exception ex) {
423
                LOGGER.warn("Can't set value to field '"+name+"'.",ex);
424
            }
425
        }
426
        this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values);
427
        try {
428
            if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this) ) {
429
                this.setReadOnly(true);
430
            }
431
        } catch (Exception ex) {
432
        }
433
        
434
    }
435

    
436
    public Iterable<JDynFormField> getFields() {
437
        if (!this.isContentsInitialized()) {
438
            this.initComponents();
439
        }
440
        return this.components.values();
441
    }
442

    
443
    @Override
444
    public JDynFormField getField(String fieldName) {
445
        if (!this.isContentsInitialized()) {
446
            this.initComponents();
447
        }
448
        JDynFormField field = this.components.get(fieldName);
449
        return field;
450
    }
451

    
452
    @Override
453
    public void getValues(DynObject values) {
454
        if (values == null) {
455
            return;
456
        }
457
        for (JDynFormField jfield : this.getFields()) {
458
            try {
459
                jfield.fetch(values);
460
            } catch (Exception ex) {
461
                LOGGER.warn("Can't get value of field '" + jfield.getName() + "'.", ex);
462
            }
463
        }
464
    }
465

    
466
    @Override
467
    public Object getValue(String fieldName) {
468
        JDynFormField field = (JDynFormField) this.getField(fieldName);
469
        return field.getValue();
470
    }
471

    
472
    @Override
473
    public void setValue(String fieldName, Object value) {
474
        JDynFormField field = (JDynFormField) this.getField(fieldName);
475
        try {
476
            value = field.getDefinition().getDataType().coerce(value);
477
        } catch (CoercionException e) {
478
            String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'.";
479
            LOGGER.warn(msg, e);
480
            throw new RuntimeException(msg, e);
481
        }
482
        field.setValue(value);
483
    }
484

    
485
    @Override
486
    public boolean hasValidValues() {
487
        for (JDynFormField jfield : this.getFields()) {
488
            if (!jfield.hasValidValue()) {
489
                return false;
490
            }
491
        }
492
        try {
493
            return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this);
494
        } catch (Exception ex) {
495
        }
496
        return true;
497
    }
498

    
499
    @Override
500
    public boolean hasValidValues(List<String> fieldsName) {
501
        if (fieldsName == null) {
502
            fieldsName = new ArrayList<>();
503
        }
504
        for (JDynFormField jfield : this.getFields()) {
505
            if (!jfield.hasValidValue()) {
506
                fieldsName.add(jfield.getName());
507
            }
508
        }
509
        return fieldsName.isEmpty();
510
    }
511

    
512
    @Override
513
    public boolean isModified() {
514
        if( this.isReadOnly() ) {
515
            return false;
516
        }
517
        try {
518
            if( this.getScript()!=null ) {
519
                return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values);
520
            }
521
        } catch (NoSuchMethodException ex) {
522
        } catch (Exception ex) {
523
            LOGGER.warn("Error calling user function form_IsModified.",ex);
524
        }
525
        for (JDynFormField jfield : this.getFields()) {
526
            if (jfield.isModified()) {
527
                return true;
528
            }
529
        }
530
        return false;
531
    }
532

    
533
    @Override
534
    public void clear() {
535
        for (JDynFormField jfield : this.getFields()) {
536
            jfield.clear();
537
        }
538
        this.callUserEvent(USERCODE_FORM_CLEAR, this);
539
    }
540

    
541
    @Override
542
    public void fieldEnter(JDynFormField field) {
543
        message(field.getDefinition().getDescription());
544
        this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field);
545
    }
546

    
547
    @Override
548
    public void fieldExit(JDynFormField field) {
549
        message();
550
        this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field);
551
    }
552

    
553
    @Override
554
    public void fieldChanged(JDynFormField field) {
555
        fireFieldChangeEvent(field);
556
        this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field);
557
    }
558

    
559
    @Override
560
    public void message(JDynFormField field, String message) {
561
        message(message);
562
    }
563

    
564
}