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 java.lang.ref.*; 14 import java.util.*; 15 import javax.swing.plaf.basic.BasicToggleButtonUI ; 16 17 import javax.swing.*; 18 import javax.swing.border.*; 19 import javax.swing.plaf.*; 20 import javax.swing.*; 21 22 import java.io.Serializable ; 23 24 39 public class MetalToggleButtonUI extends BasicToggleButtonUI { 40 41 private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI (); 42 43 protected Color focusColor; 44 protected Color selectColor; 45 protected Color disabledTextColor; 46 47 private boolean defaults_initialized = false; 48 49 public static ComponentUI createUI(JComponent b) { 53 return metalToggleButtonUI; 54 } 55 56 public void installDefaults(AbstractButton b) { 60 super.installDefaults(b); 61 if(!defaults_initialized) { 62 focusColor = UIManager.getColor(getPropertyPrefix() + "focus"); 63 selectColor = UIManager.getColor(getPropertyPrefix() + "select"); 64 disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText"); 65 defaults_initialized = true; 66 } 67 } 68 69 protected void uninstallDefaults(AbstractButton b) { 70 super.uninstallDefaults(b); 71 defaults_initialized = false; 72 } 73 74 protected Color getSelectColor() { 78 return selectColor; 79 } 80 81 protected Color getDisabledTextColor() { 82 return disabledTextColor; 83 } 84 85 protected Color getFocusColor() { 86 return focusColor; 87 } 88 89 90 105 public void update(Graphics g, JComponent c) { 106 AbstractButton button = (AbstractButton)c; 107 if ((c.getBackground() instanceof UIResource) && 108 button.isContentAreaFilled() && c.isEnabled()) { 109 ButtonModel model = button.getModel(); 110 if (!MetalUtils.isToolBarButton(c)) { 111 if (!model.isArmed() && !model.isPressed() && 112 MetalUtils.drawGradient( 113 c, g, "ToggleButton.gradient", 0, 0, c.getWidth(), 114 c.getHeight(), true)) { 115 paint(g, c); 116 return; 117 } 118 } 119 else if ((model.isRollover() || model.isSelected()) && 120 MetalUtils.drawGradient(c, g, "ToggleButton.gradient", 121 0, 0, c.getWidth(), c.getHeight(), true)) { 122 paint(g, c); 123 return; 124 } 125 } 126 super.update(g, c); 127 } 128 129 protected void paintButtonPressed(Graphics g, AbstractButton b) { 130 if ( b.isContentAreaFilled() ) { 131 g.setColor(getSelectColor()); 132 g.fillRect(0, 0, b.getWidth(), b.getHeight()); 133 } 134 } 135 136 protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { 137 AbstractButton b = (AbstractButton) c; 138 ButtonModel model = b.getModel(); 139 FontMetrics fm = SwingUtilities2.getFontMetrics(b, g); 140 int mnemIndex = b.getDisplayedMnemonicIndex(); 141 142 143 if(model.isEnabled()) { 144 145 g.setColor(b.getForeground()); 146 } 147 else { 148 149 if (model.isSelected()) { 150 g.setColor(c.getBackground()); 151 } else { 152 g.setColor(getDisabledTextColor()); 153 } 154 } 155 SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex, 156 textRect.x, textRect.y + fm.getAscent()); 157 } 158 159 protected void paintFocus(Graphics g, AbstractButton b, 160 Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ 161 162 Rectangle focusRect = new Rectangle(); 163 String text = b.getText(); 164 boolean isIcon = b.getIcon() != null; 165 166 if ( text != null && !text.equals( "" ) ) { 168 if ( !isIcon ) { 169 focusRect.setBounds( textRect ); 170 } 171 else { 172 focusRect.setBounds( iconRect.union( textRect ) ); 173 } 174 } 175 else if ( isIcon ) { 177 focusRect.setBounds( iconRect ); 178 } 179 180 g.setColor(getFocusColor()); 181 g.drawRect((focusRect.x-1), (focusRect.y-1), 182 focusRect.width+1, focusRect.height+1); 183 184 } 185 186 196 protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) { 197 super.paintIcon(g, b, iconRect); 198 } 199 } 200 | Popular Tags |