Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / dynform / src / org / gvsig / tools / dynform / impl / dynformfield / AbstractJDynFormField.java @ 841

History | View | Annotate | Download (5.39 KB)

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

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.util.HashSet;
6
import java.util.Iterator;
7
import java.util.Set;
8

    
9
import javax.swing.JComponent;
10
import javax.swing.JLabel;
11
import javax.swing.JPanel;
12

    
13
import org.gvsig.tools.dynform.api.DynFormFieldDefinition;
14
import org.gvsig.tools.dynform.api.DynFormManager;
15
import org.gvsig.tools.dynform.api.JDynFormField;
16
import org.gvsig.tools.dynobject.DynObject;
17
import org.gvsig.tools.service.Manager;
18
import org.gvsig.tools.service.spi.ServiceManager;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
@SuppressWarnings({"rawtypes", "unchecked"})
23
public abstract class AbstractJDynFormField implements JDynFormField {
24

    
25
        protected static final Logger logger = LoggerFactory
26
                        .getLogger(AbstractJDynFormField.class);
27
        
28
        private DynFormManager manager = null;
29
        private DynFormFieldDefinition definition = null;
30
        private JLabel jlabel = null;
31
        private JPanel jlabelpanel = null;
32
        private Set listeners = null;
33
        private ProblemIndicator problemIndicator = null;
34

    
35
        protected DynObject parameters = null;
36
        protected JComponent contents = null;
37

    
38
        private boolean readOnly = false;
39

    
40

    
41
        public AbstractJDynFormField(DynObject parameters,
42
                        ServiceManager serviceManager) {
43
                this.parameters = parameters;
44
                this.manager = (DynFormManager) serviceManager;
45
                this.definition = this.getDefinition();
46
                this.listeners = new HashSet();
47
                this.readOnly = this.definition.isReadOnly();
48
                this.problemIndicator = new ProblemIndicator(this);
49
        }
50

    
51
        public abstract void initComponent();
52

    
53
        public Object getParameterValue() {
54
                return this.parameters.getDynValue(DynFormManager.FIELD_VALUE);
55
        }
56

    
57
        public JComponent asJComponent() {
58
                if (this.contents == null) {
59
                        this.initComponent();
60
                        if( this.readOnly ) {
61
                                this.setReadOnly(readOnly);
62
                        }
63
                }
64
                return this.contents;
65
        }
66

    
67
        public String getName() {
68
                return this.definition.getName();
69
        }
70

    
71
        public String getLabel() {
72
                return definition.getLabel();
73
        }
74

    
75
        public JComponent getJLabel() {
76
                if (this.jlabel == null) {
77
                        this.jlabel = new JLabel(this.getLabel(), JLabel.TRAILING);
78
                        this.jlabel.setLabelFor(this.contents);
79
                        if( this.getDefinition().isMandatory() ) {
80
                                this.jlabel.setForeground(Color.red.darker());
81
                        }
82
                        this.jlabelpanel = new JPanel();
83
                        this.jlabelpanel.setLayout(new BorderLayout());
84
                        this.jlabelpanel.add(jlabel,BorderLayout.CENTER);
85
                        this.jlabelpanel.add(problemIndicator.asJComponent(), BorderLayout.LINE_END);
86
                }
87
                return this.jlabelpanel;
88
        }
89

    
90
        public DynFormFieldDefinition getDefinition() {
91
                return (DynFormFieldDefinition) this.parameters
92
                                .getDynValue(DynFormManager.FIELD_FIELDDEFINITION);
93
        }
94

    
95
        public Manager getManager() {
96
                return this.manager;
97
        }
98

    
99
        public void addListener(JDynFormFieldListener listener) {
100
                this.listeners.add(listener);
101
        }
102

    
103
        public void removeListener(JDynFormFieldListener listener) {
104
                this.listeners.remove(listener);
105
        }
106
        
107
        protected void fireFieldChangedEvent() {
108
                Iterator it = this.listeners.iterator();
109
                while (it.hasNext()) {
110
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
111
                        try {
112
                                listener.fieldChanged(this);
113
                        } catch (Exception ex) {
114
                                logger.info("Error calling listener " + listener.toString()
115
                                                + "(" + listener.getClass().getName() + ") from "
116
                                                + this.toString() + "(" + this.getClass().getName()
117
                                                + ").", ex);
118
                        }
119
                }
120
        }
121

    
122
        protected void fireFieldEnterEvent() {
123
                Iterator it = this.listeners.iterator();
124
                while (it.hasNext()) {
125
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
126
                        try {
127
                                listener.fieldEnter(this);
128
                        } catch (Exception ex) {
129
                                logger.info("Error calling listener " + listener.toString()
130
                                                + "(" + listener.getClass().getName() + ") from "
131
                                                + this.toString() + "(" + this.getClass().getName()
132
                                                + ").", ex);
133
                        }
134
                }
135
        }
136

    
137
        protected void fireFieldExitEvent() {
138
                Iterator it = this.listeners.iterator();
139
                while (it.hasNext()) {
140
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
141
                        try {
142
                                listener.fieldExit(this);
143
                        } catch (Exception ex) {
144
                                logger.info("Error calling listener " + listener.toString()
145
                                                + "(" + listener.getClass().getName() + ") from "
146
                                                + this.toString() + "(" + this.getClass().getName()
147
                                                + ").", ex);
148
                        }
149
                }
150
        }
151
        
152
        protected void fireMessageEvent(String message) {
153
                Iterator it = this.listeners.iterator();
154
                while (it.hasNext()) {
155
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
156
                        try {
157
                                listener.message(this, message);
158
                        } catch (Exception ex) {
159
                                logger.info("Error calling listener " + listener.toString()
160
                                                + "(" + listener.getClass().getName() + ") from "
161
                                                + this.toString() + "(" + this.getClass().getName()
162
                                                + ").", ex);
163
                        }
164
                }
165
        }
166

    
167
        public boolean isReadOnly() {
168
                return this.readOnly ;
169
        }
170
        
171
        public void setReadOnly(boolean readonly) {
172
                // FIXME: Implememtacion por defecto, sobreescribirla en las subclases
173
                // segun convenga para cada componente.
174
                
175
                this.readOnly = readonly;
176
                if( this.contents != null ) {
177
                        this.contents.setEnabled(!readOnly);
178
                }
179
        }
180

    
181
        public ProblemIndicator problemIndicator() {
182
                return this.problemIndicator;
183
        }
184
                
185
        public class IllegalFieldValue extends RuntimeException {
186
                
187
                /**
188
                 * 
189
                 */
190
                private static final long serialVersionUID = -4409236610055983438L;
191

    
192
                public IllegalFieldValue(JDynFormField field, String message) {
193
                        super("The value of field '"+field.getLabel()+"' is not valid. " + message);
194
                }
195
        }
196
}