/* * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.plaf.metal; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.*; import javax.swing.border.*; import javax.swing.plaf.basic.*; import java.io.Serializable; import java.beans.*; /** * Metal UI for JComboBox *
* Warning:
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeansTM
* has been added to the java.beans
package.
* Please see {@link java.beans.XMLEncoder}.
*
* @see MetalComboBoxEditor
* @see MetalComboBoxButton
* @author Tom Santos
*/
public class MetalComboBoxUI extends BasicComboBoxUI {
public static ComponentUI createUI(JComponent c) {
return new MetalComboBoxUI();
}
public void paint(Graphics g, JComponent c) {
if (MetalLookAndFeel.usingOcean()) {
super.paint(g, c);
}
}
/**
* If necessary paints the currently selected item.
*
* @param g Graphics to paint to
* @param bounds Region to paint current value to
* @param hasFocus whether or not the JComboBox has focus
* @throws NullPointerException if any of the arguments are null.
* @since 1.5
*/
public void paintCurrentValue(Graphics g, Rectangle bounds,
boolean hasFocus) {
// This is really only called if we're using ocean.
if (MetalLookAndFeel.usingOcean()) {
bounds.x += 2;
bounds.width -= 3;
if (arrowButton != null) {
Insets buttonInsets = arrowButton.getInsets();
bounds.y += buttonInsets.top;
bounds.height -= (buttonInsets.top + buttonInsets.bottom);
}
else {
bounds.y += 2;
bounds.height -= 4;
}
super.paintCurrentValue(g, bounds, hasFocus);
}
else if (g == null || bounds == null) {
throw new NullPointerException(
"Must supply a non-null Graphics and Rectangle");
}
}
/**
* If necessary paints the background of the currently selected item.
*
* @param g Graphics to paint to
* @param bounds Region to paint background to
* @param hasFocus whether or not the JComboBox has focus
* @throws NullPointerException if any of the arguments are null.
* @since 1.5
*/
public void paintCurrentValueBackground(Graphics g, Rectangle bounds,
boolean hasFocus) {
// This is really only called if we're using ocean.
if (MetalLookAndFeel.usingOcean()) {
g.setColor(MetalLookAndFeel.getControlDarkShadow());
g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1);
g.setColor(MetalLookAndFeel.getControlShadow());
g.drawRect(bounds.x + 1, bounds.y + 1, bounds.width - 2,
bounds.height - 3);
if (hasFocus && !isPopupVisible(comboBox) &&
arrowButton != null) {
g.setColor(listBox.getSelectionBackground());
Insets buttonInsets = arrowButton.getInsets();
if (buttonInsets.top > 2) {
g.fillRect(bounds.x + 2, bounds.y + 2, bounds.width - 3,
buttonInsets.top - 2);
}
if (buttonInsets.bottom > 2) {
g.fillRect(bounds.x + 2, bounds.y + bounds.height -
buttonInsets.bottom, bounds.width - 3,
buttonInsets.bottom - 2);
}
}
}
else if (g == null || bounds == null) {
throw new NullPointerException(
"Must supply a non-null Graphics and Rectangle");
}
}
/**
* Returns the baseline.
*
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @see javax.swing.JComponent#getBaseline(int, int)
* @since 1.6
*/
public int getBaseline(JComponent c, int width, int height) {
int baseline;
if (MetalLookAndFeel.usingOcean() && height >= 4) {
height -= 4;
baseline = super.getBaseline(c, width, height);
if (baseline >= 0) {
baseline += 2;
}
}
else {
baseline = super.getBaseline(c, width, height);
}
return baseline;
}
protected ComboBoxEditor createEditor() {
return new MetalComboBoxEditor.UIResource();
}
protected ComboPopup createPopup() {
return super.createPopup();
}
protected JButton createArrowButton() {
boolean iconOnly = (comboBox.isEditable() ||
MetalLookAndFeel.usingOcean());
JButton button = new MetalComboBoxButton( comboBox,
new MetalComboBoxIcon(),
iconOnly,
currentValuePane,
listBox );
button.setMargin( new Insets( 0, 1, 1, 3 ) );
if (MetalLookAndFeel.usingOcean()) {
// Disabled rollover effect.
button.putClientProperty(MetalBorders.NO_BUTTON_ROLLOVER,
Boolean.TRUE);
}
updateButtonForOcean(button);
return button;
}
/**
* Resets the necessary state on the ComboBoxButton for ocean.
*/
private void updateButtonForOcean(JButton button) {
if (MetalLookAndFeel.usingOcean()) {
// Ocean renders the focus in a different way, this
// would be redundant.
button.setFocusPainted(comboBox.isEditable());
}
}
public PropertyChangeListener createPropertyChangeListener() {
return new MetalPropertyChangeListener();
}
/**
* This inner class is marked "public" due to a compiler bug.
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of