gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / impl / DefaultJDynForm.java @ 2547
History | View | Annotate | Download (26.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.tools.dynform.impl; |
24 |
|
25 |
import java.awt.Component; |
26 |
import org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm; |
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import javax.swing.Action; |
34 |
import javax.swing.JComponent; |
35 |
import javax.swing.JTabbedPane; |
36 |
|
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dataTypes.CoercionException; |
40 |
import org.gvsig.tools.dataTypes.DataType; |
41 |
import org.gvsig.tools.dataTypes.DataTypes; |
42 |
import org.gvsig.tools.dynform.DynFormDefinition; |
43 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
44 |
import org.gvsig.tools.dynform.JDynFormField; |
45 |
import org.gvsig.tools.dynform.JDynFormField.JDynFormFieldListener; |
46 |
import org.gvsig.tools.dynform.spi.dynformfield.SupportPopupMenu; |
47 |
import org.gvsig.tools.dynobject.DynClass; |
48 |
import org.gvsig.tools.dynobject.DynField; |
49 |
import org.gvsig.tools.dynobject.DynField_v2; |
50 |
import org.gvsig.tools.dynobject.DynObject; |
51 |
import org.gvsig.tools.service.ServiceException; |
52 |
|
53 |
import java.awt.GridBagConstraints; |
54 |
import java.awt.GridBagLayout; |
55 |
import java.awt.Insets; |
56 |
import java.util.Collection; |
57 |
import javax.swing.JLabel; |
58 |
import javax.swing.JPanel; |
59 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
60 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
61 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_CLEAR; |
62 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISMODIFIED; |
63 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ISREADONLY; |
64 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDCHANGED; |
65 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDENTER; |
66 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONFIELDEXIT; |
67 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONLOAD; |
68 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_ONSETVALUES; |
69 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.USERCODE_FORM_VALIDATE; |
70 |
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
71 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
72 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
73 |
import org.gvsig.tools.i18n.I18nManager; |
74 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
75 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
76 |
|
77 |
@SuppressWarnings("UseSpecificCatch") |
78 |
public class DefaultJDynForm extends AbstractJDynForm implements JDynFormFieldListener { |
79 |
|
80 |
private Map components = null; |
81 |
private final ComponentsFactory componentsFactory; |
82 |
private JComponent fieldsContainer; |
83 |
|
84 |
public DefaultJDynForm(
|
85 |
DynFormSPIManager manager, |
86 |
JDynFormFactory factory, |
87 |
DynFormDefinition definition, |
88 |
DynFormContext context |
89 |
) { |
90 |
super(manager, factory, definition, context);
|
91 |
this.components = new HashMap(); |
92 |
this.componentsFactory = manager.createDefaultComponentsFactory();
|
93 |
} |
94 |
|
95 |
@Override
|
96 |
public JComponent getFieldsContainer() { |
97 |
if( this.fieldsContainer != null ) { |
98 |
return this.fieldsContainer; |
99 |
} |
100 |
try {
|
101 |
JComponent component;
|
102 |
switch (this.getLayoutMode()) { |
103 |
case USE_PLAIN:
|
104 |
case USE_TREE:
|
105 |
default:
|
106 |
component = getFieldsContainerPlain(); |
107 |
break;
|
108 |
case USE_TABS:
|
109 |
component = getFieldsContainerUseTabs(); |
110 |
break;
|
111 |
case USE_SEPARATORS:
|
112 |
component = getFieldsContainerUseSeparators(); |
113 |
break;
|
114 |
} |
115 |
this.callUserEvent(USERCODE_FORM_ONLOAD, this); |
116 |
this.fieldsContainer = component;
|
117 |
return component;
|
118 |
} catch (ServiceException ex) {
|
119 |
throw new RuntimeException(ex); |
120 |
} |
121 |
} |
122 |
|
123 |
private JComponent getFieldsContainerPlain() throws ServiceException { |
124 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
125 |
GridBagConstraints c = new GridBagConstraints(); |
126 |
c.insets = new Insets(2, 2, 2, 2); |
127 |
c.anchor = GridBagConstraints.PAGE_START;
|
128 |
JPanel fieldsPanel = new JPanel(); |
129 |
fieldsPanel.setLayout(new GridBagLayout()); |
130 |
int gridy = 0; |
131 |
List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(); |
132 |
for (DynFormFieldDefinition fieldDefinition : fields) {
|
133 |
if (fieldDefinition.isHidden()) {
|
134 |
continue;
|
135 |
} |
136 |
JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
137 |
this.getContext(),
|
138 |
fieldDefinition |
139 |
); |
140 |
JDynFormField jfield = factory.create( |
141 |
getServiceManager(), |
142 |
this.componentsFactory,
|
143 |
fieldDefinition, |
144 |
null
|
145 |
); |
146 |
if (jfield instanceof AbstractJDynFormField) { |
147 |
((AbstractJDynFormField) jfield).setForm(this);
|
148 |
} |
149 |
jfield.addListener(this);
|
150 |
if (this.isReadOnly()) { |
151 |
jfield.setReadOnly(this.isReadOnly());
|
152 |
} |
153 |
this.configurePopupMenu(jfield);
|
154 |
String sep = jfield.getSeparatorTitleToUseBefore();
|
155 |
if( !StringUtils.isBlank(sep) ) {
|
156 |
c.gridx = 0;
|
157 |
c.gridy = gridy; |
158 |
c.gridwidth = 2;
|
159 |
c.gridheight = 1;
|
160 |
c.fill = GridBagConstraints.HORIZONTAL;
|
161 |
c.weightx = 1;
|
162 |
c.weighty = 0;
|
163 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(ToolsLocator.getI18nManager().getTranslation(sep)), c); |
164 |
gridy++; |
165 |
} |
166 |
if( jfield.useEmptyLabel() ) {
|
167 |
c.gridx = 0;
|
168 |
c.gridy = gridy; |
169 |
c.gridwidth = 2;
|
170 |
c.gridheight = 1;
|
171 |
} else {
|
172 |
c.gridx = 0;
|
173 |
c.gridy = gridy; |
174 |
c.gridwidth = 1;
|
175 |
c.gridheight = 1;
|
176 |
c.fill = GridBagConstraints.HORIZONTAL;
|
177 |
c.weightx = 0;
|
178 |
c.weighty = 0;
|
179 |
jfield.getJLabel().setAlignmentY(Component.TOP_ALIGNMENT);
|
180 |
fieldsPanel.add(jfield.getJLabel(), c); |
181 |
c.gridx = 1;
|
182 |
c.gridy = gridy; |
183 |
c.gridwidth = 1;
|
184 |
c.gridheight = 1;
|
185 |
} |
186 |
double weighty = jfield.getResizeWeight();
|
187 |
if( weighty>0 ) { |
188 |
c.fill = GridBagConstraints.BOTH;
|
189 |
c.weightx = 1;
|
190 |
c.weighty = weighty; |
191 |
fieldsPanel.add(jfield.asJComponent(), c); |
192 |
} else {
|
193 |
c.fill = GridBagConstraints.HORIZONTAL;
|
194 |
c.weightx = 1;
|
195 |
c.weighty = 0;
|
196 |
fieldsPanel.add(jfield.asJComponent(), c); |
197 |
} |
198 |
this.components.put(jfield.getName(), jfield);
|
199 |
gridy++; |
200 |
|
201 |
} |
202 |
// Con esto forzamos a alinear los componentes arriba.
|
203 |
c.gridx = 0;
|
204 |
c.gridy = gridy; |
205 |
c.gridwidth = 1;
|
206 |
c.gridheight = 1;
|
207 |
c.fill = GridBagConstraints.VERTICAL;
|
208 |
c.weightx = 0;
|
209 |
c.weighty = 0.001;
|
210 |
fieldsPanel.add(new JLabel(), c); |
211 |
return fieldsPanel;
|
212 |
} |
213 |
|
214 |
private JComponent getFieldsContainerUseSeparators() throws ServiceException { |
215 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
216 |
List<String> groups = this.getDefinition().getGroups(); |
217 |
GridBagConstraints c = new GridBagConstraints(); |
218 |
c.insets = new Insets(2, 2, 2, 2); |
219 |
c.anchor = GridBagConstraints.PAGE_START;
|
220 |
JPanel fieldsPanel = new JPanel(); |
221 |
fieldsPanel.setLayout(new GridBagLayout()); |
222 |
int gridy = 0; |
223 |
for (String group : groups) { |
224 |
boolean firstField = true; |
225 |
List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(group); |
226 |
for (DynFormFieldDefinition fieldDefinition : fields) {
|
227 |
if (fieldDefinition.isHidden()) {
|
228 |
continue;
|
229 |
} |
230 |
JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
231 |
this.getContext(),
|
232 |
fieldDefinition |
233 |
); |
234 |
JDynFormField jfield = factory.create( |
235 |
getServiceManager(), |
236 |
this.componentsFactory,
|
237 |
fieldDefinition, |
238 |
null
|
239 |
); |
240 |
if (jfield instanceof AbstractJDynFormField) { |
241 |
((AbstractJDynFormField) jfield).setForm(this);
|
242 |
} |
243 |
jfield.addListener(this);
|
244 |
if (this.isReadOnly()) { |
245 |
jfield.setReadOnly(this.isReadOnly());
|
246 |
} |
247 |
|
248 |
this.configurePopupMenu(jfield);
|
249 |
if( firstField ) {
|
250 |
firstField = false;
|
251 |
c.gridx = 0;
|
252 |
c.gridy = gridy; |
253 |
c.gridwidth = 2;
|
254 |
c.gridheight = 1;
|
255 |
c.fill = GridBagConstraints.HORIZONTAL;
|
256 |
c.weightx = 1;
|
257 |
c.weighty = 0;
|
258 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(ToolsLocator.getI18nManager().getTranslation(group)), c); |
259 |
gridy++; |
260 |
} |
261 |
String sep = jfield.getSeparatorTitleToUseBefore();
|
262 |
if( !StringUtils.isBlank(sep) ) {
|
263 |
c.gridx = 0;
|
264 |
c.gridy = gridy; |
265 |
c.gridwidth = 2;
|
266 |
c.gridheight = 1;
|
267 |
c.fill = GridBagConstraints.HORIZONTAL;
|
268 |
c.weightx = 1;
|
269 |
c.weighty = 0;
|
270 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(ToolsLocator.getI18nManager().getTranslation(sep)), c); |
271 |
gridy++; |
272 |
} |
273 |
if( jfield.useEmptyLabel() ) {
|
274 |
c.gridx = 0;
|
275 |
c.gridy = gridy; |
276 |
c.gridwidth = 2;
|
277 |
c.gridheight = 1;
|
278 |
} else {
|
279 |
c.gridx = 0;
|
280 |
c.gridy = gridy; |
281 |
c.gridwidth = 1;
|
282 |
c.gridheight = 1;
|
283 |
c.fill = GridBagConstraints.HORIZONTAL;
|
284 |
c.weightx = 0;
|
285 |
c.weighty = 0;
|
286 |
fieldsPanel.add(jfield.getJLabel(), c); |
287 |
c.gridx = 1;
|
288 |
c.gridy = gridy; |
289 |
c.gridwidth = 1;
|
290 |
c.gridheight = 1;
|
291 |
} |
292 |
double weighty = jfield.getResizeWeight();
|
293 |
if( weighty>0 ) { |
294 |
c.fill = GridBagConstraints.BOTH;
|
295 |
c.weightx = 1;
|
296 |
c.weighty = weighty; |
297 |
fieldsPanel.add(jfield.asJComponent(), c); |
298 |
} else {
|
299 |
c.fill = GridBagConstraints.HORIZONTAL;
|
300 |
c.weightx = 1;
|
301 |
c.weighty = 0;
|
302 |
fieldsPanel.add(jfield.asJComponent(), c); |
303 |
} |
304 |
this.components.put(jfield.getName(), jfield);
|
305 |
gridy++; |
306 |
} |
307 |
} |
308 |
// Con esto forzamos a alinear los componentes arriba.
|
309 |
c.gridx = 0;
|
310 |
c.gridy = gridy; |
311 |
c.gridwidth = 1;
|
312 |
c.gridheight = 1;
|
313 |
c.fill = GridBagConstraints.VERTICAL;
|
314 |
c.weightx = 0;
|
315 |
c.weighty = 0.001;
|
316 |
fieldsPanel.add(new JLabel(), c); |
317 |
return fieldsPanel;
|
318 |
} |
319 |
|
320 |
private JComponent getFieldsContainerUseTabs() throws ServiceException { |
321 |
|
322 |
JTabbedPane tabbedPane = new JTabbedPane(); |
323 |
if (this.getDefinition().getTags().has("TabPlacement")) { |
324 |
try {
|
325 |
tabbedPane.setTabPlacement(this.getDefinition().getTags().getInt("TabPlacement")); |
326 |
} catch (Exception e) { |
327 |
// Ignorada
|
328 |
} |
329 |
} |
330 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
331 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
332 |
List<String> groups = this.getDefinition().getGroups(); |
333 |
GridBagConstraints c = new GridBagConstraints(); |
334 |
c.insets = new Insets(2, 2, 2, 2); |
335 |
c.anchor = GridBagConstraints.PAGE_START;
|
336 |
for (String group : groups) { |
337 |
JPanel fieldsPanel = new JPanel(); |
338 |
GridBagLayout gridbag = new GridBagLayout(); |
339 |
fieldsPanel.setLayout(gridbag); |
340 |
List<DynFormFieldDefinition> fields = this.getDefinition().getDefinitions(group); |
341 |
int gridy = 0; |
342 |
for (DynFormFieldDefinition fieldDefinition : fields) {
|
343 |
if (fieldDefinition.isHidden()) {
|
344 |
continue;
|
345 |
} |
346 |
JDynFormFieldFactory factory = getServiceManager().getJDynFormFieldFactory( |
347 |
this.getContext(),
|
348 |
fieldDefinition |
349 |
); |
350 |
JDynFormField jfield = factory.create( |
351 |
getServiceManager(), |
352 |
this.componentsFactory,
|
353 |
fieldDefinition, |
354 |
null
|
355 |
); |
356 |
if (jfield instanceof AbstractJDynFormField) { |
357 |
((AbstractJDynFormField) jfield).setForm(this);
|
358 |
} |
359 |
jfield.setReadOnly(this.isReadOnly());
|
360 |
jfield.addListener(this);
|
361 |
if (this.isReadOnly()) { |
362 |
jfield.setReadOnly(this.isReadOnly());
|
363 |
} |
364 |
this.configurePopupMenu(jfield);
|
365 |
String sep = jfield.getSeparatorTitleToUseBefore();
|
366 |
if( !StringUtils.isBlank(sep) ) {
|
367 |
c.gridx = 0;
|
368 |
c.gridy = gridy; |
369 |
c.gridwidth = 2;
|
370 |
c.gridheight = 1;
|
371 |
c.fill = GridBagConstraints.HORIZONTAL;
|
372 |
c.weightx = 1;
|
373 |
c.weighty = 0;
|
374 |
fieldsPanel.add(toolsSwingManager.createTitledSeparator(ToolsLocator.getI18nManager().getTranslation(sep)), c); |
375 |
gridy++; |
376 |
} |
377 |
if( jfield.useEmptyLabel() ) {
|
378 |
c.gridx = 0;
|
379 |
c.gridy = gridy; |
380 |
c.gridwidth = 2;
|
381 |
c.gridheight = 1;
|
382 |
} else {
|
383 |
c.gridx = 0;
|
384 |
c.gridy = gridy; |
385 |
c.gridwidth = 1;
|
386 |
c.gridheight = 1;
|
387 |
c.fill = GridBagConstraints.HORIZONTAL;
|
388 |
c.weightx = 0;
|
389 |
c.weighty = 0;
|
390 |
jfield.getJLabel().setAlignmentY(Component.TOP_ALIGNMENT);
|
391 |
fieldsPanel.add(jfield.getJLabel(), c); |
392 |
c.gridx = 1;
|
393 |
c.gridy = gridy; |
394 |
c.gridwidth = 1;
|
395 |
c.gridheight = 1;
|
396 |
} |
397 |
double weighty = jfield.getResizeWeight();
|
398 |
if( weighty>0 ) { |
399 |
c.fill = GridBagConstraints.BOTH;
|
400 |
c.weightx = 1;
|
401 |
c.weighty = weighty; |
402 |
fieldsPanel.add(jfield.asJComponent(), c); |
403 |
} else {
|
404 |
c.fill = GridBagConstraints.HORIZONTAL;
|
405 |
c.weightx = 1;
|
406 |
c.weighty = 0;
|
407 |
fieldsPanel.add(jfield.asJComponent(), c); |
408 |
} |
409 |
this.components.put(jfield.getName(), jfield);
|
410 |
gridy++; |
411 |
} |
412 |
if( gridy>0 ) { |
413 |
String tablabel = group;
|
414 |
if (StringUtils.isEmpty(tablabel)) {
|
415 |
tablabel = ToolsLocator.getI18nManager().getTranslation("General");
|
416 |
} |
417 |
tablabel = i18n.getTranslation(tablabel); |
418 |
|
419 |
// Con esto forzamos a alinear los componentes arriba.
|
420 |
c.gridx = 0;
|
421 |
c.gridy = gridy; |
422 |
c.gridwidth = 1;
|
423 |
c.gridheight = 1;
|
424 |
c.fill = GridBagConstraints.VERTICAL;
|
425 |
c.weightx = 0;
|
426 |
c.weighty = 0.001;
|
427 |
fieldsPanel.add(new JLabel(), c); |
428 |
|
429 |
tabbedPane.addTab(tablabel,fieldsPanel); |
430 |
} |
431 |
} |
432 |
|
433 |
try {
|
434 |
String defaultGroupName = (String) this.getDefinition().getTags().get("defaultGroup", DataTypes.STRING); |
435 |
List<String> groupsNames = this.getDefinition().getGroups(); |
436 |
int groupIndex = groupsNames.indexOf(defaultGroupName);
|
437 |
if (groupIndex >= 0) { |
438 |
tabbedPane.setSelectedIndex(groupIndex); |
439 |
} |
440 |
} catch (Exception ex) { |
441 |
// Ignore errors and don't set a tab by default
|
442 |
} |
443 |
|
444 |
return tabbedPane;
|
445 |
} |
446 |
|
447 |
@Override
|
448 |
public void setReadOnly(boolean readOnly) { |
449 |
if( this.isReadOnly() == readOnly ) { |
450 |
return;
|
451 |
} |
452 |
super.setReadOnly(readOnly);
|
453 |
if (this.isContentsInitialized()) { |
454 |
Iterator it = this.getFieldsIterator(); |
455 |
while (it.hasNext()) {
|
456 |
JDynFormField field = (JDynFormField) it.next(); |
457 |
if( readOnly ) {
|
458 |
field.setReadOnly(true);
|
459 |
} else if( field.getDefinition().isReadOnly() ) { |
460 |
field.setReadOnly(true);
|
461 |
} else {
|
462 |
field.setReadOnly(false);
|
463 |
} |
464 |
} |
465 |
} |
466 |
} |
467 |
|
468 |
@Override
|
469 |
public void setValues(DynObject values) { |
470 |
if (!this.isContentsInitialized()) { |
471 |
this.values = values;
|
472 |
return;
|
473 |
} |
474 |
|
475 |
DynFormDefinition def = this.getDefinition();
|
476 |
for (DynFormFieldDefinition field : def) {
|
477 |
if( field.isHidden() ) {
|
478 |
continue;
|
479 |
} |
480 |
String name = field.getName();
|
481 |
if ( StringUtils.isBlank(name) ) {
|
482 |
LOGGER.warn("Field name is null or empty ");
|
483 |
continue;
|
484 |
} |
485 |
JDynFormField jfield = (JDynFormField) this.getField(name);
|
486 |
if (jfield == null) { |
487 |
LOGGER.warn("Can't retrieve form field asociated to the field '" + name + "' of class '" + def.getName() + "'."); |
488 |
continue;
|
489 |
} |
490 |
Object value = values.getDynValue(name);
|
491 |
if ( value == null) { |
492 |
if (field.getDataType().getType() == DataTypes.LIST) {
|
493 |
try {
|
494 |
if (((DynField_v2) field).getDynClassOfItems() != null) { |
495 |
values.setDynValue(name, new ArrayList<>()); |
496 |
} |
497 |
} catch (Exception e) { |
498 |
LOGGER.warn("Problems initializing the DynObject List", e);
|
499 |
} |
500 |
} |
501 |
} |
502 |
jfield.setValue(value); |
503 |
} |
504 |
this.callUserEvent(USERCODE_FORM_ONSETVALUES, this, values); |
505 |
try {
|
506 |
if( (boolean) this.callUserFunction(USERCODE_FORM_ISREADONLY, this) ) { |
507 |
this.setReadOnly(true); |
508 |
} |
509 |
} catch (Exception ex) { |
510 |
} |
511 |
} |
512 |
|
513 |
@Override
|
514 |
public void getValues(DynObject values) { |
515 |
if (values == null) { |
516 |
return;
|
517 |
} |
518 |
DynField[] fields = values.getDynClass().getDynFields();
|
519 |
for (DynField field : fields) {
|
520 |
String name = field.getName();
|
521 |
JDynFormField jfield = (JDynFormField) this.getField(name);
|
522 |
if (jfield != null && !jfield.isReadOnly()) { |
523 |
try {
|
524 |
jfield.fetch(values); |
525 |
} catch (Exception ex) { |
526 |
LOGGER.warn("Can't get value of field '" + name + "'.", ex); |
527 |
} |
528 |
} |
529 |
} |
530 |
} |
531 |
|
532 |
@Override
|
533 |
public boolean hasValidValues() { |
534 |
Iterator it = this.getFieldsIterator(); |
535 |
while (it.hasNext()) {
|
536 |
JDynFormField jfield = (JDynFormField) it.next(); |
537 |
if (!jfield.hasValidValue()) {
|
538 |
return false; |
539 |
} |
540 |
} |
541 |
try {
|
542 |
return (boolean) this.callUserFunction(USERCODE_FORM_VALIDATE, this); |
543 |
} catch (Exception ex) { |
544 |
} |
545 |
return true; |
546 |
} |
547 |
|
548 |
@Override
|
549 |
public boolean hasValidValues(List<String> fieldsName) { |
550 |
if (fieldsName == null) { |
551 |
fieldsName = new ArrayList<>(); |
552 |
} |
553 |
Iterator it = this.getFieldsIterator(); |
554 |
while (it.hasNext()) {
|
555 |
JDynFormField jfield = (JDynFormField) it.next(); |
556 |
if (!jfield.hasValidValue()) {
|
557 |
fieldsName.add(jfield.getName()); |
558 |
} |
559 |
} |
560 |
return fieldsName.isEmpty();
|
561 |
} |
562 |
|
563 |
@Override
|
564 |
public Object getValue(String fieldName) { |
565 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
566 |
return field.getValue();
|
567 |
} |
568 |
|
569 |
@Override
|
570 |
public void setValue(String fieldName, Object value) { |
571 |
JDynFormField field = (JDynFormField) this.getField(fieldName);
|
572 |
try {
|
573 |
value = field.getDefinition().getDataType().coerce(value); |
574 |
} catch (CoercionException e) {
|
575 |
String msg = "Invalid value '" + ((value == null) ? "(null)" : value.toString()) + "' for field '" + fieldName + "'."; |
576 |
LOGGER.warn(msg, e); |
577 |
throw new RuntimeException(msg, e); |
578 |
} |
579 |
field.setValue(value); |
580 |
} |
581 |
|
582 |
@Override
|
583 |
public boolean isModified() { |
584 |
if( this.isReadOnly() ) { |
585 |
return false; |
586 |
} |
587 |
try {
|
588 |
if (this.getScript() !=null) { |
589 |
return (boolean) this.callUserFunction(USERCODE_FORM_ISMODIFIED, this, values); |
590 |
} |
591 |
} catch (NoSuchMethodException ex) { |
592 |
} catch (Exception ex) { |
593 |
LOGGER.warn("Error calling user function form_IsModified.",ex);
|
594 |
} |
595 |
Iterator it = this.getFieldsIterator(); |
596 |
while (it.hasNext()) {
|
597 |
JDynFormField jfield = (JDynFormField) it.next(); |
598 |
if (jfield.isModified()) {
|
599 |
return true; |
600 |
} |
601 |
} |
602 |
return false; |
603 |
} |
604 |
|
605 |
@Override
|
606 |
public void fieldEnter(JDynFormField field) { |
607 |
message(field.getDefinition().getDescription()); |
608 |
this.callUserEvent(USERCODE_FORM_ONFIELDENTER, this, field); |
609 |
} |
610 |
|
611 |
@Override
|
612 |
public void fieldExit(JDynFormField field) { |
613 |
message(); |
614 |
this.callUserEvent(USERCODE_FORM_ONFIELDEXIT, this, field); |
615 |
} |
616 |
|
617 |
@Override
|
618 |
public void message(JDynFormField field, String message) { |
619 |
message(message); |
620 |
} |
621 |
|
622 |
@Override
|
623 |
public void fieldChanged(JDynFormField field) { |
624 |
fireFieldChangeEvent(field); |
625 |
this.callUserEvent(USERCODE_FORM_ONFIELDCHANGED, this, field); |
626 |
} |
627 |
|
628 |
@Override
|
629 |
public JDynFormField getField(String fieldName) { |
630 |
JDynFormField field = (JDynFormField) this.components.get(fieldName);
|
631 |
return field;
|
632 |
} |
633 |
|
634 |
public Iterator getFieldsIterator() { |
635 |
if (!this.isContentsInitialized()) { |
636 |
this.initComponents();
|
637 |
} |
638 |
return this.components.values().iterator(); |
639 |
} |
640 |
|
641 |
public Collection getShowFields() { |
642 |
return this.components.values(); |
643 |
} |
644 |
|
645 |
@Override
|
646 |
public void clear() { |
647 |
Iterator it = this.components.entrySet().iterator(); |
648 |
while (it.hasNext()) {
|
649 |
Map.Entry entry = (Map.Entry) it.next(); |
650 |
((JDynFormField) (entry.getValue())).clear(); |
651 |
} |
652 |
this.callUserEvent(USERCODE_FORM_CLEAR, this); |
653 |
} |
654 |
|
655 |
@Override
|
656 |
public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
657 |
super.addActionToPopupMenu(tipo, name, action);
|
658 |
if ( this.isContentsInitialized() ) { |
659 |
Iterator it = this.components.keySet().iterator(); |
660 |
while (it.hasNext()) {
|
661 |
String key = (String) it.next(); |
662 |
Object obj = this.components.get(key); |
663 |
if (obj instanceof JDynFormField) { |
664 |
JDynFormField field = (JDynFormField) obj; |
665 |
if (tipo == field.getDefinition().getDataType()) {
|
666 |
if (field.asJComponent() instanceof SupportPopupMenu) { |
667 |
field.addActionToPopupMenu(name, action); |
668 |
} |
669 |
} |
670 |
} |
671 |
} |
672 |
} |
673 |
} |
674 |
|
675 |
@Override
|
676 |
public void addSeparatorToPopupMenu(DataType tipo) { |
677 |
super.addSeparatorToPopupMenu(tipo);
|
678 |
if ( this.isContentsInitialized() ) { |
679 |
Iterator it = this.components.keySet().iterator(); |
680 |
while (it.hasNext()) {
|
681 |
String key = (String) it.next(); |
682 |
Object obj = this.components.get(key); |
683 |
if (obj instanceof JDynFormField) { |
684 |
JDynFormField field = (JDynFormField) obj; |
685 |
if (tipo == field.getDefinition().getDataType()) {
|
686 |
if (field.asJComponent() instanceof SupportPopupMenu) { |
687 |
((SupportPopupMenu) field.asJComponent()).addSeparatorToPopupMenu(); |
688 |
} |
689 |
} |
690 |
} |
691 |
} |
692 |
} |
693 |
} |
694 |
|
695 |
@Override
|
696 |
public void setBorder(boolean border) { |
697 |
super.setBorder(border && this.getShowFields().size() >1); |
698 |
} |
699 |
|
700 |
} |