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<E> extends JLabel implements ListCellRenderer<E> {
  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<? extends E> list,
  76                                                   final E value, int index,
  77                                                   final boolean isSelected,
  78                                                   final boolean cellHasFocus) {
  79         fInList = (index >= 0); // When the button wants the item painted, it passes in -1
  80         fSelected = isSelected;
  81         if (index < 0) {
  82             index = fComboBox.getSelectedIndex();
  83         }
  84 
  85         // changed this to not ask for selected index but directly compare the current item and selected item
  86         // different from basic because basic has no concept of checked, just has the last one selected,
  87         // and the user changes selection. We have selection and a check mark.
  88         // we used to call fComboBox.getSelectedIndex which ends up being a very bad call for large checkboxes
  89         // it does a linear compare of every object in the checkbox until it finds the selected one, so if
  90         // we have a 5000 element list we will 5000 * (selected index) .equals() of objects.
  91         // See Radar #3141307
  92 
  93         // Fix for Radar # 3204287 where we ask for an item at a negative index!
  94         if (index >= 0) {
  95             final Object item = fComboBox.getItemAt(index);
  96             fChecked = fInList && item != null && item.equals(fComboBox.getSelectedItem());
  97         } else {
  98             fChecked = false;
  99         }
 100 
 101         fEditable = fComboBox.isEditable();
 102         if (isSelected) {
 103             if (fEditable) {
 104                 setBackground(UIManager.getColor("List.selectionBackground"));
 105                 setForeground(UIManager.getColor("List.selectionForeground"));
 106             } else {
 107                 setBackground(list.getSelectionBackground());
 108                 setForeground(list.getSelectionForeground());
 109             }
 110         } else {
 111             if (fEditable) {
 112                 setBackground(UIManager.getColor("List.background"));
 113                 setForeground(UIManager.getColor("List.foreground"));
 114             } else {
 115                 setBackground(list.getBackground());
 116                 setForeground(list.getForeground());
 117             }
 118         }
 119 
 120         setFont(list.getFont());
 121 
 122         if (value instanceof Icon) {
 123             setIcon((Icon)value);
 124         } else {
 125             setText((value == null) ? " " : value.toString());
 126         }
 127         return this;
 128     }
 129 
 130     public Insets getInsets(Insets insets) {
 131         if (insets == null) insets = new Insets(0, 0, 0, 0);
 132         insets.top = 1;
 133         insets.bottom = 1;
 134         insets.right = 5;
 135         insets.left = (fInList && !fEditable ? 16 + 7 : 5);
 136         return insets;
 137     }
 138 
 139     protected void setDrawCheckedItem(final boolean drawCheckedItem) {
 140         this.fDrawCheckedItem = drawCheckedItem;
 141     }
 142 
 143     // Paint this component, and a checkbox if it's the selected item and not in the button
 144     protected void paintComponent(final Graphics g) {
 145         if (fInList) {
 146             if (fSelected && !fEditable) {
 147                 AquaMenuPainter.instance().paintSelectedMenuItemBackground(g, getWidth(), getHeight());
 148             } else {
 149                 g.setColor(getBackground());
 150                 g.fillRect(0, 0, getWidth(), getHeight());
 151             }
 152 
 153             if (fChecked && !fEditable && fDrawCheckedItem) {
 154                 final int y = getHeight() - 4;
 155                 g.setColor(getForeground());
 156                 SwingUtilities2.getTextUIDrawing(fComboBox)
 157                         .drawString(fComboBox, g, "\u2713", 6, y);
 158             }
 159         }
 160         super.paintComponent(g);
 161     }
 162 }