0N/A/*
2362N/A * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.metal;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.border.*;
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * JButton subclass to help out MetalComboBoxUI
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @see MetalComboBoxButton
0N/A * @author Tom Santos
0N/A */
0N/Apublic class MetalComboBoxButton extends JButton {
0N/A protected JComboBox comboBox;
0N/A protected JList listBox;
0N/A protected CellRendererPane rendererPane;
0N/A protected Icon comboIcon;
0N/A protected boolean iconOnly = false;
0N/A
0N/A public final JComboBox getComboBox() { return comboBox;}
0N/A public final void setComboBox( JComboBox cb ) { comboBox = cb;}
0N/A
0N/A public final Icon getComboIcon() { return comboIcon;}
0N/A public final void setComboIcon( Icon i ) { comboIcon = i;}
0N/A
0N/A public final boolean isIconOnly() { return iconOnly;}
0N/A public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
0N/A
0N/A MetalComboBoxButton() {
0N/A super( "" );
0N/A DefaultButtonModel model = new DefaultButtonModel() {
0N/A public void setArmed( boolean armed ) {
0N/A super.setArmed( isPressed() ? true : armed );
0N/A }
0N/A };
0N/A setModel( model );
0N/A }
0N/A
0N/A public MetalComboBoxButton( JComboBox cb, Icon i,
0N/A CellRendererPane pane, JList list ) {
0N/A this();
0N/A comboBox = cb;
0N/A comboIcon = i;
0N/A rendererPane = pane;
0N/A listBox = list;
0N/A setEnabled( comboBox.isEnabled() );
0N/A }
0N/A
0N/A public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
0N/A CellRendererPane pane, JList list ) {
0N/A this( cb, i, pane, list );
0N/A iconOnly = onlyIcon;
0N/A }
0N/A
0N/A public boolean isFocusTraversable() {
0N/A return false;
0N/A }
0N/A
0N/A public void setEnabled(boolean enabled) {
0N/A super.setEnabled(enabled);
0N/A
0N/A // Set the background and foreground to the combobox colors.
0N/A if (enabled) {
0N/A setBackground(comboBox.getBackground());
0N/A setForeground(comboBox.getForeground());
0N/A } else {
0N/A setBackground(UIManager.getColor("ComboBox.disabledBackground"));
0N/A setForeground(UIManager.getColor("ComboBox.disabledForeground"));
0N/A }
0N/A }
0N/A
0N/A public void paintComponent( Graphics g ) {
0N/A boolean leftToRight = MetalUtils.isLeftToRight(comboBox);
0N/A
0N/A // Paint the button as usual
0N/A super.paintComponent( g );
0N/A
0N/A Insets insets = getInsets();
0N/A
0N/A int width = getWidth() - (insets.left + insets.right);
0N/A int height = getHeight() - (insets.top + insets.bottom);
0N/A
0N/A if ( height <= 0 || width <= 0 ) {
0N/A return;
0N/A }
0N/A
0N/A int left = insets.left;
0N/A int top = insets.top;
0N/A int right = left + (width - 1);
0N/A int bottom = top + (height - 1);
0N/A
0N/A int iconWidth = 0;
0N/A int iconLeft = (leftToRight) ? right : left;
0N/A
0N/A // Paint the icon
0N/A if ( comboIcon != null ) {
0N/A iconWidth = comboIcon.getIconWidth();
0N/A int iconHeight = comboIcon.getIconHeight();
0N/A int iconTop = 0;
0N/A
0N/A if ( iconOnly ) {
0N/A iconLeft = (getWidth() / 2) - (iconWidth / 2);
0N/A iconTop = (getHeight() / 2) - (iconHeight / 2);
0N/A }
0N/A else {
0N/A if (leftToRight) {
0N/A iconLeft = (left + (width - 1)) - iconWidth;
0N/A }
0N/A else {
0N/A iconLeft = left;
0N/A }
0N/A iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
0N/A }
0N/A
0N/A comboIcon.paintIcon( this, g, iconLeft, iconTop );
0N/A
0N/A // Paint the focus
0N/A if ( comboBox.hasFocus() && (!MetalLookAndFeel.usingOcean() ||
0N/A comboBox.isEditable())) {
0N/A g.setColor( MetalLookAndFeel.getFocusColor() );
0N/A g.drawRect( left - 1, top - 1, width + 3, height + 1 );
0N/A }
0N/A }
0N/A
0N/A if (MetalLookAndFeel.usingOcean()) {
0N/A // With Ocean the button only paints the arrow, bail.
0N/A return;
0N/A }
0N/A
0N/A // Let the renderer paint
0N/A if ( ! iconOnly && comboBox != null ) {
0N/A ListCellRenderer renderer = comboBox.getRenderer();
0N/A Component c;
0N/A boolean renderPressed = getModel().isPressed();
0N/A c = renderer.getListCellRendererComponent(listBox,
0N/A comboBox.getSelectedItem(),
0N/A -1,
0N/A renderPressed,
0N/A false);
0N/A c.setFont(rendererPane.getFont());
0N/A
0N/A if ( model.isArmed() && model.isPressed() ) {
0N/A if ( isOpaque() ) {
0N/A c.setBackground(UIManager.getColor("Button.select"));
0N/A }
0N/A c.setForeground(comboBox.getForeground());
0N/A }
0N/A else if ( !comboBox.isEnabled() ) {
0N/A if ( isOpaque() ) {
0N/A c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
0N/A }
0N/A c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
0N/A }
0N/A else {
0N/A c.setForeground(comboBox.getForeground());
0N/A c.setBackground(comboBox.getBackground());
0N/A }
0N/A
0N/A
0N/A int cWidth = width - (insets.right + iconWidth);
0N/A
0N/A // Fix for 4238829: should lay out the JPanel.
0N/A boolean shouldValidate = false;
0N/A if (c instanceof JPanel) {
0N/A shouldValidate = true;
0N/A }
0N/A
0N/A if (leftToRight) {
0N/A rendererPane.paintComponent( g, c, this,
0N/A left, top, cWidth, height, shouldValidate );
0N/A }
0N/A else {
0N/A rendererPane.paintComponent( g, c, this,
0N/A left + iconWidth, top, cWidth, height, shouldValidate );
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Dimension getMinimumSize() {
0N/A Dimension ret = new Dimension();
0N/A Insets insets = getInsets();
0N/A ret.width = insets.left + getComboIcon().getIconWidth() + insets.right;
0N/A ret.height = insets.bottom + getComboIcon().getIconHeight() + insets.top;
0N/A return ret;
0N/A }
0N/A}