jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java
author xdono
Mon, 15 Dec 2008 16:55:25 -0800
changeset 1639 a97859015238
parent 1290 da8902cd496c
child 4225 57ec8171fcd5
permissions -rw-r--r--
6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1290
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * BasicToggleButton implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class BasicToggleButtonUI extends BasicButtonUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final static String propertyPrefix = "ToggleButton" + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    //          Create PLAF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static ComponentUI createUI(JComponent b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return toggleButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return propertyPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    //          Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        AbstractButton b = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Dimension size = b.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Insets i = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Rectangle viewRect = new Rectangle(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        viewRect.x += i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        viewRect.y += i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        viewRect.width -= (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        viewRect.height -= (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Rectangle textRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Font f = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        g.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // layout the text and icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String text = SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            c, fm, b.getText(), b.getIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            viewRect, iconRect, textRect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            b.getText() == null ? 0 : b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        g.setColor(b.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (model.isArmed() && model.isPressed() || model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            paintButtonPressed(g,b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Paint the Icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if(b.getIcon() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            paintIcon(g, b, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Draw the Text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if(text != null && !text.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
               v.paint(g, textRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
               paintText(g, b, textRect, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // draw the dashed focus line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (b.isFocusPainted() && b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            paintFocus(g, b, viewRect, textRect, iconRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Icon icon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if(!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if(model.isSelected()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   128
               icon = b.getDisabledSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   130
               icon = b.getDisabledIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else if(model.isPressed() && model.isArmed()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   133
            icon = b.getPressedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if(icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                // Use selected icon
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   136
                icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else if(model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   140
                icon = b.getRolloverSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (icon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   142
                    icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   145
                icon = b.getSelectedIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else if(b.isRolloverEnabled() && model.isRollover()) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   148
            icon = b.getRolloverIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if(icon == null) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   152
            icon = b.getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        icon.paintIcon(b, g, iconRect.x, iconRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Overriden so that the text will not be rendered as shifted for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Toggle buttons and subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected int getTextShiftOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}