0N/A/*
2362N/A * Copyright (c) 1998, 2005, 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 sun.swing.SwingUtilities2;
1859N/Aimport sun.awt.AppContext;
1859N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.lang.ref.*;
0N/Aimport java.util.*;
0N/Aimport javax.swing.plaf.basic.BasicToggleButtonUI;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.*;
0N/A
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * MetalToggleButton implementation
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 * @author Tom Santos
0N/A */
0N/Apublic class MetalToggleButtonUI extends BasicToggleButtonUI {
0N/A
1859N/A private static final Object METAL_TOGGLE_BUTTON_UI_KEY = new Object();
0N/A
0N/A protected Color focusColor;
0N/A protected Color selectColor;
0N/A protected Color disabledTextColor;
0N/A
0N/A private boolean defaults_initialized = false;
0N/A
0N/A // ********************************
0N/A // Create PLAF
0N/A // ********************************
0N/A public static ComponentUI createUI(JComponent b) {
1859N/A AppContext appContext = AppContext.getAppContext();
1859N/A MetalToggleButtonUI metalToggleButtonUI =
1859N/A (MetalToggleButtonUI) appContext.get(METAL_TOGGLE_BUTTON_UI_KEY);
1859N/A if (metalToggleButtonUI == null) {
1859N/A metalToggleButtonUI = new MetalToggleButtonUI();
1859N/A appContext.put(METAL_TOGGLE_BUTTON_UI_KEY, metalToggleButtonUI);
1859N/A }
0N/A return metalToggleButtonUI;
0N/A }
0N/A
0N/A // ********************************
0N/A // Install Defaults
0N/A // ********************************
0N/A public void installDefaults(AbstractButton b) {
0N/A super.installDefaults(b);
0N/A if(!defaults_initialized) {
0N/A focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
0N/A selectColor = UIManager.getColor(getPropertyPrefix() + "select");
0N/A disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
0N/A defaults_initialized = true;
0N/A }
0N/A }
0N/A
0N/A protected void uninstallDefaults(AbstractButton b) {
0N/A super.uninstallDefaults(b);
0N/A defaults_initialized = false;
0N/A }
0N/A
0N/A // ********************************
0N/A // Default Accessors
0N/A // ********************************
0N/A protected Color getSelectColor() {
0N/A return selectColor;
0N/A }
0N/A
0N/A protected Color getDisabledTextColor() {
0N/A return disabledTextColor;
0N/A }
0N/A
0N/A protected Color getFocusColor() {
0N/A return focusColor;
0N/A }
0N/A
0N/A
0N/A // ********************************
0N/A // Paint Methods
0N/A // ********************************
0N/A /**
0N/A * If necessary paints the background of the component, then invokes
0N/A * <code>paint</code>.
0N/A *
0N/A * @param g Graphics to paint to
0N/A * @param c JComponent painting on
0N/A * @throws NullPointerException if <code>g</code> or <code>c</code> is
0N/A * null
0N/A * @see javax.swing.plaf.ComponentUI#update
0N/A * @see javax.swing.plaf.ComponentUI#paint
0N/A * @since 1.5
0N/A */
0N/A public void update(Graphics g, JComponent c) {
0N/A AbstractButton button = (AbstractButton)c;
0N/A if ((c.getBackground() instanceof UIResource) &&
0N/A button.isContentAreaFilled() && c.isEnabled()) {
0N/A ButtonModel model = button.getModel();
0N/A if (!MetalUtils.isToolBarButton(c)) {
0N/A if (!model.isArmed() && !model.isPressed() &&
0N/A MetalUtils.drawGradient(
0N/A c, g, "ToggleButton.gradient", 0, 0, c.getWidth(),
0N/A c.getHeight(), true)) {
0N/A paint(g, c);
0N/A return;
0N/A }
0N/A }
0N/A else if ((model.isRollover() || model.isSelected()) &&
0N/A MetalUtils.drawGradient(c, g, "ToggleButton.gradient",
0N/A 0, 0, c.getWidth(), c.getHeight(), true)) {
0N/A paint(g, c);
0N/A return;
0N/A }
0N/A }
0N/A super.update(g, c);
0N/A }
0N/A
0N/A protected void paintButtonPressed(Graphics g, AbstractButton b) {
0N/A if ( b.isContentAreaFilled() ) {
0N/A g.setColor(getSelectColor());
0N/A g.fillRect(0, 0, b.getWidth(), b.getHeight());
0N/A }
0N/A }
0N/A
0N/A protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
0N/A AbstractButton b = (AbstractButton) c;
0N/A ButtonModel model = b.getModel();
0N/A FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
0N/A int mnemIndex = b.getDisplayedMnemonicIndex();
0N/A
0N/A /* Draw the Text */
0N/A if(model.isEnabled()) {
0N/A /*** paint the text normally */
0N/A g.setColor(b.getForeground());
0N/A }
0N/A else {
0N/A /*** paint the text disabled ***/
0N/A if (model.isSelected()) {
0N/A g.setColor(c.getBackground());
0N/A } else {
0N/A g.setColor(getDisabledTextColor());
0N/A }
0N/A }
0N/A SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
0N/A textRect.x, textRect.y + fm.getAscent());
0N/A }
0N/A
0N/A protected void paintFocus(Graphics g, AbstractButton b,
0N/A Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
0N/A
0N/A Rectangle focusRect = new Rectangle();
0N/A String text = b.getText();
0N/A boolean isIcon = b.getIcon() != null;
0N/A
0N/A // If there is text
0N/A if ( text != null && !text.equals( "" ) ) {
0N/A if ( !isIcon ) {
0N/A focusRect.setBounds( textRect );
0N/A }
0N/A else {
0N/A focusRect.setBounds( iconRect.union( textRect ) );
0N/A }
0N/A }
0N/A // If there is an icon and no text
0N/A else if ( isIcon ) {
0N/A focusRect.setBounds( iconRect );
0N/A }
0N/A
0N/A g.setColor(getFocusColor());
0N/A g.drawRect((focusRect.x-1), (focusRect.y-1),
0N/A focusRect.width+1, focusRect.height+1);
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Paints the appropriate icon of the button <code>b</code> in the
0N/A * space <code>iconRect</code>.
0N/A *
0N/A * @param g Graphics to paint to
0N/A * @param b Button to render for
0N/A * @param iconRect space to render in
0N/A * @throws NullPointerException if any of the arguments are null.
0N/A * @since 1.5
0N/A */
0N/A protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
0N/A super.paintIcon(g, b, iconRect);
0N/A }
0N/A}