Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/laf/AquaComboBoxButton.java
38831 views
/*1* Copyright (c) 2011, 2012, 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 java.awt.*;2829import javax.swing.*;30import javax.swing.plaf.UIResource;3132import apple.laf.JRSUIState;33import apple.laf.JRSUIConstants.*;3435class AquaComboBoxButton extends JButton {36final protected JComboBox comboBox;37final protected JList list;38final protected CellRendererPane rendererPane;39final protected AquaComboBoxUI ui;4041protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());42boolean isPopDown;43boolean isSquare;4445protected AquaComboBoxButton(final AquaComboBoxUI ui, final JComboBox comboBox, final CellRendererPane rendererPane, final JList list) {46super("");47putClientProperty("JButton.buttonType", "comboboxInternal");4849this.ui = ui;50this.comboBox = comboBox;51this.rendererPane = rendererPane;52this.list = list;5354setModel(new DefaultButtonModel() {55public void setArmed(final boolean armed) {56super.setArmed(isPressed() ? true : armed);57}58});5960setEnabled(comboBox.isEnabled());61}6263public boolean isEnabled() {64return comboBox == null ? true : comboBox.isEnabled();65}6667public boolean isFocusTraversable() {68return false;69}7071protected void setIsPopDown(final boolean isPopDown) {72this.isPopDown = isPopDown;73repaint();74}7576protected void setIsSquare(final boolean isSquare) {77this.isSquare = isSquare;78repaint();79}8081protected State getState(final ButtonModel buttonModel) {82if (!comboBox.isEnabled()) return State.DISABLED;83if (!AquaFocusHandler.isActive(comboBox)) return State.INACTIVE;84if (buttonModel.isArmed()) return State.PRESSED;85return State.ACTIVE;86}8788public void paintComponent(final Graphics g) {89// Don't Paint the button as usual90// super.paintComponent( g );91final boolean editable = comboBox.isEditable();9293int top = 0;94int left = 0;95int width = getWidth();96int height = getHeight();9798if (comboBox.isOpaque()) {99g.setColor(getBackground());100g.fillRect(0, 0, width, height);101}102103final Size size = AquaUtilControlSize.getUserSizeFrom(comboBox);104painter.state.set(size == null ? Size.REGULAR : size);105106final ButtonModel buttonModel = getModel();107painter.state.set(getState(buttonModel));108109painter.state.set(AlignmentVertical.CENTER);110111if (AquaComboBoxUI.isTableCellEditor(comboBox)) {112painter.state.set(AlignmentHorizontal.RIGHT);113painter.state.set(Widget.BUTTON_POP_UP);114painter.state.set(ArrowsOnly.YES);115painter.paint(g, this, left, top, width, height);116doRendererPaint(g, buttonModel, editable, getInsets(), left, top, width, height);117return;118}119120painter.state.set(AlignmentHorizontal.CENTER);121final Insets insets = getInsets();122if (!editable) {123top += insets.top;124left += insets.left;125width -= insets.left + insets.right;126height -= insets.top + insets.bottom;127}128129if (height <= 0 || width <= 0) {130return;131}132133boolean hasFocus = comboBox.hasFocus();134if (editable) {135painter.state.set(Widget.BUTTON_COMBO_BOX);136painter.state.set(IndicatorOnly.YES);137painter.state.set(AlignmentHorizontal.LEFT);138hasFocus |= comboBox.getEditor().getEditorComponent().hasFocus();139} else {140painter.state.set(IndicatorOnly.NO);141painter.state.set(AlignmentHorizontal.CENTER);142if (isPopDown) {143painter.state.set(isSquare ? Widget.BUTTON_POP_DOWN_SQUARE : Widget.BUTTON_POP_DOWN);144} else {145painter.state.set(isSquare ? Widget.BUTTON_POP_UP_SQUARE : Widget.BUTTON_POP_UP);146}147}148painter.state.set(hasFocus ? Focused.YES : Focused.NO);149150if (isSquare) {151painter.paint(g, comboBox, left + 2, top - 1, width - 4, height);152} else {153painter.paint(g, comboBox, left, top, width, height);154}155156// Let the renderer paint157if (!editable && comboBox != null) {158doRendererPaint(g, buttonModel, editable, insets, left, top, width, height);159}160}161162protected void doRendererPaint(final Graphics g, final ButtonModel buttonModel, final boolean editable, final Insets insets, int left, int top, int width, int height) {163final ListCellRenderer renderer = comboBox.getRenderer();164165// fake it out! not renderPressed166final Component c = renderer.getListCellRendererComponent(list, comboBox.getSelectedItem(), -1, false, false);167// System.err.println("Renderer: " + renderer);168169if (!editable && !AquaComboBoxUI.isTableCellEditor(comboBox)) {170final int indentLeft = 10;171final int buttonWidth = 24;172173// hardcoded for now. We should adjust as necessary.174top += 1;175height -= 4;176left += indentLeft;177width -= (indentLeft + buttonWidth);178}179180c.setFont(rendererPane.getFont());181182if (buttonModel.isArmed() && buttonModel.isPressed()) {183if (isOpaque()) {184c.setBackground(UIManager.getColor("Button.select"));185}186c.setForeground(comboBox.getForeground());187} else if (!comboBox.isEnabled()) {188if (isOpaque()) {189c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));190}191c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));192} else {193c.setForeground(comboBox.getForeground());194c.setBackground(comboBox.getBackground());195}196197// Sun Fix for 4238829: should lay out the JPanel.198boolean shouldValidate = false;199if (c instanceof JPanel) {200shouldValidate = true;201}202203final int iconWidth = 0;204final int cWidth = width - (insets.right + iconWidth);205206// fix for 3156483 we need to crop images that are too big.207// if (height > 18)208// always crop.209{210top = height / 2 - 8;211height = 19;212}213214// It doesn't need to draw its background, we handled it215final Color bg = c.getBackground();216final boolean inhibitBackground = bg instanceof UIResource;217if (inhibitBackground) c.setBackground(new Color(0, 0, 0, 0));218219rendererPane.paintComponent(g, c, this, left, top, cWidth, height, shouldValidate); // h - (insets.top + insets.bottom) );220221if (inhibitBackground) c.setBackground(bg);222}223}224225226