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