jdk/src/share/classes/javax/swing/plaf/metal/MetalComboBoxEditor.java
author alexp
Fri, 06 Nov 2009 19:48:09 +0300
changeset 4225 57ec8171fcd5
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6657138: Mutable statics in Windows PL&F (findbugs) Reviewed-by: peterz, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.metal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.basic.BasicComboBoxEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The default editor for Metal editable combo boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Steve Wilson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class MetalComboBoxEditor extends BasicComboBoxEditor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public MetalComboBoxEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        //editor.removeFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        editor = new JTextField("",9) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                // workaround for 4530952
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                public void setText(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    if (getText().equals(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    super.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            // The preferred and minimum sizes are overriden and padded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // 4 to keep the size as it previously was.  Refer to bugs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // 4775789 and 4517214 for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                Dimension pref = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                pref.height += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                Dimension min = super.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                min.height += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        editor.setBorder( new EditorBorder() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        //editor.addFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    * The default editor border <code>Insets</code>. This field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    * might not be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected static Insets editorBorderInsets = new Insets( 2, 2, 2, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    class EditorBorder extends AbstractBorder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            g.translate( x, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (MetalLookAndFeel.usingOcean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                g.setColor(MetalLookAndFeel.getControlDarkShadow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                g.drawRect(0, 0, w, h - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                g.setColor(MetalLookAndFeel.getControlShadow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                g.drawRect(1, 1, w - 2, h - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                g.setColor( MetalLookAndFeel.getControlDarkShadow() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                g.drawLine( 0, 0, w-1, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                g.drawLine( 0, 0, 0, h-2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                g.drawLine( 0, h-2, w-1, h-2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                g.setColor( MetalLookAndFeel.getControlHighlight() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                g.drawLine( 1, 1, w-1, 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                g.drawLine( 1, 1, 1, h-1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                g.drawLine( 1, h-1, w-1, h-1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                g.setColor( MetalLookAndFeel.getControl() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                g.drawLine( 1, h-2, 1, h-2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            g.translate( -x, -y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            insets.set(2, 2, 2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * A subclass of BasicComboBoxEditor that implements UIResource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * BasicComboBoxEditor doesn't implement UIResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * directly so that applications can safely override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * cellRenderer property with BasicListCellRenderer subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static class UIResource extends MetalComboBoxEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    implements javax.swing.plaf.UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}