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