svn-gvsig-desktop / trunk / libraries / libUIComponent / src-test-ui / org / gvsig / gui / beans / comboboxconfigurablelookup / programmertests / SampleComboBoxEditor.java @ 18795
History | View | Annotate | Download (5.29 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
|
20 |
package org.gvsig.gui.beans.comboboxconfigurablelookup.programmertests; |
21 |
|
22 |
import java.awt.Component; |
23 |
import java.awt.event.ActionListener; |
24 |
import java.awt.event.FocusEvent; |
25 |
import java.awt.event.FocusListener; |
26 |
import java.lang.reflect.Method; |
27 |
|
28 |
import javax.swing.ComboBoxEditor; |
29 |
import javax.swing.JTextField; |
30 |
import javax.swing.border.Border; |
31 |
import javax.swing.plaf.basic.BasicComboBoxEditor; |
32 |
|
33 |
import org.gvsig.gui.beans.editabletextcomponent.JEditableTextField; |
34 |
|
35 |
/**
|
36 |
* The default editor for editable combo boxes. The editor is implemented as a JTextField.
|
37 |
*
|
38 |
* @version 12/02/08
|
39 |
* @author Arnaud Weber
|
40 |
* @author Mark Davidson
|
41 |
* @author Pablo Piqueras Bartolom?
|
42 |
*/
|
43 |
public class SampleComboBoxEditor implements ComboBoxEditor, FocusListener { |
44 |
protected JTextField editor; |
45 |
private Object oldValue; |
46 |
|
47 |
public SampleComboBoxEditor() {
|
48 |
editor = new BorderlessTextField("",9); |
49 |
editor.setBorder(null);
|
50 |
} |
51 |
|
52 |
public Component getEditorComponent() { |
53 |
return editor;
|
54 |
} |
55 |
|
56 |
/**
|
57 |
* Sets the item that should be edited.
|
58 |
*
|
59 |
* @param anObject the displayed value of the editor
|
60 |
*/
|
61 |
public void setItem(Object anObject) { |
62 |
if ( anObject != null ) { |
63 |
editor.setText(anObject.toString()); |
64 |
|
65 |
oldValue = anObject; |
66 |
} else {
|
67 |
editor.setText("");
|
68 |
} |
69 |
} |
70 |
|
71 |
public Object getItem() { |
72 |
Object newValue = editor.getText();
|
73 |
|
74 |
if (oldValue != null && !(oldValue instanceof String)) { |
75 |
// The original value is not a string. Should return the value in it's
|
76 |
// original type.
|
77 |
if (newValue.equals(oldValue.toString())) {
|
78 |
return oldValue;
|
79 |
} else {
|
80 |
// Must take the value from the editor and get the value and cast it to the new type.
|
81 |
Class cls = oldValue.getClass();
|
82 |
try {
|
83 |
Method method = cls.getMethod("valueOf", new Class[]{String.class}); |
84 |
newValue = method.invoke(oldValue, new Object[] { editor.getText()}); |
85 |
} catch (Exception ex) { |
86 |
// Fail silently and return the newValue (a String object)
|
87 |
} |
88 |
} |
89 |
} |
90 |
return newValue;
|
91 |
} |
92 |
|
93 |
public void selectAll() { |
94 |
editor.selectAll(); |
95 |
editor.requestFocus(); |
96 |
} |
97 |
|
98 |
// This used to do something but now it doesn't. It couldn't be
|
99 |
// removed because it would be an API change to do so.
|
100 |
public void focusGained(FocusEvent e) {} |
101 |
|
102 |
// This used to do something but now it doesn't. It couldn't be
|
103 |
// removed because it would be an API change to do so.
|
104 |
public void focusLost(FocusEvent e) {} |
105 |
|
106 |
public void addActionListener(ActionListener l) { |
107 |
editor.addActionListener(l); |
108 |
} |
109 |
|
110 |
public void removeActionListener(ActionListener l) { |
111 |
editor.removeActionListener(l); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Inner text field editable as a component of a ComboBox editor.
|
116 |
*
|
117 |
* @see JEditableTextField
|
118 |
*
|
119 |
* @version 12/02/08
|
120 |
* @author Arnaud Weber
|
121 |
* @author Mark Davidson
|
122 |
* @author Pablo Piqueras Bartolom?
|
123 |
*/
|
124 |
private class BorderlessTextField extends JEditableTextField { |
125 |
private static final long serialVersionUID = -3333236529430569318L; |
126 |
|
127 |
public BorderlessTextField(String value,int n) { |
128 |
super(value,n);
|
129 |
} |
130 |
|
131 |
// workaround for 4530952
|
132 |
public void setText(String s) { |
133 |
if (getText().equals(s)) {
|
134 |
return;
|
135 |
} |
136 |
super.setText(s);
|
137 |
} |
138 |
|
139 |
public void setBorder(Border b) {} |
140 |
} |
141 |
|
142 |
/**
|
143 |
* A subclass of BasicComboBoxEditor that implements UIResource.
|
144 |
* BasicComboBoxEditor doesn't implement UIResource
|
145 |
* directly so that applications can safely override the
|
146 |
* cellRenderer property with BasicListCellRenderer subclasses.
|
147 |
* <p>
|
148 |
* <strong>Warning:</strong>
|
149 |
* Serialized objects of this class will not be compatible with
|
150 |
* future Swing releases. The current serialization support is
|
151 |
* appropriate for short term storage or RMI between applications running
|
152 |
* the same version of Swing. As of 1.4, support for long term storage
|
153 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
154 |
* has been added to the <code>java.beans</code> package.
|
155 |
* Please see {@link java.beans.XMLEncoder}.
|
156 |
*/
|
157 |
public static class UIResource extends BasicComboBoxEditor |
158 |
implements javax.swing.plaf.UIResource {
|
159 |
} |
160 |
} |