1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
61: public class BasicRadioButtonUI extends BasicToggleButtonUI
62: {
63:
68: protected Icon icon;
69:
70:
75: public static ComponentUI createUI(final JComponent c)
76: {
77: return new BasicRadioButtonUI();
78: }
79:
80:
83: public BasicRadioButtonUI()
84: {
85:
86: }
87:
88:
94: protected void installDefaults(AbstractButton b)
95: {
96: super.installDefaults(b);
97: icon = UIManager.getIcon(getPropertyPrefix() + "icon");
98: }
99:
100:
106: protected String getPropertyPrefix()
107: {
108: return "RadioButton.";
109: }
110:
111:
119: public Icon getDefaultIcon()
120: {
121: return icon;
122: }
123:
124:
130: public void paint(Graphics g, JComponent c)
131: {
132: AbstractButton b = (AbstractButton) c;
133: Dimension size = c.getSize();
134: Insets i = b.getInsets();
135: textR.x = 0;
136: textR.y = 0;
137: textR.width = 0;
138: textR.height = 0;
139: iconR.x = 0;
140: iconR.y = 0;
141: iconR.width = 0;
142: iconR.height = 0;
143: viewR.x = i.left;
144: viewR.y = i.right;
145: viewR.width = size.width - i.left - i.right;
146: viewR.height = size.height - i.top - i.bottom;
147:
148: Font f = c.getFont();
149:
150: g.setFont(f);
151:
152:
153: Icon icon = b.getIcon();
154: if (icon == null)
155: icon = getDefaultIcon();
156:
157:
158: Icon currentIcon = getCurrentIcon(b);
159:
160:
161: String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
162: b.getText(), currentIcon == null ? getDefaultIcon() : currentIcon,
163: b.getVerticalAlignment(), b.getHorizontalAlignment(),
164: b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
165: viewR, iconR, textR, b.getIconTextGap());
166:
167:
168: if (currentIcon != null)
169: currentIcon.paintIcon(c, g, iconR.x, iconR.y);
170:
171:
172: if (text != null)
173: {
174:
175: View v = (View) c.getClientProperty(BasicHTML.propertyKey);
176: if (v != null)
177: v.paint(g, textR);
178: else
179: paintText(g, b, textR, text);
180:
181:
182: if (b.hasFocus() && b.isFocusPainted()
183: && textR.width > 0 && textR.height > 0)
184: paintFocus(g, textR, size);
185: }
186: }
187:
188:
195: private Icon getCurrentIcon(AbstractButton b)
196: {
197: ButtonModel m = b.getModel();
198: Icon currentIcon = b.getIcon();
199:
200: if (currentIcon == null)
201: {
202: currentIcon = getDefaultIcon();
203: }
204: else
205: {
206: if (! m.isEnabled())
207: {
208: if (m.isSelected())
209: currentIcon = b.getDisabledSelectedIcon();
210: else
211: currentIcon = b.getDisabledIcon();
212: }
213: else if (m.isPressed() && m.isArmed())
214: {
215: currentIcon = b.getPressedIcon();
216: if (currentIcon == null)
217: currentIcon = b.getSelectedIcon();
218: }
219: else if (m.isSelected())
220: {
221: if (b.isRolloverEnabled() && m.isRollover())
222: {
223: currentIcon = b.getRolloverSelectedIcon();
224: if (currentIcon == null)
225: currentIcon = b.getSelectedIcon();
226: }
227: else
228: currentIcon = b.getSelectedIcon();
229: }
230: else if (b.isRolloverEnabled() && m.isRollover())
231: {
232: currentIcon = b.getRolloverIcon();
233: }
234: if (currentIcon == null)
235: currentIcon = b.getIcon();
236: }
237: return currentIcon;
238: }
239:
240: public Dimension getPreferredSize(JComponent c)
241: {
242:
243:
244:
245:
246:
247:
248: AbstractButton b = (AbstractButton) c;
249:
250: Insets insets = b.getInsets();
251:
252: String text = b.getText();
253: Icon i = b.getIcon();
254: if (i == null)
255: i = getDefaultIcon();
256:
257: textR.x = 0;
258: textR.y = 0;
259: textR.width = 0;
260: textR.height = 0;
261: iconR.x = 0;
262: iconR.y = 0;
263: iconR.width = 0;
264: iconR.height = 0;
265: viewR.x = 0;
266: viewR.y = 0;
267: viewR.width = Short.MAX_VALUE;
268: viewR.height = Short.MAX_VALUE;
269:
270: SwingUtilities.layoutCompoundLabel(b,
271: b.getFontMetrics(b.getFont()),
272: text, i, b.getVerticalAlignment(),
273: b.getHorizontalAlignment(),
274: b.getVerticalTextPosition(),
275: b.getHorizontalTextPosition(),
276: viewR, iconR, textR,
277: text == null ? 0 : b.getIconTextGap());
278:
279: Rectangle r = SwingUtilities.computeUnion(textR.x, textR.y, textR.width,
280: textR.height, iconR);
281:
282: return new Dimension(insets.left + r.width + insets.right,
283: insets.top + r.height + insets.bottom);
284: }
285:
286:
293: protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
294: {
295: Color focusColor = UIManager.getColor(getPropertyPrefix() + ".focus");
296: Color saved = g.getColor();
297: g.setColor(focusColor);
298: g.drawRect(tr.x, tr.y, tr.width, tr.height);
299: g.setColor(saved);
300: }
301: }