Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.serv / org.gvsig.tools.swing.serv.field / src / main / java / org / gvsig / tools / swing / serv / field / component / JNullValueMuttableTextField.java @ 477

History | View | Annotate | Download (8.19 KB)

1
package org.gvsig.tools.swing.serv.field.component;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.awt.event.FocusEvent;
6
import java.awt.event.FocusListener;
7
import java.awt.event.KeyEvent;
8
import java.awt.event.KeyListener;
9
import java.text.ParseException;
10
import java.util.Locale;
11

    
12
import javax.swing.BorderFactory;
13
import javax.swing.Box;
14
import javax.swing.BoxLayout;
15
import javax.swing.ImageIcon;
16
import javax.swing.JButton;
17
import javax.swing.JPanel;
18
import javax.swing.JSpinner;
19
import javax.swing.JTextField;
20
import javax.swing.SwingConstants;
21

    
22
import org.gvsig.tools.dynobject.DynField;
23
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
24
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
25
import org.gvsig.tools.swing.serv.field.component.spinner.DynFieldEditor;
26

    
27
/**
28
 * 
29
 * This class is used a JSpinner to accept null values by
30
 * changing to an empty JTextField when the value is null
31
 * when clicking to the null component, the JTextField will
32
 * disappear and the currentValue will be shown in the
33
 * JSpinner.
34
 * 
35
 * @author gvSIG Team
36
 * @version $Id$
37
 * 
38
 */
39
public class JNullValueMuttableTextField extends JPanel implements KeyListener {
40

    
41
    /**
42
     * 
43
     */
44
    private static final long serialVersionUID = -7772372354714908644L;
45
    private JTextField textField;
46
    private JButton btnEmptyLabel;
47

    
48
    private DynFieldEditor editor;
49
    private DynField field;
50
    private Object value;
51

    
52
    private boolean isReadOnly;
53

    
54
    private ImageIcon icon;
55

    
56
    /**
57
     * Constructor.
58
     * 
59
     * @param field
60
     *            the {@link DynField} to which the field refers to.
61
     * @param initialValue
62
     *            the initial value to be set.
63
     * @param locale
64
     */
65
    public JNullValueMuttableTextField(DynField field, Object initialValue,
66
        Locale locale, ImageIcon btnEmptyImageIcon) {
67
        super();
68
        this.setLocale(locale);
69
        this.field = field;
70
        this.value = null;
71
        this.isReadOnly = false;
72
        this.icon = btnEmptyImageIcon;
73
        initUI();
74
        afterUI(initialValue, locale);
75
        this.fireValueChangedEvent();
76
    }
77

    
78
    public void addEmptyButtonActionListener(ActionListener listener) {
79
        this.getBtnEmpty().addActionListener(listener);
80
    }
81

    
82
    public void addValueChangedListener(ValueChangedListener listener) {
83
        if (listener instanceof JDynFieldComponent) {
84
            this.listenerList.add(ValueChangedListener.class, listener);
85
        }
86
    }
87

    
88
    private void afterUI(Object initialValue, Locale locale) {
89
        if (this.field.isReadOnly()) {
90
            this.isReadOnly = true;
91
            getJTextField().setVisible(false);
92
            getBtnEmpty().setVisible(false);
93

    
94
            getJSpinner().setValue(initialValue);
95
            getJSpinner().setEnabled(false);
96
        } else {
97
            setValue(initialValue);
98
        }
99
    }
100

    
101
    public void fireValueChangedEvent() {
102
        ValueChangedListener[] list =
103
            this.listenerList.getListeners(ValueChangedListener.class);
104
        for (ValueChangedListener listener : list) {
105
            listener.handleValueChanged((JDynFieldComponent) listener);
106
        }
107
    }
108

    
109
    /**
110
     * Gets the image button component
111
     * 
112
     * @return the Image button component
113
     */
114
    private JButton getBtnEmpty() {
115
        if (btnEmptyLabel != null) {
116
            return btnEmptyLabel;
117
        }
118

    
119
        String description = "Empties the field value.";
120
        icon.setDescription(description);
121

    
122
        btnEmptyLabel = new JButton();
123
        btnEmptyLabel.setActionCommand("BTN_OK");
124
        btnEmptyLabel.setOpaque(false);
125
        btnEmptyLabel.setBorderPainted(false);
126
        btnEmptyLabel.setBorder(BorderFactory.createEmptyBorder());
127
        btnEmptyLabel.setSize(new java.awt.Dimension(icon.getIconWidth(), icon
128
            .getIconHeight()));
129
        btnEmptyLabel.setHorizontalAlignment(SwingConstants.RIGHT);
130
        btnEmptyLabel.setVerticalAlignment(SwingConstants.TOP);
131
        btnEmptyLabel.setIcon(icon);
132

    
133
        // btnEmptyPanel = new JPanel();
134
        // btnEmptyPanel.add(btnEmptyLabel);
135
        // btnEmptyLabel.setOpaque(false);
136
        btnEmptyLabel.addActionListener(new ActionListener() {
137

    
138
            public void actionPerformed(ActionEvent e) {
139
                setEmptyValue();
140
            }
141
        });
142
        return btnEmptyLabel;
143
    }
144

    
145
    protected Object getDefaultValue() {
146
        return this.editor.getDefaultValue();
147
    }
148

    
149
    protected DynField getDynField() {
150
        return this.field;
151
    }
152

    
153
    /**
154
     * Gets the current JSpinner component
155
     * 
156
     * @return the spinner component
157
     */
158
    private JSpinner getJSpinner() {
159
        return this.editor.getSpinner();
160
    }
161

    
162
    /**
163
     * Gets the current JTextField component
164
     * 
165
     * @return the JTextField component
166
     */
167
    private JTextField getJTextField() {
168
        if (textField != null) {
169
            return textField;
170
        }
171

    
172
        textField = new JTextField();
173
        // textField.setEditable(false);
174
        textField.addFocusListener(new FocusListener() {
175

    
176
            public void focusGained(FocusEvent e) {
177
                setValue(editor.getDefaultValue());
178
            }
179

    
180
            public void focusLost(FocusEvent e) {
181
                fireValueChangedEvent();
182
            }
183
        });
184
        return textField;
185
    }
186

    
187
    public Object getTextFieldValue() {
188
        if (this.textField.isVisible()) {
189
            return null;
190
        }
191
        try {
192
            return this.editor.getFormat().parse(
193
                this.editor.getTextField().getText());
194
        } catch (ParseException e) {
195
            return null;
196
        }
197
    }
198

    
199
    /**
200
     * Gets the current value of this component. This value can be null.
201
     * 
202
     * @return
203
     */
204
    public Object getValue() {
205
        return this.value;
206
    }
207

    
208
    /**
209
     * Inits the main graphic user interface
210
     */
211
    private void initUI() {
212
        this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
213
        this.editor = new DynFieldEditor(this.field, this.getLocale());
214
        this.editor.getTextField().addKeyListener(this);
215
        // Adding components
216
        this.add(getJSpinner());
217
        this.add(getJTextField());
218
        this.add(Box.createHorizontalStrut(3));
219
        this.add(getBtnEmpty());
220
        this.add(Box.createHorizontalStrut(4));
221
    }
222

    
223
    /**
224
     * Determines if this field is readonly or not.
225
     * 
226
     * @return true if it is only readable, false otherwise.
227
     */
228
    private boolean isReadOnly() {
229
        return this.isReadOnly;
230
    }
231

    
232
    public void keyPressed(KeyEvent e) {
233
    }
234

    
235
    public void keyReleased(KeyEvent e) {
236

    
237
    }
238

    
239
    public void keyTyped(KeyEvent e) {
240
        this.fireValueChangedEvent();
241
    }
242

    
243
    public void setEditable(boolean isEditable) {
244
        this.textField.setEditable(isEditable);
245
        getJSpinner().setEnabled(isEditable);
246
    }
247

    
248
    /**
249
     * Sets the appropiate components properties for
250
     * when the value is null.
251
     */
252
    private void setEmptyValue() {
253
        this.value = null;
254

    
255
        // this.editor.setText(null);
256
        getJSpinner().setVisible(false);
257
        this.textField.setVisible(true);
258
        fireValueChangedEvent();
259

    
260
    }
261

    
262
    /**
263
     * Sets the appropiate components properties for
264
     * when the value is not null.
265
     */
266
    private void setNonEmptyValue(Object value) {
267
        // this.editor.setText(value);
268
        // this.value = editor.getValue();
269
        getJSpinner().setValue(value);
270
        this.value = getJSpinner().getValue();
271

    
272
        this.textField.setVisible(false);
273
        getJSpinner().setVisible(true);
274
        getJSpinner().requestFocus();
275

    
276
        fireValueChangedEvent();
277

    
278
    }
279

    
280
    /**
281
     * Sets a value if it is not readonly.
282
     * 
283
     * @param value
284
     */
285
    public void setValue(Object value) {
286
        if (isReadOnly()) {
287
            return;
288
        }
289

    
290
        if (value == null) {
291
            setEmptyValue();
292
        } else {
293
            setNonEmptyValue(value);
294
        }
295
    }
296

    
297
}