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 / date / JNullValueMuttableTextFieldComponent.java @ 583
History | View | Annotate | Download (8.05 KB)
1 |
package org.gvsig.tools.swing.serv.field.component.date; |
---|---|
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.util.Locale; |
10 |
|
11 |
import javax.swing.BorderFactory; |
12 |
import javax.swing.Box; |
13 |
import javax.swing.BoxLayout; |
14 |
import javax.swing.ImageIcon; |
15 |
import javax.swing.JButton; |
16 |
import javax.swing.JPanel; |
17 |
import javax.swing.JSpinner; |
18 |
import javax.swing.JTextField; |
19 |
import javax.swing.SwingConstants; |
20 |
|
21 |
import org.gvsig.tools.dynobject.DynField; |
22 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
24 |
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener; |
25 |
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
26 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
27 |
import org.gvsig.tools.swing.api.usability.DynFieldEditor; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* This class is used a JSpinner to accept null values by changing to an empty
|
32 |
* JTextField when the value is null when clicking to the null component, the
|
33 |
* JTextField will disappear and the currentValue will be shown in the JSpinner.
|
34 |
*
|
35 |
* @author gvSIG Team
|
36 |
* @version $Id$
|
37 |
*
|
38 |
*/
|
39 |
public class JNullValueMuttableTextFieldComponent extends JPanel implements |
40 |
NullValueMuttableModel, KeyListener, FocusListener { |
41 |
|
42 |
/**
|
43 |
*
|
44 |
*/
|
45 |
private static final long serialVersionUID = -7772372354714908644L; |
46 |
|
47 |
private JTextField textField; |
48 |
private JButton btnEmptyLabel; |
49 |
|
50 |
private DynFieldEditor editor;
|
51 |
private ValueField field;
|
52 |
// private Object value;
|
53 |
|
54 |
private boolean isReadOnly; |
55 |
|
56 |
private ImageIcon icon; |
57 |
|
58 |
/**
|
59 |
* Constructor.
|
60 |
*
|
61 |
* @param field
|
62 |
* the {@link DynField} to which the field refers to.
|
63 |
* @param initialValue
|
64 |
* the initial value to be set.
|
65 |
* @param locale
|
66 |
*/
|
67 |
public JNullValueMuttableTextFieldComponent(ValueField field,
|
68 |
Object initialValue, Locale locale, ImageIcon btnEmptyImageIcon) { |
69 |
super();
|
70 |
this.setLocale(locale);
|
71 |
this.field = field;
|
72 |
this.isReadOnly = field.getDynField().isReadOnly();
|
73 |
this.icon = btnEmptyImageIcon;
|
74 |
initUI(); |
75 |
afterUI(initialValue, locale); |
76 |
this.fireValueChangedEvent();
|
77 |
} |
78 |
|
79 |
public void addEmptyButtonActionListener(ActionListener listener) { |
80 |
this.getBtnEmpty().addActionListener(listener);
|
81 |
} |
82 |
|
83 |
public void addValueChangedListener(ValueChangedListener listener) { |
84 |
if (listener instanceof JDynFieldComponent) { |
85 |
this.listenerList.add(ValueChangedListener.class, listener);
|
86 |
} |
87 |
} |
88 |
|
89 |
private void afterUI(Object initialValue, Locale locale) { |
90 |
if (getDynField().isReadOnly()) {
|
91 |
this.isReadOnly = true; |
92 |
getJTextField().setVisible(false);
|
93 |
getBtnEmpty().setVisible(false);
|
94 |
|
95 |
getJSpinner().setValue(initialValue); |
96 |
getJSpinner().setEnabled(false);
|
97 |
} else {
|
98 |
setValue(initialValue); |
99 |
} |
100 |
} |
101 |
|
102 |
public void fireValueChangedEvent() { |
103 |
ValueChangedListener[] list = this.listenerList |
104 |
.getListeners(ValueChangedListener.class); |
105 |
for (ValueChangedListener listener : list) {
|
106 |
listener.handleValueChanged((JDynFieldComponent) listener); |
107 |
} |
108 |
} |
109 |
|
110 |
public String getValidationMessage() { |
111 |
try {
|
112 |
this.getDynField().validate(this.getValue()); |
113 |
return ""; |
114 |
} catch (DynFieldValidateException e) {
|
115 |
return e.getMessage(3); |
116 |
} |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Gets the image button component
|
121 |
*
|
122 |
* @return the Image button component
|
123 |
*/
|
124 |
private JButton getBtnEmpty() { |
125 |
if (btnEmptyLabel != null) { |
126 |
return btnEmptyLabel;
|
127 |
} |
128 |
|
129 |
String description = "Empties the field value."; |
130 |
|
131 |
btnEmptyLabel = new JButton(); |
132 |
btnEmptyLabel.setActionCommand("BTN_CLEAR");
|
133 |
btnEmptyLabel.setOpaque(false);
|
134 |
btnEmptyLabel.setBorderPainted(false);
|
135 |
|
136 |
btnEmptyLabel.setBorder(BorderFactory.createEmptyBorder());
|
137 |
btnEmptyLabel.setHorizontalAlignment(SwingConstants.RIGHT);
|
138 |
btnEmptyLabel.setVerticalAlignment(SwingConstants.TOP);
|
139 |
|
140 |
if (icon != null) { |
141 |
icon.setDescription(description); |
142 |
btnEmptyLabel.setSize(new java.awt.Dimension(icon.getIconWidth(),
|
143 |
icon.getIconHeight())); |
144 |
|
145 |
btnEmptyLabel.setIcon(icon); |
146 |
} |
147 |
|
148 |
btnEmptyLabel.addActionListener(new ActionListener() { |
149 |
|
150 |
public void actionPerformed(ActionEvent e) { |
151 |
setEmptyValue(); |
152 |
} |
153 |
}); |
154 |
return btnEmptyLabel;
|
155 |
} |
156 |
|
157 |
protected Object getDefaultValue() { |
158 |
return this.editor.getDefaultValue(); |
159 |
} |
160 |
|
161 |
public DynField getDynField() {
|
162 |
return this.field.getDynField(); |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Gets the current JSpinner component
|
167 |
*
|
168 |
* @return the spinner component
|
169 |
*/
|
170 |
private JSpinner getJSpinner() { |
171 |
return this.editor.getSpinner(); |
172 |
} |
173 |
|
174 |
/**
|
175 |
* Gets the current JTextField component
|
176 |
*
|
177 |
* @return the JTextField component
|
178 |
*/
|
179 |
private JTextField getJTextField() { |
180 |
if (textField != null) { |
181 |
return textField;
|
182 |
} |
183 |
|
184 |
textField = new JTextField(); |
185 |
|
186 |
textField.addFocusListener(new FocusListener() { |
187 |
|
188 |
public void focusGained(FocusEvent e) { |
189 |
setValue(editor.getDefaultValue()); |
190 |
} |
191 |
|
192 |
public void focusLost(FocusEvent e) { |
193 |
setValue(editor.getTextFieldValue()); |
194 |
fireValueChangedEvent(); |
195 |
} |
196 |
}); |
197 |
return textField;
|
198 |
} |
199 |
|
200 |
public Object getTextFieldValue() { |
201 |
return this.editor.getTextFieldValue(); |
202 |
} |
203 |
|
204 |
/**
|
205 |
* Gets the current value of this component. This value can be null.
|
206 |
*
|
207 |
* @return
|
208 |
*/
|
209 |
public Object getValue() { |
210 |
return this.getTextFieldValue(); |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Sets the appropiate components properties for when the value is not null.
|
215 |
*/
|
216 |
private void setNonEmptyValue(Object value) { |
217 |
|
218 |
this.editor.setTextFieldValue(value);
|
219 |
getJSpinner().setValue(this.editor.getTextFieldValue());
|
220 |
getJSpinner().requestFocus(); |
221 |
getJSpinner().setVisible(true);
|
222 |
|
223 |
fireValueChangedEvent(); |
224 |
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* Inits the main graphic user interface
|
229 |
*/
|
230 |
private void initUI() { |
231 |
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); |
232 |
this.editor = ToolsSwingLocator.getUsabilitySwingManager()
|
233 |
.createNullSpinnerEditor(this, this.field.getDynField(), |
234 |
this.getLocale());
|
235 |
this.editor.getTextField().addKeyListener(this); |
236 |
|
237 |
// Adding components
|
238 |
this.add(getJSpinner());
|
239 |
this.add(getJTextField());
|
240 |
this.add(Box.createHorizontalStrut(3)); |
241 |
this.add(getBtnEmpty());
|
242 |
this.add(Box.createHorizontalStrut(4)); |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Determines if this field is readonly or not.
|
247 |
*
|
248 |
* @return true if it is only readable, false otherwise.
|
249 |
*/
|
250 |
private boolean isReadOnly() { |
251 |
return this.isReadOnly; |
252 |
} |
253 |
|
254 |
public void keyPressed(KeyEvent e) { |
255 |
} |
256 |
|
257 |
public void keyReleased(KeyEvent e) { |
258 |
|
259 |
} |
260 |
|
261 |
public void keyTyped(KeyEvent e) { |
262 |
this.fireValueChangedEvent();
|
263 |
} |
264 |
|
265 |
public void setEditable(boolean isEditable) { |
266 |
this.textField.setEditable(isEditable);
|
267 |
getJSpinner().setEnabled(isEditable); |
268 |
} |
269 |
|
270 |
/**
|
271 |
* Sets the appropiate components properties for when the value is null.
|
272 |
*/
|
273 |
private void setEmptyValue() { |
274 |
// this.value = null;
|
275 |
|
276 |
this.editor.setTextFieldValue(null); |
277 |
} |
278 |
|
279 |
/**
|
280 |
* Sets a value if it is not readonly.
|
281 |
*
|
282 |
* @param value
|
283 |
*/
|
284 |
public void setValue(Object value) { |
285 |
if (isReadOnly()) {
|
286 |
value = this.getDefaultValue();
|
287 |
} |
288 |
|
289 |
if (value == null) { |
290 |
setEmptyValue(); |
291 |
} else {
|
292 |
setNonEmptyValue(value); |
293 |
} |
294 |
|
295 |
boolean hasValue = (this.getValue() != null); |
296 |
this.textField.setVisible(!hasValue);
|
297 |
getJSpinner().setVisible(hasValue); |
298 |
|
299 |
fireValueChangedEvent(); |
300 |
} |
301 |
|
302 |
public JNullValueMuttableTextFieldComponent getModel() {
|
303 |
return this; |
304 |
} |
305 |
|
306 |
public Object getDefaultFieldValue() { |
307 |
return field.getDefaultFieldValue();
|
308 |
} |
309 |
|
310 |
public Object getFieldValue() { |
311 |
return field.getFieldValue();
|
312 |
} |
313 |
|
314 |
public void setFieldValue(Object value) { |
315 |
field.setFieldValue(value); |
316 |
|
317 |
} |
318 |
|
319 |
public void focusGained(FocusEvent arg0) { |
320 |
Object value = getValue();
|
321 |
if (value == null) { |
322 |
this.setValue(null); |
323 |
} |
324 |
} |
325 |
|
326 |
public void focusLost(FocusEvent arg0) { |
327 |
setValue(getValue()); |
328 |
fireValueChangedEvent(); |
329 |
} |
330 |
|
331 |
public DynField getParentDynField() {
|
332 |
return field.getDynField();
|
333 |
} |
334 |
|
335 |
} |