Statistics
| Revision:

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 954 jbadia
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 1405 jjdelcerro
 * 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 954 jbadia
 *
11 1405 jjdelcerro
 * 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 954 jbadia
 *
16 1405 jjdelcerro
 * 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 954 jbadia
 *
20 1405 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 954 jbadia
 */
23 919 jjdelcerro
package org.gvsig.tools.dynform.spi.dynformfield;
24
25
import java.awt.BorderLayout;
26
import java.awt.Color;
27 2496 jjdelcerro
import java.awt.Dimension;
28 2928 jjdelcerro
import java.awt.event.ActionEvent;
29 2474 jjdelcerro
import java.awt.event.FocusEvent;
30
import java.awt.event.FocusListener;
31 2800 fdiaz
import java.io.PrintWriter;
32
import java.io.StringWriter;
33 967 jbadia
import java.util.ArrayList;
34 2928 jjdelcerro
import java.util.HashMap;
35 919 jjdelcerro
import java.util.HashSet;
36
import java.util.Iterator;
37 967 jbadia
import java.util.List;
38 2928 jjdelcerro
import java.util.Map;
39 2487 jolivas
import java.util.Objects;
40 919 jjdelcerro
import java.util.Set;
41 3005 jjdelcerro
import javax.json.JsonObject;
42 2928 jjdelcerro
import javax.swing.AbstractButton;
43 919 jjdelcerro
44
import javax.swing.Action;
45 2496 jjdelcerro
import javax.swing.ImageIcon;
46 2928 jjdelcerro
import javax.swing.JButton;
47 1145 jjdelcerro
import javax.swing.JComboBox;
48 919 jjdelcerro
import javax.swing.JComponent;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51 940 jjdelcerro
import javax.swing.JScrollPane;
52
import javax.swing.JViewport;
53
import javax.swing.text.JTextComponent;
54 2487 jolivas
import org.apache.commons.lang3.StringUtils;
55 1946 jjdelcerro
import org.gvsig.tools.ToolsLocator;
56 1225 jjdelcerro
import org.gvsig.tools.dataTypes.CoercionException;
57
import org.gvsig.tools.dataTypes.DataTypes;
58 919 jjdelcerro
59 933 jjdelcerro
import org.gvsig.tools.dynform.DynFormFieldDefinition;
60 1149 jjdelcerro
import org.gvsig.tools.dynform.JDynForm;
61 933 jjdelcerro
import org.gvsig.tools.dynform.JDynFormField;
62 919 jjdelcerro
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
63 1881 jjdelcerro
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
64 1947 jjdelcerro
import org.gvsig.tools.dynobject.DynClass;
65
import org.gvsig.tools.dynobject.DynField;
66 1225 jjdelcerro
import org.gvsig.tools.dynobject.DynField_v2;
67 919 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
68 1405 jjdelcerro
import org.gvsig.tools.dynobject.Tags;
69 2928 jjdelcerro
import org.gvsig.tools.script.Script;
70 2496 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
71 2928 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingUtils;
72 2496 jjdelcerro
import org.gvsig.tools.swing.icontheme.IconTheme;
73
import org.gvsig.tools.swing.icontheme.IconThemeManager;
74 919 jjdelcerro
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
76
77 2928 jjdelcerro
@SuppressWarnings({"rawtypes", "unchecked", "UseSpecificCatch"})
78 2474 jjdelcerro
public abstract class AbstractJDynFormField implements JDynFormField, FocusListener {
79 919 jjdelcerro
80 1881 jjdelcerro
    protected static final Logger LOGGER = LoggerFactory
81 1405 jjdelcerro
            .getLogger(AbstractJDynFormField.class);
82 919 jjdelcerro
83 1881 jjdelcerro
    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 1947 jjdelcerro
    protected JLabel jlabel = null;
91 1405 jjdelcerro
    private JPanel jlabelpanel = null;
92
    private Set listeners = null;
93
    private JProblemIndicator problemIndicator = null;
94 919 jjdelcerro
95 1405 jjdelcerro
    protected JComponent contents = null;
96
97 1947 jjdelcerro
    protected boolean readOnly = false;
98 1405 jjdelcerro
    private final List customActions;
99
    protected boolean emptyToNull = false;
100 2928 jjdelcerro
    private Map<String, AbstractButton> buttonsOfEvents;
101 919 jjdelcerro
102 1881 jjdelcerro
    public AbstractJDynFormField(
103
            DynFormSPIManager serviceManager,
104
            ComponentsFactory componentsFactory,
105
            JDynFormFieldFactory factory,
106
            DynFormFieldDefinition definition,
107
            Object value
108
        ) {
109 2928 jjdelcerro
        this.buttonsOfEvents = null;
110 1881 jjdelcerro
        this.manager = serviceManager;
111
        this.factory = factory;
112
        this.definition = definition;
113
        this.componentsFactory = componentsFactory;
114 1405 jjdelcerro
        this.listeners = new HashSet();
115
        this.problemIndicator = this.manager.createProblemIndicator(this);
116
        this.customActions = new ArrayList<>();
117 1423 jbadia
        if(this.definition != null){
118
                this.readOnly = this.definition.isReadOnly();
119
                this.loadDefaultValuesFromTags(definition.getTags());
120
        }
121 1405 jjdelcerro
    }
122 919 jjdelcerro
123 1881 jjdelcerro
    public ComponentsFactory getComponentsFactory() {
124
        return this.componentsFactory;
125
    }
126
127 1405 jjdelcerro
    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 919 jjdelcerro
138 1405 jjdelcerro
    public abstract void initComponent();
139 919 jjdelcerro
140 1405 jjdelcerro
    public abstract Object getAssignedValue();
141 919 jjdelcerro
142 1881 jjdelcerro
//    public Object getParameterValue() {
143
//        return this.parameters.getDynValue(DynFormSPIManager.FIELD_VALUE);
144
//    }
145
//
146 1405 jjdelcerro
    @Override
147
    public JComponent asJComponent() {
148
        if (this.contents == null) {
149 1881 jjdelcerro
            try {
150
                this.initComponent();
151
            } catch(Throwable th) {
152 2800 fdiaz
                StringWriter sw = new StringWriter();
153
                th.printStackTrace(new PrintWriter(sw));
154 1881 jjdelcerro
                this.contents = new JLabel("##ERROR##");
155 2800 fdiaz
                this.problemIndicator().set("Can't create component\n"+sw.toString());
156 1881 jjdelcerro
            }
157 1405 jjdelcerro
            if (!this.customActions.isEmpty()) {
158
                addPopupComponents();
159
            }
160
            if (this.readOnly) {
161
                this.setReadOnly(readOnly);
162
            }
163
        }
164
        return this.contents;
165
    }
166 919 jjdelcerro
167 1881 jjdelcerro
    @Override
168 1405 jjdelcerro
    public String getName() {
169
        return this.definition.getName();
170
    }
171 919 jjdelcerro
172 1881 jjdelcerro
    @Override
173 1405 jjdelcerro
    public String getLabel() {
174
        if (definition.getLabel() != null) {
175 1946 jjdelcerro
            String label = definition.getLabel();
176
            label = ToolsLocator.getI18nManager().getTranslation(label);
177
            return label;
178 1405 jjdelcerro
        } else {
179
            return this.getName();
180
        }
181
    }
182 919 jjdelcerro
183 1881 jjdelcerro
    @Override
184 2035 jjdelcerro
    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 1405 jjdelcerro
    public JComponent getJLabel() {
203
        if (this.jlabel == null) {
204 1881 jjdelcerro
            this.jlabel = this.getComponentsFactory().getJLabel(definition, null);
205
            if( this.jlabel==null ) {
206
                this.jlabel = new JLabel();
207 1405 jjdelcerro
            }
208 2035 jjdelcerro
            if (!this.useEmptyLabel() ) {
209 1881 jjdelcerro
                this.jlabel.setText(this.getLabel());
210
            }
211 1405 jjdelcerro
            this.jlabel.setLabelFor(this.contents);
212
            if (this.getDefinition().isMandatory()) {
213
                this.jlabel.setForeground(Color.red.darker());
214 2852 jjdelcerro
                this.jlabel.setText("<html><b>"+this.getLabel()+"</b></html>");
215 1405 jjdelcerro
            }
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 919 jjdelcerro
224 1881 jjdelcerro
    @Override
225 1405 jjdelcerro
    public DynFormFieldDefinition getDefinition() {
226 1881 jjdelcerro
        return this.definition;
227 1405 jjdelcerro
    }
228 1881 jjdelcerro
229 1405 jjdelcerro
    public DynFormSPIManager getServiceManager() {
230
        return this.manager;
231
    }
232 919 jjdelcerro
233 1881 jjdelcerro
    @Override
234 1405 jjdelcerro
    public void addListener(JDynFormFieldListener listener) {
235
        this.listeners.add(listener);
236
    }
237 919 jjdelcerro
238 1881 jjdelcerro
    @Override
239 1405 jjdelcerro
    public void removeListener(JDynFormFieldListener listener) {
240
        this.listeners.remove(listener);
241
    }
242 919 jjdelcerro
243 2551 fdiaz
    /*
244
     * Estos eventos no se deben disparar cuando se asignan valores por c?digo,
245
     * solo cuando los modifica el usuario
246
     */
247 2928 jjdelcerro
    public void fireFieldChangedEvent() {
248 1862 jjdelcerro
        if( this.isReadOnly() ) {
249
            return;
250
        }
251 1405 jjdelcerro
        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 1881 jjdelcerro
                LOGGER.info("Error calling listener " + listener.toString()
258 1405 jjdelcerro
                        + "(" + 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 1881 jjdelcerro
                LOGGER.info("Error calling listener " + listener.toString()
273 1405 jjdelcerro
                        + "(" + 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 1881 jjdelcerro
                LOGGER.info("Error calling listener " + listener.toString()
288 1405 jjdelcerro
                        + "(" + listener.getClass().getName() + ") from "
289
                        + this.toString() + "(" + this.getClass().getName()
290
                        + ").", ex);
291
            }
292
        }
293
    }
294
295 1881 jjdelcerro
    @Override
296 1405 jjdelcerro
    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 1881 jjdelcerro
                LOGGER.info("Error calling listener " + listener.toString()
304 1405 jjdelcerro
                        + "(" + listener.getClass().getName() + ") from "
305
                        + this.toString() + "(" + this.getClass().getName()
306
                        + ").", ex);
307
            }
308
        }
309
    }
310
311 1881 jjdelcerro
    @Override
312 1405 jjdelcerro
    public boolean isReadOnly() {
313
        return this.readOnly;
314
    }
315 1947 jjdelcerro
316
    protected boolean isForcedReadOnly() {
317
        return this.getTagValueAsBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY, false);
318
    }
319 1405 jjdelcerro
320 1881 jjdelcerro
    @Override
321 1405 jjdelcerro
    public void setReadOnly(boolean readonly) {
322 1881 jjdelcerro
        // FIXME: Implememtacion por defecto, sobreescribirla en las subclases
323 1405 jjdelcerro
        // segun convenga para cada componente.
324 1947 jjdelcerro
        if( !readonly && this.isForcedReadOnly() ) {
325
            readonly = true;
326
        }
327 1405 jjdelcerro
        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 1881 jjdelcerro
                JTextComponent x = (JTextComponent) this.contents;
334 1405 jjdelcerro
                x.setEditable(!readonly);
335 1881 jjdelcerro
336
            } else if (this.contents instanceof JComboBox ) {
337
                JComboBox x = (JComboBox) this.contents;
338
                x.setEnabled(!readonly);
339
340 1405 jjdelcerro
            } else if (this.contents instanceof JScrollPane) {
341
                try {
342
                    JViewport port = ((JScrollPane) this.contents).getViewport();
343 1881 jjdelcerro
                    JTextComponent x = (JTextComponent) port.getView();
344 1405 jjdelcerro
                    x.setEditable(!readonly);
345
                } catch (Exception ex) {
346
                    this.contents.setEnabled(!readOnly);
347
                }
348
            } else {
349
                this.contents.setEnabled(!readOnly);
350
            }
351
        }
352 2928 jjdelcerro
        this.setReadOnlyButtonsOfEvents(readonly);
353 1405 jjdelcerro
    }
354 919 jjdelcerro
355 1405 jjdelcerro
    public JProblemIndicator problemIndicator() {
356
        return this.problemIndicator;
357
    }
358 919 jjdelcerro
359 1405 jjdelcerro
    public class IllegalFieldValue extends RuntimeException {
360 919 jjdelcerro
361 1405 jjdelcerro
        /**
362
         *
363
         */
364
        private static final long serialVersionUID = -4409236610055983438L;
365 919 jjdelcerro
366 1405 jjdelcerro
        public IllegalFieldValue(JDynFormField field, String message) {
367
            super("The value of field '" + field.getLabel() + "' is not valid. " + message);
368
        }
369
    }
370 1145 jjdelcerro
371 1881 jjdelcerro
    @Override
372 1405 jjdelcerro
    public String toString() {
373
        return super.toString() + "{" + this.getName() + "}";
374
    }
375
376 1881 jjdelcerro
    @Override
377 1405 jjdelcerro
    public boolean isModified() {
378
        boolean modified = false;
379
        try {
380
            if (!this.isReadOnly()) {
381
                Object value = this.getValue();
382
                if (value == null) {
383 2487 jolivas
                    Object x = this.getAssignedValue();
384
                    if (StringUtils.isNotBlank(Objects.toString(x,null))) {
385 1405 jjdelcerro
                        modified = true;
386
                    }
387
                } else {
388 2487 jolivas
                    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 1405 jjdelcerro
                        modified = true;
395
                    }
396
                }
397
            }
398
        } catch (IllegalFieldValue ex) {
399 2546 fdiaz
            // Si es incorrecto el valor del campo decimos a capon que esta modificado.
400 1405 jjdelcerro
            modified = true;
401
        }
402
        return modified;
403
    }
404 2548 fdiaz
405
//    protected Object getValueWithoutValidate() {
406
//
407
//    }
408 1405 jjdelcerro
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 1881 jjdelcerro
    @Override
431 1405 jjdelcerro
    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 1881 jjdelcerro
    @Override
445 1405 jjdelcerro
    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 1881 jjdelcerro
    @Override
465 1145 jjdelcerro
    public void clear() {
466 1405 jjdelcerro
        if (this.contents == null) {
467 1145 jjdelcerro
            return;
468
        }
469 2487 jolivas
        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 1145 jjdelcerro
    }
497
498 1149 jjdelcerro
    public void setForm(JDynForm form) {
499
        this.form = form;
500
    }
501 1405 jjdelcerro
502
    @Override
503 1149 jjdelcerro
    public JDynForm getForm() {
504
        return this.form;
505
    }
506 1225 jjdelcerro
507 1405 jjdelcerro
    @Override
508
    public void fetch(DynObject container) {
509 2537 jjdelcerro
//        if( this.isReadOnly() ) {
510
//            return;
511
//        }
512 1405 jjdelcerro
        Object value = this.getValue();
513 1947 jjdelcerro
        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 1405 jjdelcerro
        container.setDynValue(this.getName(), value);
521
    }
522
523 1225 jjdelcerro
    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 1405 jjdelcerro
        DynField_v2 fielddef = (DynField_v2) this.getDefinition();
529 1881 jjdelcerro
        String tagname;
530 1405 jjdelcerro
        if (fielddef.getTags().has(tagname1)) {
531 1225 jjdelcerro
            tagname = tagname1;
532 1405 jjdelcerro
        } else if (fielddef.getTags().has(tagname2)) {
533 1225 jjdelcerro
            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 1881 jjdelcerro
            LOGGER.warn("Can't parse tag '" + tagname + "' as int for field '" + fielddef.getName() + "'.", ex);
542 1225 jjdelcerro
        }
543
        return defaultVaue;
544
    }
545 1405 jjdelcerro
546 1225 jjdelcerro
    protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) {
547
        return getTagValueAsBoolean(tagname, null, defaultVaue);
548
    }
549 1405 jjdelcerro
550 1225 jjdelcerro
    protected boolean getTagValueAsBoolean(String tagname1, String tagname2, boolean defaultVaue) {
551 1405 jjdelcerro
        DynField_v2 fielddef = (DynField_v2) this.getDefinition();
552 1881 jjdelcerro
        String tagname;
553 1405 jjdelcerro
        if (fielddef.getTags().has(tagname1)) {
554 1225 jjdelcerro
            tagname = tagname1;
555 1405 jjdelcerro
        } else if (fielddef.getTags().has(tagname2)) {
556 1225 jjdelcerro
            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 1881 jjdelcerro
            LOGGER.warn("Can't parse tag '" + tagname + "' as boolean for field '" + fielddef.getName() + "'.", ex);
565 1225 jjdelcerro
        }
566
        return defaultVaue;
567
    }
568 1405 jjdelcerro
569 1225 jjdelcerro
    protected String getTagValueAsString(String tagname, String defaultVaue) {
570
        return getTagValueAsString(tagname, null, defaultVaue);
571
    }
572 1405 jjdelcerro
573 1225 jjdelcerro
    protected String getTagValueAsString(String tagname1, String tagname2, String defaultVaue) {
574 1405 jjdelcerro
        DynField_v2 fielddef = (DynField_v2) this.getDefinition();
575 1881 jjdelcerro
        String tagname;
576 1405 jjdelcerro
        if (fielddef.getTags().has(tagname1)) {
577 1225 jjdelcerro
            tagname = tagname1;
578 1405 jjdelcerro
        } else if (fielddef.getTags().has(tagname2)) {
579 1225 jjdelcerro
            tagname = tagname2;
580
        } else {
581
            return defaultVaue;
582
        }
583
        try {
584 1405 jjdelcerro
            String value = (String) fielddef.getTags().get(tagname, DataTypes.STRING);
585 1225 jjdelcerro
            return value;
586
        } catch (CoercionException ex) {
587 1881 jjdelcerro
            LOGGER.warn("Can't parse tag '" + tagname + "' as string for field '" + fielddef.getName() + "'.", ex);
588 1225 jjdelcerro
        }
589
        return defaultVaue;
590
    }
591 1405 jjdelcerro
592 2031 jjdelcerro
    @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 2474 jjdelcerro
    @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 2496 jjdelcerro
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 2868 jjdelcerro
640
    @Override
641
    public String toHTML() {
642
        return null;
643
    }
644 2928 jjdelcerro
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 2031 jjdelcerro
659 2928 jjdelcerro
    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 3005 jjdelcerro
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 2928 jjdelcerro
721
    protected void setReadOnlyButtonsOfEvents(boolean readonly) {
722
        if( this.buttonsOfEvents==null ) {
723
            return;
724
        }
725 3005 jjdelcerro
        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 2928 jjdelcerro
        }
736
    }
737 1026 jjdelcerro
}