gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynform / AbeilleJDynForm.java @ 1409
History | View | Annotate | Download (13.8 KB)
1 |
package org.gvsig.tools.dynform.services.dynform; |
---|---|
2 |
|
3 |
import com.jeta.forms.components.panel.FormPanel; |
4 |
import java.awt.Component; |
5 |
import java.util.ArrayList; |
6 |
import java.util.HashMap; |
7 |
import java.util.Iterator; |
8 |
import java.util.List; |
9 |
import java.util.Map; |
10 |
import javax.swing.ComboBoxModel; |
11 |
import javax.swing.JCheckBox; |
12 |
import javax.swing.JComboBox; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.text.JTextComponent; |
15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.tools.ToolsLocator; |
17 |
import org.gvsig.tools.dataTypes.CoercionException; |
18 |
import org.gvsig.tools.dataTypes.DataTypes; |
19 |
import org.gvsig.tools.dynform.DynFormDefinition; |
20 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
21 |
import org.gvsig.tools.dynform.JDynFormField; |
22 |
import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener; |
23 |
import org.gvsig.tools.dynform.spi.AbstractJDynForm; |
24 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
25 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
26 |
import org.gvsig.tools.dynobject.DynClass; |
27 |
import org.gvsig.tools.dynobject.DynField; |
28 |
import org.gvsig.tools.dynobject.DynField_v2; |
29 |
import org.gvsig.tools.dynobject.DynMethod; |
30 |
import org.gvsig.tools.dynobject.DynObject; |
31 |
import org.gvsig.tools.dynobject.DynObjectManager; |
32 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
33 |
import org.gvsig.tools.dynobject.Tags; |
34 |
import org.gvsig.tools.service.Manager; |
35 |
import org.gvsig.tools.service.Service; |
36 |
import org.gvsig.tools.service.ServiceException; |
37 |
import org.gvsig.tools.service.spi.ServiceManager; |
38 |
|
39 |
public class AbeilleJDynForm extends AbstractJDynForm implements Service, JDynFormFieldListener { |
40 |
|
41 |
private Map components = null; |
42 |
private FormPanel form = null; |
43 |
private String formPathname = null; |
44 |
private final DynFormSPIManager spimanager; |
45 |
|
46 |
public AbeilleJDynForm(ServiceManager manager, DynFormDefinition definition) throws ServiceException { |
47 |
super(((DynFormSPIManager) manager).getDynFormManager(), definition);
|
48 |
this.spimanager = (DynFormSPIManager) manager;
|
49 |
this.components = new HashMap(); |
50 |
formPathname = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM);
|
51 |
if (formPathname == null) { |
52 |
throw new IllegalArgumentException("Need tag 'dynform.abeille.form'."); |
53 |
} |
54 |
this.form = null; |
55 |
} |
56 |
|
57 |
@Override
|
58 |
public Manager getManager() {
|
59 |
return this.spimanager; |
60 |
} |
61 |
|
62 |
@Override
|
63 |
protected JComponent getFieldsContainer() { |
64 |
this.form = new FormPanel(formPathname); |
65 |
|
66 |
List fields = this.getDefinition().getDefinitions(); |
67 |
Iterator it = fields.iterator();
|
68 |
while (it.hasNext()) {
|
69 |
DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
70 |
if (fieldDefinition.isHidden()) {
|
71 |
continue;
|
72 |
} |
73 |
Component component = this.form.getComponentByName("txt" + fieldDefinition.getName()); |
74 |
if (component != null && component instanceof JTextComponent) { |
75 |
JDynFormField jfield = new AbeilleTextJDynFormField(fieldDefinition, (JTextComponent) component); |
76 |
((AbstractJDynFormField) jfield).setForm(this);
|
77 |
jfield.setReadOnly(this.isReadOnly());
|
78 |
jfield.addListener(this);
|
79 |
if (this.isReadOnly()) { |
80 |
jfield.setReadOnly(this.isReadOnly());
|
81 |
} |
82 |
|
83 |
this.components.put(jfield.getName(), jfield);
|
84 |
} else {
|
85 |
component = this.form.getComponentByName("cbo" + fieldDefinition.getName()); |
86 |
if (component != null && component instanceof JComboBox) { |
87 |
} else {
|
88 |
component = this.form.getComponentByName("chk" + fieldDefinition.getName()); |
89 |
if (component != null && component instanceof JCheckBox) { |
90 |
} |
91 |
} |
92 |
|
93 |
} |
94 |
} |
95 |
return this.form; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void setValues(DynObject values) { |
100 |
if (!this.isContentsInitialized()) { |
101 |
this.values = values;
|
102 |
return;
|
103 |
} |
104 |
DynClass def = values.getDynClass(); |
105 |
DynField[] fields = def.getDynFields();
|
106 |
for (int i = 0; i < fields.length; i++) { |
107 |
String name = fields[i].getName();
|
108 |
if (StringUtils.isEmpty(name)) {
|
109 |
logger.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty "); |
110 |
continue;
|
111 |
} |
112 |
JDynFormField jfield = (JDynFormField) this.getField(name);
|
113 |
if (jfield == null) { |
114 |
logger.info("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'."); |
115 |
continue;
|
116 |
} |
117 |
if (values.getDynValue(name) == null) { |
118 |
if (fields[i].getDataType().getType() == DataTypes.LIST) {
|
119 |
try {
|
120 |
if (((DynField_v2) fields[i]).getDynClassOfItems() != null) { |
121 |
values.setDynValue(name, new ArrayList<DynObject>()); |
122 |
} |
123 |
} catch (Exception e) { |
124 |
logger.warn("Problems initializing the DynObject List", e);
|
125 |
} |
126 |
} |
127 |
} |
128 |
jfield.setValue(values.getDynValue(name)); |
129 |
} |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public JDynFormField getField(String fieldName) { |
134 |
JDynFormField field = (JDynFormField) this.components.get(fieldName);
|
135 |
return field;
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public void getValues(DynObject values) { |
140 |
if (values == null) { |
141 |
return;
|
142 |
} |
143 |
DynField[] fields = values.getDynClass().getDynFields();
|
144 |
for (DynField field : fields) {
|
145 |
String name = field.getName();
|
146 |
JDynFormField jfield = (JDynFormField) this.getField(name);
|
147 |
if (jfield != null) { |
148 |
try {
|
149 |
jfield.fetch(values); |
150 |
} catch (Exception ex) { |
151 |
logger.warn("Can't get value of field '" + name + "'.", ex); |
152 |
} |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public Object getValue(String fieldName) { |
159 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
160 |
return field.getValue();
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void setValue(String fieldName, Object value) { |
165 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
166 |
try {
|
167 |
value = field.getDefinition().getDataType().coerce(value); |
168 |
} catch (CoercionException e) {
|
169 |
String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'."; |
170 |
logger.warn(msg, e); |
171 |
throw new RuntimeException(msg, e); |
172 |
} |
173 |
field.setValue(value); |
174 |
} |
175 |
|
176 |
public Iterator getFieldsIterator() { |
177 |
if (!this.isContentsInitialized()) { |
178 |
this.initComponents();
|
179 |
} |
180 |
return this.components.values().iterator(); |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public boolean hasValidValues() { |
185 |
Iterator it = this.getFieldsIterator(); |
186 |
while (it.hasNext()) {
|
187 |
JDynFormField jfield = (JDynFormField) it.next(); |
188 |
if (!jfield.hasValidValue()) {
|
189 |
return false; |
190 |
} |
191 |
} |
192 |
return true; |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public boolean hasValidValues(List<String> fieldsName) { |
197 |
if (fieldsName == null) { |
198 |
fieldsName = new ArrayList<>(); |
199 |
} |
200 |
Iterator it = this.getFieldsIterator(); |
201 |
while (it.hasNext()) {
|
202 |
JDynFormField jfield = (JDynFormField) it.next(); |
203 |
if (!jfield.hasValidValue()) {
|
204 |
fieldsName.add(jfield.getName()); |
205 |
} |
206 |
} |
207 |
return fieldsName.isEmpty();
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public boolean isModified() { |
212 |
Iterator it = this.getFieldsIterator(); |
213 |
while (it.hasNext()) {
|
214 |
JDynFormField jfield = (JDynFormField) it.next(); |
215 |
if (jfield.isModified()) {
|
216 |
return true; |
217 |
} |
218 |
} |
219 |
return false; |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public void clear() { |
224 |
Iterator it = this.components.entrySet().iterator(); |
225 |
while (it.hasNext()) {
|
226 |
Map.Entry entry = (Map.Entry) it.next(); |
227 |
((JDynFormField) (entry.getValue())).clear(); |
228 |
} |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void fieldEnter(JDynFormField field) { |
233 |
message(field.getDefinition().getDescription()); |
234 |
} |
235 |
|
236 |
@Override
|
237 |
public void fieldExit(JDynFormField field) { |
238 |
message(); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void fieldChanged(JDynFormField field) { |
243 |
fireFieldChangeEvent(field); |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public void message(JDynFormField field, String message) { |
248 |
message(message); |
249 |
} |
250 |
|
251 |
private class AbeilleTextJDynFormField extends AbstractJDynFormField { |
252 |
|
253 |
private final JTextComponent text; |
254 |
private final DynFormFieldDefinition fieldDefinition; |
255 |
|
256 |
private AbeilleTextJDynFormField(DynFormFieldDefinition fieldDefinition, JTextComponent text) { |
257 |
super(null, AbeilleJDynForm.this.getServiceManager()); |
258 |
this.text = text;
|
259 |
this.fieldDefinition = fieldDefinition;
|
260 |
} |
261 |
|
262 |
@Override
|
263 |
public Object getParameterValue() { |
264 |
return null; |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public DynFormFieldDefinition getDefinition() {
|
269 |
return this.fieldDefinition; |
270 |
} |
271 |
|
272 |
@Override
|
273 |
public void initComponent() { |
274 |
// Do nothing
|
275 |
} |
276 |
|
277 |
@Override
|
278 |
public Object getAssignedValue() { |
279 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
280 |
} |
281 |
|
282 |
@Override
|
283 |
public boolean hasValidValue() { |
284 |
try {
|
285 |
String s = this.text.getText(); |
286 |
Object v = this.fieldDefinition.getDataType().coerce(s); |
287 |
return true; |
288 |
} catch (CoercionException ex) {
|
289 |
return false; |
290 |
} |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public void setValue(Object value) { |
295 |
if (value == null) { |
296 |
this.text.setText(""); |
297 |
} else {
|
298 |
this.text.setText(value.toString());
|
299 |
} |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public Object getValue() { |
304 |
try {
|
305 |
String s = this.text.getText(); |
306 |
return this.fieldDefinition.getDataType().coerce(s); |
307 |
} catch (CoercionException ex) {
|
308 |
return null; |
309 |
} |
310 |
} |
311 |
|
312 |
} |
313 |
|
314 |
private class AbeilleComboJDynFormField extends AbstractJDynFormField { |
315 |
|
316 |
private final JComboBox combo; |
317 |
private final DynFormFieldDefinition fieldDefinition; |
318 |
|
319 |
private AbeilleComboJDynFormField(DynFormFieldDefinition fieldDefinition, JComboBox combo) { |
320 |
super(null, AbeilleJDynForm.this.getServiceManager()); |
321 |
this.combo = combo;
|
322 |
this.fieldDefinition = fieldDefinition;
|
323 |
ComboBoxModel model = null; |
324 |
try {
|
325 |
Tags tags = this.fieldDefinition.getTags();
|
326 |
String createComboModelMethodName = this.getTagValueAsString("dynform.abeille.comboModel", null); |
327 |
if (createComboModelMethodName != null) { |
328 |
DynObjectManager dynobjectManager = ToolsLocator.getDynObjectManager(); |
329 |
DynMethod createComboModelMethod = dynobjectManager.getDynMethod(createComboModelMethodName); |
330 |
if (createComboModelMethod != null) { |
331 |
model = (ComboBoxModel) createComboModelMethod.invoke(null, new Object[]{this.fieldDefinition}); |
332 |
} |
333 |
} |
334 |
} catch (Exception ex) { |
335 |
// TODO: log error
|
336 |
} |
337 |
if (model != null) { |
338 |
this.combo.setModel(model);
|
339 |
} |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public Object getParameterValue() { |
344 |
return null; |
345 |
} |
346 |
|
347 |
@Override
|
348 |
public DynFormFieldDefinition getDefinition() {
|
349 |
return this.fieldDefinition; |
350 |
} |
351 |
|
352 |
@Override
|
353 |
public void initComponent() { |
354 |
// Do nothing
|
355 |
} |
356 |
|
357 |
@Override
|
358 |
public Object getAssignedValue() { |
359 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
360 |
} |
361 |
|
362 |
@Override
|
363 |
public boolean hasValidValue() { |
364 |
try {
|
365 |
DynObjectValueItem value = (DynObjectValueItem) this.combo.getSelectedItem();
|
366 |
if( value == null ) { |
367 |
return true; |
368 |
} |
369 |
this.fieldDefinition.getDataType().coerce(value.getValue());
|
370 |
return true; |
371 |
} catch (CoercionException ex) {
|
372 |
return false; |
373 |
} |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void setValue(Object value) { |
378 |
if (value == null) { |
379 |
this.combo.setSelectedIndex(-1); |
380 |
} else {
|
381 |
ComboBoxModel model = this.combo.getModel(); |
382 |
for( int i=0; i<model.getSize(); i++) { |
383 |
DynObjectValueItem item = (DynObjectValueItem)model.getElementAt(i); |
384 |
if( value.equals(item.getValue()) ) {
|
385 |
this.combo.setSelectedIndex(i);
|
386 |
return;
|
387 |
} |
388 |
} |
389 |
this.combo.setSelectedIndex(-1); |
390 |
} |
391 |
} |
392 |
|
393 |
@Override
|
394 |
public Object getValue() { |
395 |
try {
|
396 |
DynObjectValueItem value = (DynObjectValueItem) this.combo.getSelectedItem();
|
397 |
if( value == null ) { |
398 |
return null; |
399 |
} |
400 |
return this.fieldDefinition.getDataType().coerce(value.getValue()); |
401 |
} catch (CoercionException ex) {
|
402 |
return null; |
403 |
} |
404 |
} |
405 |
|
406 |
} |
407 |
} |