author | darcy |
Wed, 02 Jul 2014 23:03:27 -0700 | |
changeset 25565 | ce603b34c98d |
parent 23715 | 54ae9dd9df73 |
child 25761 | c408b10ef757 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf.basic; |
|
27 |
||
4225 | 28 |
import sun.awt.AppContext; |
29 |
||
2 | 30 |
import java.awt.*; |
31 |
import java.awt.event.*; |
|
32 |
||
33 |
import javax.swing.*; |
|
34 |
import javax.swing.border.*; |
|
35 |
import javax.swing.plaf.*; |
|
36 |
import javax.swing.text.View; |
|
37 |
||
38 |
||
39 |
||
40 |
/** |
|
41 |
* BasicToggleButton implementation |
|
42 |
* |
|
43 |
* @author Jeff Dinkins |
|
44 |
*/ |
|
45 |
public class BasicToggleButtonUI extends BasicButtonUI { |
|
46 |
||
4225 | 47 |
private static final Object BASIC_TOGGLE_BUTTON_UI_KEY = new Object(); |
2 | 48 |
|
49 |
private final static String propertyPrefix = "ToggleButton" + "."; |
|
50 |
||
51 |
// ******************************** |
|
52 |
// Create PLAF |
|
53 |
// ******************************** |
|
54 |
public static ComponentUI createUI(JComponent b) { |
|
4225 | 55 |
AppContext appContext = AppContext.getAppContext(); |
56 |
BasicToggleButtonUI toggleButtonUI = |
|
57 |
(BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY); |
|
58 |
if (toggleButtonUI == null) { |
|
59 |
toggleButtonUI = new BasicToggleButtonUI(); |
|
60 |
appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI); |
|
61 |
} |
|
2 | 62 |
return toggleButtonUI; |
63 |
} |
|
64 |
||
65 |
protected String getPropertyPrefix() { |
|
66 |
return propertyPrefix; |
|
67 |
} |
|
68 |
||
69 |
||
70 |
// ******************************** |
|
71 |
// Paint Methods |
|
72 |
// ******************************** |
|
73 |
public void paint(Graphics g, JComponent c) { |
|
74 |
AbstractButton b = (AbstractButton) c; |
|
75 |
ButtonModel model = b.getModel(); |
|
76 |
||
77 |
Dimension size = b.getSize(); |
|
78 |
FontMetrics fm = g.getFontMetrics(); |
|
79 |
||
80 |
Insets i = c.getInsets(); |
|
81 |
||
82 |
Rectangle viewRect = new Rectangle(size); |
|
83 |
||
84 |
viewRect.x += i.left; |
|
85 |
viewRect.y += i.top; |
|
86 |
viewRect.width -= (i.right + viewRect.x); |
|
87 |
viewRect.height -= (i.bottom + viewRect.y); |
|
88 |
||
89 |
Rectangle iconRect = new Rectangle(); |
|
90 |
Rectangle textRect = new Rectangle(); |
|
91 |
||
92 |
Font f = c.getFont(); |
|
93 |
g.setFont(f); |
|
94 |
||
95 |
// layout the text and icon |
|
96 |
String text = SwingUtilities.layoutCompoundLabel( |
|
97 |
c, fm, b.getText(), b.getIcon(), |
|
98 |
b.getVerticalAlignment(), b.getHorizontalAlignment(), |
|
99 |
b.getVerticalTextPosition(), b.getHorizontalTextPosition(), |
|
100 |
viewRect, iconRect, textRect, |
|
101 |
b.getText() == null ? 0 : b.getIconTextGap()); |
|
102 |
||
103 |
g.setColor(b.getBackground()); |
|
104 |
||
105 |
if (model.isArmed() && model.isPressed() || model.isSelected()) { |
|
106 |
paintButtonPressed(g,b); |
|
107 |
} |
|
108 |
||
109 |
// Paint the Icon |
|
110 |
if(b.getIcon() != null) { |
|
111 |
paintIcon(g, b, iconRect); |
|
112 |
} |
|
113 |
||
114 |
// Draw the Text |
|
115 |
if(text != null && !text.equals("")) { |
|
116 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
|
117 |
if (v != null) { |
|
118 |
v.paint(g, textRect); |
|
119 |
} else { |
|
120 |
paintText(g, b, textRect, text); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
// draw the dashed focus line. |
|
125 |
if (b.isFocusPainted() && b.hasFocus()) { |
|
126 |
paintFocus(g, b, viewRect, textRect, iconRect); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) { |
|
131 |
ButtonModel model = b.getModel(); |
|
132 |
Icon icon = null; |
|
133 |
||
134 |
if(!model.isEnabled()) { |
|
135 |
if(model.isSelected()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
136 |
icon = b.getDisabledSelectedIcon(); |
2 | 137 |
} else { |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
138 |
icon = b.getDisabledIcon(); |
2 | 139 |
} |
140 |
} else if(model.isPressed() && model.isArmed()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
141 |
icon = b.getPressedIcon(); |
2 | 142 |
if(icon == null) { |
143 |
// Use selected icon |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
144 |
icon = b.getSelectedIcon(); |
2 | 145 |
} |
146 |
} else if(model.isSelected()) { |
|
147 |
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.getRolloverSelectedIcon(); |
2 | 149 |
if (icon == null) { |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
150 |
icon = b.getSelectedIcon(); |
2 | 151 |
} |
152 |
} else { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
153 |
icon = b.getSelectedIcon(); |
2 | 154 |
} |
155 |
} else if(b.isRolloverEnabled() && model.isRollover()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
156 |
icon = b.getRolloverIcon(); |
2 | 157 |
} |
158 |
||
159 |
if(icon == null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
160 |
icon = b.getIcon(); |
2 | 161 |
} |
162 |
||
163 |
icon.paintIcon(b, g, iconRect.x, iconRect.y); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Overriden so that the text will not be rendered as shifted for |
|
168 |
* Toggle buttons and subclasses. |
|
169 |
*/ |
|
170 |
protected int getTextShiftOffset() { |
|
171 |
return 0; |
|
172 |
} |
|
173 |
||
174 |
} |