Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.spi / src / main / java / org / gvsig / tools / dynform / spi / dynformfield / AbstractJDynFormField.java @ 1405

History | View | Annotate | Download (16 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.spi.dynformfield;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.Color;
27
import java.util.ArrayList;
28
import java.util.HashSet;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Set;
32

    
33
import javax.swing.Action;
34
import javax.swing.JComboBox;
35
import javax.swing.JComponent;
36
import javax.swing.JLabel;
37
import javax.swing.JList;
38
import javax.swing.JPanel;
39
import javax.swing.JScrollPane;
40
import javax.swing.JSpinner;
41
import javax.swing.JTextField;
42
import javax.swing.JViewport;
43
import javax.swing.text.JTextComponent;
44
import org.gvsig.tools.dataTypes.CoercionException;
45
import org.gvsig.tools.dataTypes.DataTypes;
46

    
47
import org.gvsig.tools.dynform.DynFormFieldDefinition;
48
import org.gvsig.tools.dynform.JDynForm;
49
import org.gvsig.tools.dynform.JDynFormField;
50
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
51
import org.gvsig.tools.dynobject.DynField_v2;
52
import org.gvsig.tools.dynobject.DynObject;
53
import org.gvsig.tools.dynobject.Tags;
54
import org.gvsig.tools.service.Manager;
55
import org.gvsig.tools.service.spi.ServiceManager;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
@SuppressWarnings({"rawtypes", "unchecked"})
60
public abstract class AbstractJDynFormField implements JDynFormField {
61

    
62
    protected static final Logger logger = LoggerFactory
63
            .getLogger(AbstractJDynFormField.class);
64

    
65
    private DynFormSPIManager manager = null;
66
    private DynFormFieldDefinition definition = null;
67
    private JLabel jlabel = null;
68
    private JPanel jlabelpanel = null;
69
    private Set listeners = null;
70
    private JProblemIndicator problemIndicator = null;
71

    
72
    protected DynObject parameters = null;
73
    protected JComponent contents = null;
74

    
75
    private boolean readOnly = false;
76
    private final List customActions;
77
    protected boolean emptyToNull = false;
78
    private JDynForm form;
79

    
80
    public AbstractJDynFormField(DynObject parameters,
81
            ServiceManager serviceManager) {
82
        this.parameters = parameters;
83
        this.manager = (DynFormSPIManager) serviceManager;
84
        this.definition = this.getDefinition();
85
        this.listeners = new HashSet();
86
        this.readOnly = this.definition.isReadOnly();
87
        this.problemIndicator = this.manager.createProblemIndicator(this);
88
        this.customActions = new ArrayList<>();
89
        this.loadDefaultValuesFromTags(definition.getTags());
90
    }
91

    
92
    public void loadDefaultValuesFromTags(Tags tags) {
93
        try {
94
            this.readOnly = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY);
95
        } catch (CoercionException ex) {
96
        }
97
        try {
98
            this.emptyToNull = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_TRANSLATE_EMPTY_TO_NULL);
99
        } catch (CoercionException ex) {
100
        }
101
    }
102

    
103
    public abstract void initComponent();
104

    
105
    public abstract Object getAssignedValue();
106

    
107
    public Object getParameterValue() {
108
        return this.parameters.getDynValue(DynFormSPIManager.FIELD_VALUE);
109
    }
110

    
111
    @Override
112
    public JComponent asJComponent() {
113
        if (this.contents == null) {
114
            this.initComponent();
115
            if (!this.customActions.isEmpty()) {
116
                addPopupComponents();
117
            }
118
            if (this.readOnly) {
119
                this.setReadOnly(readOnly);
120
            }
121
        }
122
        return this.contents;
123
    }
124

    
125
    public String getName() {
126
        return this.definition.getName();
127
    }
128

    
129
    public String getLabel() {
130
        if (definition.getLabel() != null) {
131
            return definition.getLabel();
132
        } else {
133
            return this.getName();
134
        }
135
    }
136

    
137
    public JComponent getJLabel() {
138
        if (this.jlabel == null) {
139
            if (getTagValueAsBoolean(DynFormSPIManager.TAG_DYNFORM_LABEL_EMPTY, false)) {
140
                this.jlabel = new JLabel("");
141
            } else {
142
                this.jlabel = new JLabel(this.getLabel());
143
            }
144
            this.jlabel.setLabelFor(this.contents);
145
            if (this.getDefinition().isMandatory()) {
146
                this.jlabel.setForeground(Color.red.darker());
147
            }
148
            this.jlabelpanel = new JPanel();
149
            this.jlabelpanel.setLayout(new BorderLayout());
150
            this.jlabelpanel.add(jlabel, BorderLayout.CENTER);
151
            this.jlabelpanel.add(problemIndicator.asJComponent(), BorderLayout.LINE_END);
152
        }
153
        return this.jlabelpanel;
154
    }
155

    
156
    public DynFormFieldDefinition getDefinition() {
157
        return (DynFormFieldDefinition) this.parameters
158
                .getDynValue(DynFormSPIManager.FIELD_FIELDDEFINITION);
159
    }
160

    
161
    public Manager getManager() {
162
        return this.manager;
163
    }
164

    
165
    public DynFormSPIManager getServiceManager() {
166
        return this.manager;
167
    }
168

    
169
    public void addListener(JDynFormFieldListener listener) {
170
        this.listeners.add(listener);
171
    }
172

    
173
    public void removeListener(JDynFormFieldListener listener) {
174
        this.listeners.remove(listener);
175
    }
176

    
177
    protected void fireFieldChangedEvent() {
178
        Iterator it = this.listeners.iterator();
179
        while (it.hasNext()) {
180
            JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
181
            try {
182
                listener.fieldChanged(this);
183
            } catch (Exception ex) {
184
                logger.info("Error calling listener " + listener.toString()
185
                        + "(" + listener.getClass().getName() + ") from "
186
                        + this.toString() + "(" + this.getClass().getName()
187
                        + ").", ex);
188
            }
189
        }
190
    }
191

    
192
    protected void fireFieldEnterEvent() {
193
        Iterator it = this.listeners.iterator();
194
        while (it.hasNext()) {
195
            JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
196
            try {
197
                listener.fieldEnter(this);
198
            } catch (Exception ex) {
199
                logger.info("Error calling listener " + listener.toString()
200
                        + "(" + listener.getClass().getName() + ") from "
201
                        + this.toString() + "(" + this.getClass().getName()
202
                        + ").", ex);
203
            }
204
        }
205
    }
206

    
207
    protected void fireFieldExitEvent() {
208
        Iterator it = this.listeners.iterator();
209
        while (it.hasNext()) {
210
            JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
211
            try {
212
                listener.fieldExit(this);
213
            } catch (Exception ex) {
214
                logger.info("Error calling listener " + listener.toString()
215
                        + "(" + listener.getClass().getName() + ") from "
216
                        + this.toString() + "(" + this.getClass().getName()
217
                        + ").", ex);
218
            }
219
        }
220
    }
221

    
222
    public void fireMessageEvent(String message) {
223
        Iterator it = this.listeners.iterator();
224
        while (it.hasNext()) {
225
            JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
226
            try {
227
                listener.message(this, message);
228
            } catch (Exception ex) {
229
                logger.info("Error calling listener " + listener.toString()
230
                        + "(" + listener.getClass().getName() + ") from "
231
                        + this.toString() + "(" + this.getClass().getName()
232
                        + ").", ex);
233
            }
234
        }
235
    }
236

    
237
    public boolean isReadOnly() {
238
        return this.readOnly;
239
    }
240

    
241
    public void setReadOnly(boolean readonly) {
242
                // FIXME: Implememtacion por defecto, sobreescribirla en las subclases
243
        // segun convenga para cada componente.
244
        JTextComponent x = null;
245

    
246
        this.readOnly = readonly;
247
        if (jlabel != null) {
248
            this.jlabel.setEnabled(!readonly);
249
        }
250
        if (this.contents != null) {
251
            if (this.contents instanceof JTextComponent) {
252
                x = (JTextComponent) this.contents;
253
                x.setEditable(!readonly);
254
            } else if (this.contents instanceof JScrollPane) {
255
                try {
256
                    JViewport port = ((JScrollPane) this.contents).getViewport();
257
                    x = (JTextComponent) port.getView();
258
                    x.setEditable(!readonly);
259
                } catch (Exception ex) {
260
                    this.contents.setEnabled(!readOnly);
261
                }
262
            } else {
263
                this.contents.setEnabled(!readOnly);
264
            }
265
        }
266
    }
267

    
268
    public JProblemIndicator problemIndicator() {
269
        return this.problemIndicator;
270
    }
271

    
272
    public class IllegalFieldValue extends RuntimeException {
273

    
274
        /**
275
         *
276
         */
277
        private static final long serialVersionUID = -4409236610055983438L;
278

    
279
        public IllegalFieldValue(JDynFormField field, String message) {
280
            super("The value of field '" + field.getLabel() + "' is not valid. " + message);
281
        }
282
    }
283

    
284
    public String toString() {
285
        return super.toString() + "{" + this.getName() + "}";
286
    }
287

    
288
    public boolean isModified() {
289
        boolean modified = false;
290
        try {
291
            if (!this.isReadOnly()) {
292
                Object value = this.getValue();
293
                if (value == null) {
294
                    if (value != this.getAssignedValue()) {
295
                        modified = true;
296
                    }
297
                } else {
298
                    if (!value.equals(this.getAssignedValue())) {
299
                        modified = true;
300
                    }
301
                }
302
            }
303
        } catch (IllegalFieldValue ex) {
304
            // Si es incorrecto el valor del campo decimos a capom que esta modificado.
305
            modified = true;
306
        }
307
        return modified;
308
    }
309

    
310
    private void addPopupComponents() {
311
        Iterator it = this.customActions.iterator();
312
        while (it.hasNext()) {
313
            Object obj = it.next();
314
            if (obj != null) {
315
                Action act = (Action) obj;
316
                if (contents instanceof SupportPopupMenu) {
317
                    DynFormFieldAction sact = new DynFormFieldAction(this, act);
318
                    ((SupportPopupMenu) this.contents).addActionToPopupMenu(
319
                            sact.getName(),
320
                            sact);
321
                }
322
            } else {
323
                if (contents instanceof SupportPopupMenu) {
324
                    ((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
325
                }
326
            }
327

    
328
        }
329
    }
330

    
331
    public void addActionToPopupMenu(String name, Action action) {
332
        if (contents != null) {
333
            if (contents instanceof SupportPopupMenu) {
334
                DynFormFieldAction sact = new DynFormFieldAction(this, action);
335
                ((SupportPopupMenu) this.contents).addActionToPopupMenu(
336
                        sact.getName(),
337
                        sact);
338
            }
339
        } else {
340
            this.customActions.add(action);
341
        }
342
    }
343

    
344
    public void addSeparatorToPopupMenu() {
345
        if (contents != null) {
346
            if (contents instanceof SupportPopupMenu) {
347
                ((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
348
            }
349
        } else {
350
            this.customActions.add(null);
351
        }
352
    }
353

    
354
    public void setTranslateEmptyToNull(boolean emptyToNull) {
355
        this.emptyToNull = emptyToNull;
356
    }
357

    
358
    public boolean translateEmptyToNull() {
359
        return this.emptyToNull;
360
    }
361

    
362
    @SuppressWarnings("UnnecessaryReturnStatement")
363
    public void clear() {
364
        if (this.contents == null) {
365
            return;
366
        }
367
        if (this.contents instanceof JTextField) {
368
            Object value = this.getDefinition().getDefaultValue();
369
            if (value != null) {
370
                value = value.toString();
371
            } else {
372
                value = "";
373
            }
374
            ((JTextField) this.contents).setText((String) value);
375
            return;
376
        }
377
        if (this.contents instanceof JSpinner) {
378
            Object value = this.getDefinition().getDefaultValue();
379
            if (value != null) {
380
                value = value.toString();
381
            }
382
            ((JSpinner) this.contents).setValue(value);
383
            return;
384
        }
385
        if (this.contents instanceof JList) {
386
            ((JList) this.contents).setSelectedIndex(-1);
387
            return;
388
        }
389
        if (this.contents instanceof JComboBox) {
390
            ((JComboBox) this.contents).setSelectedIndex(-1);
391
            return;
392
        }
393
    }
394

    
395
    public void setForm(JDynForm form) {
396
        this.form = form;
397
    }
398

    
399
    @Override
400
    public JDynForm getForm() {
401
        return this.form;
402
    }
403

    
404
    @Override
405
    public void fetch(DynObject container) {
406
        Object value = this.getValue();
407
        container.setDynValue(this.getName(), value);
408
    }
409

    
410
    protected int getTagValueAsInt(String tagname, int defaultVaue) {
411
        return getTagValueAsInt(tagname, null, defaultVaue);
412
    }
413

    
414
    protected int getTagValueAsInt(String tagname1, String tagname2, int defaultVaue) {
415
        DynField_v2 fielddef = (DynField_v2) this.getDefinition();
416
        String tagname = null;
417
        if (fielddef.getTags().has(tagname1)) {
418
            tagname = tagname1;
419
        } else if (fielddef.getTags().has(tagname2)) {
420
            tagname = tagname2;
421
        } else {
422
            return defaultVaue;
423
        }
424
        try {
425
            int value = fielddef.getTags().getInt(tagname);
426
            return value;
427
        } catch (CoercionException ex) {
428
            logger.warn("Can't parse tag '" + tagname + "' as int for field '" + fielddef.getName() + "'.", ex);
429
        }
430
        return defaultVaue;
431
    }
432

    
433
    protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) {
434
        return getTagValueAsBoolean(tagname, null, defaultVaue);
435
    }
436

    
437
    protected boolean getTagValueAsBoolean(String tagname1, String tagname2, boolean defaultVaue) {
438
        DynField_v2 fielddef = (DynField_v2) this.getDefinition();
439
        String tagname = null;
440
        if (fielddef.getTags().has(tagname1)) {
441
            tagname = tagname1;
442
        } else if (fielddef.getTags().has(tagname2)) {
443
            tagname = tagname2;
444
        } else {
445
            return defaultVaue;
446
        }
447
        try {
448
            boolean value = fielddef.getTags().getBoolean(tagname);
449
            return value;
450
        } catch (CoercionException ex) {
451
            logger.warn("Can't parse tag '" + tagname + "' as boolean for field '" + fielddef.getName() + "'.", ex);
452
        }
453
        return defaultVaue;
454
    }
455

    
456
    protected String getTagValueAsString(String tagname, String defaultVaue) {
457
        return getTagValueAsString(tagname, null, defaultVaue);
458
    }
459

    
460
    protected String getTagValueAsString(String tagname1, String tagname2, String defaultVaue) {
461
        DynField_v2 fielddef = (DynField_v2) this.getDefinition();
462
        String tagname = null;
463
        if (fielddef.getTags().has(tagname1)) {
464
            tagname = tagname1;
465
        } else if (fielddef.getTags().has(tagname2)) {
466
            tagname = tagname2;
467
        } else {
468
            return defaultVaue;
469
        }
470
        try {
471
            String value = (String) fielddef.getTags().get(tagname, DataTypes.STRING);
472
            return value;
473
        } catch (CoercionException ex) {
474
            logger.warn("Can't parse tag '" + tagname + "' as string for field '" + fielddef.getName() + "'.", ex);
475
        }
476
        return defaultVaue;
477
    }
478

    
479
}