1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/* * @(#)BasicComboBoxEditor.java 1.28 06/05/25 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.plaf.basic; import javax.swing.*; import javax.swing.border.Border; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; import java.awt.*; import java.awt.event.*; import java.lang.reflect.Method; /** * The default editor for editable combo boxes. The editor is implemented as a JTextField. * * @version 1.28 05/25/06 * @author Arnaud Weber * @author Mark Davidson */ public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener { protected JTextField editor; private Object oldValue; public BasicComboBoxEditor() { editor = createEditorComponent(); } public Component getEditorComponent() { return editor; } /** * Creates the internal editor component. Override this to provide * a custom implementation. * * @return a new editor component * @since 1.6 */ protected JTextField createEditorComponent() { JTextField editor = new BorderlessTextField("",9); editor.setBorder(null); return editor; } /** * Sets the item that should be edited. * * @param anObject the displayed value of the editor */ public void setItem(Object anObject) { if ( anObject != null ) { editor.setText(anObject.toString()); oldValue = anObject; } else { editor.setText(""); } } public Object getItem() { Object newValue = editor.getText(); if (oldValue != null && !(oldValue instanceof String)) { // The original value is not a string. Should return the value in it's // original type. if (newValue.equals(oldValue.toString())) { return oldValue; } else { // Must take the value from the editor and get the value and cast it to the new type. Class cls = oldValue.getClass(); try { Method method = cls.getMethod("valueOf", new Class[]{String.class}); newValue = method.invoke(oldValue, new Object[] { editor.getText()}); } catch (Exception ex) { // Fail silently and return the newValue (a String object) } } } return newValue; } public void selectAll() { editor.selectAll(); editor.requestFocus(); } // This used to do something but now it doesn't. It couldn't be // removed because it would be an API change to do so. public void focusGained(FocusEvent e) {} // This used to do something but now it doesn't. It couldn't be // removed because it would be an API change to do so. public void focusLost(FocusEvent e) {} public void addActionListener(ActionListener l) { editor.addActionListener(l); } public void removeActionListener(ActionListener l) { editor.removeActionListener(l); } static class BorderlessTextField extends JTextField { public BorderlessTextField(String value,int n) { super(value,n); } // workaround for 4530952 public void setText(String s) { if (getText().equals(s)) { return; } super.setText(s); } public void setBorder(Border b) { if (!(b instanceof UIResource)) { super.setBorder(b); } } } /** * A subclass of BasicComboBoxEditor that implements UIResource. * BasicComboBoxEditor doesn't implement UIResource * directly so that applications can safely override the * cellRenderer property with BasicListCellRenderer subclasses. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ public static class UIResource extends BasicComboBoxEditor implements javax.swing.plaf.UIResource { } }