KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > gtk > GtkToolBarButtonUI


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 package org.netbeans.swing.plaf.gtk;
21
22 import javax.swing.*;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import javax.swing.plaf.ButtonUI JavaDoc;
26 import javax.swing.plaf.basic.BasicButtonListener JavaDoc;
27 import java.awt.*;
28
29 /** A GTK-style toolbar button UI
30  *
31  * @author Tim Boudreau
32  */

33 class GtkToolBarButtonUI extends ButtonUI JavaDoc implements ChangeListener JavaDoc {
34     private static BasicButtonListener JavaDoc listener =
35         new BasicButtonListener JavaDoc(null);
36
37     /** Creates a new instance of AquaToolBarButtonUI */
38     public GtkToolBarButtonUI() {
39     }
40     
41     public void installUI (JComponent c) {
42         AbstractButton b = (AbstractButton) c;
43         b.addMouseListener (listener);
44         b.addChangeListener(this);
45         b.setContentAreaFilled(false);
46         b.setOpaque(false);
47         b.setFocusable(false);
48         b.setBorderPainted(false);
49         b.setBorder (BorderFactory.createEmptyBorder());
50         b.putClientProperty("hideActionText", Boolean.TRUE); //NOI18N
51
}
52     
53     public void uninstallUI(JComponent c) {
54         c.removeMouseListener (listener);
55     }
56     
57     public void stateChanged(ChangeEvent JavaDoc e) {
58         ((AbstractButton) e.getSource()).repaint();
59     }
60     
61     private final Rectangle scratch = new Rectangle();
62     public void paint (Graphics g, JComponent c) {
63         Rectangle r = c.getBounds(scratch);
64         AbstractButton b = (AbstractButton) c;
65         r.x = 0;
66         r.y = 0;
67         Paint temp = ((Graphics2D) g).getPaint();
68         paintBackground ((Graphics2D)g, b, r);
69         paintIcon (g, b, r);
70         ((Graphics2D) g).setPaint(temp);
71     }
72     
73     private void paintBackground (Graphics2D g, AbstractButton b, Rectangle r) {
74         if (!b.isEnabled()) {
75         } else if (b.getModel().isPressed()) {
76             compositeColor (g, r, Color.BLUE, 0.3f);
77         } else if (b.getModel().isSelected()) {
78             compositeColor (g, r, new Color (0, 120, 255), 0.2f);;
79         }
80     }
81     
82     private void compositeColor (Graphics2D g, Rectangle r, Color c, float alpha) {
83         g.setColor (c);
84         Composite comp = g.getComposite();
85
86         g.setComposite(AlphaComposite.getInstance(
87             AlphaComposite.SRC_OVER, alpha));
88
89         g.fillRect (r.x, r.y, r.width, r.height);
90         g.setComposite(comp);
91     }
92     
93     private static boolean isFirst (AbstractButton b) {
94         if (b.getParent() != null && b.getParent().getComponentCount() > 1) {
95             //The grip is always component 0, so see if the button
96
//is component 1
97
return b == b.getParent().getComponent(1);
98         } else {
99             return false;
100         }
101     }
102     
103     private void paintIcon (Graphics g, AbstractButton b, Rectangle r) {
104         Icon ic = getIconForState (b);
105         if (ic != null) {
106             int iconX = 0;
107             int iconY = 0;
108             int iconW = ic.getIconWidth();
109             int iconH = ic.getIconHeight();
110             
111             if (iconW <= r.width) {
112                 iconX = (r.width / 2) - (iconW / 2);
113             }
114             if (iconH <= r.height) {
115                 iconY = (r.height / 2) - (iconH / 2);
116             }
117             ic.paintIcon(b, g, iconX, iconY);
118         }
119     }
120     
121     private Icon getIconForState (AbstractButton b) {
122         ButtonModel mdl = b.getModel();
123         Icon result = null;
124         if (!b.isEnabled()) {
125             result = mdl.isSelected() ? b.getDisabledSelectedIcon() : b.getDisabledIcon();
126             if (result == null && mdl.isSelected()) {
127                 result = b.getDisabledIcon();
128             }
129         } else {
130             if (mdl.isArmed() && !mdl.isPressed()) {
131                 result = mdl.isSelected() ? b.getRolloverSelectedIcon() : b.getRolloverIcon();
132                 if (result == null & mdl.isSelected()) {
133                     result = b.getRolloverIcon();
134                 }
135             }
136             if (mdl.isPressed()) {
137                 result = b.getPressedIcon();
138             } else if (mdl.isSelected()) {
139                 result = b.getSelectedIcon();
140             }
141         }
142         if (result == null) {
143             result = b.getIcon();
144         }
145         return result;
146     }
147     
148     private static final int minButtonSize = 32;
149     public Dimension getPreferredSize(JComponent c) {
150         if (c instanceof AbstractButton) {
151             Icon ic = getIconForState((AbstractButton) c);
152             Dimension result;
153             int minSize = isFirst((AbstractButton)c) ? 0 : minButtonSize;
154             if (ic != null) {
155                 result = new Dimension(Math.max(minSize, ic.getIconWidth()+1),
156                     Math.max(minButtonSize,ic.getIconHeight() + 1));
157             } else {
158                 result = new Dimension (minButtonSize, minButtonSize);
159             }
160             result.width += 4;
161             return result;
162         } else {
163             if (c.getLayout() != null) {
164                 return c.getLayout().preferredLayoutSize(c);
165             }
166         }
167         return null;
168     }
169 }
170
Popular Tags