1 7 8 package javax.swing.plaf.basic; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import javax.swing.*; 13 import javax.swing.border.*; 14 import javax.swing.plaf.*; 15 import javax.swing.text.View ; 16 import com.sun.java.swing.SwingUtilities2; 17 18 19 25 public class BasicRadioButtonUI extends BasicToggleButtonUI 26 { 27 private final static BasicRadioButtonUI radioButtonUI = new BasicRadioButtonUI (); 28 29 protected Icon icon; 30 31 private boolean defaults_initialized = false; 32 33 private final static String propertyPrefix = "RadioButton" + "."; 34 35 public static ComponentUI createUI(JComponent b) { 39 return radioButtonUI; 40 } 41 42 protected String getPropertyPrefix() { 43 return propertyPrefix; 44 } 45 46 protected void installDefaults(AbstractButton b){ 50 super.installDefaults(b); 51 if(!defaults_initialized) { 52 icon = UIManager.getIcon(getPropertyPrefix() + "icon"); 53 defaults_initialized = true; 54 } 55 } 56 57 protected void uninstallDefaults(AbstractButton b){ 61 super.uninstallDefaults(b); 62 defaults_initialized = false; 63 } 64 65 public Icon getDefaultIcon() { 66 return icon; 67 } 68 69 70 76 private static Dimension size = new Dimension(); 77 private static Rectangle viewRect = new Rectangle(); 78 private static Rectangle iconRect = new Rectangle(); 79 private static Rectangle textRect = new Rectangle(); 80 81 84 public synchronized void paint(Graphics g, JComponent c) { 85 AbstractButton b = (AbstractButton) c; 86 ButtonModel model = b.getModel(); 87 88 Font f = c.getFont(); 89 g.setFont(f); 90 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); 91 92 Insets i = c.getInsets(); 93 size = b.getSize(size); 94 viewRect.x = i.left; 95 viewRect.y = i.top; 96 viewRect.width = size.width - (i.right + viewRect.x); 97 viewRect.height = size.height - (i.bottom + viewRect.y); 98 iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; 99 textRect.x = textRect.y = textRect.width = textRect.height = 0; 100 101 Icon altIcon = b.getIcon(); 102 Icon selectedIcon = null; 103 Icon disabledIcon = null; 104 105 String text = SwingUtilities.layoutCompoundLabel( 106 c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(), 107 b.getVerticalAlignment(), b.getHorizontalAlignment(), 108 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 109 viewRect, iconRect, textRect, 110 b.getText() == null ? 0 : b.getIconTextGap()); 111 112 if(c.isOpaque()) { 114 g.setColor(b.getBackground()); 115 g.fillRect(0,0, size.width, size.height); 116 } 117 118 119 if(altIcon != null) { 121 122 if(!model.isEnabled()) { 123 if(model.isSelected()) { 124 altIcon = b.getDisabledSelectedIcon(); 125 } else { 126 altIcon = b.getDisabledIcon(); 127 } 128 } else if(model.isPressed() && model.isArmed()) { 129 altIcon = b.getPressedIcon(); 130 if(altIcon == null) { 131 altIcon = b.getSelectedIcon(); 133 } 134 } else if(model.isSelected()) { 135 if(b.isRolloverEnabled() && model.isRollover()) { 136 altIcon = (Icon) b.getRolloverSelectedIcon(); 137 if (altIcon == null) { 138 altIcon = (Icon) b.getSelectedIcon(); 139 } 140 } else { 141 altIcon = (Icon) b.getSelectedIcon(); 142 } 143 } else if(b.isRolloverEnabled() && model.isRollover()) { 144 altIcon = (Icon) b.getRolloverIcon(); 145 } 146 147 if(altIcon == null) { 148 altIcon = b.getIcon(); 149 } 150 151 altIcon.paintIcon(c, g, iconRect.x, iconRect.y); 152 153 } else { 154 getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y); 155 } 156 157 158 if(text != null) { 160 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 161 if (v != null) { 162 v.paint(g, textRect); 163 } else { 164 paintText(g, b, textRect, text); 165 } 166 if(b.hasFocus() && b.isFocusPainted() && 167 textRect.width > 0 && textRect.height > 0 ) { 168 paintFocus(g, textRect, size); 169 } 170 } 171 } 172 173 protected void paintFocus(Graphics g, Rectangle textRect, Dimension size){ 174 } 175 176 177 183 private static Rectangle prefViewRect = new Rectangle(); 184 private static Rectangle prefIconRect = new Rectangle(); 185 private static Rectangle prefTextRect = new Rectangle(); 186 private static Insets prefInsets = new Insets(0, 0, 0, 0); 187 188 191 public Dimension getPreferredSize(JComponent c) { 192 if(c.getComponentCount() > 0) { 193 return null; 194 } 195 196 AbstractButton b = (AbstractButton) c; 197 198 String text = b.getText(); 199 200 Icon buttonIcon = (Icon) b.getIcon(); 201 if(buttonIcon == null) { 202 buttonIcon = getDefaultIcon(); 203 } 204 205 Font font = b.getFont(); 206 FontMetrics fm = b.getFontMetrics(font); 207 208 prefViewRect.x = prefViewRect.y = 0; 209 prefViewRect.width = Short.MAX_VALUE; 210 prefViewRect.height = Short.MAX_VALUE; 211 prefIconRect.x = prefIconRect.y = prefIconRect.width = prefIconRect.height = 0; 212 prefTextRect.x = prefTextRect.y = prefTextRect.width = prefTextRect.height = 0; 213 214 SwingUtilities.layoutCompoundLabel( 215 c, fm, text, buttonIcon, 216 b.getVerticalAlignment(), b.getHorizontalAlignment(), 217 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 218 prefViewRect, prefIconRect, prefTextRect, 219 text == null ? 0 : b.getIconTextGap()); 220 221 int x1 = Math.min(prefIconRect.x, prefTextRect.x); 223 int x2 = Math.max(prefIconRect.x + prefIconRect.width, 224 prefTextRect.x + prefTextRect.width); 225 int y1 = Math.min(prefIconRect.y, prefTextRect.y); 226 int y2 = Math.max(prefIconRect.y + prefIconRect.height, 227 prefTextRect.y + prefTextRect.height); 228 int width = x2 - x1; 229 int height = y2 - y1; 230 231 prefInsets = b.getInsets(prefInsets); 232 width += prefInsets.left + prefInsets.right; 233 height += prefInsets.top + prefInsets.bottom; 234 return new Dimension(width, height); 235 } 236 } 237 | Popular Tags |