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

History | View | Annotate | Download (16 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.io.File;
24
import java.util.ArrayList;
25
import java.util.List;
26
import javax.swing.JButton;
27
import javax.swing.JLabel;
28
import javax.swing.JList;
29
import javax.swing.JScrollPane;
30
import javax.swing.JTable;
31
import javax.swing.JTextArea;
32
import javax.swing.JViewport;
33
import org.apache.commons.io.FilenameUtils;
34
import org.apache.commons.io.IOUtils;
35
import org.apache.commons.lang3.StringUtils;
36
import org.gvsig.tools.dataTypes.CoercionException;
37
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
38
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM;
39
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory;
40
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
41
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
42
import org.gvsig.tools.dynobject.DynObject;
43
import org.gvsig.tools.util.ResourcesStorage;
44
import org.gvsig.tools.util.ResourcesStorage.Resource;
45

    
46
@SuppressWarnings("UseSpecificCatch")
47
public class AbeilleJDynForm extends AbstractJDynForm implements JDynFormFieldListener {
48

    
49
    private class AbeilleComponentsFactory implements ComponentsFactory {
50

    
51
        private class DefaultScrolledComponent<T> implements ScrolledComponent<T> {
52
            private final T component;
53
            
54
            public DefaultScrolledComponent(T component) {
55
                this.component = component;
56
            }
57

    
58
            @Override
59
            public JScrollPane getScrollPane() {
60
                try {
61
                    Object p1 = ((JComponent) component).getParent();
62
                    if( p1 instanceof JViewport ) {
63
                        p1 = ((JComponent) component).getParent();
64
                    }
65
                    if( p1 instanceof JScrollPane ) {
66
                        return (JScrollPane) p1;
67
                    }
68
                } catch(Exception ex) {
69

    
70
                }
71
                return null;
72
            }
73

    
74
            @Override
75
            public T getComponent() {
76
                return this.component;
77
            }
78
        }
79

    
80
        private static final String PREFIX_JCHECKBOX = "chk";
81
        private static final String PREFIX_JLABEL = "lbl";
82
        private static final String PREFIX_JCOMBOBOX = "cbo";
83
        private static final String PREFIX_JSPINNER = "spn";
84
        private static final String PREFIX_JTEXTFIELD = "txt";
85
        private static final String PREFIX_JTEXTAREA = "txt";
86
        private static final String PREFIX_JBUTTON = "btn";
87
        private static final String PREFIX_JLIST = "lst";
88
        private static final String PREFIX_JTABLE = "tbl";
89
        
90
        private final FormPanel form;
91

    
92
        public AbeilleComponentsFactory(FormPanel form) {
93
            this.form = form;
94
        }
95
        
96
        @Override
97
        public boolean containsComponents(DynFormFieldDefinition definition) {
98
            String[] prefixes = new String[] {
99
                PREFIX_JCHECKBOX,
100
                PREFIX_JLABEL,
101
                PREFIX_JCOMBOBOX,
102
                PREFIX_JSPINNER,
103
                PREFIX_JTEXTFIELD,
104
                PREFIX_JTEXTAREA,
105
                PREFIX_JBUTTON,
106
                PREFIX_JLIST,
107
                PREFIX_JTABLE
108
            };
109
            for (String prefix : prefixes) {
110
                Component component = this.form.getComponentByName(prefix + definition.getName());
111
                if( component!=null ) {
112
                    return true;
113
                }
114
            }
115
            return false;
116
        }
117

    
118
        @Override
119
        public boolean containsJComboBox(DynFormFieldDefinition definition, String prefix) {
120
            Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
121
            return component!=null;
122
        }
123

    
124
        @Override
125
        public boolean containsJButton(DynFormFieldDefinition definition, String prefix) {
126
            Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
127
            return component!=null;
128
        }
129

    
130
        @Override
131
        public boolean containsJSpinner(DynFormFieldDefinition definition, String prefix) {
132
            Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
133
            return component!=null;
134
        }
135

    
136
        @Override
137
        public boolean containsJTextField(DynFormFieldDefinition definition, String prefix) {
138
            Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
139
            return component!=null;
140
        }
141

    
142
        @Override
143
        public JCheckBox getJCheckBox(DynFormFieldDefinition definition, String prefix) {
144
            try {
145
                Component component = this.form.getComponentByName(PREFIX_JCHECKBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
146
                return (JCheckBox) component;
147
            } catch(Throwable th) {
148
                return null;
149
            }
150
        }
151

    
152
        @Override
153
        public JLabel getJLabel(DynFormFieldDefinition definition, String prefix) {
154
            try {
155
                Component component = this.form.getComponentByName(PREFIX_JLABEL+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
156
                return (JLabel) component;
157
            } catch(Throwable th) {
158
                return null;
159
            }
160
        }
161

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

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

    
182
        @Override
183
        public ScrolledComponent<JTextArea> getJTextArea(DynFormFieldDefinition definition, String prefix) {
184
            try {
185
                JTextArea component = (JTextArea) this.form.getComponentByName(
186
                        PREFIX_JTEXTAREA + StringUtils.defaultIfBlank(prefix, "") + definition.getName()
187
                );
188
                return new DefaultScrolledComponent<>(component);
189
            } catch(Throwable th) {
190
                return null;
191
            }
192
        }
193

    
194
        @Override
195
        public ScrolledComponent<JList> getJList(DynFormFieldDefinition definition, String prefix) {
196
            try {
197
                JList component = (JList) this.form.getComponentByName(
198
                        PREFIX_JLIST+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
199
                );
200
                return new DefaultScrolledComponent<>(component);
201
            } catch(Throwable th) {
202
                return null;
203
            }
204
        }
205

    
206
        @Override
207
        public ScrolledComponent<JTable> getJTable(DynFormFieldDefinition definition, String prefix) {
208
            try {
209
                JTable component = (JTable) this.form.getComponentByName(
210
                        PREFIX_JTABLE+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
211
                );
212
                return new DefaultScrolledComponent<>(component);
213
            } catch(Throwable th) {
214
                return null;
215
            }
216
        }
217

    
218
        
219
        @Override
220
        public JSpinner getJSpinner(DynFormFieldDefinition definition, String prefix) {
221
            try {
222
                Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
223
                return (JSpinner) component;
224
            } catch(Throwable th) {
225
                return null;
226
            }
227
        }
228
        
229
        @Override
230
        public JButton getJButton(DynFormFieldDefinition definition, String prefix) {
231
            try {
232
                Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
233
                return (JButton) component;
234
            } catch(Throwable th) {
235
                return null;
236
            }
237
        }
238
        
239
        
240
    }
241
    private Map<String,JDynFormField> components = null;
242
    private String resourceName = null;
243
    private AbeilleComponentsFactory componentsFactory;
244

    
245

    
246
    public AbeilleJDynForm(
247
            DynFormSPIManager manager,
248
            JDynFormFactory factory,
249
            DynFormContext context,
250
            DynFormDefinition definition
251
    ) {
252
        super(manager, factory, definition, context);
253
        this.components = new HashMap();
254
        resourceName = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM);
255
        if (resourceName == null) {
256
            throw new IllegalArgumentException("Need tag '"+TAG_DYNFORM_ABEILLE_FORM+"'.");
257
        }
258
        this.componentsFactory = null;
259
    }
260

    
261
    @Override
262
    protected JComponent getFieldsContainer() {
263
        InputStream is = null;
264
        FormPanel form = null;
265
        Resource resource = null;
266
        try {
267
            File f = new File(resourceName);
268
            if( f.isAbsolute() && f.exists() ) {
269
                is = new FileInputStream(f);
270
            } else {
271
                ResourcesStorage storage = this.getContext().getResourcesStorage();
272
                resource = storage.getResource(FilenameUtils.getName(resourceName));
273
                if( resource == null ) {
274
                    throw new IllegalArgumentException("Can't locate abeille form '"+resourceName+"'.");
275
                }
276
                is = resource.asInputStream();
277
            }
278
            form = new FormPanel(is);
279
            this.componentsFactory = new AbeilleComponentsFactory(form);
280
            for (DynFormFieldDefinition definition : this.getDefinition().getDefinitions()) {
281
                if( definition==null ) {
282
                    continue;
283
                }
284
                if (definition.isHidden()) {
285
                    continue;
286
                }
287
                if( !this.componentsFactory.containsComponents(definition) ) {
288
                    continue;
289
                }
290
                try {
291
                    JDynFormFieldFactory factory = this.getServiceManager()
292
                            .getJDynFormFieldFactory(this.getContext(),definition);
293
                    JDynFormField jfield = factory.create(
294
                            this.getServiceManager(), 
295
                            this.componentsFactory, 
296
                            definition, 
297
                            null
298
                    );
299
                    if (jfield instanceof AbstractJDynFormField) {
300
                        ((AbstractJDynFormField) jfield).setForm(this);
301
                    }
302
                    if (this.isReadOnly()) {
303
                        jfield.setReadOnly(true);
304
                    } else {
305
                        jfield.setReadOnly(definition.isReadOnly());
306
                    }
307
                    jfield.addListener(this);
308

    
309
                    this.components.put(jfield.getName(), jfield);
310
                    jfield.asJComponent(); // Forzamos que se inicialize
311
                    jfield.getJLabel();  // Forzamos que se inicialize 
312
                } catch(Throwable th1) {
313
                    LOGGER.warn("Can't load field '"+definition.getName()+"' for abeille form '"+resourceName+"'.", th1);
314
                }
315
            }
316
        } catch (Throwable th) {
317
            LOGGER.warn("Can't load abeille form '"+resourceName+"'.", th);
318
        } finally {
319
            IOUtils.closeQuietly(is);
320
            IOUtils.closeQuietly(resource);
321
        }
322

    
323
        return form;
324
    }
325

    
326
    @Override
327
    public void setValues(DynObject values) {
328
        if (!this.isContentsInitialized()) {
329
            this.values = values;
330
            return;
331
        }
332
        for (JDynFormField jfield : this.getFields()) {
333
            String name = "unknown";
334
            try {
335
                name = jfield.getName();
336
                jfield.setValue(values.getDynValue(jfield.getName()));
337
            } catch(Exception ex) {
338
                LOGGER.warn("Can't set value to field '"+name+"'.",ex);
339
            }
340
        }
341
    }
342

    
343
    public Iterable<JDynFormField> getFields() {
344
        if (!this.isContentsInitialized()) {
345
            this.initComponents();
346
        }
347
        return this.components.values();
348
    }
349

    
350
    @Override
351
    public JDynFormField getField(String fieldName) {
352
        if (!this.isContentsInitialized()) {
353
            this.initComponents();
354
        }
355
        JDynFormField field = this.components.get(fieldName);
356
        return field;
357
    }
358

    
359
    @Override
360
    public void getValues(DynObject values) {
361
        if (values == null) {
362
            return;
363
        }
364
        for (JDynFormField jfield : this.getFields()) {
365
            try {
366
                jfield.fetch(values);
367
            } catch (Exception ex) {
368
                LOGGER.warn("Can't get value of field '" + jfield.getName() + "'.", ex);
369
            }
370
        }
371
    }
372

    
373
    @Override
374
    public Object getValue(String fieldName) {
375
        JDynFormField field = (JDynFormField) this.getField(fieldName);
376
        return field.getValue();
377
    }
378

    
379
    @Override
380
    public void setValue(String fieldName, Object value) {
381
        JDynFormField field = (JDynFormField) this.getField(fieldName);
382
        try {
383
            value = field.getDefinition().getDataType().coerce(value);
384
        } catch (CoercionException e) {
385
            String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'.";
386
            LOGGER.warn(msg, e);
387
            throw new RuntimeException(msg, e);
388
        }
389
        field.setValue(value);
390
    }
391

    
392
    @Override
393
    public boolean hasValidValues() {
394
        for (JDynFormField jfield : this.getFields()) {
395
            if (!jfield.hasValidValue()) {
396
                return false;
397
            }
398
        }
399
        return true;
400
    }
401

    
402
    @Override
403
    public boolean hasValidValues(List<String> fieldsName) {
404
        if (fieldsName == null) {
405
            fieldsName = new ArrayList<>();
406
        }
407
        for (JDynFormField jfield : this.getFields()) {
408
            if (!jfield.hasValidValue()) {
409
                fieldsName.add(jfield.getName());
410
            }
411
        }
412
        return fieldsName.isEmpty();
413
    }
414

    
415
    @Override
416
    public boolean isModified() {
417
        for (JDynFormField jfield : this.getFields()) {
418
            if (jfield.isModified()) {
419
                return true;
420
            }
421
        }
422
        return false;
423
    }
424

    
425
    @Override
426
    public void clear() {
427
        for (JDynFormField jfield : this.getFields()) {
428
            jfield.clear();
429
        }
430
    }
431

    
432
    @Override
433
    public void fieldEnter(JDynFormField field) {
434
        message(field.getDefinition().getDescription());
435
    }
436

    
437
    @Override
438
    public void fieldExit(JDynFormField field) {
439
        message();
440
    }
441

    
442
    @Override
443
    public void fieldChanged(JDynFormField field) {
444
        fireFieldChangeEvent(field);
445
    }
446

    
447
    @Override
448
    public void message(JDynFormField field, String message) {
449
        message(message);
450
    }
451

    
452
}