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