Source Code Cross Referenced for BasicComboBoxEditor.java in  » 6.0-JDK-Core » swing » javax » swing » plaf » basic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing.plaf.basic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025        package javax.swing.plaf.basic;
026
027        import javax.swing.*;
028        import javax.swing.border.Border;
029
030        import javax.swing.text.AttributeSet;
031        import javax.swing.text.BadLocationException;
032        import javax.swing.text.PlainDocument;
033
034        import java.awt.*;
035        import java.awt.event.*;
036
037        import java.lang.reflect.Method;
038
039        /**
040         * The default editor for editable combo boxes. The editor is implemented as a JTextField.
041         *
042         * @version 1.34 05/05/07
043         * @author Arnaud Weber
044         * @author Mark Davidson
045         */
046        public class BasicComboBoxEditor implements  ComboBoxEditor,
047                FocusListener {
048            protected JTextField editor;
049            private Object oldValue;
050
051            public BasicComboBoxEditor() {
052                editor = createEditorComponent();
053            }
054
055            public Component getEditorComponent() {
056                return editor;
057            }
058
059            /**
060             * Creates the internal editor component. Override this to provide
061             * a custom implementation.
062             *
063             * @return a new editor component
064             * @since 1.6
065             */
066            protected JTextField createEditorComponent() {
067                JTextField editor = new BorderlessTextField("", 9);
068                editor.setBorder(null);
069                return editor;
070            }
071
072            /** 
073             * Sets the item that should be edited. 
074             *
075             * @param anObject the displayed value of the editor
076             */
077            public void setItem(Object anObject) {
078                if (anObject != null) {
079                    editor.setText(anObject.toString());
080
081                    oldValue = anObject;
082                } else {
083                    editor.setText("");
084                }
085            }
086
087            public Object getItem() {
088                Object newValue = editor.getText();
089
090                if (oldValue != null && !(oldValue instanceof  String)) {
091                    // The original value is not a string. Should return the value in it's
092                    // original type.
093                    if (newValue.equals(oldValue.toString())) {
094                        return oldValue;
095                    } else {
096                        // Must take the value from the editor and get the value and cast it to the new type.
097                        Class cls = oldValue.getClass();
098                        try {
099                            Method method = cls.getMethod("valueOf",
100                                    new Class[] { String.class });
101                            newValue = method.invoke(oldValue,
102                                    new Object[] { editor.getText() });
103                        } catch (Exception ex) {
104                            // Fail silently and return the newValue (a String object)
105                        }
106                    }
107                }
108                return newValue;
109            }
110
111            public void selectAll() {
112                editor.selectAll();
113                editor.requestFocus();
114            }
115
116            // This used to do something but now it doesn't.  It couldn't be
117            // removed because it would be an API change to do so.
118            public void focusGained(FocusEvent e) {
119            }
120
121            // This used to do something but now it doesn't.  It couldn't be
122            // removed because it would be an API change to do so.
123            public void focusLost(FocusEvent e) {
124            }
125
126            public void addActionListener(ActionListener l) {
127                editor.addActionListener(l);
128            }
129
130            public void removeActionListener(ActionListener l) {
131                editor.removeActionListener(l);
132            }
133
134            static class BorderlessTextField extends JTextField {
135                public BorderlessTextField(String value, int n) {
136                    super (value, n);
137                }
138
139                // workaround for 4530952
140                public void setText(String s) {
141                    if (getText().equals(s)) {
142                        return;
143                    }
144                    super .setText(s);
145                }
146
147                public void setBorder(Border b) {
148                    if (!(b instanceof  UIResource)) {
149                        super .setBorder(b);
150                    }
151                }
152            }
153
154            /**
155             * A subclass of BasicComboBoxEditor that implements UIResource.
156             * BasicComboBoxEditor doesn't implement UIResource
157             * directly so that applications can safely override the
158             * cellRenderer property with BasicListCellRenderer subclasses.
159             * <p>
160             * <strong>Warning:</strong>
161             * Serialized objects of this class will not be compatible with
162             * future Swing releases. The current serialization support is
163             * appropriate for short term storage or RMI between applications running
164             * the same version of Swing.  As of 1.4, support for long term storage
165             * of all JavaBeans<sup><font size="-2">TM</font></sup>
166             * has been added to the <code>java.beans</code> package.
167             * Please see {@link java.beans.XMLEncoder}.
168             */
169            public static class UIResource extends BasicComboBoxEditor
170                    implements  javax.swing.plaf.UIResource {
171            }
172        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.