001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * AquaToolBarButtonUI.java
043: *
044: * Created on January 17, 2004, 1:54 PM
045: */
046:
047: package org.netbeans.swing.plaf.aqua;
048:
049: import javax.swing.*;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import javax.swing.plaf.ButtonUI;
053: import javax.swing.plaf.basic.BasicButtonListener;
054: import java.awt.*;
055: import java.awt.image.BufferedImage;
056:
057: /** A finder-style aqua toolbar button UI
058: *
059: * @author Tim Boudreau
060: */
061: class AquaToolBarButtonUI extends ButtonUI implements ChangeListener {
062: private static BasicButtonListener listener = new BasicButtonListener(
063: null);
064:
065: /** Creates a new instance of AquaToolBarButtonUI */
066: public AquaToolBarButtonUI() {
067: }
068:
069: public void installUI(JComponent c) {
070: AbstractButton b = (AbstractButton) c;
071: b.addMouseListener(listener);
072: b.addChangeListener(this );
073: b.setContentAreaFilled(false);
074: b.setOpaque(false);
075: b.setFocusable(false);
076: b.setBorderPainted(false);
077: b.setBorder(BorderFactory.createEmptyBorder());
078: }
079:
080: public void uninstallUI(JComponent c) {
081: c.removeMouseListener(listener);
082: }
083:
084: public void stateChanged(ChangeEvent e) {
085: ((AbstractButton) e.getSource()).repaint();
086: }
087:
088: private final Rectangle scratch = new Rectangle();
089:
090: public void paint(Graphics g, JComponent c) {
091: Rectangle r = c.getBounds(scratch);
092: AbstractButton b = (AbstractButton) c;
093: r.x = 0;
094: r.y = 0;
095: Paint temp = ((Graphics2D) g).getPaint();
096: paintBackground((Graphics2D) g, b, r);
097: paintIcon(g, b, r);
098: paintText(g, b, r);
099: ((Graphics2D) g).setPaint(temp);
100: }
101:
102: private FontMetrics fm = null; //We are not setting any custom fonts, can use one
103:
104: private void paintText(Graphics g, AbstractButton b, Rectangle r) {
105: String s = b.getText();
106: if (s == null || s.length() == 0) {
107: return;
108: }
109: g.setColor(b.getForeground());
110: Font f = b.getFont();
111: if (b.isSelected()) {
112: // don't use deriveFont() - see #49973 for details
113: f = new Font(f.getName(), Font.BOLD, f.getSize());
114: }
115: g.setFont(f);
116: FontMetrics fm = g.getFontMetrics();
117: if (this .fm == null) {
118: this .fm = fm;
119: }
120: int x = 0;
121: Icon ic = b.getIcon();
122: if (ic != null) {
123: x = ic.getIconWidth() + 2;
124: } else {
125: int w = fm.stringWidth(s);
126: if (w <= r.width) {
127: x = (r.width / 2) - (w / 2);
128: }
129: }
130: int h = fm.getHeight();
131: int y = fm.getMaxAscent();
132: if (h <= r.height) {
133: y += (r.height / 2) - (h / 2);
134: }
135: g.drawString(s, x, y);
136: }
137:
138: private void paintBackground(Graphics2D g, AbstractButton b,
139: Rectangle r) {
140: if (!b.isEnabled()) {
141: } else if (b.getModel().isPressed()) {
142: compositeColor(g, r, Color.BLUE, 0.3f);
143: } else if (b.getModel().isSelected()) {
144: compositeColor(g, r, new Color(0, 120, 255), 0.2f);
145: ;
146: }
147: }
148:
149: private void compositeColor(Graphics2D g, Rectangle r, Color c,
150: float alpha) {
151: g.setColor(c);
152: Composite comp = g.getComposite();
153:
154: g.setComposite(AlphaComposite.getInstance(
155: AlphaComposite.SRC_OVER, alpha));
156:
157: g.fillRect(r.x, r.y, r.width, r.height);
158: g.setComposite(comp);
159: }
160:
161: private static boolean isFirst(AbstractButton b) {
162: if (b.getParent() != null
163: && b.getParent().getComponentCount() > 1) {
164: //The grip is always component 0, so see if the button
165: //is component 1
166: return b == b.getParent().getComponent(1);
167: } else {
168: return false;
169: }
170: }
171:
172: private void paintIcon(Graphics g, AbstractButton b, Rectangle r) {
173: Icon ic = getIconForState(b);
174: boolean noText = b.getText() == null
175: || b.getText().length() == 0;
176: if (ic != null) {
177: int iconX = 0;
178: int iconY = 0;
179: int iconW = ic.getIconWidth();
180: int iconH = ic.getIconHeight();
181:
182: if (iconW <= r.width && noText) {
183: iconX = (r.width / 2) - (iconW / 2);
184: }
185: if (iconH <= r.height) {
186: iconY = (r.height / 2) - (iconH / 2);
187: }
188: ic.paintIcon(b, g, iconX, iconY);
189: }
190: }
191:
192: private Icon getIconForState(AbstractButton b) {
193: ButtonModel mdl = b.getModel();
194: Icon result = null;
195: if (!b.isEnabled()) {
196: result = mdl.isSelected() ? b.getDisabledSelectedIcon() : b
197: .getDisabledIcon();
198: if (result == null && mdl.isSelected()) {
199: result = b.getDisabledIcon();
200: }
201: } else {
202: if (mdl.isArmed() && !mdl.isPressed()) {
203: result = mdl.isSelected() ? b.getRolloverSelectedIcon()
204: : b.getRolloverIcon();
205: if (result == null & mdl.isSelected()) {
206: result = b.getRolloverIcon();
207: }
208: }
209: if (mdl.isPressed()) {
210: result = b.getPressedIcon();
211: } else if (mdl.isSelected()) {
212: result = b.getSelectedIcon();
213: }
214: }
215: if (result == null) {
216: result = b.getIcon();
217: }
218: return result;
219: }
220:
221: private static final int minButtonSize = 32;
222:
223: public Dimension getPreferredSize(JComponent c) {
224: AbstractButton b = (AbstractButton) c;
225:
226: boolean noText = b.getText() == null
227: || b.getText().length() == 0;
228:
229: Icon ic = getIconForState((AbstractButton) c);
230: int w = isFirst(b) ? 0 : minButtonSize;
231: Dimension result = ic == null ? new Dimension(noText ? 32 : 0,
232: minButtonSize) : new Dimension(Math.max(w, ic
233: .getIconWidth() + 1), Math.max(minButtonSize, ic
234: .getIconHeight() + 1));
235:
236: if (!noText) {
237: FontMetrics fm = this .fm;
238: if (fm == null && c.getGraphicsConfiguration() != null) {
239: fm = c.getGraphicsConfiguration()
240: .createCompatibleImage(1, 1).getGraphics()
241: .getFontMetrics(c.getFont());
242: }
243: if (fm == null) {
244: //init
245: fm = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB)
246: .getGraphics().getFontMetrics(c.getFont());
247: }
248: result.width += fm.stringWidth(b.getText());
249: }
250: return result;
251: }
252: }
|