Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / dynform / src / org / gvsig / tools / dynform / impl / dynformfield / integer / JDynFormFieldInteger.java @ 846

History | View | Annotate | Download (3.77 KB)

1
package org.gvsig.tools.dynform.impl.dynformfield.integer;
2

    
3
import java.awt.event.FocusEvent;
4
import java.awt.event.FocusListener;
5

    
6
import javax.swing.JComboBox;
7
import javax.swing.JTextField;
8
import javax.swing.event.DocumentEvent;
9
import javax.swing.event.DocumentListener;
10

    
11
import org.gvsig.tools.dynform.api.JDynFormField;
12
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormField;
13
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormField.IllegalFieldValue;
14
import org.gvsig.tools.dynobject.DynObject;
15
import org.gvsig.tools.dynobject.DynObjectValueItem;
16
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
17
import org.gvsig.tools.service.spi.ServiceManager;
18
import org.omg.CORBA.portable.ValueInputStream;
19

    
20
public class JDynFormFieldInteger extends AbstractJDynFormField implements JDynFormField, FocusListener {
21
        
22
        private Object assignedValue  = null;
23
        
24
        public JDynFormFieldInteger(DynObject parameters,
25
                        ServiceManager serviceManager) {
26
                super(parameters, serviceManager);
27
                this.assignedValue = this.getParameterValue();
28
        }
29

    
30
        public Object getAssignedValue() {
31
                return this.assignedValue;
32
        }
33

    
34
        private JTextField getJTextField() {
35
                return (JTextField) this.contents;
36
        }
37
        
38
        private JComboBox getJComboBox() {
39
                return (JComboBox) this.contents;
40
        }
41

    
42
        
43
        public void initComponent() {
44
                DynObjectValueItem[] availableValues = this.getDefinition().getAvailableValues();
45
                if( availableValues==null ) {
46
                        this.contents = new JTextField();
47
                        this.contents.addFocusListener(this);
48
                        if( this.getDefinition().isReadOnly() ) {
49
                                this.getJTextField().setEditable(false);;
50
                        }
51
                } else {
52
                        this.contents = new JComboBox(availableValues);
53
                        this.contents.addFocusListener(this);
54
                        if( this.getDefinition().isReadOnly() ) {
55
                                this.getJComboBox().setEditable(false);
56
                        }
57
                }
58
                this.setValue(this.assignedValue);
59
        }
60
        
61
        public void setValue(Object value) {
62
                String s = null;
63
                if( value == null ) {
64
                        value = this.getDefinition().getDefaultValue();
65
                        if( value == null ) {
66
                                s = "0";
67
                        } else {
68
                                s = value.toString();
69
                        }
70
                } else {
71
                        s = value.toString();
72
                        try {
73
                                this.getDefinition().validate(value);
74
                                this.problemIndicator().clear();
75
                        } catch (DynFieldValidateException e) {
76
                                this.problemIndicator().set(e.getLocalizedMessage());
77
                        }
78
                }
79
                if( this.contents instanceof JTextField ) {
80
                        this.getJTextField().setText(s);
81
                } else {
82
                        // FIXME: Falta por implementar el set sobre el combo.
83
                }
84
                this.assignedValue = value;
85
        }
86
        
87
        public Object getValue() {
88
                Object value = null;
89
                if( this.contents instanceof JTextField ) {
90
                        String s = getJTextField().getText();
91
                        if( s.trim().length()==0 ) {
92
                                value = this.getDefinition().getDefaultValue();
93
                        } else {
94
                                try {
95
                                        value = Integer.valueOf(s);
96
                                } catch(Exception ex) {
97
                                        throw new IllegalFieldValue(this, "Is not a valid integer.");
98
                                }
99
                        }
100
                } else {
101
                        DynObjectValueItem x = (DynObjectValueItem) ((JComboBox)this.contents).getSelectedItem();
102
                        if( x==null ) {
103
                                value = null;
104
                        } else {
105
                                value = x.getValue();
106
                        }
107
                }
108
                try {
109
                        this.getDefinition().validate(value);
110
                        this.problemIndicator().clear();
111
                } catch (DynFieldValidateException e) {
112
                        throw new IllegalFieldValue(this, e.getLocalizedMessage());
113
                }
114
                return value;
115
        }
116

    
117
        @SuppressWarnings("unused")
118
        public boolean hasValidValue() {
119
                try {
120
                        Object value = this.getValue();
121
                } catch(Exception e) {
122
                        return false;
123
                }
124
                return true;
125
        }
126

    
127
        public void focusGained(FocusEvent arg0) {
128
                fireFieldEnterEvent();
129
                this.problemIndicator().restore();
130
        }
131

    
132
        public void focusLost(FocusEvent arg0) {
133
                if( this.hasValidValue() ) {
134
                        this.problemIndicator().clear();
135
                } else {
136
                        try {
137
                                Object value = this.getValue();
138
                        } catch(Exception e) {
139
                                this.problemIndicator().set(e.getLocalizedMessage());
140
                        }
141
                }
142
                fireFieldExitEvent();
143
        }
144

    
145
}