Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxButton.java
41154 views
/*1* Copyright (c) 2011, 2015, 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.*;3435@SuppressWarnings("serial") // Superclass is not serializable across versions36class AquaComboBoxButton extends JButton {37protected final JComboBox<Object> comboBox;38protected final JList<?> list;39protected final CellRendererPane rendererPane;40protected final AquaComboBoxUI ui;4142protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());43boolean isPopDown;44boolean isSquare;4546@SuppressWarnings("serial") // anonymous class47protected AquaComboBoxButton(final AquaComboBoxUI ui,48final JComboBox<Object> comboBox,49final CellRendererPane rendererPane,50final JList<?> list) {51super("");52putClientProperty("JButton.buttonType", "comboboxInternal");5354this.ui = ui;55this.comboBox = comboBox;56this.rendererPane = rendererPane;57this.list = list;5859setModel(new DefaultButtonModel() {60@Override61public void setArmed(final boolean armed) {62super.setArmed(isPressed() ? true : armed);63}64});6566setEnabled(comboBox.isEnabled());67}6869@Override70public boolean isEnabled() {71return comboBox == null ? true : comboBox.isEnabled();72}7374@Override75public boolean isFocusable() {76return false;77}7879protected void setIsPopDown(final boolean isPopDown) {80this.isPopDown = isPopDown;81repaint();82}8384protected void setIsSquare(final boolean isSquare) {85this.isSquare = isSquare;86repaint();87}8889protected State getState(final ButtonModel buttonModel) {90if (!comboBox.isEnabled()) return State.DISABLED;91if (!AquaFocusHandler.isActive(comboBox)) return State.INACTIVE;92if (buttonModel.isArmed()) return State.PRESSED;93return State.ACTIVE;94}9596@Override97public void paintComponent(final Graphics g) {98// Don't Paint the button as usual99// super.paintComponent( g );100final boolean editable = comboBox.isEditable();101102int top = 0;103int left = 0;104int width = getWidth();105int height = getHeight();106107if (comboBox.isOpaque()) {108g.setColor(getBackground());109g.fillRect(0, 0, width, height);110}111112final Size size = AquaUtilControlSize.getUserSizeFrom(comboBox);113painter.state.set(size == null ? Size.REGULAR : size);114115final ButtonModel buttonModel = getModel();116painter.state.set(getState(buttonModel));117118painter.state.set(AlignmentVertical.CENTER);119120if (AquaComboBoxUI.isTableCellEditor(comboBox)) {121painter.state.set(AlignmentHorizontal.RIGHT);122painter.state.set(Widget.BUTTON_POP_UP);123painter.state.set(ArrowsOnly.YES);124painter.paint(g, this, left, top, width, height);125doRendererPaint(g, buttonModel, editable, getInsets(), left, top, width, height);126return;127}128129painter.state.set(AlignmentHorizontal.CENTER);130final Insets insets = getInsets();131if (!editable) {132top += insets.top;133left += insets.left;134width -= insets.left + insets.right;135height -= insets.top + insets.bottom;136}137138if (height <= 0 || width <= 0) {139return;140}141142boolean hasFocus = comboBox.hasFocus();143if (editable) {144painter.state.set(Widget.BUTTON_COMBO_BOX);145painter.state.set(IndicatorOnly.YES);146painter.state.set(AlignmentHorizontal.LEFT);147hasFocus |= comboBox.getEditor().getEditorComponent().hasFocus();148} else {149painter.state.set(IndicatorOnly.NO);150painter.state.set(AlignmentHorizontal.CENTER);151if (isPopDown) {152painter.state.set(isSquare ? Widget.BUTTON_POP_DOWN_SQUARE : Widget.BUTTON_POP_DOWN);153} else {154painter.state.set(isSquare ? Widget.BUTTON_POP_UP_SQUARE : Widget.BUTTON_POP_UP);155}156}157painter.state.set(hasFocus ? Focused.YES : Focused.NO);158159if (isSquare) {160painter.paint(g, comboBox, left + 2, top - 1, width - 4, height);161} else {162painter.paint(g, comboBox, left, top, width, height);163}164165// Let the renderer paint166if (!editable && comboBox != null) {167doRendererPaint(g, buttonModel, editable, insets, left, top, width, height);168}169}170171protected void doRendererPaint(final Graphics g, final ButtonModel buttonModel, final boolean editable, final Insets insets, int left, int top, int width, int height) {172final ListCellRenderer<Object> renderer = comboBox.getRenderer();173174// fake it out! not renderPressed175final Component c = renderer.getListCellRendererComponent(list, comboBox.getSelectedItem(), -1, false, false);176// System.err.println("Renderer: " + renderer);177178if (!editable && !AquaComboBoxUI.isTableCellEditor(comboBox)) {179final int indentLeft = 10;180final int buttonWidth = 24;181182// hardcoded for now. We should adjust as necessary.183top += 1;184height -= 4;185left += indentLeft;186width -= (indentLeft + buttonWidth);187}188189c.setFont(rendererPane.getFont());190191if (buttonModel.isArmed() && buttonModel.isPressed()) {192if (isOpaque()) {193c.setBackground(UIManager.getColor("Button.select"));194}195c.setForeground(comboBox.getForeground());196} else if (!comboBox.isEnabled()) {197if (isOpaque()) {198c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));199}200c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));201} else {202c.setForeground(comboBox.getForeground());203c.setBackground(comboBox.getBackground());204}205206// Sun Fix for 4238829: should lay out the JPanel.207boolean shouldValidate = false;208if (c instanceof JPanel) {209shouldValidate = true;210}211212final int iconWidth = 0;213final int cWidth = width - (insets.right + iconWidth);214215// fix for 3156483 we need to crop images that are too big.216// if (height > 18)217// always crop.218{219top = height / 2 - 8;220height = 19;221}222223// It doesn't need to draw its background, we handled it224final Color bg = c.getBackground();225final boolean inhibitBackground = bg instanceof UIResource;226if (inhibitBackground) c.setBackground(new Color(0, 0, 0, 0));227228rendererPane.paintComponent(g, c, this, left, top, cWidth, height, shouldValidate); // h - (insets.top + insets.bottom) );229230if (inhibitBackground) c.setBackground(bg);231232// Remove component from renderer pane, allowing it to be gc'ed.233rendererPane.remove(c);234}235}236237238