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 @ 1948
History | View | Annotate | Download (19.4 KB)
1 |
package org.gvsig.tools.dynform.services.dynform; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.InputStream; |
6 |
import java.util.HashMap; |
7 |
import java.util.Map; |
8 |
|
9 |
import javax.swing.JCheckBox; |
10 |
import javax.swing.JComboBox; |
11 |
import javax.swing.JComponent; |
12 |
import javax.swing.JSpinner; |
13 |
import javax.swing.text.JTextComponent; |
14 |
|
15 |
import org.gvsig.tools.dynform.DynFormDefinition; |
16 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
17 |
import org.gvsig.tools.dynform.JDynFormField; |
18 |
import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener; |
19 |
import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm; |
20 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
21 |
|
22 |
import com.jeta.forms.components.panel.FormPanel; |
23 |
import java.io.File; |
24 |
import java.util.ArrayList; |
25 |
import java.util.List; |
26 |
import javax.swing.JButton; |
27 |
import javax.swing.JLabel; |
28 |
import javax.swing.JList; |
29 |
import javax.swing.JScrollPane; |
30 |
import javax.swing.JTable; |
31 |
import javax.swing.JTextArea; |
32 |
import javax.swing.JViewport; |
33 |
import org.apache.commons.io.FilenameUtils; |
34 |
import org.apache.commons.io.IOUtils; |
35 |
import org.apache.commons.lang3.StringUtils; |
36 |
import org.gvsig.tools.dataTypes.CoercionException; |
37 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
38 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM; |
39 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_CLEAR; |
40 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISMODIFIED; |
41 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISREADONLY; |
42 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDCHANGED; |
43 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDENTER; |
44 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDEXIT; |
45 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONLOAD; |
46 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONSETVALUES; |
47 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_VALIDATE; |
48 |
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
49 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
50 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
51 |
import org.gvsig.tools.dynobject.DynObject; |
52 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
53 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource; |
54 |
|
55 |
@SuppressWarnings("UseSpecificCatch") |
56 |
public class AbeilleJDynForm extends AbstractJDynForm implements JDynFormFieldListener { |
57 |
|
58 |
private class AbeilleComponentsFactory implements ComponentsFactory { |
59 |
|
60 |
private class DefaultScrolledComponent<T> implements ScrolledComponent<T> { |
61 |
private final T component; |
62 |
|
63 |
public DefaultScrolledComponent(T component) {
|
64 |
this.component = component;
|
65 |
} |
66 |
|
67 |
@Override
|
68 |
public JScrollPane getScrollPane() { |
69 |
try {
|
70 |
Object p1 = ((JComponent) component).getParent(); |
71 |
if( p1 instanceof JViewport ) { |
72 |
p1 = ((JComponent) component).getParent();
|
73 |
} |
74 |
if( p1 instanceof JScrollPane ) { |
75 |
return (JScrollPane) p1; |
76 |
} |
77 |
} catch(Exception ex) { |
78 |
|
79 |
} |
80 |
return null; |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public T getComponent() {
|
85 |
return this.component; |
86 |
} |
87 |
} |
88 |
|
89 |
private static final String PREFIX_JCHECKBOX = "chk"; |
90 |
private static final String PREFIX_JLABEL = "lbl"; |
91 |
private static final String PREFIX_DROPDOWN = "lblDdn"; |
92 |
private static final String PREFIX_JCOMBOBOX = "cbo"; |
93 |
private static final String PREFIX_JSPINNER = "spn"; |
94 |
private static final String PREFIX_JTEXTFIELD = "txt"; |
95 |
private static final String PREFIX_JTEXTAREA = "txt"; |
96 |
private static final String PREFIX_JBUTTON = "btn"; |
97 |
private static final String PREFIX_JLIST = "lst"; |
98 |
private static final String PREFIX_JTABLE = "tbl"; |
99 |
|
100 |
private final FormPanel form; |
101 |
|
102 |
public AbeilleComponentsFactory(FormPanel form) {
|
103 |
this.form = form;
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public boolean containsComponents(DynFormFieldDefinition definition) { |
108 |
String[] prefixes = new String[] { |
109 |
PREFIX_JCHECKBOX, |
110 |
PREFIX_JLABEL, |
111 |
PREFIX_DROPDOWN, |
112 |
PREFIX_JCOMBOBOX, |
113 |
PREFIX_JSPINNER, |
114 |
PREFIX_JTEXTFIELD, |
115 |
PREFIX_JTEXTAREA, |
116 |
PREFIX_JBUTTON, |
117 |
PREFIX_JLIST, |
118 |
PREFIX_JTABLE |
119 |
}; |
120 |
for (String prefix : prefixes) { |
121 |
Component component = this.form.getComponentByName(prefix + definition.getName()); |
122 |
if( component!=null ) { |
123 |
return true; |
124 |
} |
125 |
} |
126 |
return false; |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public boolean containsJComboBox(DynFormFieldDefinition definition, String prefix) { |
131 |
Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
132 |
return component!=null; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public boolean containsJButton(DynFormFieldDefinition definition, String prefix) { |
137 |
Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
138 |
return component!=null; |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public boolean containsJLabel(DynFormFieldDefinition definition, String prefix) { |
143 |
Component component = this.form.getComponentByName(PREFIX_JLABEL+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
144 |
return component!=null; |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public boolean containsJSpinner(DynFormFieldDefinition definition, String prefix) { |
149 |
Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
150 |
return component!=null; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public boolean containsJTextField(DynFormFieldDefinition definition, String prefix) { |
155 |
Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
156 |
return component!=null; |
157 |
} |
158 |
|
159 |
@Override
|
160 |
public JCheckBox getJCheckBox(DynFormFieldDefinition definition, String prefix) { |
161 |
try {
|
162 |
Component component = this.form.getComponentByName(PREFIX_JCHECKBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
163 |
return (JCheckBox) component; |
164 |
} catch(Throwable th) { |
165 |
return null; |
166 |
} |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public JLabel getJLabel(DynFormFieldDefinition definition, String prefix) { |
171 |
try {
|
172 |
Component component = this.form.getComponentByName(PREFIX_JLABEL+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
173 |
return (JLabel) component; |
174 |
} catch(Throwable th) { |
175 |
return null; |
176 |
} |
177 |
} |
178 |
|
179 |
@Override
|
180 |
public JComboBox getJComboBox(DynFormFieldDefinition definition, String prefix) { |
181 |
try {
|
182 |
Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
183 |
return (JComboBox) component; |
184 |
} catch(Throwable th) { |
185 |
return null; |
186 |
} |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public JTextComponent getJTextField(DynFormFieldDefinition definition, String prefix) { |
191 |
try {
|
192 |
Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
193 |
return (JTextComponent) component; |
194 |
} catch(Throwable th) { |
195 |
return null; |
196 |
} |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public ScrolledComponent<JTextArea> getJTextArea(DynFormFieldDefinition definition, String prefix) { |
201 |
try {
|
202 |
JTextArea component = (JTextArea) this.form.getComponentByName( |
203 |
PREFIX_JTEXTAREA + StringUtils.defaultIfBlank(prefix, "") + definition.getName()
|
204 |
); |
205 |
return new DefaultScrolledComponent<>(component); |
206 |
} catch(Throwable th) { |
207 |
return null; |
208 |
} |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public ScrolledComponent<JList> getJList(DynFormFieldDefinition definition, String prefix) { |
213 |
try {
|
214 |
JList component = (JList) this.form.getComponentByName( |
215 |
PREFIX_JLIST+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
|
216 |
); |
217 |
return new DefaultScrolledComponent<>(component); |
218 |
} catch(Throwable th) { |
219 |
return null; |
220 |
} |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public ScrolledComponent<JTable> getJTable(DynFormFieldDefinition definition, String prefix) { |
225 |
try {
|
226 |
JTable component = (JTable) this.form.getComponentByName( |
227 |
PREFIX_JTABLE+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
|
228 |
); |
229 |
return new DefaultScrolledComponent<>(component); |
230 |
} catch(Throwable th) { |
231 |
return null; |
232 |
} |
233 |
} |
234 |
|
235 |
|
236 |
@Override
|
237 |
public JSpinner getJSpinner(DynFormFieldDefinition definition, String prefix) { |
238 |
try {
|
239 |
Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
240 |
return (JSpinner) component; |
241 |
} catch(Throwable th) { |
242 |
return null; |
243 |
} |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public JButton getJButton(DynFormFieldDefinition definition, String prefix) { |
248 |
try {
|
249 |
Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName()); |
250 |
return (JButton) component; |
251 |
} catch(Throwable th) { |
252 |
return null; |
253 |
} |
254 |
} |
255 |
|
256 |
|
257 |
} |
258 |
private Map<String,JDynFormField> components = null; |
259 |
private String resourceName = null; |
260 |
private AbeilleComponentsFactory componentsFactory;
|
261 |
|
262 |
|
263 |
public AbeilleJDynForm(
|
264 |
DynFormSPIManager manager, |
265 |
JDynFormFactory factory, |
266 |
DynFormContext context, |
267 |
DynFormDefinition definition |
268 |
) { |
269 |
super(manager, factory, definition, context);
|
270 |
this.components = new HashMap(); |
271 |
resourceName = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM);
|
272 |
if (resourceName == null) { |
273 |
ResourcesStorage storage = context.getResourcesStorage(); |
274 |
if( storage.exists("jetaform") ) { |
275 |
this.resourceName = "jetaform"; |
276 |
} else if( storage.exists("jfrm") ) { |
277 |
this.resourceName = "jfrm"; |
278 |
} else {
|
279 |
throw new IllegalArgumentException("Can't locate form resource. Need tag '"+TAG_DYNFORM_ABEILLE_FORM+"' or resource 'jetaform'."); |
280 |
} |
281 |
} |
282 |
this.componentsFactory = null; |
283 |
} |
284 |
|
285 |
@Override
|
286 |
protected JComponent getFieldsContainer() { |
287 |
InputStream is = null; |
288 |
FormPanel form = null;
|
289 |
Resource resource = null;
|
290 |
try {
|
291 |
File f = new File(resourceName); |
292 |
if( f.isAbsolute() && f.exists() ) {
|
293 |
is = new FileInputStream(f); |
294 |
} else {
|
295 |
ResourcesStorage storage = this.getContext().getResourcesStorage();
|
296 |
resource = storage.getResource(FilenameUtils.getName(resourceName)); |
297 |
if( resource == null || !resource.exists() ) { |
298 |
throw new IllegalArgumentException("Can't locate abeille form '"+resourceName+"'."); |
299 |
} |
300 |
is = resource.asInputStream(); |
301 |
} |
302 |
form = new FormPanel(is);
|
303 |
this.componentsFactory = new AbeilleComponentsFactory(form); |
304 |
for (DynFormFieldDefinition definition : this.getDefinition().getDefinitions()) { |
305 |
if( definition==null ) { |
306 |
continue;
|
307 |
} |
308 |
if (definition.isHidden()) {
|
309 |
continue;
|
310 |
} |
311 |
if( !this.componentsFactory.containsComponents(definition) ) { |
312 |
continue;
|
313 |
} |
314 |
try {
|
315 |
JDynFormFieldFactory factory = this.getServiceManager()
|
316 |
.getJDynFormFieldFactory(this.getContext(),definition);
|
317 |
JDynFormField jfield = factory.create( |
318 |
this.getServiceManager(),
|
319 |
this.componentsFactory,
|
320 |
definition, |
321 |
null
|
322 |
); |
323 |
if (jfield instanceof AbstractJDynFormField) { |
324 |
((AbstractJDynFormField) jfield).setForm(this);
|
325 |
} |
326 |
if (this.isReadOnly()) { |
327 |
jfield.setReadOnly(true);
|
328 |
} else {
|
329 |
jfield.setReadOnly(definition.isReadOnly()); |
330 |
} |
331 |
jfield.addListener(this);
|
332 |
|
333 |
this.components.put(jfield.getName(), jfield);
|
334 |
jfield.asJComponent(); // Forzamos que se inicialize
|
335 |
jfield.getJLabel(); // Forzamos que se inicialize
|
336 |
} catch(Throwable th1) { |
337 |
LOGGER.warn("Can't load field '"+definition.getName()+"' for abeille form '"+resourceName+"'.", th1); |
338 |
} |
339 |
} |
340 |
this.bindUserCode(form);
|
341 |
this.callUserEvent(USERCODE_FORM_ONLOAD, this); |
342 |
} catch (Throwable th) { |
343 |
LOGGER.warn("Can't load abeille form '"+resourceName+"'.", th); |
344 |
} finally {
|
345 |
IOUtils.closeQuietly(is); |
346 |
IOUtils.closeQuietly(resource); |
347 |
} |
348 |
|
349 |
return form;
|
350 |
} |
351 |
|
352 |
private void bindUserCode(FormPanel form) { |
353 |
// for(Function fn : this.getUserFunctions()) {
|
354 |
// String name = fn.name();
|
355 |
// if( name.endsWith("_click") ) {
|
356 |
// Component c = form.getComponentByName(name.substring(0, name.length()-6));
|
357 |
// if( c instanceof JButton ) {
|
358 |
// JButton button = (JButton) c;
|
359 |
// final String fnName = fn.name();
|
360 |
// button.addActionListener(new ActionListener() {
|
361 |
// @Override
|
362 |
// public void actionPerformed(ActionEvent e) {
|
363 |
// callUserEvent(fnName, e);
|
364 |
// }
|
365 |
// });
|
366 |
// }
|
367 |
// }
|
368 |
// }
|
369 |
//
|
370 |
} |
371 |
|
372 |
@Override
|
373 |
public void setValues(DynObject values) { |
374 |
if (!this.isContentsInitialized()) { |
375 |
this.values = values;
|
376 |
return;
|
377 |
} |
378 |
for (JDynFormField jfield : this.getFields()) { |
379 |
String name = "unknown"; |
380 |
try {
|
381 |
name = jfield.getName(); |
382 |
jfield.setValue(values.getDynValue(jfield.getName())); |
383 |
} catch(Exception ex) { |
384 |
LOGGER.warn("Can't set value to field '"+name+"'.",ex); |
385 |
} |
386 |
} |
387 |
this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values); |
388 |
try {
|
389 |
if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this, values) ) { |
390 |
this.setReadOnly(true); |
391 |
} |
392 |
} catch (Exception ex) { |
393 |
} |
394 |
|
395 |
} |
396 |
|
397 |
public Iterable<JDynFormField> getFields() { |
398 |
if (!this.isContentsInitialized()) { |
399 |
this.initComponents();
|
400 |
} |
401 |
return this.components.values(); |
402 |
} |
403 |
|
404 |
@Override
|
405 |
public JDynFormField getField(String fieldName) { |
406 |
if (!this.isContentsInitialized()) { |
407 |
this.initComponents();
|
408 |
} |
409 |
JDynFormField field = this.components.get(fieldName);
|
410 |
return field;
|
411 |
} |
412 |
|
413 |
@Override
|
414 |
public void getValues(DynObject values) { |
415 |
if (values == null) { |
416 |
return;
|
417 |
} |
418 |
for (JDynFormField jfield : this.getFields()) { |
419 |
try {
|
420 |
jfield.fetch(values); |
421 |
} catch (Exception ex) { |
422 |
LOGGER.warn("Can't get value of field '" + jfield.getName() + "'.", ex); |
423 |
} |
424 |
} |
425 |
} |
426 |
|
427 |
@Override
|
428 |
public Object getValue(String fieldName) { |
429 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
430 |
return field.getValue();
|
431 |
} |
432 |
|
433 |
@Override
|
434 |
public void setValue(String fieldName, Object value) { |
435 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
436 |
try {
|
437 |
value = field.getDefinition().getDataType().coerce(value); |
438 |
} catch (CoercionException e) {
|
439 |
String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'."; |
440 |
LOGGER.warn(msg, e); |
441 |
throw new RuntimeException(msg, e); |
442 |
} |
443 |
field.setValue(value); |
444 |
} |
445 |
|
446 |
@Override
|
447 |
public boolean hasValidValues() { |
448 |
for (JDynFormField jfield : this.getFields()) { |
449 |
if (!jfield.hasValidValue()) {
|
450 |
return false; |
451 |
} |
452 |
} |
453 |
try {
|
454 |
return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this); |
455 |
} catch (Exception ex) { |
456 |
} |
457 |
return true; |
458 |
} |
459 |
|
460 |
@Override
|
461 |
public boolean hasValidValues(List<String> fieldsName) { |
462 |
if (fieldsName == null) { |
463 |
fieldsName = new ArrayList<>(); |
464 |
} |
465 |
for (JDynFormField jfield : this.getFields()) { |
466 |
if (!jfield.hasValidValue()) {
|
467 |
fieldsName.add(jfield.getName()); |
468 |
} |
469 |
} |
470 |
return fieldsName.isEmpty();
|
471 |
} |
472 |
|
473 |
@Override
|
474 |
public boolean isModified() { |
475 |
if( this.isReadOnly() ) { |
476 |
return false; |
477 |
} |
478 |
try {
|
479 |
return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values); |
480 |
} catch (NoSuchMethodException ex) { |
481 |
} catch (Exception ex) { |
482 |
LOGGER.warn("Error calling user function form_IsModified.",ex);
|
483 |
} |
484 |
for (JDynFormField jfield : this.getFields()) { |
485 |
if (jfield.isModified()) {
|
486 |
return true; |
487 |
} |
488 |
} |
489 |
return false; |
490 |
} |
491 |
|
492 |
@Override
|
493 |
public void clear() { |
494 |
for (JDynFormField jfield : this.getFields()) { |
495 |
jfield.clear(); |
496 |
} |
497 |
this.callUserEvent(USERCODE_FORM_CLEAR, this); |
498 |
} |
499 |
|
500 |
@Override
|
501 |
public void fieldEnter(JDynFormField field) { |
502 |
message(field.getDefinition().getDescription()); |
503 |
this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field); |
504 |
} |
505 |
|
506 |
@Override
|
507 |
public void fieldExit(JDynFormField field) { |
508 |
message(); |
509 |
this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field); |
510 |
} |
511 |
|
512 |
@Override
|
513 |
public void fieldChanged(JDynFormField field) { |
514 |
fireFieldChangeEvent(field); |
515 |
this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field); |
516 |
} |
517 |
|
518 |
@Override
|
519 |
public void message(JDynFormField field, String message) { |
520 |
message(message); |
521 |
} |
522 |
|
523 |
} |