Revision 1409
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 | ||
---|---|---|
1 |
|
|
2 | 1 |
package org.gvsig.tools.dynform.services.dynform; |
3 | 2 |
|
4 | 3 |
import com.jeta.forms.components.panel.FormPanel; |
... | ... | |
8 | 7 |
import java.util.Iterator; |
9 | 8 |
import java.util.List; |
10 | 9 |
import java.util.Map; |
10 |
import javax.swing.ComboBoxModel; |
|
11 |
import javax.swing.JCheckBox; |
|
12 |
import javax.swing.JComboBox; |
|
11 | 13 |
import javax.swing.JComponent; |
12 | 14 |
import javax.swing.text.JTextComponent; |
13 | 15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.tools.ToolsLocator; |
|
14 | 17 |
import org.gvsig.tools.dataTypes.CoercionException; |
15 | 18 |
import org.gvsig.tools.dataTypes.DataTypes; |
16 | 19 |
import org.gvsig.tools.dynform.DynFormDefinition; |
... | ... | |
23 | 26 |
import org.gvsig.tools.dynobject.DynClass; |
24 | 27 |
import org.gvsig.tools.dynobject.DynField; |
25 | 28 |
import org.gvsig.tools.dynobject.DynField_v2; |
29 |
import org.gvsig.tools.dynobject.DynMethod; |
|
26 | 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; |
|
27 | 34 |
import org.gvsig.tools.service.Manager; |
28 | 35 |
import org.gvsig.tools.service.Service; |
29 | 36 |
import org.gvsig.tools.service.ServiceException; |
30 | 37 |
import org.gvsig.tools.service.spi.ServiceManager; |
31 | 38 |
|
32 |
|
|
33 | 39 |
public class AbeilleJDynForm extends AbstractJDynForm implements Service, JDynFormFieldListener { |
34 | 40 |
|
35 | 41 |
private Map components = null; |
36 | 42 |
private FormPanel form = null; |
37 | 43 |
private String formPathname = null; |
38 | 44 |
private final DynFormSPIManager spimanager; |
39 |
|
|
45 |
|
|
40 | 46 |
public AbeilleJDynForm(ServiceManager manager, DynFormDefinition definition) throws ServiceException { |
41 |
super(((DynFormSPIManager)manager).getDynFormManager(),definition);
|
|
47 |
super(((DynFormSPIManager) manager).getDynFormManager(), definition);
|
|
42 | 48 |
this.spimanager = (DynFormSPIManager) manager; |
43 | 49 |
this.components = new HashMap(); |
44 | 50 |
formPathname = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM); |
45 |
if( formPathname == null ) {
|
|
51 |
if (formPathname == null) {
|
|
46 | 52 |
throw new IllegalArgumentException("Need tag 'dynform.abeille.form'."); |
47 | 53 |
} |
48 | 54 |
this.form = null; |
... | ... | |
53 | 59 |
return this.spimanager; |
54 | 60 |
} |
55 | 61 |
|
56 |
|
|
57 | 62 |
@Override |
58 | 63 |
protected JComponent getFieldsContainer() { |
59 | 64 |
this.form = new FormPanel(formPathname); |
... | ... | |
65 | 70 |
if (fieldDefinition.isHidden()) { |
66 | 71 |
continue; |
67 | 72 |
} |
68 |
Component component = this.form.getComponentByName("txt"+fieldDefinition.getName());
|
|
69 |
if( component != null && component instanceof JTextComponent ) {
|
|
70 |
JDynFormField jfield = new AbeilleTextJDynFormField(fieldDefinition, (JTextComponent)component); |
|
73 |
Component component = this.form.getComponentByName("txt" + fieldDefinition.getName());
|
|
74 |
if (component != null && component instanceof JTextComponent) {
|
|
75 |
JDynFormField jfield = new AbeilleTextJDynFormField(fieldDefinition, (JTextComponent) component);
|
|
71 | 76 |
((AbstractJDynFormField) jfield).setForm(this); |
72 | 77 |
jfield.setReadOnly(this.isReadOnly()); |
73 | 78 |
jfield.addListener(this); |
... | ... | |
76 | 81 |
} |
77 | 82 |
|
78 | 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 |
|
|
79 | 93 |
} |
80 | 94 |
} |
81 | 95 |
return this.form; |
... | ... | |
91 | 105 |
DynField[] fields = def.getDynFields(); |
92 | 106 |
for (int i = 0; i < fields.length; i++) { |
93 | 107 |
String name = fields[i].getName(); |
94 |
if ( StringUtils.isEmpty(name) ) {
|
|
108 |
if (StringUtils.isEmpty(name)) {
|
|
95 | 109 |
logger.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty "); |
96 | 110 |
continue; |
97 | 111 |
} |
... | ... | |
120 | 134 |
JDynFormField field = (JDynFormField) this.components.get(fieldName); |
121 | 135 |
return field; |
122 | 136 |
} |
123 |
|
|
137 |
|
|
124 | 138 |
@Override |
125 | 139 |
public void getValues(DynObject values) { |
126 | 140 |
if (values == null) { |
... | ... | |
158 | 172 |
} |
159 | 173 |
field.setValue(value); |
160 | 174 |
} |
161 |
|
|
175 |
|
|
162 | 176 |
public Iterator getFieldsIterator() { |
163 | 177 |
if (!this.isContentsInitialized()) { |
164 | 178 |
this.initComponents(); |
165 | 179 |
} |
166 | 180 |
return this.components.values().iterator(); |
167 | 181 |
} |
168 |
|
|
182 |
|
|
169 | 183 |
@Override |
170 | 184 |
public boolean hasValidValues() { |
171 | 185 |
Iterator it = this.getFieldsIterator(); |
... | ... | |
175 | 189 |
return false; |
176 | 190 |
} |
177 | 191 |
} |
178 |
return true; } |
|
192 |
return true; |
|
193 |
} |
|
179 | 194 |
|
180 | 195 |
@Override |
181 | 196 |
public boolean hasValidValues(List<String> fieldsName) { |
... | ... | |
232 | 247 |
public void message(JDynFormField field, String message) { |
233 | 248 |
message(message); |
234 | 249 |
} |
235 |
|
|
250 |
|
|
236 | 251 |
private class AbeilleTextJDynFormField extends AbstractJDynFormField { |
252 |
|
|
237 | 253 |
private final JTextComponent text; |
238 | 254 |
private final DynFormFieldDefinition fieldDefinition; |
239 | 255 |
|
... | ... | |
245 | 261 |
|
246 | 262 |
@Override |
247 | 263 |
public Object getParameterValue() { |
248 |
return null;
|
|
249 |
}
|
|
250 |
|
|
264 |
return null;
|
|
265 |
}
|
|
266 |
|
|
251 | 267 |
@Override |
252 |
public DynFormFieldDefinition getDefinition() {
|
|
253 |
return this.fieldDefinition;
|
|
254 |
}
|
|
255 |
|
|
268 |
public DynFormFieldDefinition getDefinition() {
|
|
269 |
return this.fieldDefinition;
|
|
270 |
}
|
|
271 |
|
|
256 | 272 |
@Override |
257 | 273 |
public void initComponent() { |
258 | 274 |
// Do nothing |
... | ... | |
276 | 292 |
|
277 | 293 |
@Override |
278 | 294 |
public void setValue(Object value) { |
279 |
if( value == null ) {
|
|
295 |
if (value == null) {
|
|
280 | 296 |
this.text.setText(""); |
281 | 297 |
} else { |
282 | 298 |
this.text.setText(value.toString()); |
... | ... | |
292 | 308 |
return null; |
293 | 309 |
} |
294 | 310 |
} |
295 |
|
|
296 |
|
|
311 |
|
|
297 | 312 |
} |
298 |
|
|
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 |
} |
|
299 | 407 |
} |
Also available in: Unified diff