001 /*
002 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.basic;
027
028 import java.awt.*;
029 import java.awt.event.*;
030
031 import javax.swing.*;
032 import javax.swing.border.*;
033 import javax.swing.plaf.*;
034 import javax.swing.text.View;
035
036 /**
037 * BasicToggleButton implementation
038 * <p>
039 *
040 * @version 1.65 05/05/07
041 * @author Jeff Dinkins
042 */
043 public class BasicToggleButtonUI extends BasicButtonUI {
044
045 private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI();
046
047 private final static String propertyPrefix = "ToggleButton" + ".";
048
049 // ********************************
050 // Create PLAF
051 // ********************************
052 public static ComponentUI createUI(JComponent b) {
053 return toggleButtonUI;
054 }
055
056 protected String getPropertyPrefix() {
057 return propertyPrefix;
058 }
059
060 // ********************************
061 // Paint Methods
062 // ********************************
063 public void paint(Graphics g, JComponent c) {
064 AbstractButton b = (AbstractButton) c;
065 ButtonModel model = b.getModel();
066
067 Dimension size = b.getSize();
068 FontMetrics fm = g.getFontMetrics();
069
070 Insets i = c.getInsets();
071
072 Rectangle viewRect = new Rectangle(size);
073
074 viewRect.x += i.left;
075 viewRect.y += i.top;
076 viewRect.width -= (i.right + viewRect.x);
077 viewRect.height -= (i.bottom + viewRect.y);
078
079 Rectangle iconRect = new Rectangle();
080 Rectangle textRect = new Rectangle();
081
082 Font f = c.getFont();
083 g.setFont(f);
084
085 // layout the text and icon
086 String text = SwingUtilities.layoutCompoundLabel(c, fm, b
087 .getText(), b.getIcon(), b.getVerticalAlignment(), b
088 .getHorizontalAlignment(), b.getVerticalTextPosition(),
089 b.getHorizontalTextPosition(), viewRect, iconRect,
090 textRect, b.getText() == null ? 0 : b.getIconTextGap());
091
092 g.setColor(b.getBackground());
093
094 if (model.isArmed() && model.isPressed() || model.isSelected()) {
095 paintButtonPressed(g, b);
096 }
097
098 // Paint the Icon
099 if (b.getIcon() != null) {
100 paintIcon(g, b, iconRect);
101 }
102
103 // Draw the Text
104 if (text != null && !text.equals("")) {
105 View v = (View) c.getClientProperty(BasicHTML.propertyKey);
106 if (v != null) {
107 v.paint(g, textRect);
108 } else {
109 paintText(g, b, textRect, text);
110 }
111 }
112
113 // draw the dashed focus line.
114 if (b.isFocusPainted() && b.hasFocus()) {
115 paintFocus(g, b, viewRect, textRect, iconRect);
116 }
117 }
118
119 protected void paintIcon(Graphics g, AbstractButton b,
120 Rectangle iconRect) {
121 ButtonModel model = b.getModel();
122 Icon icon = null;
123
124 if (!model.isEnabled()) {
125 if (model.isSelected()) {
126 icon = (Icon) b.getDisabledSelectedIcon();
127 } else {
128 icon = (Icon) b.getDisabledIcon();
129 }
130 } else if (model.isPressed() && model.isArmed()) {
131 icon = (Icon) b.getPressedIcon();
132 if (icon == null) {
133 // Use selected icon
134 icon = (Icon) b.getSelectedIcon();
135 }
136 } else if (model.isSelected()) {
137 if (b.isRolloverEnabled() && model.isRollover()) {
138 icon = (Icon) b.getRolloverSelectedIcon();
139 if (icon == null) {
140 icon = (Icon) b.getSelectedIcon();
141 }
142 } else {
143 icon = (Icon) b.getSelectedIcon();
144 }
145 } else if (b.isRolloverEnabled() && model.isRollover()) {
146 icon = (Icon) b.getRolloverIcon();
147 }
148
149 if (icon == null) {
150 icon = (Icon) b.getIcon();
151 }
152
153 icon.paintIcon(b, g, iconRect.x, iconRect.y);
154 }
155
156 /**
157 * Overriden so that the text will not be rendered as shifted for
158 * Toggle buttons and subclasses.
159 */
160 protected int getTextShiftOffset() {
161 return 0;
162 }
163
164 }
|