jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicComboBoxEditor.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
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 {
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    44
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    45
     * An instance of {@code JTextField}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    46
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected JTextField editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Object oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    50
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    51
     * Constructs a new instance of {@code BasicComboBoxEditor}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    52
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public BasicComboBoxEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        editor = createEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public Component getEditorComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Creates the internal editor component. Override this to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * a custom implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @return a new editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected JTextField createEditorComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        JTextField editor = new BorderlessTextField("",9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        editor.setBorder(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Sets the item that should be edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param anObject the displayed value of the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void setItem(Object anObject) {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    80
        String text;
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    81
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if ( anObject != null )  {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    83
            text = anObject.toString();
18126
ad2f991576db 8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents: 5506
diff changeset
    84
            if (text == null) {
ad2f991576db 8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents: 5506
diff changeset
    85
                text = "";
ad2f991576db 8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents: 5506
diff changeset
    86
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            oldValue = anObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    89
            text = "";
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    90
        }
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    91
        // workaround for 4530952
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    92
        if (! text.equals(editor.getText())) {
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 1639
diff changeset
    93
            editor.setText(text);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public Object getItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Object newValue = editor.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (oldValue != null && !(oldValue instanceof String))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // The original value is not a string. Should return the value in it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // original type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (newValue.equals(oldValue.toString()))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                // 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
   107
                Class<?> cls = oldValue.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                try {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23697
diff changeset
   109
                    Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
23693
0b8a5e89bac8 8035188: KSS: javax.swing.plaf.basic.BasicComboBoxEditor
malenkov
parents: 22574
diff changeset
   110
                    newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    // Fail silently and return the newValue (a String object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void selectAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        editor.selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        editor.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // This used to do something but now it doesn't.  It couldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // removed because it would be an API change to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void focusGained(FocusEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // This used to do something but now it doesn't.  It couldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // removed because it would be an API change to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void focusLost(FocusEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void addActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        editor.addActionListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void removeActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        editor.removeActionListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23693
diff changeset
   140
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static class BorderlessTextField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public BorderlessTextField(String value,int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            super(value,n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // workaround for 4530952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public void setText(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (getText().equals(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            super.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public void setBorder(Border b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (!(b instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                super.setBorder(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * A subclass of BasicComboBoxEditor that implements UIResource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * BasicComboBoxEditor doesn't implement UIResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * directly so that applications can safely override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * cellRenderer property with BasicListCellRenderer subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * 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
   172
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
   176
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static class UIResource extends BasicComboBoxEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    implements javax.swing.plaf.UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}