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

History | View | Annotate | Download (9.18 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dynform.spi.dynformfield;
25

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

    
34
import javax.swing.Action;
35
import javax.swing.JComponent;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.JScrollPane;
39
import javax.swing.JViewport;
40
import javax.swing.text.JTextComponent;
41

    
42
import org.gvsig.tools.dynform.DynFormFieldDefinition;
43
import org.gvsig.tools.dynform.JDynFormField;
44
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
45
import org.gvsig.tools.dynobject.DynObject;
46
import org.gvsig.tools.observer.Observable;
47
import org.gvsig.tools.observer.Observer;
48
import org.gvsig.tools.service.Manager;
49
import org.gvsig.tools.service.spi.ServiceManager;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
@SuppressWarnings({"rawtypes", "unchecked"})
54
public abstract class AbstractJDynFormField implements JDynFormField{
55

    
56
        protected static final Logger logger = LoggerFactory
57
                        .getLogger(AbstractJDynFormField.class);
58
        
59
        private DynFormSPIManager manager = null;
60
        private DynFormFieldDefinition definition = null;
61
        private JLabel jlabel = null;
62
        private JPanel jlabelpanel = null;
63
        private Set listeners = null;
64
        private JProblemIndicator problemIndicator = null;
65

    
66
        protected DynObject parameters = null;
67
        protected JComponent contents = null;
68

    
69
        private boolean readOnly = false;
70
        private List customActions;
71
        protected boolean emptyToNull = false;
72

    
73

    
74
        public AbstractJDynFormField(DynObject parameters,
75
                        ServiceManager serviceManager) {
76
                this.parameters = parameters;
77
                this.manager = (DynFormSPIManager) serviceManager;
78
                this.definition = this.getDefinition();
79
                this.listeners = new HashSet();
80
                this.readOnly = this.definition.isReadOnly();
81
                this.problemIndicator = this.manager.createProblemIndicator(this);
82
                this.customActions = new ArrayList<Action>();
83
        }
84

    
85
        public abstract void initComponent();
86
        public abstract Object getAssignedValue();
87

    
88
        public Object getParameterValue() {
89
                return this.parameters.getDynValue(DynFormSPIManager.FIELD_VALUE);
90
        }
91

    
92
        public JComponent asJComponent() {
93
                if (this.contents == null) {
94
                        this.initComponent();
95
                        if(!this.customActions.isEmpty()){
96
                                addPopupComponents();
97
                        }
98
                        if( this.readOnly ) {
99
                                this.setReadOnly(readOnly);
100
                        }
101
                }
102
                return this.contents;
103
        }
104

    
105
        public String getName() {
106
                return this.definition.getName();
107
        }
108

    
109
        public String getLabel() {
110
                if(definition.getLabel() != null)
111
                        return definition.getLabel();
112
                else
113
                        return this.getName();
114
        }
115

    
116
        public JComponent getJLabel() {
117
                if (this.jlabel == null) {
118
                        this.jlabel = new JLabel(this.getLabel());
119
                        this.jlabel.setLabelFor(this.contents);
120
                        if( this.getDefinition().isMandatory() ) {
121
                                this.jlabel.setForeground(Color.red.darker());
122
                        }
123
                        this.jlabelpanel = new JPanel();
124
                        this.jlabelpanel.setLayout(new BorderLayout());
125
                        this.jlabelpanel.add(jlabel,BorderLayout.CENTER);
126
                        this.jlabelpanel.add(problemIndicator.asJComponent(), BorderLayout.LINE_END);
127
                }
128
                return this.jlabelpanel;
129
        }
130

    
131
        public DynFormFieldDefinition getDefinition() {
132
                return (DynFormFieldDefinition) this.parameters
133
                                .getDynValue(DynFormSPIManager.FIELD_FIELDDEFINITION);
134
        }
135

    
136
        public Manager getManager() {
137
                return this.manager;
138
        }
139

    
140
        public void addListener(JDynFormFieldListener listener) {
141
                this.listeners.add(listener);
142
        }
143

    
144
        public void removeListener(JDynFormFieldListener listener) {
145
                this.listeners.remove(listener);
146
        }
147
        
148
        protected void fireFieldChangedEvent() {
149
                Iterator it = this.listeners.iterator();
150
                while (it.hasNext()) {
151
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
152
                        try {
153
                                listener.fieldChanged(this);
154
                        } catch (Exception ex) {
155
                                logger.info("Error calling listener " + listener.toString()
156
                                                + "(" + listener.getClass().getName() + ") from "
157
                                                + this.toString() + "(" + this.getClass().getName()
158
                                                + ").", ex);
159
                        }
160
                }
161
        }
162

    
163
        protected void fireFieldEnterEvent() {
164
                Iterator it = this.listeners.iterator();
165
                while (it.hasNext()) {
166
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
167
                        try {
168
                                listener.fieldEnter(this);
169
                        } catch (Exception ex) {
170
                                logger.info("Error calling listener " + listener.toString()
171
                                                + "(" + listener.getClass().getName() + ") from "
172
                                                + this.toString() + "(" + this.getClass().getName()
173
                                                + ").", ex);
174
                        }
175
                }
176
        }
177

    
178
        protected void fireFieldExitEvent() {
179
                Iterator it = this.listeners.iterator();
180
                while (it.hasNext()) {
181
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
182
                        try {
183
                                listener.fieldExit(this);
184
                        } catch (Exception ex) {
185
                                logger.info("Error calling listener " + listener.toString()
186
                                                + "(" + listener.getClass().getName() + ") from "
187
                                                + this.toString() + "(" + this.getClass().getName()
188
                                                + ").", ex);
189
                        }
190
                }
191
        }
192
        
193
        public void fireMessageEvent(String message) {
194
                Iterator it = this.listeners.iterator();
195
                while (it.hasNext()) {
196
                        JDynFormFieldListener listener = (JDynFormFieldListener) it.next();
197
                        try {
198
                                listener.message(this, message);
199
                        } catch (Exception ex) {
200
                                logger.info("Error calling listener " + listener.toString()
201
                                                + "(" + listener.getClass().getName() + ") from "
202
                                                + this.toString() + "(" + this.getClass().getName()
203
                                                + ").", ex);
204
                        }
205
                }
206
        }
207

    
208
        public boolean isReadOnly() {
209
                return this.readOnly ;
210
        }
211
        
212
        public void setReadOnly(boolean readonly) {
213
                // FIXME: Implememtacion por defecto, sobreescribirla en las subclases
214
                // segun convenga para cada componente.
215
                JTextComponent x = null;
216
                                
217
                this.readOnly = readonly;
218
                if( this.contents != null ) {
219
                        if( this.contents instanceof JTextComponent ) {
220
                                x = (JTextComponent) this.contents;
221
                                x.setEditable(!readonly);
222
                        } else if( this.contents instanceof JScrollPane ) {
223
                                try {
224
                                        JViewport port = ((JScrollPane)this.contents).getViewport();
225
                                        x = (JTextComponent)  port.getView();
226
                                        x.setEditable(!readonly);
227
                                } catch(Exception ex) {
228
                                        this.contents.setEnabled(!readOnly);
229
                                }
230
                        } else {
231
                                this.contents.setEnabled(!readOnly);
232
                        }
233
                }
234
        }
235

    
236
        public JProblemIndicator problemIndicator() {
237
                return this.problemIndicator;
238
        }
239

    
240
        public class IllegalFieldValue extends RuntimeException {
241
                
242
                /**
243
                 * 
244
                 */
245
                private static final long serialVersionUID = -4409236610055983438L;
246

    
247
                public IllegalFieldValue(JDynFormField field, String message) {
248
                        super("The value of field '"+field.getLabel()+"' is not valid. " + message);
249
                }
250
        }
251
        
252
        public String toString() {
253
                return super.toString() + "{" + this.getName() + "}";
254
        }
255
        
256
        public boolean isModified() {
257
                boolean modified = false;
258
                try {
259
                        if( !this.isReadOnly() ) {
260
                                Object value = this.getValue();
261
                                if( value == null ) {
262
                                        if( value != this.getAssignedValue() ) {
263
                                                modified = true;
264
                                        }
265
                                } else {
266
                                        if( ! value.equals(this.getAssignedValue()) ) {
267
                                                modified = true;
268
                                        }
269
                                }
270
                        }
271
                } catch(IllegalFieldValue ex) {
272
                        // Si es incorrecto el valor del campo decimos a capom que esta modificado.
273
                        modified = true;
274
                }
275
                return modified;
276
        }
277
        
278
        private void addPopupComponents(){
279
                Iterator it = this.customActions.iterator();
280
                while(it.hasNext()){
281
                        Object obj = it.next();
282
                        if(obj!=null){
283
                                Action act = (Action) obj;
284
                                if(contents instanceof SupportPopupMenu) {
285
                                        DynFormFieldAction sact = new DynFormFieldAction(this, act);
286
                                        ((SupportPopupMenu) this.contents).addActionToPopupMenu(
287
                                                sact.getName(), 
288
                                                sact);
289
                                }
290
                        }else{
291
                                if(contents instanceof SupportPopupMenu) {
292
                                        ((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
293
                                }        
294
                        }
295
                        
296
                }
297
        }
298

    
299
        public void addActionToPopupMenu(String name, Action action) {
300
                if(contents != null){
301
                        if(contents instanceof SupportPopupMenu ) {
302
                                DynFormFieldAction sact = new DynFormFieldAction(this,action);
303
                                ((SupportPopupMenu) this.contents).addActionToPopupMenu(
304
                                                sact.getName(), 
305
                                                sact);
306
                        }
307
                }else{
308
                        this.customActions.add(action);
309
                }
310
        }
311

    
312
        public void addSeparatorToPopupMenu() {
313
                if(contents != null){
314
                        if( contents instanceof SupportPopupMenu ) {
315
                                ((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
316
                        }
317
                }else{
318
                        this.customActions.add(null);
319
                }
320
        }
321
        
322
        public void setTranslateEmptyToNull(boolean emptyToNull) {
323
          this.emptyToNull = emptyToNull;
324
        }
325
        
326
        public boolean translateEmptyToNull() {
327
          return this.emptyToNull;
328
        }
329
        
330
}