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