Revision 1881 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

View differences:

AbeilleJDynForm.java
2 2

  
3 3
import java.awt.Component;
4 4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6 5
import java.io.InputStream;
7
import java.util.ArrayList;
8
import java.util.Date;
9 6
import java.util.HashMap;
10
import java.util.Iterator;
11
import java.util.List;
12 7
import java.util.Map;
13 8

  
14
import javax.swing.ComboBoxModel;
15 9
import javax.swing.JCheckBox;
16 10
import javax.swing.JComboBox;
17 11
import javax.swing.JComponent;
18 12
import javax.swing.JSpinner;
19
import javax.swing.JSpinner.DateEditor;
20
import javax.swing.SpinnerDateModel;
21 13
import javax.swing.text.JTextComponent;
22 14

  
23
import org.apache.commons.lang3.StringUtils;
24
import org.gvsig.tools.ToolsLocator;
25
import org.gvsig.tools.dataTypes.CoercionException;
26
import org.gvsig.tools.dataTypes.DataTypes;
27 15
import org.gvsig.tools.dynform.DynFormDefinition;
28 16
import org.gvsig.tools.dynform.DynFormFieldDefinition;
29 17
import org.gvsig.tools.dynform.JDynFormField;
30 18
import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener;
31
import org.gvsig.tools.dynform.spi.AbstractJDynForm;
19
import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm;
32 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 org.gvsig.tools.dynform.spi.dynform.JDynFormFactory;
33 40
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
34
import org.gvsig.tools.dynobject.DynClass;
35
import org.gvsig.tools.dynobject.DynField;
36
import org.gvsig.tools.dynobject.DynField_v2;
37
import org.gvsig.tools.dynobject.DynMethod;
41
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
38 42
import org.gvsig.tools.dynobject.DynObject;
39
import org.gvsig.tools.dynobject.DynObjectManager;
40
import org.gvsig.tools.dynobject.DynObjectValueItem;
41
import org.gvsig.tools.service.Manager;
42
import org.gvsig.tools.service.Service;
43
import org.gvsig.tools.service.ServiceException;
44
import org.gvsig.tools.service.spi.ServiceManager;
43
import org.gvsig.tools.util.ResourcesStorage;
44
import org.gvsig.tools.util.ResourcesStorage.Resource;
45 45

  
46
import com.jeta.forms.components.panel.FormPanel;
47
import com.jeta.forms.gui.common.FormException;
46
@SuppressWarnings("UseSpecificCatch")
47
public class AbeilleJDynForm extends AbstractJDynForm implements JDynFormFieldListener {
48 48

  
49
public class AbeilleJDynForm extends AbstractJDynForm implements Service, JDynFormFieldListener {
49
    private class AbeilleComponentsFactory implements ComponentsFactory {
50 50

  
51
	private Map components = null;
52
	private FormPanel form = null;
53
	private String formPathname = null;
54
	private final DynFormSPIManager spimanager;
51
        private class DefaultScrolledComponent<T> implements ScrolledComponent<T> {
52
            private final T component;
53
            
54
            public DefaultScrolledComponent(T component) {
55
                this.component = component;
56
            }
55 57

  
56
	public AbeilleJDynForm(
57
                ServiceManager manager, 
58
                DynFormDefinition definition,
59
                DynFormContext context
60
            ) throws ServiceException {
61
		super(((DynFormSPIManager) manager).getDynFormManager(), definition, context);
62
		this.spimanager = (DynFormSPIManager) manager;
63
		this.components = new HashMap();
64
		formPathname = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM);
65
		if (formPathname == null) {
66
			throw new IllegalArgumentException("Need tag 'dynform.abeille.form'.");
67
		}
68
		this.form = null;
69
	}
58
            @Override
59
            public JScrollPane getScrollPane() {
60
                try {
61
                    Object p1 = ((JComponent) component).getParent();
62
                    if( p1 instanceof JViewport ) {
63
                        p1 = ((JComponent) component).getParent();
64
                    }
65
                    if( p1 instanceof JScrollPane ) {
66
                        return (JScrollPane) p1;
67
                    }
68
                } catch(Exception ex) {
70 69

  
71
	@Override
72
	public Manager getManager() {
73
		return this.spimanager;
74
	}
70
                }
71
                return null;
72
            }
75 73

  
76
	@Override
77
	protected JComponent getFieldsContainer() {
78
		InputStream is = null;
79
		try{
80
			is = new FileInputStream(formPathname);
81
			this.form = new FormPanel(is);
82
			List fields = this.getDefinition().getDefinitions();
83
			Iterator it = fields.iterator();
84
			while (it.hasNext()) {
85
				DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next();
86
				if (fieldDefinition.isHidden()) {
87
					continue;
88
				}
89
				Component component = this.form.getComponentByName("txt"+fieldDefinition.getName());
90
				if (component != null && component instanceof JTextComponent) {
91
					JDynFormField jfield = new AbeilleTextJDynFormField(fieldDefinition, (JTextComponent) component);
92
					((AbstractJDynFormField) jfield).setForm(this);
93
					jfield.setReadOnly(this.isReadOnly());
94
					jfield.addListener(this);
95
					if (this.isReadOnly()) {
96
						jfield.setReadOnly(this.isReadOnly());
97
					}
74
            @Override
75
            public T getComponent() {
76
                return this.component;
77
            }
78
        }
98 79

  
99
					this.components.put(jfield.getName(), jfield);
100
				} else {
101
					component = this.form.getComponentByName("date"+fieldDefinition.getName());
102
					if (component != null && component instanceof JSpinner) {
103
						JSpinner spinComp = (JSpinner) component;
104
						//			            	DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
105
						String pattern = "dd-MM-yyyy";
106
						//			    			try{
107
						//			    			    SimpleDateFormat sdf = (SimpleDateFormat)dateFormat;
108
						//			    			    pattern =sdf.toPattern();
109
						//			    			} catch (ClassCastException e) {
110
						//			    			    logger.warn("Can't determinate date format pattern, using default pattern:"+pattern);
111
						//			    			}
112
						//			    			
80
        private static final String PREFIX_JCHECKBOX = "chk";
81
        private static final String PREFIX_JLABEL = "lbl";
82
        private static final String PREFIX_JCOMBOBOX = "cbo";
83
        private static final String PREFIX_JSPINNER = "spn";
84
        private static final String PREFIX_JTEXTFIELD = "txt";
85
        private static final String PREFIX_JTEXTAREA = "txt";
86
        private static final String PREFIX_JBUTTON = "btn";
87
        private static final String PREFIX_JLIST = "lst";
88
        private static final String PREFIX_JTABLE = "tbl";
89
        
90
        private final FormPanel form;
113 91

  
114
						SpinnerNullDateModel model = new SpinnerNullDateModel();
115
						spinComp.setModel(model);
116
						DateEditor timeEditor = new DateEditor(spinComp, pattern);
117
						spinComp.setEditor(timeEditor);
92
        public AbeilleComponentsFactory(FormPanel form) {
93
            this.form = form;
94
        }
95
        
96
        @Override
97
        public boolean containsComponents(DynFormFieldDefinition definition) {
98
            String[] prefixes = new String[] {
99
                PREFIX_JCHECKBOX,
100
                PREFIX_JLABEL,
101
                PREFIX_JCOMBOBOX,
102
                PREFIX_JSPINNER,
103
                PREFIX_JTEXTFIELD,
104
                PREFIX_JTEXTAREA,
105
                PREFIX_JBUTTON,
106
                PREFIX_JLIST,
107
                PREFIX_JTABLE
108
            };
109
            for (String prefix : prefixes) {
110
                Component component = this.form.getComponentByName(prefix + definition.getName());
111
                if( component!=null ) {
112
                    return true;
113
                }
114
            }
115
            return false;
116
        }
118 117

  
119
						JDynFormField jfield = new AbeilleDateJDynFormField(fieldDefinition, spinComp);
120
						((AbstractJDynFormField) jfield).setForm(this);
121
						jfield.setReadOnly(this.isReadOnly());
122
						jfield.addListener(this);
123
						if (this.isReadOnly()) {
124
							jfield.setReadOnly(this.isReadOnly());
125
						}
118
        @Override
119
        public boolean containsJComboBox(DynFormFieldDefinition definition, String prefix) {
120
            Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
121
            return component!=null;
122
        }
126 123

  
127
						this.components.put(jfield.getName(), jfield);
128
					} else {
129
						component = this.form.getComponentByName("cbo" + fieldDefinition.getName());
130
						if (component != null && component instanceof JComboBox) {
131
							JDynFormField jfield = new AbeilleComboJDynFormField(fieldDefinition, (JComboBox) component);
132
							((AbstractJDynFormField) jfield).setForm(this);
133
							jfield.setReadOnly(this.isReadOnly());
134
							jfield.addListener(this);
135
							if (this.isReadOnly()) {
136
								jfield.setReadOnly(this.isReadOnly());
137
							}
124
        @Override
125
        public boolean containsJButton(DynFormFieldDefinition definition, String prefix) {
126
            Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
127
            return component!=null;
128
        }
138 129

  
139
							this.components.put(jfield.getName(), jfield);
140
						} else {
141
							component = this.form.getComponentByName("chk" + fieldDefinition.getName());
142
							if (component != null && component instanceof JCheckBox) {
143
								JDynFormField jfield = new AbeilleCheckJDynFormField(fieldDefinition, (JCheckBox) component);
144
								((AbstractJDynFormField) jfield).setForm(this);
145
								jfield.setReadOnly(this.isReadOnly());
146
								jfield.addListener(this);
147
								if (this.isReadOnly()) {
148
									jfield.setReadOnly(this.isReadOnly());
149
								}
130
        @Override
131
        public boolean containsJSpinner(DynFormFieldDefinition definition, String prefix) {
132
            Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
133
            return component!=null;
134
        }
150 135

  
151
								this.components.put(jfield.getName(), jfield);
152
							}
153
						}
154
					}
155
				}
156
			}
157
		} catch (FormException | FileNotFoundException e) {
158
			// TODO Auto-generated catch block
159
			e.printStackTrace();
160
		}
136
        @Override
137
        public boolean containsJTextField(DynFormFieldDefinition definition, String prefix) {
138
            Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
139
            return component!=null;
140
        }
161 141

  
142
        @Override
143
        public JCheckBox getJCheckBox(DynFormFieldDefinition definition, String prefix) {
144
            try {
145
                Component component = this.form.getComponentByName(PREFIX_JCHECKBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
146
                return (JCheckBox) component;
147
            } catch(Throwable th) {
148
                return null;
149
            }
150
        }
162 151

  
163
		return this.form;
164
	}
152
        @Override
153
        public JLabel getJLabel(DynFormFieldDefinition definition, String prefix) {
154
            try {
155
                Component component = this.form.getComponentByName(PREFIX_JLABEL+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
156
                return (JLabel) component;
157
            } catch(Throwable th) {
158
                return null;
159
            }
160
        }
165 161

  
166
	@Override
167
	public void setValues(DynObject values) {
168
		if (!this.isContentsInitialized()) {
169
			this.values = values;
170
			return;
171
		}
172
		DynClass def = values.getDynClass();
173
		DynField[] fields = def.getDynFields();
174
		for (int i = 0; i < fields.length; i++) {
175
			String name = fields[i].getName();
176
			if (StringUtils.isEmpty(name)) {
177
				logger.warn("Field name " + i + " of '" + def.getFullName() + "' is null or empty ");
178
				continue;
179
			}
180
			JDynFormField jfield = (JDynFormField) this.getField(name);
181
			if (jfield == null) {
182
				logger.info("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getFullName() + "'.");
183
				continue;
184
			}
185
			if (values.getDynValue(name) == null) {
186
				if (fields[i].getDataType().getType() == DataTypes.LIST) {
187
					try {
188
						if (((DynField_v2) fields[i]).getDynClassOfItems() != null) {
189
							values.setDynValue(name, new ArrayList<DynObject>());
190
						}
191
					} catch (Exception e) {
192
						logger.warn("Problems initializing the DynObject List", e);
193
					}
194
				}
195
			}
196
			jfield.setValue(values.getDynValue(name));
197
		}
198
	}
162
        @Override
163
        public JComboBox getJComboBox(DynFormFieldDefinition definition, String prefix) {
164
            try {
165
                Component component = this.form.getComponentByName(PREFIX_JCOMBOBOX+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
166
                return (JComboBox) component;
167
            } catch(Throwable th) {
168
                return null;
169
            }
170
        }
199 171

  
200
	@Override
201
	public JDynFormField getField(String fieldName) {
202
		JDynFormField field = (JDynFormField) this.components.get(fieldName);
203
		return field;
204
	}
172
        @Override
173
        public JTextComponent getJTextField(DynFormFieldDefinition definition, String prefix) {
174
            try {
175
                Component component = this.form.getComponentByName(PREFIX_JTEXTFIELD+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
176
                return (JTextComponent) component;
177
            } catch(Throwable th) {
178
                return null;
179
            }
180
        }
205 181

  
206
	@Override
207
	public void getValues(DynObject values) {
208
		if (values == null) {
209
			return;
210
		}
211
		DynField[] fields = values.getDynClass().getDynFields();
212
		for (DynField field : fields) {
213
			String name = field.getName();
214
			JDynFormField jfield = (JDynFormField) this.getField(name);
215
			if (jfield != null) {
216
				try {
217
					jfield.fetch(values);
218
				} catch (Exception ex) {
219
					logger.warn("Can't get value of field '" + name + "'.", ex);
220
				}
221
			}
222
		}
223
	}
182
        @Override
183
        public ScrolledComponent<JTextArea> getJTextArea(DynFormFieldDefinition definition, String prefix) {
184
            try {
185
                JTextArea component = (JTextArea) this.form.getComponentByName(
186
                        PREFIX_JTEXTAREA + StringUtils.defaultIfBlank(prefix, "") + definition.getName()
187
                );
188
                return new DefaultScrolledComponent<>(component);
189
            } catch(Throwable th) {
190
                return null;
191
            }
192
        }
224 193

  
225
	@Override
226
	public Object getValue(String fieldName) {
227
		JDynFormField field = (JDynFormField) this.getField(fieldName);
228
		return field.getValue();
229
	}
194
        @Override
195
        public ScrolledComponent<JList> getJList(DynFormFieldDefinition definition, String prefix) {
196
            try {
197
                JList component = (JList) this.form.getComponentByName(
198
                        PREFIX_JLIST+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
199
                );
200
                return new DefaultScrolledComponent<>(component);
201
            } catch(Throwable th) {
202
                return null;
203
            }
204
        }
230 205

  
231
	@Override
232
	public void setValue(String fieldName, Object value) {
233
		JDynFormField field = (JDynFormField) this.getField(fieldName);
234
		try {
235
			value = field.getDefinition().getDataType().coerce(value);
236
		} catch (CoercionException e) {
237
			String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'.";
238
			logger.warn(msg, e);
239
			throw new RuntimeException(msg, e);
240
		}
241
		field.setValue(value);
242
	}
206
        @Override
207
        public ScrolledComponent<JTable> getJTable(DynFormFieldDefinition definition, String prefix) {
208
            try {
209
                JTable component = (JTable) this.form.getComponentByName(
210
                        PREFIX_JTABLE+StringUtils.defaultIfBlank(prefix, "") + definition.getName()
211
                );
212
                return new DefaultScrolledComponent<>(component);
213
            } catch(Throwable th) {
214
                return null;
215
            }
216
        }
243 217

  
244
	public Iterator getFieldsIterator() {
245
		if (!this.isContentsInitialized()) {
246
			this.initComponents();
247
		}
248
		return this.components.values().iterator();
249
	}
218
        
219
        @Override
220
        public JSpinner getJSpinner(DynFormFieldDefinition definition, String prefix) {
221
            try {
222
                Component component = this.form.getComponentByName(PREFIX_JSPINNER+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
223
                return (JSpinner) component;
224
            } catch(Throwable th) {
225
                return null;
226
            }
227
        }
228
        
229
        @Override
230
        public JButton getJButton(DynFormFieldDefinition definition, String prefix) {
231
            try {
232
                Component component = this.form.getComponentByName(PREFIX_JBUTTON+StringUtils.defaultIfBlank(prefix, "") + definition.getName());
233
                return (JButton) component;
234
            } catch(Throwable th) {
235
                return null;
236
            }
237
        }
238
        
239
        
240
    }
241
    private Map<String,JDynFormField> components = null;
242
    private String resourceName = null;
243
    private AbeilleComponentsFactory componentsFactory;
250 244

  
251
	@Override
252
	public boolean hasValidValues() {
253
		Iterator it = this.getFieldsIterator();
254
		while (it.hasNext()) {
255
			JDynFormField jfield = (JDynFormField) it.next();
256
			if (!jfield.hasValidValue()) {
257
				return false;
258
			}
259
		}
260
		return true;
261
	}
262 245

  
263
	@Override
264
	public boolean hasValidValues(List<String> fieldsName) {
265
		if (fieldsName == null) {
266
			fieldsName = new ArrayList<>();
267
		}
268
		Iterator it = this.getFieldsIterator();
269
		while (it.hasNext()) {
270
			JDynFormField jfield = (JDynFormField) it.next();
271
			if (!jfield.hasValidValue()) {
272
				fieldsName.add(jfield.getName());
273
			}
274
		}
275
		return fieldsName.isEmpty();
276
	}
246
    public AbeilleJDynForm(
247
            DynFormSPIManager manager,
248
            JDynFormFactory factory,
249
            DynFormContext context,
250
            DynFormDefinition definition
251
    ) {
252
        super(manager, factory, definition, context);
253
        this.components = new HashMap();
254
        resourceName = (String) definition.getTags().get(DynFormSPIManager.TAG_DYNFORM_ABEILLE_FORM);
255
        if (resourceName == null) {
256
            throw new IllegalArgumentException("Need tag '"+TAG_DYNFORM_ABEILLE_FORM+"'.");
257
        }
258
        this.componentsFactory = null;
259
    }
277 260

  
278
	@Override
279
	public boolean isModified() {
280
		Iterator it = this.getFieldsIterator();
281
		while (it.hasNext()) {
282
			JDynFormField jfield = (JDynFormField) it.next();
283
			if (jfield.isModified()) {
284
				return true;
285
			}
286
		}
287
		return false;
288
	}
261
    @Override
262
    protected JComponent getFieldsContainer() {
263
        InputStream is = null;
264
        FormPanel form = null;
265
        Resource resource = null;
266
        try {
267
            File f = new File(resourceName);
268
            if( f.isAbsolute() && f.exists() ) {
269
                is = new FileInputStream(f);
270
            } else {
271
                ResourcesStorage storage = this.getContext().getResourcesStorage();
272
                resource = storage.getResource(FilenameUtils.getName(resourceName));
273
                if( resource == null ) {
274
                    throw new IllegalArgumentException("Can't locate abeille form '"+resourceName+"'.");
275
                }
276
                is = resource.asInputStream();
277
            }
278
            form = new FormPanel(is);
279
            this.componentsFactory = new AbeilleComponentsFactory(form);
280
            for (DynFormFieldDefinition definition : this.getDefinition().getDefinitions()) {
281
                if( definition==null ) {
282
                    continue;
283
                }
284
                if (definition.isHidden()) {
285
                    continue;
286
                }
287
                if( !this.componentsFactory.containsComponents(definition) ) {
288
                    continue;
289
                }
290
                try {
291
                    JDynFormFieldFactory factory = this.getServiceManager()
292
                            .getJDynFormFieldFactory(definition);
293
                    JDynFormField jfield = factory.create(
294
                            this.getServiceManager(), 
295
                            this.componentsFactory, 
296
                            definition, 
297
                            null
298
                    );
299
                    if (jfield instanceof AbstractJDynFormField) {
300
                        ((AbstractJDynFormField) jfield).setForm(this);
301
                    }
302
                    if (this.isReadOnly()) {
303
                        jfield.setReadOnly(true);
304
                    } else {
305
                        jfield.setReadOnly(definition.isReadOnly());
306
                    }
307
                    jfield.addListener(this);
289 308

  
290
	@Override
291
	public void clear() {
292
		Iterator it = this.components.entrySet().iterator();
293
		while (it.hasNext()) {
294
			Map.Entry entry = (Map.Entry) it.next();
295
			((JDynFormField) (entry.getValue())).clear();
296
		}
297
	}
309
                    this.components.put(jfield.getName(), jfield);
310
                    jfield.asJComponent(); // Forzamos que se inicialize
311
                    jfield.getJLabel();  // Forzamos que se inicialize 
312
                } catch(Throwable th1) {
313
                    LOGGER.warn("Can't load field '"+definition.getName()+"' for abeille form '"+resourceName+"'.", th1);
314
                }
315
            }
316
        } catch (Throwable th) {
317
            LOGGER.warn("Can't load abeille form '"+resourceName+"'.", th);
318
        } finally {
319
            IOUtils.closeQuietly(is);
320
            IOUtils.closeQuietly(resource);
321
        }
298 322

  
299
	@Override
300
	public void fieldEnter(JDynFormField field) {
301
		message(field.getDefinition().getDescription());
302
	}
323
        return form;
324
    }
303 325

  
304
	@Override
305
	public void fieldExit(JDynFormField field) {
306
		message();
307
	}
326
    @Override
327
    public void setValues(DynObject values) {
328
        if (!this.isContentsInitialized()) {
329
            this.values = values;
330
            return;
331
        }
332
        for (JDynFormField jfield : this.getFields()) {
333
            String name = "unknown";
334
            try {
335
                name = jfield.getName();
336
                jfield.setValue(values.getDynValue(jfield.getName()));
337
            } catch(Exception ex) {
338
                LOGGER.warn("Can't set value to field '"+name+"'.",ex);
339
            }
340
        }
341
    }
308 342

  
309
	@Override
310
	public void fieldChanged(JDynFormField field) {
311
		fireFieldChangeEvent(field);
312
	}
343
    public Iterable<JDynFormField> getFields() {
344
        if (!this.isContentsInitialized()) {
345
            this.initComponents();
346
        }
347
        return this.components.values();
348
    }
313 349

  
314
	@Override
315
	public void message(JDynFormField field, String message) {
316
		message(message);
317
	}
350
    @Override
351
    public JDynFormField getField(String fieldName) {
352
        if (!this.isContentsInitialized()) {
353
            this.initComponents();
354
        }
355
        JDynFormField field = this.components.get(fieldName);
356
        return field;
357
    }
318 358

  
319
	public class AbeilleTextJDynFormField extends AbstractJDynFormField {
359
    @Override
360
    public void getValues(DynObject values) {
361
        if (values == null) {
362
            return;
363
        }
364
        for (JDynFormField jfield : this.getFields()) {
365
            try {
366
                jfield.fetch(values);
367
            } catch (Exception ex) {
368
                LOGGER.warn("Can't get value of field '" + jfield.getName() + "'.", ex);
369
            }
370
        }
371
    }
320 372

  
321
		private final JTextComponent text;
322
		private final DynFormFieldDefinition fieldDefinition;
373
    @Override
374
    public Object getValue(String fieldName) {
375
        JDynFormField field = (JDynFormField) this.getField(fieldName);
376
        return field.getValue();
377
    }
323 378

  
324
		public AbeilleTextJDynFormField(DynFormFieldDefinition fieldDefinition, JTextComponent text) {
325
			super(null, AbeilleJDynForm.this.getServiceManager());
326
			this.text = text;
327
			this.fieldDefinition = fieldDefinition;
328
		}
379
    @Override
380
    public void setValue(String fieldName, Object value) {
381
        JDynFormField field = (JDynFormField) this.getField(fieldName);
382
        try {
383
            value = field.getDefinition().getDataType().coerce(value);
384
        } catch (CoercionException e) {
385
            String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'.";
386
            LOGGER.warn(msg, e);
387
            throw new RuntimeException(msg, e);
388
        }
389
        field.setValue(value);
390
    }
329 391

  
330
		@Override
331
		public Object getParameterValue() {
332
			return null;
333
		}
392
    @Override
393
    public boolean hasValidValues() {
394
        for (JDynFormField jfield : this.getFields()) {
395
            if (!jfield.hasValidValue()) {
396
                return false;
397
            }
398
        }
399
        return true;
400
    }
334 401

  
335
		@Override
336
		public String getName() {
337
			return this.fieldDefinition.getName();
338
		}
402
    @Override
403
    public boolean hasValidValues(List<String> fieldsName) {
404
        if (fieldsName == null) {
405
            fieldsName = new ArrayList<>();
406
        }
407
        for (JDynFormField jfield : this.getFields()) {
408
            if (!jfield.hasValidValue()) {
409
                fieldsName.add(jfield.getName());
410
            }
411
        }
412
        return fieldsName.isEmpty();
413
    }
339 414

  
340
		@Override
341
		public DynFormFieldDefinition getDefinition() {
342
			return this.fieldDefinition;
343
		}
415
    @Override
416
    public boolean isModified() {
417
        for (JDynFormField jfield : this.getFields()) {
418
            if (jfield.isModified()) {
419
                return true;
420
            }
421
        }
422
        return false;
423
    }
344 424

  
345
		@Override
346
		public void initComponent() {
347
			// Do nothing
348
		}
425
    @Override
426
    public void clear() {
427
        for (JDynFormField jfield : this.getFields()) {
428
            jfield.clear();
429
        }
430
    }
349 431

  
432
    @Override
433
    public void fieldEnter(JDynFormField field) {
434
        message(field.getDefinition().getDescription());
435
    }
350 436

  
351
		@Override
352
		public boolean isModified() {
353
			return true;
354
		}
437
    @Override
438
    public void fieldExit(JDynFormField field) {
439
        message();
440
    }
355 441

  
442
    @Override
443
    public void fieldChanged(JDynFormField field) {
444
        fireFieldChangeEvent(field);
445
    }
356 446

  
447
    @Override
448
    public void message(JDynFormField field, String message) {
449
        message(message);
450
    }
357 451

  
358
		@Override
359
		public Object getAssignedValue() {
360
			throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
361
		}
362

  
363
		@Override
364
		public boolean hasValidValue() {
365
			try {
366
				String s = this.text.getText();
367
				Object v = this.fieldDefinition.getDataType().coerce(s);
368
				return true;
369
			} catch (CoercionException ex) {
370
				return false;
371
			}
372
		}
373

  
374
		@Override
375
		public void setValue(Object value) {
376
			if (value == null) {
377
				this.text.setText("");
378
			} else {
379
				this.text.setText(value.toString());
380
			}
381
		}
382

  
383
		@Override
384
		public Object getValue() {
385
			try {
386
				String s = this.text.getText();
387
				return this.fieldDefinition.getDataType().coerce(s);
388
			} catch (CoercionException ex) {
389
				return null;
390
			}
391
		}
392

  
393
	}
394

  
395
	public class AbeilleComboJDynFormField extends AbstractJDynFormField {
396

  
397
		private final JComboBox combo;
398
		private final DynFormFieldDefinition fieldDefinition;
399

  
400
		public AbeilleComboJDynFormField(DynFormFieldDefinition fieldDefinition, JComboBox combo) {
401
			super(null, AbeilleJDynForm.this.getServiceManager());
402
			this.combo = combo;
403
			this.fieldDefinition = fieldDefinition;
404
			ComboBoxModel model = null;
405
			try {
406
				String createComboModelMethodName = this.getTagValueAsString("dynform.abeille.comboModel", null);
407
				if (createComboModelMethodName != null) {
408
					DynObjectManager dynobjectManager = ToolsLocator.getDynObjectManager();
409
					DynMethod createComboModelMethod = dynobjectManager.getDynMethod(createComboModelMethodName);
410
					if (createComboModelMethod != null) {
411
						model = (ComboBoxModel) createComboModelMethod.invoke(null, new Object[]{this.fieldDefinition});
412
					}
413
				}
414
			} catch (Exception ex) {
415
				// TODO: log error
416
			}
417
			if (model != null) {
418
				this.combo.setModel(model);
419
			}
420
		}
421

  
422
		@Override
423
		public Object getParameterValue() {
424
			return null;
425
		}
426

  
427
		@Override
428
		public String getName() {
429
			return this.fieldDefinition.getName();
430
		}
431

  
432

  
433
		@Override
434
		public boolean isModified() {
435
			return true;
436
		}
437

  
438
		@Override
439
		public DynFormFieldDefinition getDefinition() {
440
			return this.fieldDefinition;
441
		}
442

  
443
		@Override
444
		public void initComponent() {
445
			// Do nothing
446
		}
447

  
448
		@Override
449
		public Object getAssignedValue() {
450
			throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
451
		}
452

  
453
		@Override
454
		public boolean hasValidValue() {
455
			try {
456
				DynObjectValueItem value = (DynObjectValueItem) this.combo.getSelectedItem();
457
				if( value == null ) {
458
					return true;
459
				}
460
				this.fieldDefinition.getDataType().coerce(value.getValue());
461
				return true;
462
			} catch (CoercionException ex) {
463
				return false;
464
			}
465
		}
466

  
467
		@Override
468
		public void setValue(Object value) {
469
			if (value == null) {
470
				this.combo.setSelectedIndex(-1);
471
			} else {
472
				ComboBoxModel model = this.combo.getModel();
473
				for( int i=0; i<model.getSize(); i++) {
474
					DynObjectValueItem item = (DynObjectValueItem)model.getElementAt(i);
475
					if( value.equals(item.getValue()) ) {
476
						this.combo.setSelectedIndex(i);
477
						return;
478
					} 
479
				}
480
				this.combo.setSelectedIndex(-1);
481
			}
482
		}
483

  
484
		@Override
485
		public Object getValue() {
486
			try {
487
				DynObjectValueItem value = (DynObjectValueItem) this.combo.getSelectedItem();
488
				if( value == null ) {
489
					return null;
490
				}
491
				return this.fieldDefinition.getDataType().coerce(value.getValue());
492
			} catch (CoercionException ex) {
493
				return null;
494
			}
495
		}
496

  
497
	}
498

  
499

  
500
	public class AbeilleCheckJDynFormField extends AbstractJDynFormField {
501

  
502
		private final JCheckBox check;
503
		private final DynFormFieldDefinition fieldDefinition;
504

  
505
		public AbeilleCheckJDynFormField(DynFormFieldDefinition fieldDefinition, JCheckBox text) {
506
			super(null, AbeilleJDynForm.this.getServiceManager());
507
			this.check = text;
508
			this.fieldDefinition = fieldDefinition;
509
		}
510

  
511
		@Override
512
		public Object getParameterValue() {
513
			return null;
514
		}
515

  
516
		@Override
517
		public String getName() {
518
			return this.fieldDefinition.getName();
519
		}
520

  
521

  
522
		@Override
523
		public boolean isModified() {
524
			return true;
525
		}
526

  
527
		@Override
528
		public DynFormFieldDefinition getDefinition() {
529
			return this.fieldDefinition;
530
		}
531

  
532
		@Override
533
		public void initComponent() {
534
			// Do nothing
535
		}
536

  
537
		@Override
538
		public Object getAssignedValue() {
539
			throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
540
		}
541

  
542
		@Override
543
		public boolean hasValidValue() {
544
			return true;
545
		}
546

  
547
		@Override
548
		public void setValue(Object value) {
549
			if(value instanceof Boolean){
550
				this.check.setSelected((Boolean)value);
551
			}
552
		}
553

  
554
		@Override
555
		public Object getValue() {
556
			try {
557
				Boolean s = this.check.isSelected();
558
				return this.fieldDefinition.getDataType().coerce(s);
559
			} catch (CoercionException ex) {
560
				return null;
561
			}
562
		}
563

  
564
	}
565

  
566

  
567
	public class AbeilleDateJDynFormField extends AbstractJDynFormField {
568

  
569
		private final JSpinner text;
570
		private final DynFormFieldDefinition fieldDefinition;
571

  
572
		public AbeilleDateJDynFormField(DynFormFieldDefinition fieldDefinition, JSpinner text) {
573
			super(null, AbeilleJDynForm.this.getServiceManager());
574
			this.text = text;
575
			this.fieldDefinition = fieldDefinition;
576
		}
577

  
578
		@Override
579
		public Object getParameterValue() {
580
			return null;
581
		}
582

  
583
		@Override
584
		public String getName() {
585
			return this.fieldDefinition.getName();
586
		}
587

  
588
		@Override
589
		public DynFormFieldDefinition getDefinition() {
590
			return this.fieldDefinition;
591
		}
592

  
593
		@Override
594
		public void initComponent() {
595
			// Do nothing
596
		}
597

  
598

  
599
		@Override
600
		public boolean isModified() {
601
			return true;
602
		}
603

  
604

  
605

  
606
		@Override
607
		public Object getAssignedValue() {
608
			throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
609
		}
610

  
611
		@Override
612
		public boolean hasValidValue() {
613
			try {
614
				Object v = this.fieldDefinition.getDataType().coerce(this.text.getValue());
615
				return true;
616
			} catch (CoercionException ex) {
617
				return false;
618
			}
619
		}
620

  
621
		@Override
622
		public void setValue(Object value) {
623
			if (value != null) {
624
				this.text.setValue(value);
625
			}
626
		}
627

  
628
		@Override
629
		public Object getValue() {
630
			try {
631
				return this.fieldDefinition.getDataType().coerce(this.text.getValue());
632
			} catch (CoercionException ex) {
633
				return null;
634
			}
635
		}
636

  
637
	}
638

  
639

  
640
	class SpinnerNullDateModel extends SpinnerDateModel{
641
		/**
642
		 * 
643
		 */
644
		private static final long serialVersionUID = 6857410464070649710L;
645
		private boolean valid ;
646

  
647
		public SpinnerNullDateModel(){
648
			super();
649
			valid = false;
650
		}
651

  
652
		public void setValue(Object val){
653
			if (val != null){
654
				try{
655
					valid=true;
656
					super.setValue(val);
657
					return;
658
				}
659
				catch (Exception e){}
660
			}
661
			valid = false;
662
		}
663

  
664
		public Object getValue(){
665
			if (valid){
666
				return super.getValue();
667
			}else{
668
				return null;
669
			}
670
		}
671

  
672
		public Object getNextValue(){
673
			if (!valid){
674
				return new Date();
675
			}
676
			return super.getNextValue();
677
		}
678

  
679
		public Object getPreviousValue(){
680
			if (!valid){
681
				return new Date();
682
			}
683
			return super.getPreviousValue();
684
		}
685
	}
686

  
687

  
688 452
}

Also available in: Unified diff