Revision 281 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
AbstractJDynField.java | ||
---|---|---|
34 | 34 |
package org.gvsig.tools.swing.spi; |
35 | 35 |
|
36 | 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; |
|
37 | 41 |
|
38 | 42 |
/** |
39 |
* @author <a href="mailto:reinhold@uji.es">cmartin</a> |
|
40 | 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 </a> - |
|
57 |
* gvSIG Team |
|
58 |
* @version $Id$ |
|
59 |
* |
|
41 | 60 |
*/ |
42 | 61 |
public abstract class AbstractJDynField { |
43 | 62 |
|
44 |
private final DynField dynField;
|
|
63 |
private ValueField parent;
|
|
45 | 64 |
|
46 |
public AbstractJDynField(DynField dynField) {
|
|
47 |
this.dynField = dynField;
|
|
65 |
public AbstractJDynField(ValueField parent) {
|
|
66 |
this.parent = parent;
|
|
48 | 67 |
} |
49 | 68 |
|
50 | 69 |
/** |
51 |
* |
|
52 |
*/ |
|
53 |
abstract protected void afterUI(); |
|
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 |
} |
|
54 | 81 |
|
55 | 82 |
/** |
56 |
* Returns the {@link DynField} being rendered. |
|
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. |
|
57 | 99 |
* |
58 |
* @return the DynField |
|
100 |
* @return |
|
101 |
* the initial value of the {@link ValueField}. It can be |
|
102 |
* <b>null</b>. |
|
59 | 103 |
*/ |
60 |
public DynField getDynField() { |
|
61 |
return dynField; |
|
104 |
public Object getInitialValue() { |
|
105 |
Object value = getFieldValue(); |
|
106 |
if (value == null) { |
|
107 |
value = getDefaultFieldValue(); |
|
108 |
} |
|
109 |
return value; |
|
62 | 110 |
} |
63 | 111 |
|
64 |
abstract protected Object getFieldValue(); |
|
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(); |
|
65 | 118 |
|
66 |
protected void init() { |
|
67 |
this.initData(); |
|
68 |
this.initUI(); |
|
69 |
if (this.getDynField().isReadOnly()) |
|
70 |
this.setReadOnly(); |
|
71 |
Object value = this.getFieldValue(); |
|
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) { |
|
72 | 131 |
if (value == null) |
73 |
value = this.getDefaultFieldValue(); |
|
74 |
this.setValue(value); |
|
132 |
setNullValue(); |
|
133 |
else |
|
134 |
setNonNullValue(value); |
|
75 | 135 |
this.fireValueChangedEvent(); |
76 |
this.afterUI(); |
|
77 | 136 |
} |
78 | 137 |
|
79 |
abstract public void fireValueChangedEvent(); |
|
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(); |
|
80 | 144 |
|
81 |
abstract protected Object getDefaultFieldValue(); |
|
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); |
|
82 | 154 |
|
83 | 155 |
/** |
84 |
* |
|
85 |
*/ |
|
86 |
abstract protected void initData(); |
|
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(); |
|
87 | 164 |
|
88 | 165 |
/** |
89 |
* |
|
90 |
*/ |
|
91 |
abstract protected void initUI(); |
|
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(); |
|
92 | 171 |
|
93 | 172 |
/** |
94 |
* |
|
95 |
*/ |
|
96 |
abstract protected void setReadOnly(); |
|
173 |
* Returns the {@link DynField} being rendered. |
|
174 |
* |
|
175 |
* @return the {@link DynField} |
|
176 |
*/ |
|
177 |
public DynField getDynField() { |
|
178 |
return getValueField().getDynField(); |
|
179 |
} |
|
97 | 180 |
|
98 |
abstract protected void setValue(Object value); |
|
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 |
} |
|
99 | 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 |
|
|
100 | 220 |
} |
Also available in: Unified diff