1 7 8 package javax.swing.plaf.metal; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import java.awt.*; 12 import java.awt.event.*; 13 import javax.swing.*; 14 import javax.swing.plaf.basic.*; 15 import javax.swing.border.*; 16 import javax.swing.plaf.*; 17 import java.io.Serializable ; 18 import javax.swing.text.View ; 19 20 21 37 public class MetalRadioButtonUI extends BasicRadioButtonUI { 38 39 private static final MetalRadioButtonUI metalRadioButtonUI = new MetalRadioButtonUI (); 40 41 protected Color focusColor; 42 protected Color selectColor; 43 protected Color disabledTextColor; 44 45 private boolean defaults_initialized = false; 46 47 public static ComponentUI createUI(JComponent c) { 51 return metalRadioButtonUI; 52 } 53 54 public void installDefaults(AbstractButton b) { 58 super.installDefaults(b); 59 if(!defaults_initialized) { 60 focusColor = UIManager.getColor(getPropertyPrefix() + "focus"); 61 selectColor = UIManager.getColor(getPropertyPrefix() + "select"); 62 disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText"); 63 defaults_initialized = true; 64 } 65 LookAndFeel.installProperty(b, "opaque", Boolean.TRUE); 66 } 67 68 protected void uninstallDefaults(AbstractButton b) { 69 super.uninstallDefaults(b); 70 defaults_initialized = false; 71 } 72 73 protected Color getSelectColor() { 77 return selectColor; 78 } 79 80 protected Color getDisabledTextColor() { 81 return disabledTextColor; 82 } 83 84 protected Color getFocusColor() { 85 return focusColor; 86 } 87 88 89 public synchronized void paint(Graphics g, JComponent c) { 93 94 AbstractButton b = (AbstractButton) c; 95 ButtonModel model = b.getModel(); 96 97 Dimension size = c.getSize(); 98 99 int w = size.width; 100 int h = size.height; 101 102 Font f = c.getFont(); 103 g.setFont(f); 104 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); 105 106 Rectangle viewRect = new Rectangle(size); 107 Rectangle iconRect = new Rectangle(); 108 Rectangle textRect = new Rectangle(); 109 110 Insets i = c.getInsets(); 111 viewRect.x += i.left; 112 viewRect.y += i.top; 113 viewRect.width -= (i.right + viewRect.x); 114 viewRect.height -= (i.bottom + viewRect.y); 115 116 Icon altIcon = b.getIcon(); 117 Icon selectedIcon = null; 118 Icon disabledIcon = null; 119 120 String text = SwingUtilities.layoutCompoundLabel( 121 c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(), 122 b.getVerticalAlignment(), b.getHorizontalAlignment(), 123 b.getVerticalTextPosition(), b.getHorizontalTextPosition(), 124 viewRect, iconRect, textRect, b.getIconTextGap()); 125 126 if(c.isOpaque()) { 128 g.setColor(b.getBackground()); 129 g.fillRect(0,0, size.width, size.height); 130 } 131 132 133 if(altIcon != null) { 135 136 if(!model.isEnabled()) { 137 if(model.isSelected()) { 138 altIcon = b.getDisabledSelectedIcon(); 139 } else { 140 altIcon = b.getDisabledIcon(); 141 } 142 } else if(model.isPressed() && model.isArmed()) { 143 altIcon = b.getPressedIcon(); 144 if(altIcon == null) { 145 altIcon = b.getSelectedIcon(); 147 } 148 } else if(model.isSelected()) { 149 if(b.isRolloverEnabled() && model.isRollover()) { 150 altIcon = (Icon) b.getRolloverSelectedIcon(); 151 if (altIcon == null) { 152 altIcon = (Icon) b.getSelectedIcon(); 153 } 154 } else { 155 altIcon = (Icon) b.getSelectedIcon(); 156 } 157 } else if(b.isRolloverEnabled() && model.isRollover()) { 158 altIcon = (Icon) b.getRolloverIcon(); 159 } 160 161 if(altIcon == null) { 162 altIcon = b.getIcon(); 163 } 164 165 altIcon.paintIcon(c, g, iconRect.x, iconRect.y); 166 167 } else { 168 getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y); 169 } 170 171 172 if(text != null) { 174 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 175 if (v != null) { 176 v.paint(g, textRect); 177 } else { 178 int mnemIndex = b.getDisplayedMnemonicIndex(); 179 if(model.isEnabled()) { 180 g.setColor(b.getForeground()); 182 } else { 183 g.setColor(getDisabledTextColor()); 185 } 186 SwingUtilities2.drawStringUnderlineCharAt(c,g,text, 187 mnemIndex, textRect.x, textRect.y + fm.getAscent()); 188 } 189 if(b.hasFocus() && b.isFocusPainted() && 190 textRect.width > 0 && textRect.height > 0 ) { 191 paintFocus(g,textRect,size); 192 } 193 } 194 } 195 196 protected void paintFocus(Graphics g, Rectangle t, Dimension d){ 197 g.setColor(getFocusColor()); 198 g.drawRect(t.x-1, t.y-1, t.width+1, t.height+1); 199 } 200 } 201 | Popular Tags |