svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / test / java / org / gvsig / gui / beans / comboboxconfigurablelookup / programmertests / SampleComboBoxEditor.java @ 40561
History | View | Annotate | Download (5.4 KB)
1 |
/**
|
---|---|
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 |
package org.gvsig.gui.beans.comboboxconfigurablelookup.programmertests; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.awt.event.FocusEvent; |
29 |
import java.awt.event.FocusListener; |
30 |
import java.lang.reflect.Method; |
31 |
|
32 |
import javax.swing.ComboBoxEditor; |
33 |
import javax.swing.JTextField; |
34 |
import javax.swing.border.Border; |
35 |
import javax.swing.plaf.basic.BasicComboBoxEditor; |
36 |
|
37 |
import org.gvsig.gui.beans.editabletextcomponent.JEditableTextField; |
38 |
|
39 |
/**
|
40 |
* <p>The default editor for editable combo boxes. The editor is implemented as a JTextField.</p>
|
41 |
*
|
42 |
* @version 12/02/08
|
43 |
* @author Arnaud Weber
|
44 |
* @author Mark Davidson
|
45 |
* @author Pablo Piqueras Bartolom?
|
46 |
*/
|
47 |
public class SampleComboBoxEditor implements ComboBoxEditor, FocusListener { |
48 |
protected JTextField editor; |
49 |
private Object oldValue; |
50 |
|
51 |
public SampleComboBoxEditor() {
|
52 |
editor = new BorderlessTextField("",9); |
53 |
editor.setBorder(null);
|
54 |
} |
55 |
|
56 |
public Component getEditorComponent() { |
57 |
return editor;
|
58 |
} |
59 |
|
60 |
/**
|
61 |
* Sets the item that should be edited.
|
62 |
*
|
63 |
* @param anObject the displayed value of the editor
|
64 |
*/
|
65 |
public void setItem(Object anObject) { |
66 |
if ( anObject != null ) { |
67 |
editor.setText(anObject.toString()); |
68 |
|
69 |
oldValue = anObject; |
70 |
} else {
|
71 |
editor.setText("");
|
72 |
} |
73 |
} |
74 |
|
75 |
public Object getItem() { |
76 |
Object newValue = editor.getText();
|
77 |
|
78 |
if (oldValue != null && !(oldValue instanceof String)) { |
79 |
// The original value is not a string. Should return the value in it's
|
80 |
// original type.
|
81 |
if (newValue.equals(oldValue.toString())) {
|
82 |
return oldValue;
|
83 |
} else {
|
84 |
// Must take the value from the editor and get the value and cast it to the new type.
|
85 |
Class cls = oldValue.getClass();
|
86 |
try {
|
87 |
Method method = cls.getMethod("valueOf", new Class[]{String.class}); |
88 |
newValue = method.invoke(oldValue, new Object[] { editor.getText()}); |
89 |
} catch (Exception ex) { |
90 |
// Fail silently and return the newValue (a String object)
|
91 |
} |
92 |
} |
93 |
} |
94 |
return newValue;
|
95 |
} |
96 |
|
97 |
public void selectAll() { |
98 |
editor.selectAll(); |
99 |
editor.requestFocus(); |
100 |
} |
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 focusGained(FocusEvent e) {} |
105 |
|
106 |
// This used to do something but now it doesn't. It couldn't be
|
107 |
// removed because it would be an API change to do so.
|
108 |
public void focusLost(FocusEvent e) {} |
109 |
|
110 |
public void addActionListener(ActionListener l) { |
111 |
editor.addActionListener(l); |
112 |
} |
113 |
|
114 |
public void removeActionListener(ActionListener l) { |
115 |
editor.removeActionListener(l); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Inner text field editable as a component of a ComboBox editor.
|
120 |
*
|
121 |
* @see JEditableTextField
|
122 |
*
|
123 |
* @version 12/02/08
|
124 |
* @author Arnaud Weber
|
125 |
* @author Mark Davidson
|
126 |
* @author Pablo Piqueras Bartolom?
|
127 |
*/
|
128 |
private class BorderlessTextField extends JEditableTextField { |
129 |
private static final long serialVersionUID = -3333236529430569318L; |
130 |
|
131 |
public BorderlessTextField(String value,int n) { |
132 |
super(value,n);
|
133 |
} |
134 |
|
135 |
// workaround for 4530952
|
136 |
public void setText(String s) { |
137 |
if (getText().equals(s)) {
|
138 |
return;
|
139 |
} |
140 |
super.setText(s);
|
141 |
} |
142 |
|
143 |
public void setBorder(Border b) {} |
144 |
} |
145 |
|
146 |
/**
|
147 |
* A subclass of BasicComboBoxEditor that implements UIResource.
|
148 |
* BasicComboBoxEditor doesn't implement UIResource
|
149 |
* directly so that applications can safely override the
|
150 |
* cellRenderer property with BasicListCellRenderer subclasses.
|
151 |
* <p>
|
152 |
* <strong>Warning:</strong>
|
153 |
* Serialized objects of this class will not be compatible with
|
154 |
* future Swing releases. The current serialization support is
|
155 |
* appropriate for short term storage or RMI between applications running
|
156 |
* the same version of Swing. As of 1.4, support for long term storage
|
157 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
158 |
* has been added to the <code>java.beans</code> package.
|
159 |
* Please see {@link java.beans.XMLEncoder}.
|
160 |
*/
|
161 |
public static class UIResource extends BasicComboBoxEditor |
162 |
implements javax.swing.plaf.UIResource {
|
163 |
} |
164 |
} |