jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java
changeset 2 90ce3da70b43
child 1290 da8902cd496c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java	Sat Dec 01 00:00:00 2007 +0000
@@ -0,0 +1,166 @@
+/*
+ * Copyright 1997-2003 Sun Microsystems, Inc.  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.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package javax.swing.plaf.basic;
+
+import java.awt.*;
+import java.awt.event.*;
+
+import javax.swing.*;
+import javax.swing.border.*;
+import javax.swing.plaf.*;
+import javax.swing.text.View;
+
+
+
+/**
+ * BasicToggleButton implementation
+ * <p>
+ *
+ * @author Jeff Dinkins
+ */
+public class BasicToggleButtonUI extends BasicButtonUI {
+
+    private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI();
+
+    private final static String propertyPrefix = "ToggleButton" + ".";
+
+    // ********************************
+    //          Create PLAF
+    // ********************************
+    public static ComponentUI createUI(JComponent b) {
+        return toggleButtonUI;
+    }
+
+    protected String getPropertyPrefix() {
+        return propertyPrefix;
+    }
+
+
+    // ********************************
+    //          Paint Methods
+    // ********************************
+    public void paint(Graphics g, JComponent c) {
+        AbstractButton b = (AbstractButton) c;
+        ButtonModel model = b.getModel();
+
+        Dimension size = b.getSize();
+        FontMetrics fm = g.getFontMetrics();
+
+        Insets i = c.getInsets();
+
+        Rectangle viewRect = new Rectangle(size);
+
+        viewRect.x += i.left;
+        viewRect.y += i.top;
+        viewRect.width -= (i.right + viewRect.x);
+        viewRect.height -= (i.bottom + viewRect.y);
+
+        Rectangle iconRect = new Rectangle();
+        Rectangle textRect = new Rectangle();
+
+        Font f = c.getFont();
+        g.setFont(f);
+
+        // layout the text and icon
+        String text = SwingUtilities.layoutCompoundLabel(
+            c, fm, b.getText(), b.getIcon(),
+            b.getVerticalAlignment(), b.getHorizontalAlignment(),
+            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
+            viewRect, iconRect, textRect,
+            b.getText() == null ? 0 : b.getIconTextGap());
+
+        g.setColor(b.getBackground());
+
+        if (model.isArmed() && model.isPressed() || model.isSelected()) {
+            paintButtonPressed(g,b);
+        }
+
+        // Paint the Icon
+        if(b.getIcon() != null) {
+            paintIcon(g, b, iconRect);
+        }
+
+        // Draw the Text
+        if(text != null && !text.equals("")) {
+            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
+            if (v != null) {
+               v.paint(g, textRect);
+            } else {
+               paintText(g, b, textRect, text);
+            }
+        }
+
+        // draw the dashed focus line.
+        if (b.isFocusPainted() && b.hasFocus()) {
+            paintFocus(g, b, viewRect, textRect, iconRect);
+        }
+    }
+
+    protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
+        ButtonModel model = b.getModel();
+        Icon icon = null;
+
+        if(!model.isEnabled()) {
+            if(model.isSelected()) {
+               icon = (Icon) b.getDisabledSelectedIcon();
+            } else {
+               icon = (Icon) b.getDisabledIcon();
+            }
+        } else if(model.isPressed() && model.isArmed()) {
+            icon = (Icon) b.getPressedIcon();
+            if(icon == null) {
+                // Use selected icon
+                icon = (Icon) b.getSelectedIcon();
+            }
+        } else if(model.isSelected()) {
+            if(b.isRolloverEnabled() && model.isRollover()) {
+                icon = (Icon) b.getRolloverSelectedIcon();
+                if (icon == null) {
+                    icon = (Icon) b.getSelectedIcon();
+                }
+            } else {
+                icon = (Icon) b.getSelectedIcon();
+            }
+        } else if(b.isRolloverEnabled() && model.isRollover()) {
+            icon = (Icon) b.getRolloverIcon();
+        }
+
+        if(icon == null) {
+            icon = (Icon) b.getIcon();
+        }
+
+        icon.paintIcon(b, g, iconRect.x, iconRect.y);
+    }
+
+    /**
+     * Overriden so that the text will not be rendered as shifted for
+     * Toggle buttons and subclasses.
+     */
+    protected int getTextShiftOffset() {
+        return 0;
+    }
+
+}