38 |
|
|
39 |
package javax.swing.plaf.basic; |
package javax.swing.plaf.basic; |
40 |
|
|
41 |
|
import java.awt.Font; |
42 |
|
import java.awt.Graphics; |
43 |
|
import java.awt.Rectangle; |
44 |
|
|
45 |
import javax.swing.AbstractButton; |
import javax.swing.AbstractButton; |
46 |
import javax.swing.Icon; |
import javax.swing.Icon; |
47 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
48 |
|
import javax.swing.SwingUtilities; |
49 |
import javax.swing.UIDefaults; |
import javax.swing.UIDefaults; |
50 |
import javax.swing.UIManager; |
import javax.swing.UIManager; |
51 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
52 |
|
|
53 |
|
/** |
54 |
|
* The BasicLookAndFeel UI implementation for |
55 |
|
* {@link javax.swing.JRadioButtons}. |
56 |
|
*/ |
57 |
public class BasicRadioButtonUI extends BasicToggleButtonUI |
public class BasicRadioButtonUI extends BasicToggleButtonUI |
58 |
{ |
{ |
59 |
|
/** |
60 |
|
* The default icon for JRadioButtons. The default icon displays the usual |
61 |
|
* RadioButton and is sensible to the selection state of the button, |
62 |
|
* and can be used both as normal icon as well as selectedIcon. |
63 |
|
*/ |
64 |
protected Icon icon; |
protected Icon icon; |
65 |
|
|
66 |
|
/** |
67 |
|
* Creates and returns a new instance of <code>BasicRadioButtonUI</code>. |
68 |
|
* |
69 |
|
* @return a new instance of <code>BasicRadioButtonUI</code> |
70 |
|
*/ |
71 |
public static ComponentUI createUI(final JComponent c) { |
public static ComponentUI createUI(final JComponent c) { |
72 |
return new BasicRadioButtonUI(); |
return new BasicRadioButtonUI(); |
73 |
} |
} |
74 |
|
|
75 |
|
/** |
76 |
|
* Creates a new instance of <code>BasicButtonUI</code>. |
77 |
|
*/ |
78 |
public BasicRadioButtonUI() |
public BasicRadioButtonUI() |
79 |
{ |
{ |
80 |
icon = getDefaultIcon(); |
icon = getDefaultIcon(); |
81 |
} |
} |
82 |
|
|
83 |
public void installUI(final JComponent c) { |
/** |
84 |
super.installUI(c); |
* Installs defaults from the Look & Feel table on the specified |
85 |
if (c instanceof AbstractButton) |
* button. |
86 |
{ |
* |
87 |
AbstractButton b = (AbstractButton) c; |
* @param b the button on which to install the defaults |
88 |
b.setIcon(icon); |
*/ |
89 |
} |
protected void installDefaults(AbstractButton b) |
90 |
|
{ |
91 |
|
super.installDefaults(b); |
92 |
|
if (b.getIcon() == null) |
93 |
|
b.setIcon(icon); |
94 |
|
if (b.getSelectedIcon() == null) |
95 |
|
b.setSelectedIcon(icon); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
|
* Returns the prefix used for UIDefaults properties. This is |
100 |
|
* <code>RadioButton</code> in this case. |
101 |
|
* |
102 |
|
* @return the prefix used for UIDefaults properties |
103 |
|
*/ |
104 |
|
protected String getPropertyPrefix() |
105 |
|
{ |
106 |
|
return "RadioButton"; |
107 |
} |
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Returns the default icon for JRadioButtons. |
111 |
|
* The default icon displays the usual |
112 |
|
* RadioButton and is sensible to the selection state of the button, |
113 |
|
* and can be used both as normal icon as well as selectedIcon. |
114 |
|
* |
115 |
|
* @return the default icon for JRadioButtons |
116 |
|
*/ |
117 |
public Icon getDefaultIcon() |
public Icon getDefaultIcon() |
118 |
{ |
{ |
119 |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
120 |
return defaults.getIcon("RadioButton.icon"); |
return defaults.getIcon(getPropertyPrefix() + ".icon"); |
121 |
} |
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
|
123 |
|
/** |
124 |
|
* Paints the RadioButton. |
125 |
|
* |
126 |
|
* @param g the Graphics context to paint with |
127 |
|
* @param c the button to paint |
128 |
|
*/ |
129 |
|
public void paint(Graphics g, JComponent c) |
130 |
|
{ |
131 |
|
AbstractButton b = (AbstractButton) c; |
132 |
|
|
133 |
|
Rectangle tr = new Rectangle(); |
134 |
|
Rectangle ir = new Rectangle(); |
135 |
|
Rectangle vr = new Rectangle(); |
136 |
|
|
137 |
|
Font f = c.getFont(); |
138 |
|
|
139 |
|
g.setFont(f); |
140 |
|
|
141 |
|
Icon currentIcon = null; |
142 |
|
if (b.isSelected()) |
143 |
|
currentIcon = b.getSelectedIcon(); |
144 |
|
else |
145 |
|
currentIcon = b.getIcon(); |
146 |
|
|
147 |
|
SwingUtilities.calculateInnerArea(b, vr); |
148 |
|
String text = SwingUtilities.layoutCompoundLabel |
149 |
|
(c, g.getFontMetrics(f), b.getText(), currentIcon, |
150 |
|
b.getVerticalAlignment(), b.getHorizontalAlignment(), |
151 |
|
b.getVerticalTextPosition(), b.getHorizontalTextPosition(), |
152 |
|
vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset); |
153 |
|
|
154 |
|
if (currentIcon != null) |
155 |
|
{ |
156 |
|
currentIcon.paintIcon(c, g, ir.x, ir.y); |
157 |
|
} |
158 |
|
if (text != null) |
159 |
|
paintText(g, b, tr, text); |
160 |
|
paintFocus(g, b, vr, tr, ir); |
161 |
|
} |
162 |
|
} |