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