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 | 165 | cmartin | /* 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 | 296 | cmartin | import java.util.ArrayList; |
37 | import java.util.List; |
||
38 | |||
39 | 165 | cmartin | import org.gvsig.tools.dynobject.DynField; |
40 | 281 | cmartin | import org.gvsig.tools.dynobject.DynObject; |
41 | 574 | cmartin | import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
42 | 281 | cmartin | 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 | 165 | cmartin | |
46 | /**
|
||
47 | *
|
||
48 | 281 | cmartin | * This interfaces provides with the logic necessary to create
|
49 | 574 | cmartin | * 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 | 281 | cmartin | *
|
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 </a> -
|
||
57 | * gvSIG Team
|
||
58 | * @version $Id$
|
||
59 | *
|
||
60 | 165 | cmartin | */
|
61 | public abstract class AbstractJDynField { |
||
62 | |||
63 | 281 | cmartin | private ValueField parent;
|
64 | 165 | cmartin | |
65 | 296 | cmartin | protected final List<ValueChangedListener> listeners; |
66 | |||
67 | 545 | cmartin | private boolean writable; |
68 | |||
69 | private DynField definition;
|
||
70 | |||
71 | 574 | cmartin | private DynFieldValidateException validateException;
|
72 | |||
73 | 545 | cmartin | public AbstractJDynField(DynField definition, ValueField parent, boolean writable) { |
74 | 281 | cmartin | this.parent = parent;
|
75 | 545 | cmartin | this.definition = definition;
|
76 | 296 | cmartin | listeners = new ArrayList<ValueChangedListener>(); |
77 | 545 | cmartin | this.writable = writable;
|
78 | 165 | cmartin | } |
79 | 545 | cmartin | |
80 | protected boolean isReadOnly(){ |
||
81 | return !writable;
|
||
82 | } |
||
83 | |||
84 | 165 | cmartin | |
85 | 545 | cmartin | public DynField getParentDynField() {
|
86 | return parent.getDynField();
|
||
87 | } |
||
88 | |||
89 | 298 | cmartin | /*
|
90 | * (non-Javadoc)
|
||
91 | *
|
||
92 | 500 | cmartin | * @see org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#
|
93 | 298 | cmartin | * 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 | 500 | cmartin | |
106 | public String getValidationMessage(){ |
||
107 | 574 | cmartin | return this.getValueField().getValidationMessage(); |
108 | 500 | cmartin | } |
109 | |||
110 | 165 | cmartin | /**
|
111 | 298 | cmartin | * The {@link JDynFieldComponent} can use this function to add any code
|
112 | * necessary
|
||
113 | * once all swing components have been initialized.
|
||
114 | 281 | cmartin | */
|
115 | 298 | cmartin | 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 | 281 | cmartin | } |
133 | 165 | cmartin | |
134 | /**
|
||
135 | 298 | cmartin | * Returns the {@link DynField} being rendered.
|
136 | *
|
||
137 | * @return the {@link DynField}
|
||
138 | 281 | cmartin | */
|
139 | 298 | cmartin | public DynField getDynField() {
|
140 | 545 | cmartin | if (definition!=null){ |
141 | return definition;
|
||
142 | } |
||
143 | 298 | cmartin | return getValueField().getDynField();
|
144 | } |
||
145 | 545 | cmartin | |
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 | 281 | cmartin | |
159 | 545 | cmartin | |
160 | 281 | cmartin | /**
|
161 | 298 | cmartin | * Gets the current value of the {@link ValueField} object.
|
162 | *
|
||
163 | * @return
|
||
164 | * the current value of the {@link ValueField} object.
|
||
165 | 281 | cmartin | */
|
166 | 298 | cmartin | public Object getFieldValue() { |
167 | return this.getValueField().getFieldValue(); |
||
168 | } |
||
169 | 281 | cmartin | |
170 | /**
|
||
171 | * Checks if the {@link ValueField} has an inputed value; if not, then it
|
||
172 | * asks
|
||
173 | * for its default value.
|
||
174 | 165 | cmartin | *
|
175 | 281 | cmartin | * @return
|
176 | * the initial value of the {@link ValueField}. It can be
|
||
177 | * <b>null</b>.
|
||
178 | 165 | cmartin | */
|
179 | 281 | cmartin | public Object getInitialValue() { |
180 | Object value = getFieldValue();
|
||
181 | if (value == null) { |
||
182 | value = getDefaultFieldValue(); |
||
183 | } |
||
184 | return value;
|
||
185 | 165 | cmartin | } |
186 | |||
187 | 281 | cmartin | /**
|
188 | 298 | cmartin | * Gets the {@link ValueField} object.
|
189 | *
|
||
190 | * @return
|
||
191 | * the {@link ValueField} object.
|
||
192 | 281 | cmartin | */
|
193 | 298 | cmartin | private ValueField getValueField() {
|
194 | return this.parent; |
||
195 | } |
||
196 | 165 | cmartin | |
197 | 281 | cmartin | /**
|
198 | 298 | cmartin | * Inits the JDynFieldComponent logic to create its components and check its
|
199 | * values.
|
||
200 | 281 | cmartin | */
|
201 | 298 | cmartin | protected void init() { |
202 | this.initData();
|
||
203 | this.initUI();
|
||
204 | 545 | cmartin | if (isReadOnly()) {
|
205 | 298 | cmartin | this.setReadOnly();
|
206 | 631 | cmartin | } |
207 | 477 | jjdelcerro | this.setValue(getInitialValue());
|
208 | 298 | cmartin | this.afterUI();
|
209 | 270 | cmartin | } |
210 | 165 | cmartin | |
211 | 281 | cmartin | /**
|
212 | 298 | cmartin | * JDynFieldComponents can use this function to initialize any
|
213 | * data containers before the swing components are created.
|
||
214 | 281 | cmartin | */
|
215 | 298 | cmartin | protected abstract void initData(); |
216 | 269 | cmartin | |
217 | 281 | cmartin | /**
|
218 | 298 | cmartin | * {@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 | 477 | jjdelcerro | public void setFieldValue(Object value) { |
230 | 298 | cmartin | this.getValueField().setFieldValue(value);
|
231 | } |
||
232 | |||
233 | protected abstract void setJDynFieldComponentListeners(); |
||
234 | |||
235 | /**
|
||
236 | 281 | cmartin | * 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 | 269 | cmartin | |
245 | 270 | cmartin | /**
|
246 | 281 | cmartin | * 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 | 165 | cmartin | |
255 | /**
|
||
256 | 298 | cmartin | * The {@link JDynFieldComponent} can implement this function to set the
|
257 | * swing
|
||
258 | * components that represent this {@link DynField} to readOnly mode.
|
||
259 | 281 | cmartin | */
|
260 | 298 | cmartin | protected abstract void setReadOnly(); |
261 | 165 | cmartin | |
262 | 582 | cmartin | /**
|
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 | 298 | cmartin | setNullValue(); |
283 | } else {
|
||
284 | 582 | cmartin | setNonNullValue(newValue); |
285 | 298 | cmartin | } |
286 | this.fireValueChangedEvent();
|
||
287 | 281 | cmartin | } |
288 | 574 | cmartin | |
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 | 165 | cmartin | } |