1 19 24 25 package org.netbeans.swing.plaf.aqua; 26 27 import javax.swing.*; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import javax.swing.plaf.ButtonUI ; 31 import javax.swing.plaf.basic.BasicButtonListener ; 32 import java.awt.*; 33 import java.awt.image.BufferedImage ; 34 35 39 class AquaToolBarButtonUI extends ButtonUI implements ChangeListener { 40 private static BasicButtonListener listener = 41 new BasicButtonListener (null); 42 43 44 public AquaToolBarButtonUI() { 45 } 46 47 public void installUI (JComponent c) { 48 AbstractButton b = (AbstractButton) c; 49 b.addMouseListener (listener); 50 b.addChangeListener(this); 51 b.setContentAreaFilled(false); 52 b.setOpaque(false); 53 b.setFocusable(false); 54 b.setBorderPainted(false); 55 b.setBorder (BorderFactory.createEmptyBorder()); 56 } 57 58 public void uninstallUI(JComponent c) { 59 c.removeMouseListener (listener); 60 } 61 62 public void stateChanged(ChangeEvent e) { 63 ((AbstractButton) e.getSource()).repaint(); 64 } 65 66 private final Rectangle scratch = new Rectangle(); 67 public void paint (Graphics g, JComponent c) { 68 Rectangle r = c.getBounds(scratch); 69 AbstractButton b = (AbstractButton) c; 70 r.x = 0; 71 r.y = 0; 72 Paint temp = ((Graphics2D) g).getPaint(); 73 paintBackground ((Graphics2D)g, b, r); 74 paintIcon (g, b, r); 75 paintText (g, b, r); 76 ((Graphics2D) g).setPaint(temp); 77 } 78 79 80 private FontMetrics fm = null; private void paintText (Graphics g, AbstractButton b, Rectangle r) { 82 String s = b.getText(); 83 if (s == null || s.length() == 0) { 84 return; 85 } 86 g.setColor (b.getForeground()); 87 Font f = b.getFont(); 88 if (b.isSelected()) { 89 f = new Font(f.getName(), Font.BOLD, f.getSize()); 91 } 92 g.setFont (f); 93 FontMetrics fm = g.getFontMetrics(); 94 if (this.fm == null) { 95 this.fm = fm; 96 } 97 int x = 0; 98 Icon ic = b.getIcon(); 99 if (ic != null) { 100 x = ic.getIconWidth() + 2; 101 } else { 102 int w = fm.stringWidth (s); 103 if (w <= r.width) { 104 x = (r.width / 2) - (w / 2); 105 } 106 } 107 int h = fm.getHeight(); 108 int y = fm.getMaxAscent(); 109 if (h <= r.height) { 110 y += (r.height / 2) - (h / 2); 111 } 112 g.drawString (s, x, y); 113 } 114 115 private void paintBackground (Graphics2D g, AbstractButton b, Rectangle r) { 116 if (!b.isEnabled()) { 117 } else if (b.getModel().isPressed()) { 118 compositeColor (g, r, Color.BLUE, 0.3f); 119 } else if (b.getModel().isSelected()) { 120 compositeColor (g, r, new Color (0, 120, 255), 0.2f);; 121 } 122 } 123 124 private void compositeColor (Graphics2D g, Rectangle r, Color c, float alpha) { 125 g.setColor (c); 126 Composite comp = g.getComposite(); 127 128 g.setComposite(AlphaComposite.getInstance( 129 AlphaComposite.SRC_OVER, alpha)); 130 131 g.fillRect (r.x, r.y, r.width, r.height); 132 g.setComposite(comp); 133 } 134 135 136 private static boolean isFirst (AbstractButton b) { 137 if (b.getParent() != null && b.getParent().getComponentCount() > 1) { 138 return b == b.getParent().getComponent(1); 141 } else { 142 return false; 143 } 144 } 145 146 private void paintIcon (Graphics g, AbstractButton b, Rectangle r) { 147 Icon ic = getIconForState (b); 148 boolean noText = b.getText() == null || b.getText().length() == 0; 149 if (ic != null) { 150 int iconX = 0; 151 int iconY = 0; 152 int iconW = ic.getIconWidth(); 153 int iconH = ic.getIconHeight(); 154 155 if (iconW <= r.width && noText) { 156 iconX = (r.width / 2) - (iconW / 2); 157 } 158 if (iconH <= r.height) { 159 iconY = (r.height / 2) - (iconH / 2); 160 } 161 ic.paintIcon(b, g, iconX, iconY); 162 } 163 } 164 165 private Icon getIconForState (AbstractButton b) { 166 ButtonModel mdl = b.getModel(); 167 Icon result = null; 168 if (!b.isEnabled()) { 169 result = mdl.isSelected() ? b.getDisabledSelectedIcon() : b.getDisabledIcon(); 170 if (result == null && mdl.isSelected()) { 171 result = b.getDisabledIcon(); 172 } 173 } else { 174 if (mdl.isArmed() && !mdl.isPressed()) { 175 result = mdl.isSelected() ? b.getRolloverSelectedIcon() : b.getRolloverIcon(); 176 if (result == null & mdl.isSelected()) { 177 result = b.getRolloverIcon(); 178 } 179 } 180 if (mdl.isPressed()) { 181 result = b.getPressedIcon(); 182 } else if (mdl.isSelected()) { 183 result = b.getSelectedIcon(); 184 } 185 } 186 if (result == null) { 187 result = b.getIcon(); 188 } 189 return result; 190 } 191 192 private static final int minButtonSize = 32; 193 public Dimension getPreferredSize(JComponent c) { 194 AbstractButton b = (AbstractButton) c; 195 196 boolean noText = 197 b.getText() == null || 198 b.getText().length() == 0; 199 200 Icon ic = getIconForState((AbstractButton) c); 201 int w = isFirst(b) ? 0 : minButtonSize; 202 Dimension result = ic == null ? new Dimension (noText ? 32 : 0, minButtonSize) : 203 new Dimension(Math.max(w, ic.getIconWidth()+1), 204 Math.max(minButtonSize,ic.getIconHeight() + 1)); 205 206 if (!noText) { 207 FontMetrics fm = this.fm; 208 if (fm == null && c.getGraphicsConfiguration() != null) { 209 fm = c.getGraphicsConfiguration().createCompatibleImage(1,1) 210 .getGraphics().getFontMetrics(c.getFont()); 211 } 212 if (fm == null) { 213 fm = new BufferedImage (1, 1, 215 BufferedImage.TYPE_INT_RGB).getGraphics().getFontMetrics(c.getFont()); 216 } 217 result.width += fm.stringWidth(b.getText()); 218 } 219 return result; 220 } 221 } 222 | Popular Tags |