1 /* 2 * Copyright (c) 2013, 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 com.apple.laf; 27 28 import sun.swing.SwingUtilities2; 29 30 import javax.swing.*; 31 import java.awt.*; 32 33 @SuppressWarnings("serial") // Superclass is not serializable across versions 34 class AquaComboBoxRendererInternal extends JLabel implements ListCellRenderer { 35 final JComboBox fComboBox; 36 boolean fSelected; 37 boolean fChecked; 38 boolean fInList; 39 boolean fEditable; 40 boolean fDrawCheckedItem = true; 41 42 // Provides space for a checkbox, and is translucent 43 public AquaComboBoxRendererInternal(final JComboBox comboBox) { 44 super(); 45 fComboBox = comboBox; 46 } 47 48 // Don't include checkIcon space, because this is also used for button size calculations 49 // - the popup-size calc will get checkIcon space from getInsets 50 public Dimension getPreferredSize() { 51 // From BasicComboBoxRenderer - trick to avoid zero-height items 52 final Dimension size; 53 54 final String text = getText(); 55 if ((text == null) || ("".equals(text))) { 56 setText(" "); 57 size = super.getPreferredSize(); 58 setText(""); 59 } else { 60 size = super.getPreferredSize(); 61 } 62 return size; 63 } 64 65 // Don't paint the border here, it gets painted by the UI 66 protected void paintBorder(final Graphics g) { 67 68 } 69 70 public int getBaseline(int width, int height) { 71 return super.getBaseline(width, height) - 1; 72 } 73 74 // Really means is the one with the mouse over it 75 public Component getListCellRendererComponent(final JList list, final Object value, int index, final boolean isSelected, final boolean cellHasFocus) { 76 fInList = (index >= 0); // When the button wants the item painted, it passes in -1 77 fSelected = isSelected; 78 if (index < 0) { 79 index = fComboBox.getSelectedIndex(); 80 } 81 82 // changed this to not ask for selected index but directly compare the current item and selected item 83 // different from basic because basic has no concept of checked, just has the last one selected, 84 // and the user changes selection. We have selection and a check mark. 85 // we used to call fComboBox.getSelectedIndex which ends up being a very bad call for large checkboxes 86 // it does a linear compare of every object in the checkbox until it finds the selected one, so if 87 // we have a 5000 element list we will 5000 * (selected index) .equals() of objects. 88 // See Radar #3141307 89 90 // Fix for Radar # 3204287 where we ask for an item at a negative index! 91 if (index >= 0) { 92 final Object item = fComboBox.getItemAt(index); 93 fChecked = fInList && item != null && item.equals(fComboBox.getSelectedItem()); 94 } else { 95 fChecked = false; 96 } 97 98 fEditable = fComboBox.isEditable(); 99 if (isSelected) { 100 if (fEditable) { 101 setBackground(UIManager.getColor("List.selectionBackground")); 102 setForeground(UIManager.getColor("List.selectionForeground")); 103 } else { 104 setBackground(list.getSelectionBackground()); 105 setForeground(list.getSelectionForeground()); 106 } 107 } else { 108 if (fEditable) { 109 setBackground(UIManager.getColor("List.background")); 110 setForeground(UIManager.getColor("List.foreground")); 111 } else { 112 setBackground(list.getBackground()); 113 setForeground(list.getForeground()); 114 } 115 } 116 117 setFont(list.getFont()); 118 119 if (value instanceof Icon) { 120 setIcon((Icon)value); 121 } else { 122 setText((value == null) ? " " : value.toString()); 123 } 124 return this; 125 } 126 127 public Insets getInsets(Insets insets) { 128 if (insets == null) insets = new Insets(0, 0, 0, 0); 129 insets.top = 1; 130 insets.bottom = 1; 131 insets.right = 5; 132 insets.left = (fInList && !fEditable ? 16 + 7 : 5); 133 return insets; 134 } 135 136 protected void setDrawCheckedItem(final boolean drawCheckedItem) { 137 this.fDrawCheckedItem = drawCheckedItem; 138 } 139 140 // Paint this component, and a checkbox if it's the selected item and not in the button 141 protected void paintComponent(final Graphics g) { 142 if (fInList) { 143 if (fSelected && !fEditable) { 144 AquaMenuPainter.instance().paintSelectedMenuItemBackground(g, getWidth(), getHeight()); 145 } else { 146 g.setColor(getBackground()); 147 g.fillRect(0, 0, getWidth(), getHeight()); 148 } 149 150 if (fChecked && !fEditable && fDrawCheckedItem) { 151 final int y = getHeight() - 4; 152 g.setColor(getForeground()); 153 SwingUtilities2.drawString(fComboBox, g, "\u2713", 6, y); 154 } 155 } 156 super.paintComponent(g); 157 } 158 }