4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.laf;
4632N/A
4632N/Aimport java.awt.*;
4632N/A
4632N/Aimport javax.swing.*;
4632N/Aimport javax.swing.plaf.UIResource;
4632N/A
4632N/Aimport apple.laf.JRSUIState;
4632N/Aimport apple.laf.JRSUIConstants.*;
4632N/A
4632N/Aclass AquaComboBoxButton extends JButton {
4632N/A final protected JComboBox comboBox;
4632N/A final protected JList list;
4632N/A final protected CellRendererPane rendererPane;
4632N/A final protected AquaComboBoxUI ui;
4632N/A
4632N/A protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
4632N/A boolean isPopDown;
4632N/A boolean isSquare;
4632N/A
4632N/A protected AquaComboBoxButton(final AquaComboBoxUI ui, final JComboBox comboBox, final CellRendererPane rendererPane, final JList list) {
4632N/A super("");
4632N/A putClientProperty("JButton.buttonType", "comboboxInternal");
4632N/A
4632N/A this.ui = ui;
4632N/A this.comboBox = comboBox;
4632N/A this.rendererPane = rendererPane;
4632N/A this.list = list;
4632N/A
4632N/A setModel(new DefaultButtonModel() {
4632N/A public void setArmed(final boolean armed) {
4632N/A super.setArmed(isPressed() ? true : armed);
4632N/A }
4632N/A });
4632N/A
4632N/A setEnabled(comboBox.isEnabled());
4632N/A }
4632N/A
4632N/A public boolean isEnabled() {
4632N/A return comboBox == null ? true : comboBox.isEnabled();
4632N/A }
4632N/A
4632N/A public boolean isFocusTraversable() {
4632N/A return false;
4632N/A }
4632N/A
4632N/A protected void setIsPopDown(final boolean isPopDown) {
4632N/A this.isPopDown = isPopDown;
4632N/A repaint();
4632N/A }
4632N/A
4632N/A protected void setIsSquare(final boolean isSquare) {
4632N/A this.isSquare = isSquare;
4632N/A repaint();
4632N/A }
4632N/A
4632N/A protected State getState(final ButtonModel buttonModel) {
4632N/A if (!comboBox.isEnabled()) return State.DISABLED;
4632N/A if (!AquaFocusHandler.isActive(comboBox)) return State.INACTIVE;
4632N/A if (buttonModel.isArmed()) return State.PRESSED;
4632N/A return State.ACTIVE;
4632N/A }
4632N/A
4632N/A public void paintComponent(final Graphics g) {
4632N/A // Don't Paint the button as usual
4632N/A // super.paintComponent( g );
4632N/A final boolean editable = comboBox.isEditable();
4632N/A
4632N/A int top = 0;
4632N/A int left = 0;
4632N/A int width = getWidth();
4632N/A int height = getHeight();
4632N/A
4632N/A if (comboBox.isOpaque()) {
4632N/A g.setColor(getBackground());
4632N/A g.fillRect(0, 0, width, height);
4632N/A }
4632N/A
4632N/A final Size size = AquaUtilControlSize.getUserSizeFrom(comboBox);
4632N/A painter.state.set(size == null ? Size.REGULAR : size);
4632N/A
4632N/A final ButtonModel buttonModel = getModel();
4632N/A painter.state.set(getState(buttonModel));
4632N/A
4632N/A painter.state.set(AlignmentVertical.CENTER);
4632N/A
4632N/A if (AquaComboBoxUI.isTableCellEditor(comboBox)) {
4632N/A painter.state.set(AlignmentHorizontal.RIGHT);
4632N/A painter.state.set(Widget.BUTTON_POP_UP);
4632N/A painter.state.set(ArrowsOnly.YES);
4632N/A painter.paint(g, this, left, top, width, height);
4632N/A doRendererPaint(g, buttonModel, editable, getInsets(), left, top, width, height);
4632N/A return;
4632N/A }
4632N/A
4632N/A painter.state.set(AlignmentHorizontal.CENTER);
4632N/A final Insets insets = getInsets();
4632N/A if (!editable) {
4632N/A top += insets.top;
4632N/A left += insets.left;
4632N/A width -= insets.left + insets.right;
4632N/A height -= insets.top + insets.bottom;
4632N/A }
4632N/A
4632N/A if (height <= 0 || width <= 0) {
4632N/A return;
4632N/A }
4632N/A
4632N/A boolean hasFocus = comboBox.hasFocus();
4632N/A if (editable) {
4632N/A painter.state.set(Widget.BUTTON_COMBO_BOX);
4632N/A painter.state.set(IndicatorOnly.YES);
4632N/A painter.state.set(AlignmentHorizontal.LEFT);
4632N/A hasFocus |= comboBox.getEditor().getEditorComponent().hasFocus();
4632N/A } else {
4632N/A painter.state.set(IndicatorOnly.NO);
4632N/A painter.state.set(AlignmentHorizontal.CENTER);
4632N/A if (isPopDown) {
4632N/A painter.state.set(isSquare ? Widget.BUTTON_POP_DOWN_SQUARE : Widget.BUTTON_POP_DOWN);
4632N/A } else {
4632N/A painter.state.set(isSquare ? Widget.BUTTON_POP_UP_SQUARE : Widget.BUTTON_POP_UP);
4632N/A }
4632N/A }
4632N/A painter.state.set(hasFocus ? Focused.YES : Focused.NO);
4632N/A
4632N/A if (isSquare) {
4632N/A painter.paint(g, comboBox, left + 2, top - 1, width - 4, height);
4632N/A } else {
4632N/A painter.paint(g, comboBox, left, top, width, height);
4632N/A }
4632N/A
4632N/A // Let the renderer paint
4632N/A if (!editable && comboBox != null) {
4632N/A doRendererPaint(g, buttonModel, editable, insets, left, top, width, height);
4632N/A }
4632N/A }
4632N/A
4632N/A protected void doRendererPaint(final Graphics g, final ButtonModel buttonModel, final boolean editable, final Insets insets, int left, int top, int width, int height) {
4632N/A final ListCellRenderer renderer = comboBox.getRenderer();
4632N/A
4632N/A // fake it out! not renderPressed
4632N/A final Component c = renderer.getListCellRendererComponent(list, comboBox.getSelectedItem(), -1, false, false);
4632N/A // System.err.println("Renderer: " + renderer);
4632N/A
4632N/A if (!editable && !AquaComboBoxUI.isTableCellEditor(comboBox)) {
4632N/A final int indentLeft = 10;
4632N/A final int buttonWidth = 24;
4632N/A
4632N/A // hardcoded for now. We should adjust as necessary.
4632N/A top += 1;
4632N/A height -= 4;
4632N/A left += indentLeft;
4632N/A width -= (indentLeft + buttonWidth);
4632N/A }
4632N/A
4632N/A c.setFont(rendererPane.getFont());
4632N/A
4632N/A if (buttonModel.isArmed() && buttonModel.isPressed()) {
4632N/A if (isOpaque()) {
4632N/A c.setBackground(UIManager.getColor("Button.select"));
4632N/A }
4632N/A c.setForeground(comboBox.getForeground());
4632N/A } else if (!comboBox.isEnabled()) {
4632N/A if (isOpaque()) {
4632N/A c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
4632N/A }
4632N/A c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
4632N/A } else {
4632N/A c.setForeground(comboBox.getForeground());
4632N/A c.setBackground(comboBox.getBackground());
4632N/A }
4632N/A
4632N/A // Sun Fix for 4238829: should lay out the JPanel.
4632N/A boolean shouldValidate = false;
4632N/A if (c instanceof JPanel) {
4632N/A shouldValidate = true;
4632N/A }
4632N/A
4632N/A final int iconWidth = 0;
4632N/A final int cWidth = width - (insets.right + iconWidth);
4632N/A
4632N/A // fix for 3156483 we need to crop images that are too big.
4632N/A // if (height > 18)
4632N/A // always crop.
4632N/A {
4632N/A top = height / 2 - 8;
4632N/A height = 19;
4632N/A }
4632N/A
4632N/A // It doesn't need to draw its background, we handled it
4632N/A final Color bg = c.getBackground();
4632N/A final boolean inhibitBackground = bg instanceof UIResource;
4632N/A if (inhibitBackground) c.setBackground(new Color(0, 0, 0, 0));
4632N/A
4632N/A rendererPane.paintComponent(g, c, this, left, top, cWidth, height, shouldValidate); // h - (insets.top + insets.bottom) );
4632N/A
4632N/A if (inhibitBackground) c.setBackground(bg);
4632N/A }
4632N/A}