Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxRendererInternal.java
41154 views
/*1* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import sun.swing.SwingUtilities2;2829import javax.swing.*;30import java.awt.*;3132@SuppressWarnings("serial") // Superclass is not serializable across versions33class AquaComboBoxRendererInternal<E> extends JLabel implements ListCellRenderer<E> {34final JComboBox<?> fComboBox;35boolean fSelected;36boolean fChecked;37boolean fInList;38boolean fEditable;39boolean fDrawCheckedItem = true;4041// Provides space for a checkbox, and is translucent42public AquaComboBoxRendererInternal(final JComboBox<?> comboBox) {43super();44fComboBox = comboBox;45}4647// Don't include checkIcon space, because this is also used for button size calculations48// - the popup-size calc will get checkIcon space from getInsets49public Dimension getPreferredSize() {50// From BasicComboBoxRenderer - trick to avoid zero-height items51final Dimension size;5253final String text = getText();54if (text == null || text.isEmpty()) {55setText(" ");56size = super.getPreferredSize();57setText("");58} else {59size = super.getPreferredSize();60}61return size;62}6364// Don't paint the border here, it gets painted by the UI65protected void paintBorder(final Graphics g) {6667}6869public int getBaseline(int width, int height) {70return super.getBaseline(width, height) - 1;71}7273// Really means is the one with the mouse over it74public Component getListCellRendererComponent(final JList<? extends E> list,75final E value, int index,76final boolean isSelected,77final boolean cellHasFocus) {78fInList = (index >= 0); // When the button wants the item painted, it passes in -179fSelected = isSelected;80if (index < 0) {81index = fComboBox.getSelectedIndex();82}8384// changed this to not ask for selected index but directly compare the current item and selected item85// different from basic because basic has no concept of checked, just has the last one selected,86// and the user changes selection. We have selection and a check mark.87// we used to call fComboBox.getSelectedIndex which ends up being a very bad call for large checkboxes88// it does a linear compare of every object in the checkbox until it finds the selected one, so if89// we have a 5000 element list we will 5000 * (selected index) .equals() of objects.90// See Radar #31413079192// Fix for Radar # 3204287 where we ask for an item at a negative index!93if (index >= 0) {94final Object item = fComboBox.getItemAt(index);95fChecked = fInList && item != null && item.equals(fComboBox.getSelectedItem());96} else {97fChecked = false;98}99100fEditable = fComboBox.isEditable();101if (isSelected) {102if (fEditable) {103setBackground(UIManager.getColor("List.selectionBackground"));104setForeground(UIManager.getColor("List.selectionForeground"));105} else {106setBackground(list.getSelectionBackground());107setForeground(list.getSelectionForeground());108}109} else {110if (fEditable) {111setBackground(UIManager.getColor("List.background"));112setForeground(UIManager.getColor("List.foreground"));113} else {114setBackground(list.getBackground());115setForeground(list.getForeground());116}117}118119setFont(list.getFont());120121if (value instanceof Icon) {122setIcon((Icon)value);123} else {124setText((value == null) ? " " : value.toString());125}126return this;127}128129public Insets getInsets(Insets insets) {130if (insets == null) insets = new Insets(0, 0, 0, 0);131insets.top = 1;132insets.bottom = 1;133insets.right = 5;134insets.left = (fInList && !fEditable ? 16 + 7 : 5);135return insets;136}137138protected void setDrawCheckedItem(final boolean drawCheckedItem) {139this.fDrawCheckedItem = drawCheckedItem;140}141142// Paint this component, and a checkbox if it's the selected item and not in the button143protected void paintComponent(final Graphics g) {144if (fInList) {145if (fSelected && !fEditable) {146AquaMenuPainter.instance().paintSelectedMenuItemBackground(g, getWidth(), getHeight());147} else {148g.setColor(getBackground());149g.fillRect(0, 0, getWidth(), getHeight());150}151152if (fChecked && !fEditable && fDrawCheckedItem) {153final int y = getHeight() - 4;154g.setColor(getForeground());155SwingUtilities2.drawString(fComboBox, g, "\u2713", 6, y);156}157}158super.paintComponent(g);159}160}161162163