author | weijun |
Thu, 11 Dec 2014 15:19:12 +0800 | |
changeset 28228 | be83f404724d |
parent 25859 | 3317bb8137f4 |
child 32865 | f9cb6e427f9e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, 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 |
// ******************************** |
|
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
54 |
|
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
55 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
56 |
* Returns an instance of {@code BasicToggleButtonUI}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
57 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
58 |
* @param b a component |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
59 |
* @return an instance of {@code BasicToggleButtonUI} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
60 |
*/ |
2 | 61 |
public static ComponentUI createUI(JComponent b) { |
4225 | 62 |
AppContext appContext = AppContext.getAppContext(); |
63 |
BasicToggleButtonUI toggleButtonUI = |
|
64 |
(BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY); |
|
65 |
if (toggleButtonUI == null) { |
|
66 |
toggleButtonUI = new BasicToggleButtonUI(); |
|
67 |
appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI); |
|
68 |
} |
|
2 | 69 |
return toggleButtonUI; |
70 |
} |
|
71 |
||
72 |
protected String getPropertyPrefix() { |
|
73 |
return propertyPrefix; |
|
74 |
} |
|
75 |
||
76 |
||
77 |
// ******************************** |
|
78 |
// Paint Methods |
|
79 |
// ******************************** |
|
80 |
public void paint(Graphics g, JComponent c) { |
|
81 |
AbstractButton b = (AbstractButton) c; |
|
82 |
ButtonModel model = b.getModel(); |
|
83 |
||
84 |
Dimension size = b.getSize(); |
|
85 |
FontMetrics fm = g.getFontMetrics(); |
|
86 |
||
87 |
Insets i = c.getInsets(); |
|
88 |
||
89 |
Rectangle viewRect = new Rectangle(size); |
|
90 |
||
91 |
viewRect.x += i.left; |
|
92 |
viewRect.y += i.top; |
|
93 |
viewRect.width -= (i.right + viewRect.x); |
|
94 |
viewRect.height -= (i.bottom + viewRect.y); |
|
95 |
||
96 |
Rectangle iconRect = new Rectangle(); |
|
97 |
Rectangle textRect = new Rectangle(); |
|
98 |
||
99 |
Font f = c.getFont(); |
|
100 |
g.setFont(f); |
|
101 |
||
102 |
// layout the text and icon |
|
103 |
String text = SwingUtilities.layoutCompoundLabel( |
|
104 |
c, fm, b.getText(), b.getIcon(), |
|
105 |
b.getVerticalAlignment(), b.getHorizontalAlignment(), |
|
106 |
b.getVerticalTextPosition(), b.getHorizontalTextPosition(), |
|
107 |
viewRect, iconRect, textRect, |
|
108 |
b.getText() == null ? 0 : b.getIconTextGap()); |
|
109 |
||
110 |
g.setColor(b.getBackground()); |
|
111 |
||
112 |
if (model.isArmed() && model.isPressed() || model.isSelected()) { |
|
113 |
paintButtonPressed(g,b); |
|
114 |
} |
|
115 |
||
116 |
// Paint the Icon |
|
117 |
if(b.getIcon() != null) { |
|
118 |
paintIcon(g, b, iconRect); |
|
119 |
} |
|
120 |
||
121 |
// Draw the Text |
|
122 |
if(text != null && !text.equals("")) { |
|
123 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
|
124 |
if (v != null) { |
|
125 |
v.paint(g, textRect); |
|
126 |
} else { |
|
127 |
paintText(g, b, textRect, text); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
// draw the dashed focus line. |
|
132 |
if (b.isFocusPainted() && b.hasFocus()) { |
|
133 |
paintFocus(g, b, viewRect, textRect, iconRect); |
|
134 |
} |
|
135 |
} |
|
136 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
137 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
138 |
* Paints an icon in the specified location. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
139 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
140 |
* @param g an instance of {@code Graphics} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
141 |
* @param b an instance of {@code Button} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
142 |
* @param iconRect bounds of an icon |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
23715
diff
changeset
|
143 |
*/ |
2 | 144 |
protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) { |
145 |
ButtonModel model = b.getModel(); |
|
146 |
Icon icon = null; |
|
147 |
||
148 |
if(!model.isEnabled()) { |
|
149 |
if(model.isSelected()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
150 |
icon = b.getDisabledSelectedIcon(); |
2 | 151 |
} else { |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
152 |
icon = b.getDisabledIcon(); |
2 | 153 |
} |
154 |
} else if(model.isPressed() && model.isArmed()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
155 |
icon = b.getPressedIcon(); |
2 | 156 |
if(icon == null) { |
157 |
// Use selected icon |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
158 |
icon = b.getSelectedIcon(); |
2 | 159 |
} |
160 |
} else if(model.isSelected()) { |
|
161 |
if(b.isRolloverEnabled() && model.isRollover()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
162 |
icon = b.getRolloverSelectedIcon(); |
2 | 163 |
if (icon == null) { |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
164 |
icon = b.getSelectedIcon(); |
2 | 165 |
} |
166 |
} else { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
167 |
icon = b.getSelectedIcon(); |
2 | 168 |
} |
169 |
} else if(b.isRolloverEnabled() && model.isRollover()) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
170 |
icon = b.getRolloverIcon(); |
2 | 171 |
} |
172 |
||
173 |
if(icon == null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
174 |
icon = b.getIcon(); |
2 | 175 |
} |
176 |
||
177 |
icon.paintIcon(b, g, iconRect.x, iconRect.y); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Overriden so that the text will not be rendered as shifted for |
|
182 |
* Toggle buttons and subclasses. |
|
183 |
*/ |
|
184 |
protected int getTextShiftOffset() { |
|
185 |
return 0; |
|
186 |
} |
|
187 |
||
188 |
} |