1 /* 2 * Copyright (c) 1997, 2014, 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 if (text == null) { 77 text = ""; 78 } 79 oldValue = anObject; 80 } else { 81 text = ""; 82 } 83 // workaround for 4530952 84 if (! text.equals(editor.getText())) { 85 editor.setText(text); 86 } 87 } 88 89 public Object getItem() { 90 Object newValue = editor.getText(); 91 92 if (oldValue != null && !(oldValue instanceof String)) { 93 // The original value is not a string. Should return the value in it's 94 // original type. 95 if (newValue.equals(oldValue.toString())) { 96 return oldValue; 97 } else { 98 // Must take the value from the editor and get the value and cast it to the new type. 99 Class<?> cls = oldValue.getClass(); 100 try { 101 Method method = cls.getMethod("valueOf", new Class[]{String.class}); 102 newValue = method.invoke(oldValue, 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 // This used to do something but now it doesn't. It couldn't be 121 // removed because it would be an API change to do so. 122 public void focusLost(FocusEvent e) {} 123 124 public void addActionListener(ActionListener l) { 125 editor.addActionListener(l); 126 } 127 128 public void removeActionListener(ActionListener l) { 129 editor.removeActionListener(l); 130 } 131 132 @SuppressWarnings("serial") // Superclass is not serializable across versions 133 static class BorderlessTextField extends JTextField { 134 public BorderlessTextField(String value,int n) { 135 super(value,n); 136 } 137 138 // workaround for 4530952 139 public void setText(String s) { 140 if (getText().equals(s)) { 141 return; 142 } 143 super.setText(s); 144 } 145 146 public void setBorder(Border b) { 147 if (!(b instanceof UIResource)) { 148 super.setBorder(b); 149 } 150 } 151 } 152 153 /** 154 * A subclass of BasicComboBoxEditor that implements UIResource. 155 * BasicComboBoxEditor doesn't implement UIResource 156 * directly so that applications can safely override the 157 * cellRenderer property with BasicListCellRenderer subclasses. 158 * <p> 159 * <strong>Warning:</strong> 160 * Serialized objects of this class will not be compatible with 161 * future Swing releases. The current serialization support is 162 * appropriate for short term storage or RMI between applications running 163 * the same version of Swing. As of 1.4, support for long term storage 164 * of all JavaBeans™ 165 * has been added to the <code>java.beans</code> package. 166 * Please see {@link java.beans.XMLEncoder}. 167 */ 168 @SuppressWarnings("serial") // Same-version serialization only 169 public static class UIResource extends BasicComboBoxEditor 170 implements javax.swing.plaf.UIResource { 171 } 172 }