gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.spi / src / main / java / org / gvsig / tools / dynform / spi / dynformfield / AbstractJDynFormField.java @ 3005
History | View | Annotate | Download (24.4 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.spi.dynformfield; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Color; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.FocusEvent; |
30 |
import java.awt.event.FocusListener; |
31 |
import java.io.PrintWriter; |
32 |
import java.io.StringWriter; |
33 |
import java.util.ArrayList; |
34 |
import java.util.HashMap; |
35 |
import java.util.HashSet; |
36 |
import java.util.Iterator; |
37 |
import java.util.List; |
38 |
import java.util.Map; |
39 |
import java.util.Objects; |
40 |
import java.util.Set; |
41 |
import javax.json.JsonObject; |
42 |
import javax.swing.AbstractButton; |
43 |
|
44 |
import javax.swing.Action; |
45 |
import javax.swing.ImageIcon; |
46 |
import javax.swing.JButton; |
47 |
import javax.swing.JComboBox; |
48 |
import javax.swing.JComponent; |
49 |
import javax.swing.JLabel; |
50 |
import javax.swing.JPanel; |
51 |
import javax.swing.JScrollPane; |
52 |
import javax.swing.JViewport; |
53 |
import javax.swing.text.JTextComponent; |
54 |
import org.apache.commons.lang3.StringUtils; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.dataTypes.CoercionException; |
57 |
import org.gvsig.tools.dataTypes.DataTypes; |
58 |
|
59 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
60 |
import org.gvsig.tools.dynform.JDynForm; |
61 |
import org.gvsig.tools.dynform.JDynFormField; |
62 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
63 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
64 |
import org.gvsig.tools.dynobject.DynClass; |
65 |
import org.gvsig.tools.dynobject.DynField; |
66 |
import org.gvsig.tools.dynobject.DynField_v2; |
67 |
import org.gvsig.tools.dynobject.DynObject; |
68 |
import org.gvsig.tools.dynobject.Tags; |
69 |
import org.gvsig.tools.script.Script; |
70 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
71 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
72 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
73 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
74 |
import org.slf4j.Logger; |
75 |
import org.slf4j.LoggerFactory; |
76 |
|
77 |
@SuppressWarnings({"rawtypes", "unchecked", "UseSpecificCatch"}) |
78 |
public abstract class AbstractJDynFormField implements JDynFormField, FocusListener { |
79 |
|
80 |
protected static final Logger LOGGER = LoggerFactory |
81 |
.getLogger(AbstractJDynFormField.class); |
82 |
|
83 |
private final DynFormSPIManager manager; |
84 |
private final JDynFormFieldFactory factory; |
85 |
private final DynFormFieldDefinition definition; |
86 |
private final ComponentsFactory componentsFactory; |
87 |
|
88 |
private JDynForm form;
|
89 |
|
90 |
protected JLabel jlabel = null; |
91 |
private JPanel jlabelpanel = null; |
92 |
private Set listeners = null; |
93 |
private JProblemIndicator problemIndicator = null; |
94 |
|
95 |
protected JComponent contents = null; |
96 |
|
97 |
protected boolean readOnly = false; |
98 |
private final List customActions; |
99 |
protected boolean emptyToNull = false; |
100 |
private Map<String, AbstractButton> buttonsOfEvents; |
101 |
|
102 |
public AbstractJDynFormField(
|
103 |
DynFormSPIManager serviceManager, |
104 |
ComponentsFactory componentsFactory, |
105 |
JDynFormFieldFactory factory, |
106 |
DynFormFieldDefinition definition, |
107 |
Object value
|
108 |
) { |
109 |
this.buttonsOfEvents = null; |
110 |
this.manager = serviceManager;
|
111 |
this.factory = factory;
|
112 |
this.definition = definition;
|
113 |
this.componentsFactory = componentsFactory;
|
114 |
this.listeners = new HashSet(); |
115 |
this.problemIndicator = this.manager.createProblemIndicator(this); |
116 |
this.customActions = new ArrayList<>(); |
117 |
if(this.definition != null){ |
118 |
this.readOnly = this.definition.isReadOnly(); |
119 |
this.loadDefaultValuesFromTags(definition.getTags());
|
120 |
} |
121 |
} |
122 |
|
123 |
public ComponentsFactory getComponentsFactory() {
|
124 |
return this.componentsFactory; |
125 |
} |
126 |
|
127 |
public void loadDefaultValuesFromTags(Tags tags) { |
128 |
try {
|
129 |
this.readOnly = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY);
|
130 |
} catch (CoercionException ex) {
|
131 |
} |
132 |
try {
|
133 |
this.emptyToNull = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_TRANSLATE_EMPTY_TO_NULL);
|
134 |
} catch (CoercionException ex) {
|
135 |
} |
136 |
} |
137 |
|
138 |
public abstract void initComponent(); |
139 |
|
140 |
public abstract Object getAssignedValue(); |
141 |
|
142 |
// public Object getParameterValue() {
|
143 |
// return this.parameters.getDynValue(DynFormSPIManager.FIELD_VALUE);
|
144 |
// }
|
145 |
//
|
146 |
@Override
|
147 |
public JComponent asJComponent() { |
148 |
if (this.contents == null) { |
149 |
try {
|
150 |
this.initComponent();
|
151 |
} catch(Throwable th) { |
152 |
StringWriter sw = new StringWriter(); |
153 |
th.printStackTrace(new PrintWriter(sw)); |
154 |
this.contents = new JLabel("##ERROR##"); |
155 |
this.problemIndicator().set("Can't create component\n"+sw.toString()); |
156 |
} |
157 |
if (!this.customActions.isEmpty()) { |
158 |
addPopupComponents(); |
159 |
} |
160 |
if (this.readOnly) { |
161 |
this.setReadOnly(readOnly);
|
162 |
} |
163 |
} |
164 |
return this.contents; |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public String getName() { |
169 |
return this.definition.getName(); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public String getLabel() { |
174 |
if (definition.getLabel() != null) { |
175 |
String label = definition.getLabel();
|
176 |
label = ToolsLocator.getI18nManager().getTranslation(label); |
177 |
return label;
|
178 |
} else {
|
179 |
return this.getName(); |
180 |
} |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public String getSeparatorTitleToUseBefore() { |
185 |
String separatorTitle = this.getTagValueAsString( |
186 |
DynFormSPIManager.TAG_DYNFORM_SEPARATOR, |
187 |
null
|
188 |
); |
189 |
return separatorTitle;
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public boolean useEmptyLabel() { |
194 |
boolean useEmpty = getTagValueAsBoolean(
|
195 |
DynFormSPIManager.TAG_DYNFORM_LABEL_EMPTY, |
196 |
false
|
197 |
); |
198 |
return useEmpty;
|
199 |
} |
200 |
|
201 |
@Override
|
202 |
public JComponent getJLabel() { |
203 |
if (this.jlabel == null) { |
204 |
this.jlabel = this.getComponentsFactory().getJLabel(definition, null); |
205 |
if( this.jlabel==null ) { |
206 |
this.jlabel = new JLabel(); |
207 |
} |
208 |
if (!this.useEmptyLabel() ) { |
209 |
this.jlabel.setText(this.getLabel()); |
210 |
} |
211 |
this.jlabel.setLabelFor(this.contents); |
212 |
if (this.getDefinition().isMandatory()) { |
213 |
this.jlabel.setForeground(Color.red.darker()); |
214 |
this.jlabel.setText("<html><b>"+this.getLabel()+"</b></html>"); |
215 |
} |
216 |
this.jlabelpanel = new JPanel(); |
217 |
this.jlabelpanel.setLayout(new BorderLayout()); |
218 |
this.jlabelpanel.add(jlabel, BorderLayout.CENTER); |
219 |
this.jlabelpanel.add(problemIndicator.asJComponent(), BorderLayout.LINE_END); |
220 |
} |
221 |
return this.jlabelpanel; |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public DynFormFieldDefinition getDefinition() {
|
226 |
return this.definition; |
227 |
} |
228 |
|
229 |
public DynFormSPIManager getServiceManager() {
|
230 |
return this.manager; |
231 |
} |
232 |
|
233 |
@Override
|
234 |
public void addListener(JDynFormFieldListener listener) { |
235 |
this.listeners.add(listener);
|
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void removeListener(JDynFormFieldListener listener) { |
240 |
this.listeners.remove(listener);
|
241 |
} |
242 |
|
243 |
/*
|
244 |
* Estos eventos no se deben disparar cuando se asignan valores por c?digo,
|
245 |
* solo cuando los modifica el usuario
|
246 |
*/
|
247 |
public void fireFieldChangedEvent() { |
248 |
if( this.isReadOnly() ) { |
249 |
return;
|
250 |
} |
251 |
Iterator it = this.listeners.iterator(); |
252 |
while (it.hasNext()) {
|
253 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
254 |
try {
|
255 |
listener.fieldChanged(this);
|
256 |
} catch (Exception ex) { |
257 |
LOGGER.info("Error calling listener " + listener.toString()
|
258 |
+ "(" + listener.getClass().getName() + ") from " |
259 |
+ this.toString() + "(" + this.getClass().getName() |
260 |
+ ").", ex);
|
261 |
} |
262 |
} |
263 |
} |
264 |
|
265 |
protected void fireFieldEnterEvent() { |
266 |
Iterator it = this.listeners.iterator(); |
267 |
while (it.hasNext()) {
|
268 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
269 |
try {
|
270 |
listener.fieldEnter(this);
|
271 |
} catch (Exception ex) { |
272 |
LOGGER.info("Error calling listener " + listener.toString()
|
273 |
+ "(" + listener.getClass().getName() + ") from " |
274 |
+ this.toString() + "(" + this.getClass().getName() |
275 |
+ ").", ex);
|
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
protected void fireFieldExitEvent() { |
281 |
Iterator it = this.listeners.iterator(); |
282 |
while (it.hasNext()) {
|
283 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
284 |
try {
|
285 |
listener.fieldExit(this);
|
286 |
} catch (Exception ex) { |
287 |
LOGGER.info("Error calling listener " + listener.toString()
|
288 |
+ "(" + listener.getClass().getName() + ") from " |
289 |
+ this.toString() + "(" + this.getClass().getName() |
290 |
+ ").", ex);
|
291 |
} |
292 |
} |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public void fireMessageEvent(String message) { |
297 |
Iterator it = this.listeners.iterator(); |
298 |
while (it.hasNext()) {
|
299 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
300 |
try {
|
301 |
listener.message(this, message);
|
302 |
} catch (Exception ex) { |
303 |
LOGGER.info("Error calling listener " + listener.toString()
|
304 |
+ "(" + listener.getClass().getName() + ") from " |
305 |
+ this.toString() + "(" + this.getClass().getName() |
306 |
+ ").", ex);
|
307 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
@Override
|
312 |
public boolean isReadOnly() { |
313 |
return this.readOnly; |
314 |
} |
315 |
|
316 |
protected boolean isForcedReadOnly() { |
317 |
return this.getTagValueAsBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY, false); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public void setReadOnly(boolean readonly) { |
322 |
// FIXME: Implememtacion por defecto, sobreescribirla en las subclases
|
323 |
// segun convenga para cada componente.
|
324 |
if( !readonly && this.isForcedReadOnly() ) { |
325 |
readonly = true;
|
326 |
} |
327 |
this.readOnly = readonly;
|
328 |
if (jlabel != null) { |
329 |
this.jlabel.setEnabled(!readonly);
|
330 |
} |
331 |
if (this.contents != null) { |
332 |
if (this.contents instanceof JTextComponent) { |
333 |
JTextComponent x = (JTextComponent) this.contents; |
334 |
x.setEditable(!readonly); |
335 |
|
336 |
} else if (this.contents instanceof JComboBox ) { |
337 |
JComboBox x = (JComboBox) this.contents; |
338 |
x.setEnabled(!readonly); |
339 |
|
340 |
} else if (this.contents instanceof JScrollPane) { |
341 |
try {
|
342 |
JViewport port = ((JScrollPane) this.contents).getViewport(); |
343 |
JTextComponent x = (JTextComponent) port.getView(); |
344 |
x.setEditable(!readonly); |
345 |
} catch (Exception ex) { |
346 |
this.contents.setEnabled(!readOnly);
|
347 |
} |
348 |
} else {
|
349 |
this.contents.setEnabled(!readOnly);
|
350 |
} |
351 |
} |
352 |
this.setReadOnlyButtonsOfEvents(readonly);
|
353 |
} |
354 |
|
355 |
public JProblemIndicator problemIndicator() {
|
356 |
return this.problemIndicator; |
357 |
} |
358 |
|
359 |
public class IllegalFieldValue extends RuntimeException { |
360 |
|
361 |
/**
|
362 |
*
|
363 |
*/
|
364 |
private static final long serialVersionUID = -4409236610055983438L; |
365 |
|
366 |
public IllegalFieldValue(JDynFormField field, String message) { |
367 |
super("The value of field '" + field.getLabel() + "' is not valid. " + message); |
368 |
} |
369 |
} |
370 |
|
371 |
@Override
|
372 |
public String toString() { |
373 |
return super.toString() + "{" + this.getName() + "}"; |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public boolean isModified() { |
378 |
boolean modified = false; |
379 |
try {
|
380 |
if (!this.isReadOnly()) { |
381 |
Object value = this.getValue(); |
382 |
if (value == null) { |
383 |
Object x = this.getAssignedValue(); |
384 |
if (StringUtils.isNotBlank(Objects.toString(x,null))) { |
385 |
modified = true;
|
386 |
} |
387 |
} else {
|
388 |
Object x = this.getAssignedValue(); |
389 |
if (x instanceof CharSequence){ |
390 |
if (!StringUtils.equals((CharSequence) x, value.toString())){ |
391 |
modified = true;
|
392 |
} |
393 |
}else if (!value.equals(this.getAssignedValue())) { |
394 |
modified = true;
|
395 |
} |
396 |
} |
397 |
} |
398 |
} catch (IllegalFieldValue ex) {
|
399 |
// Si es incorrecto el valor del campo decimos a capon que esta modificado.
|
400 |
modified = true;
|
401 |
} |
402 |
return modified;
|
403 |
} |
404 |
|
405 |
// protected Object getValueWithoutValidate() {
|
406 |
//
|
407 |
// }
|
408 |
|
409 |
private void addPopupComponents() { |
410 |
Iterator it = this.customActions.iterator(); |
411 |
while (it.hasNext()) {
|
412 |
Object obj = it.next();
|
413 |
if (obj != null) { |
414 |
Action act = (Action) obj; |
415 |
if (contents instanceof SupportPopupMenu) { |
416 |
DynFormFieldAction sact = new DynFormFieldAction(this, act); |
417 |
((SupportPopupMenu) this.contents).addActionToPopupMenu(
|
418 |
sact.getName(), |
419 |
sact); |
420 |
} |
421 |
} else {
|
422 |
if (contents instanceof SupportPopupMenu) { |
423 |
((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
|
424 |
} |
425 |
} |
426 |
|
427 |
} |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public void addActionToPopupMenu(String name, Action action) { |
432 |
if (contents != null) { |
433 |
if (contents instanceof SupportPopupMenu) { |
434 |
DynFormFieldAction sact = new DynFormFieldAction(this, action); |
435 |
((SupportPopupMenu) this.contents).addActionToPopupMenu(
|
436 |
sact.getName(), |
437 |
sact); |
438 |
} |
439 |
} else {
|
440 |
this.customActions.add(action);
|
441 |
} |
442 |
} |
443 |
|
444 |
@Override
|
445 |
public void addSeparatorToPopupMenu() { |
446 |
if (contents != null) { |
447 |
if (contents instanceof SupportPopupMenu) { |
448 |
((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
|
449 |
} |
450 |
} else {
|
451 |
this.customActions.add(null); |
452 |
} |
453 |
} |
454 |
|
455 |
public void setTranslateEmptyToNull(boolean emptyToNull) { |
456 |
this.emptyToNull = emptyToNull;
|
457 |
} |
458 |
|
459 |
public boolean translateEmptyToNull() { |
460 |
return this.emptyToNull; |
461 |
} |
462 |
|
463 |
@SuppressWarnings("UnnecessaryReturnStatement") |
464 |
@Override
|
465 |
public void clear() { |
466 |
if (this.contents == null) { |
467 |
return;
|
468 |
} |
469 |
Object value = this.getDefinition().getDefaultValue(); |
470 |
this.setValue(value);
|
471 |
// if (this.contents instanceof JTextField) {
|
472 |
// if (value != null) {
|
473 |
// value = value.toString();
|
474 |
// } else {
|
475 |
// value = "";
|
476 |
// }
|
477 |
// ((JTextField) this.contents).setText((String) value);
|
478 |
// return;
|
479 |
// }
|
480 |
// if (this.contents instanceof JSpinner) {
|
481 |
// if (value==null) {
|
482 |
// ((JSpinner) this.contents).setValue(0);
|
483 |
// return;
|
484 |
// }
|
485 |
// ((JSpinner) this.contents).setValue(value);
|
486 |
// return;
|
487 |
// }
|
488 |
// if (this.contents instanceof JList) {
|
489 |
// ((JList) this.contents).setSelectedIndex(-1);
|
490 |
// return;
|
491 |
// }
|
492 |
// if (this.contents instanceof JComboBox) {
|
493 |
// ((JComboBox) this.contents).setSelectedIndex(-1);
|
494 |
// return;
|
495 |
// }
|
496 |
} |
497 |
|
498 |
public void setForm(JDynForm form) { |
499 |
this.form = form;
|
500 |
} |
501 |
|
502 |
@Override
|
503 |
public JDynForm getForm() {
|
504 |
return this.form; |
505 |
} |
506 |
|
507 |
@Override
|
508 |
public void fetch(DynObject container) { |
509 |
// if( this.isReadOnly() ) {
|
510 |
// return;
|
511 |
// }
|
512 |
Object value = this.getValue(); |
513 |
DynClass dynclass = container.getDynClass(); |
514 |
if( dynclass!=null ) { |
515 |
DynField dynfield = dynclass.getDynField(this.getName());
|
516 |
if( dynfield!=null && dynfield.isReadOnly() ) { |
517 |
return;
|
518 |
} |
519 |
} |
520 |
container.setDynValue(this.getName(), value);
|
521 |
} |
522 |
|
523 |
protected int getTagValueAsInt(String tagname, int defaultVaue) { |
524 |
return getTagValueAsInt(tagname, null, defaultVaue); |
525 |
} |
526 |
|
527 |
protected int getTagValueAsInt(String tagname1, String tagname2, int defaultVaue) { |
528 |
DynField_v2 fielddef = (DynField_v2) this.getDefinition();
|
529 |
String tagname;
|
530 |
if (fielddef.getTags().has(tagname1)) {
|
531 |
tagname = tagname1; |
532 |
} else if (fielddef.getTags().has(tagname2)) { |
533 |
tagname = tagname2; |
534 |
} else {
|
535 |
return defaultVaue;
|
536 |
} |
537 |
try {
|
538 |
int value = fielddef.getTags().getInt(tagname);
|
539 |
return value;
|
540 |
} catch (CoercionException ex) {
|
541 |
LOGGER.warn("Can't parse tag '" + tagname + "' as int for field '" + fielddef.getName() + "'.", ex); |
542 |
} |
543 |
return defaultVaue;
|
544 |
} |
545 |
|
546 |
protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) { |
547 |
return getTagValueAsBoolean(tagname, null, defaultVaue); |
548 |
} |
549 |
|
550 |
protected boolean getTagValueAsBoolean(String tagname1, String tagname2, boolean defaultVaue) { |
551 |
DynField_v2 fielddef = (DynField_v2) this.getDefinition();
|
552 |
String tagname;
|
553 |
if (fielddef.getTags().has(tagname1)) {
|
554 |
tagname = tagname1; |
555 |
} else if (fielddef.getTags().has(tagname2)) { |
556 |
tagname = tagname2; |
557 |
} else {
|
558 |
return defaultVaue;
|
559 |
} |
560 |
try {
|
561 |
boolean value = fielddef.getTags().getBoolean(tagname);
|
562 |
return value;
|
563 |
} catch (CoercionException ex) {
|
564 |
LOGGER.warn("Can't parse tag '" + tagname + "' as boolean for field '" + fielddef.getName() + "'.", ex); |
565 |
} |
566 |
return defaultVaue;
|
567 |
} |
568 |
|
569 |
protected String getTagValueAsString(String tagname, String defaultVaue) { |
570 |
return getTagValueAsString(tagname, null, defaultVaue); |
571 |
} |
572 |
|
573 |
protected String getTagValueAsString(String tagname1, String tagname2, String defaultVaue) { |
574 |
DynField_v2 fielddef = (DynField_v2) this.getDefinition();
|
575 |
String tagname;
|
576 |
if (fielddef.getTags().has(tagname1)) {
|
577 |
tagname = tagname1; |
578 |
} else if (fielddef.getTags().has(tagname2)) { |
579 |
tagname = tagname2; |
580 |
} else {
|
581 |
return defaultVaue;
|
582 |
} |
583 |
try {
|
584 |
String value = (String) fielddef.getTags().get(tagname, DataTypes.STRING); |
585 |
return value;
|
586 |
} catch (CoercionException ex) {
|
587 |
LOGGER.warn("Can't parse tag '" + tagname + "' as string for field '" + fielddef.getName() + "'.", ex); |
588 |
} |
589 |
return defaultVaue;
|
590 |
} |
591 |
|
592 |
@Override
|
593 |
public double getResizeWeight() { |
594 |
Tags tags = this.getDefinition().getTags();
|
595 |
int resizeWeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_RESIZEWEIGHT, 0); |
596 |
if( resizeWeight<1 ) { |
597 |
return 0; |
598 |
} |
599 |
if( resizeWeight>100 ) { |
600 |
return 1; |
601 |
} |
602 |
return resizeWeight / 100.0; |
603 |
} |
604 |
|
605 |
@Override
|
606 |
public void focusGained(FocusEvent arg0) { |
607 |
fireFieldEnterEvent(); |
608 |
this.problemIndicator().restore();
|
609 |
} |
610 |
|
611 |
@Override
|
612 |
public void focusLost(FocusEvent arg0) { |
613 |
if (this.hasValidValue()) { |
614 |
this.problemIndicator().clear();
|
615 |
} else {
|
616 |
try {
|
617 |
Object value = this.getValue(); |
618 |
} catch (Exception e) { |
619 |
this.problemIndicator().set(e.getLocalizedMessage());
|
620 |
} |
621 |
} |
622 |
fireFieldExitEvent(); |
623 |
} |
624 |
|
625 |
protected void fixPreferredWidth(JComponent comp) { |
626 |
Dimension prefsize = comp.getPreferredSize();
|
627 |
if( prefsize.width>200 ) { |
628 |
prefsize.width = 200;
|
629 |
comp.setPreferredSize(prefsize); |
630 |
} |
631 |
} |
632 |
|
633 |
protected ImageIcon getIcon(String name) { |
634 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
635 |
IconTheme theme = iconThemeManager.getCurrent(); |
636 |
ImageIcon icon = theme.get(name);
|
637 |
return icon;
|
638 |
} |
639 |
|
640 |
@Override
|
641 |
public String toHTML() { |
642 |
return null; |
643 |
} |
644 |
|
645 |
@Override
|
646 |
public boolean hasClickEvent(String eventName) { |
647 |
Script script = this.getForm().getScript();
|
648 |
if( script == null ) { |
649 |
return false; |
650 |
} |
651 |
String functionName = this.getName()+"_"+eventName+"_click"; |
652 |
List<String> names = script.getNames(); |
653 |
if( names!=null && names.contains(functionName)) { |
654 |
return true; |
655 |
} |
656 |
return false; |
657 |
} |
658 |
|
659 |
public AbstractButton getButtonForClickEvent(String eventName, String tip, String iconName) { |
660 |
JDynFormField jfield = this;
|
661 |
JDynForm jform = this.getForm();
|
662 |
|
663 |
Script script = jform.getScript(); |
664 |
if( script == null ) { |
665 |
return null; |
666 |
} |
667 |
if( this.buttonsOfEvents==null ) { |
668 |
this.buttonsOfEvents = new HashMap<>(); |
669 |
} |
670 |
AbstractButton button = buttonsOfEvents.get(eventName+"_click"); |
671 |
if( button!=null ) { |
672 |
return button;
|
673 |
} |
674 |
String functionName = this.getName()+"_"+eventName+"_click"; |
675 |
button = new JButton(); |
676 |
ToolsSwingUtils.configurePickersButton( |
677 |
button, |
678 |
tip, |
679 |
iconName, |
680 |
(ActionEvent e) -> {
|
681 |
try {
|
682 |
// TODO: ?Lanzarlo en un thread aparte?
|
683 |
script.invokeFunction(functionName, new Object[] {jform, jfield}); |
684 |
} catch (Throwable t) { |
685 |
LOGGER.warn("Error calling event '"+functionName+"'",t); |
686 |
} |
687 |
}, |
688 |
new FocusListener() { |
689 |
@Override
|
690 |
public void focusGained(FocusEvent e) { |
691 |
fireFieldEnterEvent(); |
692 |
} |
693 |
|
694 |
@Override
|
695 |
public void focusLost(FocusEvent e) { |
696 |
} |
697 |
} |
698 |
); |
699 |
this.buttonsOfEvents.put(eventName+"_click",button); |
700 |
return button;
|
701 |
} |
702 |
|
703 |
protected JsonObject getEventConfiguration(String name) { |
704 |
// Habria que cachear estas configuraciones en el jDynFormField
|
705 |
JDynForm jform = this.getForm();
|
706 |
|
707 |
Script script = jform.getScript(); |
708 |
if( script == null ) { |
709 |
return null; |
710 |
} |
711 |
String functionName = this.getName()+"_"+name+"_config"; |
712 |
try {
|
713 |
Object x = script.invokeFunction(functionName, new Object[0]); |
714 |
return (JsonObject)x;
|
715 |
} catch (Throwable t) { |
716 |
LOGGER.warn("Error calling event '"+functionName+"'",t); |
717 |
return null; |
718 |
} |
719 |
} |
720 |
|
721 |
protected void setReadOnlyButtonsOfEvents(boolean readonly) { |
722 |
if( this.buttonsOfEvents==null ) { |
723 |
return;
|
724 |
} |
725 |
for (Map.Entry<String, AbstractButton> entry : buttonsOfEvents.entrySet()) { |
726 |
String name = entry.getKey();
|
727 |
AbstractButton button = entry.getValue();
|
728 |
try {
|
729 |
JsonObject config = this.getEventConfiguration(StringUtils.removeEnd(name, "_click")); |
730 |
boolean enabled = config.getBoolean("enabled"); |
731 |
button.setEnabled(enabled); |
732 |
} catch(Throwable ex) { |
733 |
button.setEnabled(!readonly); |
734 |
} |
735 |
} |
736 |
} |
737 |
} |