1 /*
   2  * Copyright (c) 1998, 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 
  26 package javax.swing.plaf.metal;
  27 
  28 import javax.swing.*;
  29 import javax.swing.border.*;
  30 import java.io.Serializable;
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 
  34 import javax.swing.plaf.basic.BasicComboBoxEditor;
  35 
  36 /**
  37  * The default editor for Metal editable combo boxes
  38  * <p>
  39  * <strong>Warning:</strong>
  40  * Serialized objects of this class will not be compatible with
  41  * future Swing releases. The current serialization support is
  42  * appropriate for short term storage or RMI between applications running
  43  * the same version of Swing.  As of 1.4, support for long term storage
  44  * of all JavaBeans&trade;
  45  * has been added to the <code>java.beans</code> package.
  46  * Please see {@link java.beans.XMLEncoder}.
  47  *
  48  * @author Steve Wilson
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public class MetalComboBoxEditor extends BasicComboBoxEditor {
  52 
  53     /**
  54      * Constructs a new instance of {@code MetalComboBoxEditor}.
  55      */
  56     public MetalComboBoxEditor() {
  57         super();
  58         //editor.removeFocusListener(this);
  59         editor = new JTextField("",9) {
  60                 // workaround for 4530952
  61                 public void setText(String s) {
  62                     if (getText().equals(s)) {
  63                         return;
  64                     }
  65                     super.setText(s);
  66                 }
  67             // The preferred and minimum sizes are overriden and padded by
  68             // 4 to keep the size as it previously was.  Refer to bugs
  69             // 4775789 and 4517214 for details.
  70             public Dimension getPreferredSize() {
  71                 Dimension pref = super.getPreferredSize();
  72                 pref.height += 4;
  73                 return pref;
  74             }
  75             public Dimension getMinimumSize() {
  76                 Dimension min = super.getMinimumSize();
  77                 min.height += 4;
  78                 return min;
  79             }
  80             };
  81 
  82         editor.setBorder( new EditorBorder() );
  83         //editor.addFocusListener(this);
  84     }
  85 
  86    /**
  87     * The default editor border <code>Insets</code>. This field
  88     * might not be used.
  89     */
  90     protected static Insets editorBorderInsets = new Insets( 2, 2, 2, 0 );
  91 
  92     class EditorBorder extends AbstractBorder {
  93         public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
  94             g.translate( x, y );
  95 
  96             if (MetalLookAndFeel.usingOcean()) {
  97                 g.setColor(MetalLookAndFeel.getControlDarkShadow());
  98                 g.drawRect(0, 0, w, h - 1);
  99                 g.setColor(MetalLookAndFeel.getControlShadow());
 100                 g.drawRect(1, 1, w - 2, h - 3);
 101             }
 102             else {
 103                 g.setColor( MetalLookAndFeel.getControlDarkShadow() );
 104                 g.drawLine( 0, 0, w-1, 0 );
 105                 g.drawLine( 0, 0, 0, h-2 );
 106                 g.drawLine( 0, h-2, w-1, h-2 );
 107                 g.setColor( MetalLookAndFeel.getControlHighlight() );
 108                 g.drawLine( 1, 1, w-1, 1 );
 109                 g.drawLine( 1, 1, 1, h-1 );
 110                 g.drawLine( 1, h-1, w-1, h-1 );
 111                 g.setColor( MetalLookAndFeel.getControl() );
 112                 g.drawLine( 1, h-2, 1, h-2 );
 113             }
 114 
 115             g.translate( -x, -y );
 116         }
 117 
 118         public Insets getBorderInsets(Component c, Insets insets) {
 119             insets.set(2, 2, 2, 0);
 120             return insets;
 121         }
 122     }
 123 
 124 
 125     /**
 126      * A subclass of BasicComboBoxEditor that implements UIResource.
 127      * BasicComboBoxEditor doesn't implement UIResource
 128      * directly so that applications can safely override the
 129      * cellRenderer property with BasicListCellRenderer subclasses.
 130      * <p>
 131      * <strong>Warning:</strong>
 132      * Serialized objects of this class will not be compatible with
 133      * future Swing releases. The current serialization support is
 134      * appropriate for short term storage or RMI between applications running
 135      * the same version of Swing.  As of 1.4, support for long term storage
 136      * of all JavaBeans&trade;
 137      * has been added to the <code>java.beans</code> package.
 138      * Please see {@link java.beans.XMLEncoder}.
 139      */
 140     @SuppressWarnings("serial") // Same-version serialization only
 141     public static class UIResource extends MetalComboBoxEditor
 142     implements javax.swing.plaf.UIResource {
 143     }
 144 }