1 7 8 package javax.swing.plaf.metal; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import javax.swing.*; 12 import javax.swing.border.*; 13 import javax.swing.plaf.basic.*; 14 import java.awt.*; 15 import java.awt.event.*; 16 import java.beans.*; 17 import javax.swing.plaf.*; 18 19 34 public class MetalButtonUI extends BasicButtonUI { 35 36 private final static MetalButtonUI metalButtonUI = new MetalButtonUI (); 37 38 protected Color focusColor; 41 protected Color selectColor; 42 protected Color disabledTextColor; 43 44 public static ComponentUI createUI(JComponent c) { 48 return metalButtonUI; 49 } 50 51 public void installDefaults(AbstractButton b) { 55 super.installDefaults(b); 56 } 57 58 public void uninstallDefaults(AbstractButton b) { 59 super.uninstallDefaults(b); 60 } 61 62 protected BasicButtonListener createButtonListener(AbstractButton b) { 66 return super.createButtonListener(b); 67 } 68 69 70 protected Color getSelectColor() { 74 selectColor = UIManager.getColor(getPropertyPrefix() + "select"); 75 return selectColor; 76 } 77 78 protected Color getDisabledTextColor() { 79 disabledTextColor = UIManager.getColor(getPropertyPrefix() + 80 "disabledText"); 81 return disabledTextColor; 82 } 83 84 protected Color getFocusColor() { 85 focusColor = UIManager.getColor(getPropertyPrefix() + "focus"); 86 return focusColor; 87 } 88 89 104 public void update(Graphics g, JComponent c) { 105 AbstractButton button = (AbstractButton)c; 106 if ((c.getBackground() instanceof UIResource) && 107 button.isContentAreaFilled() && c.isEnabled()) { 108 ButtonModel model = button.getModel(); 109 if (!MetalUtils.isToolBarButton(c)) { 110 if (!model.isArmed() && !model.isPressed() && 111 MetalUtils.drawGradient( 112 c, g, "Button.gradient", 0, 0, c.getWidth(), 113 c.getHeight(), true)) { 114 paint(g, c); 115 return; 116 } 117 } 118 else if (model.isRollover() && MetalUtils.drawGradient( 119 c, g, "Button.gradient", 0, 0, c.getWidth(), 120 c.getHeight(), true)) { 121 paint(g, c); 122 return; 123 } 124 } 125 super.update(g, c); 126 } 127 128 protected void paintButtonPressed(Graphics g, AbstractButton b) { 129 if ( b.isContentAreaFilled() ) { 130 Dimension size = b.getSize(); 131 g.setColor(getSelectColor()); 132 g.fillRect(0, 0, size.width, size.height); 133 } 134 } 135 136 protected void paintFocus(Graphics g, AbstractButton b, 137 Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ 138 139 Rectangle focusRect = new Rectangle(); 140 String text = b.getText(); 141 boolean isIcon = b.getIcon() != null; 142 143 if ( text != null && !text.equals( "" ) ) { 145 if ( !isIcon ) { 146 focusRect.setBounds( textRect ); 147 } 148 else { 149 focusRect.setBounds( iconRect.union( textRect ) ); 150 } 151 } 152 else if ( isIcon ) { 154 focusRect.setBounds( iconRect ); 155 } 156 157 g.setColor(getFocusColor()); 158 g.drawRect((focusRect.x-1), (focusRect.y-1), 159 focusRect.width+1, focusRect.height+1); 160 161 } 162 163 164 protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { 165 AbstractButton b = (AbstractButton) c; 166 ButtonModel model = b.getModel(); 167 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); 168 int mnemIndex = b.getDisplayedMnemonicIndex(); 169 170 171 if(model.isEnabled()) { 172 173 g.setColor(b.getForeground()); 174 } 175 else { 176 177 g.setColor(getDisabledTextColor()); 178 } 179 SwingUtilities2.drawStringUnderlineCharAt(c, g,text,mnemIndex, 180 textRect.x, textRect.y + fm.getAscent()); 181 } 182 } 183 | Popular Tags |