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 import sun.reflect.misc.MethodUtil;
  36 
  37 /**
  38  * The default editor for editable combo boxes. The editor is implemented as a JTextField.
  39  *
  40  * @author Arnaud Weber
  41  * @author Mark Davidson
  42  */
  43 public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener {
  44     protected JTextField editor;
  45     private Object oldValue;
  46 
  47     public BasicComboBoxEditor() {
  48         editor = createEditorComponent();
  49     }
  50 
  51     public Component getEditorComponent() {
  52         return editor;
  53     }
  54 
  55     /**
  56      * Creates the internal editor component. Override this to provide
  57      * a custom implementation.
  58      *
  59      * @return a new editor component
  60      * @since 1.6
  61      */
  62     protected JTextField createEditorComponent() {
  63         JTextField editor = new BorderlessTextField("",9);
  64         editor.setBorder(null);
  65         return editor;
  66     }
  67 
  68     /**
  69      * Sets the item that should be edited.
  70      *
  71      * @param anObject the displayed value of the editor
  72      */
  73     public void setItem(Object anObject) {
  74         String text;
  75 
  76         if ( anObject != null )  {
  77             text = anObject.toString();
  78             if (text == null) {
  79                 text = "";
  80             }
  81             oldValue = anObject;
  82         } else {
  83             text = "";
  84         }
  85         // workaround for 4530952
  86         if (! text.equals(editor.getText())) {
  87             editor.setText(text);
  88         }
  89     }
  90 
  91     public Object getItem() {
  92         Object newValue = editor.getText();
  93 
  94         if (oldValue != null && !(oldValue instanceof String))  {
  95             // The original value is not a string. Should return the value in it's
  96             // original type.
  97             if (newValue.equals(oldValue.toString()))  {
  98                 return oldValue;
  99             } else {
 100                 // Must take the value from the editor and get the value and cast it to the new type.
 101                 Class<?> cls = oldValue.getClass();
 102                 try {
 103                     Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
 104                     newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
 105                 } catch (Exception ex) {
 106                     // Fail silently and return the newValue (a String object)
 107                 }
 108             }
 109         }
 110         return newValue;
 111     }
 112 
 113     public void selectAll() {
 114         editor.selectAll();
 115         editor.requestFocus();
 116     }
 117 
 118     // This used to do something but now it doesn't.  It couldn't be
 119     // removed because it would be an API change to do so.
 120     public void focusGained(FocusEvent e) {}
 121 
 122     // This used to do something but now it doesn't.  It couldn't be
 123     // removed because it would be an API change to do so.
 124     public void focusLost(FocusEvent e) {}
 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     @SuppressWarnings("serial") // Superclass is not serializable across versions
 135     static class BorderlessTextField extends JTextField {
 136         public BorderlessTextField(String value,int n) {
 137             super(value,n);
 138         }
 139 
 140         // workaround for 4530952
 141         public void setText(String s) {
 142             if (getText().equals(s)) {
 143                 return;
 144             }
 145             super.setText(s);
 146         }
 147 
 148         public void setBorder(Border b) {
 149             if (!(b instanceof UIResource)) {
 150                 super.setBorder(b);
 151             }
 152         }
 153     }
 154 
 155     /**
 156      * A subclass of BasicComboBoxEditor that implements UIResource.
 157      * BasicComboBoxEditor doesn't implement UIResource
 158      * directly so that applications can safely override the
 159      * cellRenderer property with BasicListCellRenderer subclasses.
 160      * <p>
 161      * <strong>Warning:</strong>
 162      * Serialized objects of this class will not be compatible with
 163      * future Swing releases. The current serialization support is
 164      * appropriate for short term storage or RMI between applications running
 165      * the same version of Swing.  As of 1.4, support for long term storage
 166      * of all JavaBeans&trade;
 167      * has been added to the <code>java.beans</code> package.
 168      * Please see {@link java.beans.XMLEncoder}.
 169      */
 170     @SuppressWarnings("serial") // Same-version serialization only
 171     public static class UIResource extends BasicComboBoxEditor
 172     implements javax.swing.plaf.UIResource {
 173     }
 174 }