Showing the changes to java/pkg/javax/swing/plaf/basic/BasicComboBoxEditor.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,167 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | +package javax.swing.plaf.basic; | |
| 26 | + | |
| 27 | +import javax.swing.ComboBoxEditor; | |
| 28 | +import javax.swing.JTextField; | |
| 29 | +import javax.swing.border.Border; | |
| 30 | +import java.awt.Component; | |
| 31 | +import java.awt.event.*; | |
| 32 | + | |
| 33 | +import java.lang.reflect.Method; | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * The default editor for editable combo boxes. The editor is implemented as a JTextField. | |
| 37 | + * | |
| 38 | + * @author Arnaud Weber | |
| 39 | + * @author Mark Davidson | |
| 40 | + */ | |
| 41 | +public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener { | |
| 42 | + protected JTextField editor; | |
| 43 | + private Object oldValue; | |
| 44 | + | |
| 45 | + public BasicComboBoxEditor() { | |
| 46 | + editor = createEditorComponent(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + public Component getEditorComponent() { | |
| 50 | + return editor; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Creates the internal editor component. Override this to provide | |
| 55 | + * a custom implementation. | |
| 56 | + * | |
| 57 | + * @return a new editor component | |
| 58 | + * @since 1.6 | |
| 59 | + */ | |
| 60 | + protected JTextField createEditorComponent() { | |
| 61 | + JTextField editor = new BorderlessTextField("",9); | |
| 62 | + editor.setBorder(null); | |
| 63 | + return editor; | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Sets the item that should be edited. | |
| 68 | + * | |
| 69 | + * @param anObject the displayed value of the editor | |
| 70 | + */ | |
| 71 | + public void setItem(Object anObject) { | |
| 72 | + String text; | |
| 73 | + | |
| 74 | + if ( anObject != null ) { | |
| 75 | + text = anObject.toString(); | |
| 76 | + oldValue = anObject; | |
| 77 | + } else { | |
| 78 | + text = ""; | |
| 79 | + } | |
| 80 | + // workaround for 4530952 | |
| 81 | + if (! text.equals(editor.getText())) { | |
| 82 | + editor.setText(text); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + public Object getItem() { | |
| 87 | + Object newValue = editor.getText(); | |
| 88 | + | |
| 89 | + if (oldValue != null && !(oldValue instanceof String)) { | |
| 90 | + // The original value is not a string. Should return the value in it's | |
| 91 | + // original type. | |
| 92 | + if (newValue.equals(oldValue.toString())) { | |
| 93 | + return oldValue; | |
| 94 | + } else { | |
| 95 | + // Must take the value from the editor and get the value and cast it to the new type. | |
| 96 | + Class<?> cls = oldValue.getClass(); | |
| 97 | + try { | |
| 98 | + Method method = cls.getMethod("valueOf", new Class[]{String.class}); | |
| 99 | + newValue = method.invoke(oldValue, new Object[] { editor.getText()}); | |
| 100 | + } catch (Exception ex) { | |
| 101 | + // Fail silently and return the newValue (a String object) | |
| 102 | + } | |
| 103 | + } | |
| 104 | + } | |
| 105 | + return newValue; | |
| 106 | + } | |
| 107 | + | |
| 108 | + public void selectAll() { | |
| 109 | + editor.selectAll(); | |
| 110 | + editor.requestFocus(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + // This used to do something but now it doesn't. It couldn't be | |
| 114 | + // removed because it would be an API change to do so. | |
| 115 | + public void focusGained(FocusEvent e) {} | |
| 116 | + | |
| 117 | + // This used to do something but now it doesn't. It couldn't be | |
| 118 | + // removed because it would be an API change to do so. | |
| 119 | + public void focusLost(FocusEvent e) {} | |
| 120 | + | |
| 121 | + public void addActionListener(ActionListener l) { | |
| 122 | + editor.addActionListener(l); | |
| 123 | + } | |
| 124 | + | |
| 125 | + public void removeActionListener(ActionListener l) { | |
| 126 | + editor.removeActionListener(l); | |
| 127 | + } | |
| 128 | + | |
| 129 | + static class BorderlessTextField extends JTextField { | |
| 130 | + public BorderlessTextField(String value,int n) { | |
| 131 | + super(value,n); | |
| 132 | + } | |
| 133 | + | |
| 134 | + // workaround for 4530952 | |
| 135 | + public void setText(String s) { | |
| 136 | + if (getText().equals(s)) { | |
| 137 | + return; | |
| 138 | + } | |
| 139 | + super.setText(s); | |
| 140 | + } | |
| 141 | + | |
| 142 | + public void setBorder(Border b) { | |
| 143 | + if (!(b instanceof UIResource)) { | |
| 144 | + super.setBorder(b); | |
| 145 | + } | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * A subclass of BasicComboBoxEditor that implements UIResource. | |
| 151 | + * BasicComboBoxEditor doesn't implement UIResource | |
| 152 | + * directly so that applications can safely override the | |
| 153 | + * cellRenderer property with BasicListCellRenderer subclasses. | |
| 154 | + * <p> | |
| 155 | + * <strong>Warning:</strong> | |
| 156 | + * Serialized objects of this class will not be compatible with | |
| 157 | + * future Swing releases. The current serialization support is | |
| 158 | + * appropriate for short term storage or RMI between applications running | |
| 159 | + * the same version of Swing. As of 1.4, support for long term storage | |
| 160 | + * of all JavaBeans<sup><font size="-2">TM</font></sup> | |
| 161 | + * has been added to the <code>java.beans</code> package. | |
| 162 | + * Please see {@link java.beans.XMLEncoder}. | |
| 163 | + */ | |
| 164 | + public static class UIResource extends BasicComboBoxEditor | |
| 165 | + implements javax.swing.plaf.UIResource { | |
| 166 | + } | |
| 167 | +} | |