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 / AbstractJDynFormFieldWithValueList.java @ 1998

History | View | Annotate | Download (12.5 KB)

1 954 jbadia
/**
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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24 919 jjdelcerro
package org.gvsig.tools.dynform.spi.dynformfield;
25
26
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusListener;
28
import java.awt.event.KeyAdapter;
29
import java.awt.event.KeyEvent;
30 1881 jjdelcerro
import java.util.Objects;
31 1947 jjdelcerro
import javax.swing.BorderFactory;
32 1881 jjdelcerro
import javax.swing.ComboBoxModel;
33
import javax.swing.DefaultComboBoxModel;
34 919 jjdelcerro
35
import javax.swing.JComboBox;
36 1881 jjdelcerro
import javax.swing.JComponent;
37 1946 jjdelcerro
import javax.swing.JLabel;
38 1998 omartinez
import javax.swing.JSpinner;
39 919 jjdelcerro
import javax.swing.JTextField;
40 1947 jjdelcerro
import javax.swing.UIManager;
41 919 jjdelcerro
import javax.swing.text.JTextComponent;
42 1026 jjdelcerro
import org.apache.commons.lang3.StringUtils;
43 919 jjdelcerro
44 931 jjdelcerro
import org.gvsig.tools.dataTypes.CoercionException;
45 1881 jjdelcerro
import org.gvsig.tools.dynform.DynFormFieldDefinition;
46 933 jjdelcerro
import org.gvsig.tools.dynform.JDynFormField;
47 1881 jjdelcerro
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
48
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory;
49 1947 jjdelcerro
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_DROPDOWN;
50 931 jjdelcerro
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField.IllegalFieldValue;
51 919 jjdelcerro
import org.gvsig.tools.dynobject.DynObjectValueItem;
52
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
53 1946 jjdelcerro
import org.gvsig.tools.swing.api.DropDown;
54
import org.gvsig.tools.swing.api.ToolsSwingLocator;
55 919 jjdelcerro
56
public abstract class AbstractJDynFormFieldWithValueList extends AbstractJDynFormField implements JDynFormField, FocusListener {
57
58 1881 jjdelcerro
    protected Object assignedValue = null;
59 1946 jjdelcerro
    protected DropDown dropDown = null;
60 919 jjdelcerro
61 1881 jjdelcerro
    public AbstractJDynFormFieldWithValueList(
62
            DynFormSPIManager serviceManager,
63
            ComponentsFactory componentsFactory,
64
            JDynFormFieldFactory factory,
65
            DynFormFieldDefinition definition,
66
            Object value
67
    ) {
68
        super(serviceManager, componentsFactory, factory, definition, value);
69
        this.assignedValue = value;
70
    }
71
72
    @Override
73
    public Object getAssignedValue() {
74
        return this.assignedValue;
75
    }
76
77
    protected JTextField getJTextField() {
78 1947 jjdelcerro
        if (this.contents instanceof JTextField) {
79 1946 jjdelcerro
            return (JTextField) this.contents;
80
        }
81
        return null;
82 1881 jjdelcerro
    }
83
84 1998 omartinez
    protected JSpinner getJSpinnerField() {
85
        if (this.contents instanceof JSpinner) {
86
            return (JSpinner) this.contents;
87
        }
88
        return null;
89
    }
90
91 1946 jjdelcerro
    protected DropDown getDropDown() {
92
        return this.dropDown;
93 1881 jjdelcerro
    }
94
95
    protected DynObjectValueItem[] getAvailableValues() {
96
        return this.getDefinition().getAvailableValues();
97
    }
98
99
    @Override
100
    public void initComponent() {
101
        DynObjectValueItem[] availableValues = this.getAvailableValues();
102 1947 jjdelcerro
        ComponentsFactory theFactory = this.getComponentsFactory();
103
        if (theFactory.containsJComboBox(this.getDefinition(), null)) {
104
            JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null);
105
            this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
106
            this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
107
            this.contents = combo;
108
        } else if (theFactory.containsJLabel(this.getDefinition(), "Ddn")) {
109
            JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "Ddn");
110
            this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
111
            this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
112
            this.dropDown.setVisibleDropdownArrow(true);
113
            this.contents = label;
114 1998 omartinez
        } else if (theFactory.containsJSpinner(this.getDefinition(), null)) {
115
            this.contents = this.getComponentsFactory().getJSpinner(this.getDefinition(), null);
116
            this.dropDown = null;
117 1947 jjdelcerro
        } else if (theFactory.containsJTextField(this.getDefinition(), null)) {
118
            this.contents = theFactory.getJTextField(this.getDefinition(), null);
119 1946 jjdelcerro
            this.dropDown = null;
120 1947 jjdelcerro
        } else {
121
            if (availableValues == null) {
122
                this.dropDown = null;
123 1881 jjdelcerro
                this.contents = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
124 1946 jjdelcerro
            } else {
125 1947 jjdelcerro
                String dropdownType = this.getTagValueAsString(TAG_DYNFORM_DROPDOWN,
126
                        availableValues.length <= 20 ? "label" : "combo"
127
                ).toLowerCase().trim();
128
                switch (dropdownType) {
129
                    case "label": {
130
                        JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "Ddn");
131
                        this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
132
                        this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
133
                        this.dropDown.setVisibleDropdownArrow(true);
134
                        label.setBorder(BorderFactory.createLineBorder(UIManager.getColor("TextField.darkShadow")));
135
                        label.setForeground(UIManager.getColor("TextField.foreground"));
136
                        label.setBackground(UIManager.getColor("TextField.background"));
137
                        label.setOpaque(true);
138
                        this.contents = label;
139
                        break;
140
                    }
141
                    default:
142
                    case "combo": {
143
                        JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null);
144
                        this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
145
                        this.dropDown.setModel(new DefaultComboBoxModel(availableValues));
146
                        this.contents = combo;
147
                        break;
148
                    }
149
                }
150 1881 jjdelcerro
            }
151
        }
152 1947 jjdelcerro
        this.contents.addFocusListener(this);
153
        this.setReadOnly(readOnly); // Forzamos a actualiza rel estado del component
154 1998 omartinez
        //component dont have support to set null values
155
        if (theFactory.containsJSpinner(this.getDefinition(), null) && this.assignedValue==null) {
156
            this.setValue(0);
157
        } else {
158
            this.setValue(this.assignedValue);
159
        }
160 1881 jjdelcerro
    }
161
162 1947 jjdelcerro
    @Override
163
    public void setReadOnly(boolean readonly) {
164
        if (!readonly && this.isForcedReadOnly()) {
165
            readonly = true;
166
        }
167
        if (this.dropDown == null) {
168
            super.setReadOnly(readonly);
169
        } else {
170
            this.readOnly = readonly;
171
            if (jlabel != null) {
172
                this.jlabel.setEnabled(!readonly);
173
            }
174
            this.dropDown.setReadOnly(readonly);
175
        }
176
    }
177
178 1881 jjdelcerro
    protected String getValueFromJComponent() {
179
        String s;
180 1947 jjdelcerro
        if (this.dropDown == null) {
181 1946 jjdelcerro
            JTextField jtext = this.getJTextField();
182 1947 jjdelcerro
            if (jtext == null) {
183 1946 jjdelcerro
                s = null;
184
            } else {
185
                s = jtext.getText();
186
            }
187 1881 jjdelcerro
        } else {
188 1946 jjdelcerro
            DynObjectValueItem value = (DynObjectValueItem) this.dropDown.getSelectedItem();
189 1947 jjdelcerro
            if (value == null) {
190 1881 jjdelcerro
                s = null;
191
            } else {
192 1946 jjdelcerro
                s = Objects.toString(value.getValue(), null);
193 1881 jjdelcerro
            }
194
        }
195 1947 jjdelcerro
        if (StringUtils.isBlank(s) && this.translateEmptyToNull()) {
196 1881 jjdelcerro
            return null;
197
        }
198
        return s;
199
    }
200
201
    @Override
202
    public void setValue(Object value) {
203 1947 jjdelcerro
        if (this.dropDown == null) {
204 1946 jjdelcerro
            JTextField jtext = this.getJTextField();
205 1947 jjdelcerro
            if (jtext != null) {
206 1946 jjdelcerro
                String s;
207
                if (value == null) {
208
                    this.setTranslateEmptyToNull(true);
209
                    value = this.getDefinition().getDefaultValue();
210
                    if (value == null) {
211
                        s = this.getDefaultValue();
212
                    } else {
213
                        s = value.toString();
214
                    }
215
                } else {
216
                    this.setTranslateEmptyToNull(false);
217
                    s = value.toString();
218
                    try {
219
                        this.getDefinition().validate(value);
220
                        this.problemIndicator().clear();
221
                    } catch (DynFieldValidateException e) {
222
                        this.problemIndicator().set(e.getLocalizedMessage());
223
                    }
224
                }
225
                jtext.setText(s);
226 1998 omartinez
            } else if (this.contents instanceof JSpinner) {
227
                JSpinner jspinner = this.getJSpinnerField();
228
                if (value == null) {
229
                    this.setTranslateEmptyToNull(true);
230
                    value = this.getDefinition().getDefaultValue();
231
                    if (value != null) {
232
                        jspinner.setValue(value);
233
                    } else {
234
                        jspinner.setValue(121);
235
                    }
236
                } else {
237
                    this.setTranslateEmptyToNull(false);
238
                }
239
                jspinner.setValue(value);
240 1881 jjdelcerro
            }
241
        } else {
242 1998 omartinez
            if (value == null) {
243 1947 jjdelcerro
                this.dropDown.setSelectedIndex(-1);
244
            } else {
245
                ComboBoxModel model = this.dropDown.getModel();
246
                if (model != null) {
247
                    for (int i = 0; i < model.getSize(); i++) {
248
                        DynObjectValueItem item = (DynObjectValueItem) model.getElementAt(i);
249
                        if (item != null && Objects.equals(item.getValue(), value)) {
250
                            this.dropDown.setSelectedIndex(i);
251
                            break;
252
                        }
253
                    }
254 1026 jjdelcerro
                }
255 1881 jjdelcerro
            }
256
        }
257
        this.assignedValue = value;
258
    }
259
260
    /*
261 919 jjdelcerro
         * M?todos espec?ficos de cada tipo de datos
262 1881 jjdelcerro
     */
263
    protected String getDefaultValue() {
264
        return "";
265
    }
266 919 jjdelcerro
267 1881 jjdelcerro
    @Override
268
    public Object getValue() {
269
        Object value = null;
270
        String s = this.getValueFromJComponent();
271
        try {
272
            value = this.getDefinition().coerce(s);
273
        } catch (CoercionException e) {
274
            throw new IllegalFieldValue(this, "Can't convert value '" + s + "' to '" + this.getDefinition().getDataType().getName() + "'.");
275
        }
276
        this.problemIndicator().clear();
277
        return value;
278
    }
279 919 jjdelcerro
280 1881 jjdelcerro
    @SuppressWarnings("unused")
281
    @Override
282
    public boolean hasValidValue() {
283
        try {
284
            Object value = this.getValue();
285
        } catch (Exception e) {
286
            return false;
287
        }
288
        return true;
289
    }
290 919 jjdelcerro
291 1881 jjdelcerro
    @Override
292
    public void focusGained(FocusEvent arg0) {
293
        fireFieldEnterEvent();
294
        this.problemIndicator().restore();
295
    }
296 919 jjdelcerro
297 1881 jjdelcerro
    @Override
298
    public void focusLost(FocusEvent arg0) {
299
        if (this.hasValidValue()) {
300
            this.problemIndicator().clear();
301
        } else {
302
            try {
303
                Object value = this.getValue();
304
            } catch (Exception e) {
305
                this.problemIndicator().set(e.getLocalizedMessage());
306
            }
307
        }
308
        fireFieldExitEvent();
309
    }
310 919 jjdelcerro
311 1881 jjdelcerro
    /**
312
     * Este m?todo es por si se quiere a?adir una expresi?n regular al
313
     * JTextField Por defecto, se puede escribir cualquier cosa, pero se puede
314
     * sobreescribir este m?todo en la clase que lo requiera para a?adir
315
     * restricciones (p.ej: en los num?ricos)
316
     *
317
     * @return expresi?n regular que filtra el contenido del JTextField
318
     */
319
    public String getJTextFieldRegex() {
320
        return "[*]";
321
    }
322
323
    public class KeyAdapterRegEx extends KeyAdapter {
324
325
        /**
326
         * Key released on field.
327
         *
328
         * @param e
329
         */
330
        @Override
331
        public void keyReleased(KeyEvent e) {
332
            String curText = ((JTextComponent) e.getSource()).getText();
333
            curText = curText.replaceAll(getJTextFieldRegex(), "");
334
335
            ((JTextComponent) e.getSource()).setText(curText);
336
        }
337
    }
338 919 jjdelcerro
}