KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > aqua > AquaToolBarButtonUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * AquaToolBarButtonUI.java
21  *
22  * Created on January 17, 2004, 1:54 PM
23  */

24
25 package org.netbeans.swing.plaf.aqua;
26
27 import javax.swing.*;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import javax.swing.plaf.ButtonUI JavaDoc;
31 import javax.swing.plaf.basic.BasicButtonListener JavaDoc;
32 import java.awt.*;
33 import java.awt.image.BufferedImage JavaDoc;
34
35 /** A finder-style aqua toolbar button UI
36  *
37  * @author Tim Boudreau
38  */

39 class AquaToolBarButtonUI extends ButtonUI JavaDoc implements ChangeListener JavaDoc {
40     private static BasicButtonListener JavaDoc listener =
41         new BasicButtonListener JavaDoc(null);
42     
43     /** Creates a new instance of AquaToolBarButtonUI */
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 JavaDoc 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; //We are not setting any custom fonts, can use one
81
private void paintText (Graphics g, AbstractButton b, Rectangle r) {
82         String JavaDoc 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             // don't use deriveFont() - see #49973 for details
90
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             //The grip is always component 0, so see if the button
139
//is component 1
140
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                 //init
214
fm = new BufferedImage JavaDoc(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