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

History | View | Annotate | Download (7.49 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.swing.api.dynobject.ValueChangedListener;
42
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
43
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
44

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

    
66
    private ValueField parent;
67

    
68
    protected final List<ValueChangedListener> listeners;
69

    
70
    public AbstractJDynField(ValueField parent) {
71
        this.parent = parent;
72
        listeners = new ArrayList<ValueChangedListener>();
73
    }
74

    
75
    /*
76
     * (non-Javadoc)
77
     * 
78
     * @seeorg.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#
79
     * addValueChangedListener
80
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
81
     */
82
    public void addValueChangedListener(ValueChangedListener listener) {
83
        // If it was the first item, we need to
84
        // initialize Component listeners
85
        // add listener to the dynfield set of listeners
86
        listeners.add(listener);
87
        // set supplementary listeners.
88
        this.setJDynFieldComponentListeners();
89
    }
90

    
91
    /**
92
     * The {@link JDynFieldComponent} can use this function to add any code
93
     * necessary
94
     * once all swing components have been initialized.
95
     */
96
    protected abstract void afterUI();
97

    
98
    /**
99
     * Then all {@link ValueChangedListener}s are fired to alert them
100
     * to check if the value has changed via the {@link getValue()} function of
101
     * the {@link JDynFieldComponent}.
102
     */
103
    public abstract void fireValueChangedEvent();
104

    
105
    /**
106
     * Gets the default value of the {@link ValueField} object.
107
     * 
108
     * @return
109
     *         the default value of the {@link ValueField} object.
110
     */
111
    public Object getDefaultFieldValue() {
112
        return this.getValueField().getDefaultFieldValue();
113
    }
114

    
115
    /**
116
     * Returns the {@link DynField} being rendered.
117
     * 
118
     * @return the {@link DynField}
119
     */
120
    public DynField getDynField() {
121
        return getValueField().getDynField();
122
    }
123

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

    
134
    /**
135
     * Checks if the {@link ValueField} has an inputed value; if not, then it
136
     * asks
137
     * for its default value.
138
     * 
139
     * @return
140
     *         the initial value of the {@link ValueField}. It can be
141
     *         <b>null</b>.
142
     */
143
    public Object getInitialValue() {
144
        Object value = getFieldValue();
145
        if (value == null) {
146
            value = getDefaultFieldValue();
147
        }
148
        return value;
149
    }
150

    
151
    /**
152
     * Gets the {@link ValueField} object.
153
     * 
154
     * @return
155
     *         the {@link ValueField} object.
156
     */
157
    private ValueField getValueField() {
158
        return this.parent;
159
    }
160

    
161
    /**
162
     * Inits the JDynFieldComponent logic to create its components and check its
163
     * values.
164
     */
165
    protected void init() {
166
        this.initData();
167
        this.initUI();
168
        if (this.getDynField().isReadOnly()) {
169
            this.setReadOnly();
170
        }
171
        this.setValue(getInitialValue());
172
        this.afterUI();
173
    }
174

    
175
    /**
176
     * JDynFieldComponents can use this function to initialize any
177
     * data containers before the swing components are created.
178
     */
179
    protected abstract void initData();
180

    
181
    /**
182
     * {@link JDynFieldComponent}s can use this function to initialized any
183
     * swing components to be used afterwards, and specify their
184
     * default settings.
185
     */
186
    protected abstract void initUI();
187

    
188
    /**
189
     * Sets the current value of the {@link JDynFieldComponent} to the
190
     * {@link DynObject} value via the {@link ValueField} object.
191
     * 
192
     */
193
    public void setFieldValue(Object value) {
194
        this.getValueField().setFieldValue(value);
195
    }
196

    
197
    protected abstract void setJDynFieldComponentListeners();
198

    
199
    /**
200
     * The {@link JDynFieldComponent} can use this function to set a <b>non
201
     * null</b> value
202
     * into the correspondent swing component.
203
     * 
204
     * @param value
205
     *            the value to be set into the swing component.
206
     */
207
    protected abstract void setNonNullValue(Object value);
208

    
209
    /**
210
     * The {@link JDynFieldComponent} can use this function to set a <b>null</b>
211
     * value
212
     * into the correspondent swing component.
213
     * 
214
     * @param value
215
     *            the value to be set into the swing component.
216
     */
217
    protected abstract void setNullValue();
218

    
219
    /**
220
     * The {@link JDynFieldComponent} can implement this function to set the
221
     * swing
222
     * components that represent this {@link DynField} to readOnly mode.
223
     */
224
    protected abstract void setReadOnly();
225

    
226
    /**
227
     * Sets the value to the {@link JDynFieldComponent}. With this logic, it
228
     * checks first
229
     * if it is a null value or not. Then all {@link ValueChangedListener}s are
230
     * fired
231
     * to alert them to check if the value has changed via the {@link
232
     * getValue()} function.
233
     * 
234
     * @param value
235
     *            the value to be set into the swing component.
236
     */
237
    public void setValue(Object value) {
238
        if (value == null) {
239
            setNullValue();
240
        } else {
241
            setNonNullValue(value);
242
        }
243
        this.fireValueChangedEvent();
244
    }
245

    
246
}