Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.spi / src / main / java / org / gvsig / tools / swing / spi / AbstractJDynField.java @ 631

History | View | Annotate | Download (9.21 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
/*
23
 * AUTHORS (In addition to CIT):
24
 * 2010 Institute of New Imaging Technologies (INIT): 
25
 *   http://www.init.uji.es
26
 * Geographic Information research group: 
27
 *   http://www.geoinfo.uji.es
28
 * Universitat Jaume I, Spain
29
 */
30

    
31
/**
32
 * 
33
 */
34
package org.gvsig.tools.swing.spi;
35

    
36
import java.util.ArrayList;
37
import java.util.List;
38

    
39
import org.gvsig.tools.dynobject.DynField;
40
import org.gvsig.tools.dynobject.DynObject;
41
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
42
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
43
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
44
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
45

    
46
/**
47
 * 
48
 * This interfaces provides with the logic necessary to create
49
 * JDynFieldComponents. First, the initial values are extracted. Second, the
50
 * user interface components are created.Third, the initial value is set to the
51
 * right component, and last but not least, a fireEventChanged event is raised
52
 * to inform all Listeners that the value has changed. Some additional functions
53
 * related to DynField attributes are also implemented.
54
 * 
55
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
56
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n&nbsp;</a> -
57
 *         gvSIG Team
58
 * @version $Id$
59
 * 
60
 */
61
public abstract class AbstractJDynField {
62

    
63
    private ValueField parent;
64

    
65
    protected final List<ValueChangedListener> listeners;
66

    
67
    private boolean writable;
68

    
69
    private DynField definition;
70

    
71
        private DynFieldValidateException validateException;
72

    
73
        public AbstractJDynField(DynField definition, ValueField parent, boolean writable) {
74
        this.parent = parent;
75
        this.definition = definition;
76
        listeners = new ArrayList<ValueChangedListener>();
77
        this.writable = writable;
78
    }
79
        
80
        protected boolean isReadOnly(){
81
            return !writable;
82
        }
83
        
84

    
85
    public DynField getParentDynField() {
86
        return parent.getDynField();
87
    }
88

    
89
    /*
90
     * (non-Javadoc)
91
     * 
92
     * @see org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#
93
     * addValueChangedListener
94
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
95
     */
96
    public void addValueChangedListener(ValueChangedListener listener) {
97
        // If it was the first item, we need to
98
        // initialize Component listeners
99
        // add listener to the dynfield set of listeners
100
        listeners.add(listener);
101
        // set supplementary listeners.
102
        this.setJDynFieldComponentListeners();
103
    }
104

    
105

    
106
    public String getValidationMessage(){
107
                return this.getValueField().getValidationMessage();
108
    }
109
    
110
    /**
111
     * The {@link JDynFieldComponent} can use this function to add any code
112
     * necessary
113
     * once all swing components have been initialized.
114
     */
115
    protected abstract void afterUI();
116

    
117
    /**
118
     * Then all {@link ValueChangedListener}s are fired to alert them
119
     * to check if the value has changed via the {@link getValue()} function of
120
     * the {@link JDynFieldComponent}.
121
     */
122
    public abstract void fireValueChangedEvent();
123

    
124
    /**
125
     * Gets the default value of the {@link ValueField} object.
126
     * 
127
     * @return
128
     *         the default value of the {@link ValueField} object.
129
     */
130
    public Object getDefaultFieldValue() {
131
        return this.getValueField().getDefaultFieldValue();
132
    }
133

    
134
    /**
135
     * Returns the {@link DynField} being rendered.
136
     * 
137
     * @return the {@link DynField}
138
     */
139
    public DynField getDynField() {
140
        if (definition!=null){
141
            return definition;
142
        }
143
        return getValueField().getDynField();
144
    }
145
    
146
    /*
147
     * (non-Javadoc)
148
     * 
149
     * @see
150
     * 
151
     * 
152
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isMandatory
153
     * ()
154
     */
155
    public boolean isMandatory() {
156
        return getDynField().isMandatory();
157
    }
158

    
159

    
160
    /**
161
     * Gets the current value of the {@link ValueField} object.
162
     * 
163
     * @return
164
     *         the current value of the {@link ValueField} object.
165
     */
166
    public Object getFieldValue() {
167
        return this.getValueField().getFieldValue();
168
    }
169

    
170
    /**
171
     * Checks if the {@link ValueField} has an inputed value; if not, then it
172
     * asks
173
     * for its default value.
174
     * 
175
     * @return
176
     *         the initial value of the {@link ValueField}. It can be
177
     *         <b>null</b>.
178
     */
179
    public Object getInitialValue() {
180
        Object value = getFieldValue();
181
        if (value == null) {
182
            value = getDefaultFieldValue();
183
        }
184
        return value;
185
    }
186

    
187
    /**
188
     * Gets the {@link ValueField} object.
189
     * 
190
     * @return
191
     *         the {@link ValueField} object.
192
     */
193
    private ValueField getValueField() {
194
        return this.parent;
195
    }
196

    
197
    /**
198
     * Inits the JDynFieldComponent logic to create its components and check its
199
     * values.
200
     */
201
    protected void init() {
202
        this.initData();
203
        this.initUI();
204
        if (isReadOnly()) {
205
            this.setReadOnly();
206
                }
207
        this.setValue(getInitialValue());
208
        this.afterUI();
209
    }
210

    
211
    /**
212
     * JDynFieldComponents can use this function to initialize any
213
     * data containers before the swing components are created.
214
     */
215
    protected abstract void initData();
216

    
217
    /**
218
     * {@link JDynFieldComponent}s can use this function to initialized any
219
     * swing components to be used afterwards, and specify their
220
     * default settings.
221
     */
222
    protected abstract void initUI();
223

    
224
    /**
225
     * Sets the current value of the {@link JDynFieldComponent} to the
226
     * {@link DynObject} value via the {@link ValueField} object.
227
     * 
228
     */
229
    public void setFieldValue(Object value) {
230
        this.getValueField().setFieldValue(value);
231
    }
232

    
233
    protected abstract void setJDynFieldComponentListeners();
234

    
235
    /**
236
     * The {@link JDynFieldComponent} can use this function to set a <b>non
237
     * null</b> value
238
     * into the correspondent swing component.
239
     * 
240
     * @param value
241
     *            the value to be set into the swing component.
242
     */
243
    protected abstract void setNonNullValue(Object value);
244

    
245
    /**
246
     * The {@link JDynFieldComponent} can use this function to set a <b>null</b>
247
     * value
248
     * into the correspondent swing component.
249
     * 
250
     * @param value
251
     *            the value to be set into the swing component.
252
     */
253
    protected abstract void setNullValue();
254

    
255
    /**
256
     * The {@link JDynFieldComponent} can implement this function to set the
257
     * swing
258
     * components that represent this {@link DynField} to readOnly mode.
259
     */
260
    protected abstract void setReadOnly();
261

    
262
        /**
263
         * Sets the value to the {@link JDynFieldComponent}. With this logic, it
264
         * checks first if it is a null value or not. Then all
265
         * {@link ValueChangedListener}s are fired to alert them to check if the
266
         * value has changed via the {@link getValue()} function.
267
         * 
268
         * @param newValue
269
         *            the value to be set into the swing component.
270
         */
271
    public void setValue(Object newValue) {
272
                // oldValue and newValue is null.
273
                if ((this.getValue() == null) && (newValue == null)) {
274
                        return;
275
                }
276
                // If there's an oldValue not null and is equal to newValue, do
277
                // Nothing.
278
                if ((this.getValue() != null) && (this.getValue().equals(newValue))) {
279
                        return;
280
                }
281
                if (newValue == null) {
282
            setNullValue();
283
        } else {
284
                        setNonNullValue(newValue);
285
        }
286
        this.fireValueChangedEvent();
287
    }
288

    
289
        /**
290
         * @param value
291
         */
292
        protected boolean validate(Object value) {
293
                try {
294
                        this.getDynField().validate(value);
295
                        validateException = null;
296
                        return true;
297
                } catch (DynFieldValidateException e) {
298
                        validateException = e;
299
                        return false;
300
                }
301
        }
302

    
303
        /**
304
         * Gets the current validation exception, if any
305
         * 
306
         * @return
307
         */
308
        protected DynFieldValidateException getValidateException() {
309
                if (validateException == null) {
310
                        validate(this.getValue());
311
                }
312
                return validateException;
313
        }
314

    
315
        /**
316
         * Gets the current value of the component
317
         * 
318
         * @return
319
         */
320
        public abstract Object getValue();
321

    
322
}