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: package org.netbeans.swing.plaf.gtk;
043:
044: import javax.swing.*;
045: import javax.swing.event.ChangeEvent;
046: import javax.swing.event.ChangeListener;
047: import javax.swing.plaf.ButtonUI;
048: import javax.swing.plaf.basic.BasicButtonListener;
049: import java.awt.*;
050:
051: /** A GTK-style toolbar button UI
052: *
053: * @author Tim Boudreau
054: */
055: class GtkToolBarButtonUI extends ButtonUI implements ChangeListener {
056: private static BasicButtonListener listener = new BasicButtonListener(
057: null);
058:
059: /** Creates a new instance of AquaToolBarButtonUI */
060: public GtkToolBarButtonUI() {
061: }
062:
063: public void installUI(JComponent c) {
064: AbstractButton b = (AbstractButton) c;
065: b.addMouseListener(listener);
066: b.addChangeListener(this );
067: b.setContentAreaFilled(false);
068: b.setOpaque(false);
069: b.setFocusable(false);
070: b.setBorderPainted(false);
071: b.setBorder(BorderFactory.createEmptyBorder());
072: b.putClientProperty("hideActionText", Boolean.TRUE); //NOI18N
073: }
074:
075: public void uninstallUI(JComponent c) {
076: c.removeMouseListener(listener);
077: }
078:
079: public void stateChanged(ChangeEvent e) {
080: ((AbstractButton) e.getSource()).repaint();
081: }
082:
083: private final Rectangle scratch = new Rectangle();
084:
085: public void paint(Graphics g, JComponent c) {
086: Rectangle r = c.getBounds(scratch);
087: AbstractButton b = (AbstractButton) c;
088: r.x = 0;
089: r.y = 0;
090: Paint temp = ((Graphics2D) g).getPaint();
091: paintBackground((Graphics2D) g, b, r);
092: paintIcon(g, b, r);
093: ((Graphics2D) g).setPaint(temp);
094: }
095:
096: private void paintBackground(Graphics2D g, AbstractButton b,
097: Rectangle r) {
098: if (!b.isEnabled()) {
099: } else if (b.getModel().isPressed()) {
100: compositeColor(g, r, Color.BLUE, 0.3f);
101: } else if (b.getModel().isSelected()) {
102: compositeColor(g, r, new Color(0, 120, 255), 0.2f);
103: ;
104: }
105: }
106:
107: private void compositeColor(Graphics2D g, Rectangle r, Color c,
108: float alpha) {
109: g.setColor(c);
110: Composite comp = g.getComposite();
111:
112: g.setComposite(AlphaComposite.getInstance(
113: AlphaComposite.SRC_OVER, alpha));
114:
115: g.fillRect(r.x, r.y, r.width, r.height);
116: g.setComposite(comp);
117: }
118:
119: private static boolean isFirst(AbstractButton b) {
120: if (b.getParent() != null
121: && b.getParent().getComponentCount() > 1) {
122: //The grip is always component 0, so see if the button
123: //is component 1
124: return b == b.getParent().getComponent(1);
125: } else {
126: return false;
127: }
128: }
129:
130: private void paintIcon(Graphics g, AbstractButton b, Rectangle r) {
131: Icon ic = getIconForState(b);
132: if (ic != null) {
133: int iconX = 0;
134: int iconY = 0;
135: int iconW = ic.getIconWidth();
136: int iconH = ic.getIconHeight();
137:
138: if (iconW <= r.width) {
139: iconX = (r.width / 2) - (iconW / 2);
140: }
141: if (iconH <= r.height) {
142: iconY = (r.height / 2) - (iconH / 2);
143: }
144: ic.paintIcon(b, g, iconX, iconY);
145: }
146: }
147:
148: private Icon getIconForState(AbstractButton b) {
149: ButtonModel mdl = b.getModel();
150: Icon result = null;
151: if (!b.isEnabled()) {
152: result = mdl.isSelected() ? b.getDisabledSelectedIcon() : b
153: .getDisabledIcon();
154: if (result == null && mdl.isSelected()) {
155: result = b.getDisabledIcon();
156: }
157: } else {
158: if (mdl.isArmed() && !mdl.isPressed()) {
159: result = mdl.isSelected() ? b.getRolloverSelectedIcon()
160: : b.getRolloverIcon();
161: if (result == null & mdl.isSelected()) {
162: result = b.getRolloverIcon();
163: }
164: }
165: if (mdl.isPressed()) {
166: result = b.getPressedIcon();
167: } else if (mdl.isSelected()) {
168: result = b.getSelectedIcon();
169: }
170: }
171: if (result == null) {
172: result = b.getIcon();
173: }
174: return result;
175: }
176:
177: private static final int minButtonSize = 32;
178:
179: public Dimension getPreferredSize(JComponent c) {
180: if (c instanceof AbstractButton) {
181: Icon ic = getIconForState((AbstractButton) c);
182: Dimension result;
183: int minSize = isFirst((AbstractButton) c) ? 0
184: : minButtonSize;
185: if (ic != null) {
186: result = new Dimension(Math.max(minSize, ic
187: .getIconWidth() + 1), Math.max(minButtonSize,
188: ic.getIconHeight() + 1));
189: } else {
190: result = new Dimension(minButtonSize, minButtonSize);
191: }
192: result.width += 4;
193: return result;
194: } else {
195: if (c.getLayout() != null) {
196: return c.getLayout().preferredLayoutSize(c);
197: }
198: }
199: return null;
200: }
201: }
|