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.*;
  28 import javax.swing.event.*;
  29 import javax.swing.border.*;
  30 
  31 import java.awt.*;
  32 
  33 import java.io.Serializable;
  34 
  35 
  36 /**
  37  * ComboBox renderer
  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 Arnaud Weber
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public class BasicComboBoxRenderer extends JLabel
  52 implements ListCellRenderer, Serializable {
  53 
  54    /**
  55     * An empty <code>Border</code>. This field might not be used. To change the
  56     * <code>Border</code> used by this renderer directly set it using
  57     * the <code>setBorder</code> method.
  58     */
  59     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  60     private final static Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
  61 
  62     public BasicComboBoxRenderer() {
  63         super();
  64         setOpaque(true);
  65         setBorder(getNoFocusBorder());
  66     }
  67 
  68     private static Border getNoFocusBorder() {
  69         if (System.getSecurityManager() != null) {
  70             return SAFE_NO_FOCUS_BORDER;
  71         } else {
  72             return noFocusBorder;
  73         }
  74     }
  75 
  76     public Dimension getPreferredSize() {
  77         Dimension size;
  78 
  79         if ((this.getText() == null) || (this.getText().equals( "" ))) {
  80             setText( " " );
  81             size = super.getPreferredSize();
  82             setText( "" );
  83         }
  84         else {
  85             size = super.getPreferredSize();
  86         }
  87 
  88         return size;
  89     }
  90 
  91     public Component getListCellRendererComponent(
  92                                                  JList list,
  93                                                  Object value,
  94                                                  int index,
  95                                                  boolean isSelected,
  96                                                  boolean cellHasFocus)
  97     {
  98 
  99         /**if (isSelected) {
 100             setBackground(UIManager.getColor("ComboBox.selectionBackground"));
 101             setForeground(UIManager.getColor("ComboBox.selectionForeground"));
 102         } else {
 103             setBackground(UIManager.getColor("ComboBox.background"));
 104             setForeground(UIManager.getColor("ComboBox.foreground"));
 105         }**/
 106 
 107         if (isSelected) {
 108             setBackground(list.getSelectionBackground());
 109             setForeground(list.getSelectionForeground());
 110         }
 111         else {
 112             setBackground(list.getBackground());
 113             setForeground(list.getForeground());
 114         }
 115 
 116         setFont(list.getFont());
 117 
 118         if (value instanceof Icon) {
 119             setIcon((Icon)value);
 120         }
 121         else {
 122             setText((value == null) ? "" : value.toString());
 123         }
 124         return this;
 125     }
 126 
 127 
 128     /**
 129      * A subclass of BasicComboBoxRenderer that implements UIResource.
 130      * BasicComboBoxRenderer doesn't implement UIResource
 131      * directly so that applications can safely override the
 132      * cellRenderer property with BasicListCellRenderer subclasses.
 133      * <p>
 134      * <strong>Warning:</strong>
 135      * Serialized objects of this class will not be compatible with
 136      * future Swing releases. The current serialization support is
 137      * appropriate for short term storage or RMI between applications running
 138      * the same version of Swing.  As of 1.4, support for long term storage
 139      * of all JavaBeans&trade;
 140      * has been added to the <code>java.beans</code> package.
 141      * Please see {@link java.beans.XMLEncoder}.
 142      */
 143     @SuppressWarnings("serial") // Same-version serialization only
 144     public static class UIResource extends BasicComboBoxRenderer implements javax.swing.plaf.UIResource {
 145     }
 146 }