KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicComboBoxEditor


1 /*
2  * @(#)BasicComboBoxEditor.java 1.26 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.basic;
8
9 import javax.swing.*;
10 import javax.swing.border.Border JavaDoc;
11
12 import javax.swing.text.AttributeSet JavaDoc;
13 import javax.swing.text.BadLocationException JavaDoc;
14 import javax.swing.text.PlainDocument JavaDoc;
15
16 import java.awt.*;
17 import java.awt.event.*;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 /**
22  * The default editor for editable combo boxes. The editor is implemented as a JTextField.
23  *
24  * @version 1.26 12/19/03
25  * @author Arnaud Weber
26  * @author Mark Davidson
27  */

28 public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener {
29     protected JTextField editor;
30     private Object JavaDoc oldValue;
31
32     public BasicComboBoxEditor() {
33         editor = new BorderlessTextField("",9);
34         editor.setBorder(null);
35     }
36
37     public Component getEditorComponent() {
38         return editor;
39     }
40
41     /**
42      * Sets the item that should be edited.
43      *
44      * @param anObject the displayed value of the editor
45      */

46     public void setItem(Object JavaDoc anObject) {
47         if ( anObject != null ) {
48             editor.setText(anObject.toString());
49             
50             oldValue = anObject;
51         } else {
52             editor.setText("");
53         }
54     }
55
56     public Object JavaDoc getItem() {
57         Object JavaDoc newValue = editor.getText();
58         
59         if (oldValue != null && !(oldValue instanceof String JavaDoc)) {
60             // The original value is not a string. Should return the value in it's
61
// original type.
62
if (newValue.equals(oldValue.toString())) {
63                 return oldValue;
64             } else {
65                 // Must take the value from the editor and get the value and cast it to the new type.
66
Class JavaDoc cls = oldValue.getClass();
67                 try {
68                     Method JavaDoc method = cls.getMethod("valueOf", new Class JavaDoc[]{String JavaDoc.class});
69                     newValue = method.invoke(oldValue, new Object JavaDoc[] { editor.getText()});
70                 } catch (Exception JavaDoc ex) {
71                     // Fail silently and return the newValue (a String object)
72
}
73             }
74         }
75         return newValue;
76     }
77
78     public void selectAll() {
79         editor.selectAll();
80         editor.requestFocus();
81     }
82
83     // This used to do something but now it doesn't. It couldn't be
84
// removed because it would be an API change to do so.
85
public void focusGained(FocusEvent e) {}
86     
87     // This used to do something but now it doesn't. It couldn't be
88
// removed because it would be an API change to do so.
89
public void focusLost(FocusEvent e) {}
90
91     public void addActionListener(ActionListener l) {
92         editor.addActionListener(l);
93     }
94
95     public void removeActionListener(ActionListener l) {
96         editor.removeActionListener(l);
97     }
98
99     static class BorderlessTextField extends JTextField {
100         public BorderlessTextField(String JavaDoc value,int n) {
101             super(value,n);
102         }
103
104         // workaround for 4530952
105
public void setText(String JavaDoc s) {
106             if (getText().equals(s)) {
107                 return;
108             }
109             super.setText(s);
110         }
111
112         public void setBorder(Border JavaDoc b) {}
113     }
114     
115     /**
116      * A subclass of BasicComboBoxEditor that implements UIResource.
117      * BasicComboBoxEditor doesn't implement UIResource
118      * directly so that applications can safely override the
119      * cellRenderer property with BasicListCellRenderer subclasses.
120      * <p>
121      * <strong>Warning:</strong>
122      * Serialized objects of this class will not be compatible with
123      * future Swing releases. The current serialization support is
124      * appropriate for short term storage or RMI between applications running
125      * the same version of Swing. As of 1.4, support for long term storage
126      * of all JavaBeans<sup><font size="-2">TM</font></sup>
127      * has been added to the <code>java.beans</code> package.
128      * Please see {@link java.beans.XMLEncoder}.
129      */

130     public static class UIResource extends BasicComboBoxEditor JavaDoc
131     implements javax.swing.plaf.UIResource JavaDoc {
132     }
133 }
134
135
Popular Tags