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