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

History | View | Annotate | Download (6.66 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 org.gvsig.tools.dynobject.DynField;
37
import org.gvsig.tools.dynobject.DynObject;
38
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
39
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
40
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
41

    
42
/**
43
 * 
44
 * This interfaces provides with the logic necessary to create
45
 * JDynFieldComponents. First,
46
 * the initial values are extracted. Second, the user interface components are
47
 * created.
48
 * Third, the initial value is set to the right component, and last but not
49
 * least, a
50
 * fireEventChanged event is raised to inform all Listeners that the value has
51
 * changed.
52
 * Some additional functions related to DynField attributes are also
53
 * 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
    public AbstractJDynField(ValueField parent) {
66
        this.parent = parent;
67
    }
68

    
69
    /**
70
     * Inits the JDynFieldComponent logic to create its components and check its
71
     * values.
72
     */
73
    protected void init() {
74
        this.initData();
75
        this.initUI();
76
        if (this.getDynField().isReadOnly())
77
            this.setReadOnly();
78
        this.setValue(getInitialValue());
79
        this.afterUI();
80
    }
81

    
82
    /**
83
     * JDynFieldComponents can use this function to initialize any
84
     * data containers before the swing components are created.
85
     */
86
    protected abstract void initData();
87

    
88
    /**
89
     * {@link JDynFieldComponent}s can use this function to initialized any
90
     * swing components to be used afterwards, and specify their
91
     * default settings.
92
     */
93
    protected abstract void initUI();
94

    
95
    /**
96
     * Checks if the {@link ValueField} has an inputed value; if not, then it
97
     * asks
98
     * for its default value.
99
     * 
100
     * @return
101
     *         the initial value of the {@link ValueField}. It can be
102
     *         <b>null</b>.
103
     */
104
    public Object getInitialValue() {
105
        Object value = getFieldValue();
106
        if (value == null) {
107
            value = getDefaultFieldValue();
108
        }
109
        return value;
110
    }
111

    
112
    /**
113
     * The {@link JDynFieldComponent} can implement this function to set the
114
     * swing
115
     * components that represent this {@link DynField} to readOnly mode.
116
     */
117
    protected abstract void setReadOnly();
118

    
119
    /**
120
     * Sets the value to the {@link JDynFieldComponent}. With this logic, it
121
     * checks first
122
     * if it is a null value or not. Then all {@link ValueChangedListener}s are
123
     * fired
124
     * to alert them to check if the value has changed via the {@link
125
     * getValue()} function.
126
     * 
127
     * @param value
128
     *            the value to be set into the swing component.
129
     */
130
    public void setValue(Object value) {
131
        if (value == null)
132
            setNullValue();
133
        else
134
            setNonNullValue(value);
135
        this.fireValueChangedEvent();
136
    }
137

    
138
    /**
139
     * The {@link JDynFieldComponent} can use this function to add any code
140
     * necessary
141
     * once all swing components have been initialized.
142
     */
143
    protected abstract void afterUI();
144

    
145
    /**
146
     * The {@link JDynFieldComponent} can use this function to set a <b>non
147
     * null</b> value
148
     * into the correspondent swing component.
149
     * 
150
     * @param value
151
     *            the value to be set into the swing component.
152
     */
153
    protected abstract void setNonNullValue(Object value);
154

    
155
    /**
156
     * The {@link JDynFieldComponent} can use this function to set a <b>null</b>
157
     * value
158
     * into the correspondent swing component.
159
     * 
160
     * @param value
161
     *            the value to be set into the swing component.
162
     */
163
    protected abstract void setNullValue();
164

    
165
    /**
166
     * Then all {@link ValueChangedListener}s are fired to alert them
167
     * to check if the value has changed via the {@link getValue()} function of
168
     * the {@link JDynFieldComponent}.
169
     */
170
    public abstract void fireValueChangedEvent();
171

    
172
    /**
173
     * Returns the {@link DynField} being rendered.
174
     * 
175
     * @return the {@link DynField}
176
     */
177
    public DynField getDynField() {
178
        return getValueField().getDynField();
179
    }
180

    
181
    /**
182
     * Gets the {@link ValueField} object.
183
     * 
184
     * @return
185
     *         the {@link ValueField} object.
186
     */
187
    private ValueField getValueField() {
188
        return this.parent;
189
    }
190

    
191
    /**
192
     * Gets the default value of the {@link ValueField} object.
193
     * 
194
     * @return
195
     *         the default value of the {@link ValueField} object.
196
     */
197
    public Object getDefaultFieldValue() {
198
        return this.getValueField().getDefaultFieldValue();
199
    }
200

    
201
    /**
202
     * Gets the current value of the {@link ValueField} object.
203
     * 
204
     * @return
205
     *         the current value of the {@link ValueField} object.
206
     */
207
    public Object getFieldValue() {
208
        return this.getValueField().getFieldValue();
209
    }
210

    
211
    /**
212
     * Sets the current value of the {@link JDynFieldComponent} to the
213
     * {@link DynObject} value via the {@link ValueField} object.
214
     * 
215
     */
216
    public void setFieldValue(Object value) {
217
        this.getValueField().setFieldValue(value);
218
    }
219

    
220
}