author | rupashka |
Fri, 25 Jul 2008 17:50:36 +0400 | |
changeset 1290 | da8902cd496c |
parent 2 | 90ce3da70b43 |
child 1639 | a97859015238 |
permissions | -rw-r--r-- |
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()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
128 |
icon = b.getDisabledSelectedIcon(); |
2 | 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 | 131 |
} |
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 | 134 |
if(icon == null) { |
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 | 137 |
} |
138 |
} else if(model.isSelected()) { |
|
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 | 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 | 143 |
} |
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 | 146 |
} |
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 | 149 |
} |
150 |
||
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 | 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 |
} |