KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > taskblocks > app > MyToolBar


1 /*
2  * Copyright (C) Jakub Neubauer, 2007
3  *
4  * This file is part of TaskBlocks
5  *
6  * TaskBlocks is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * TaskBlocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */

19
20 package taskblocks.app;
21
22 import java.awt.AlphaComposite JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics2D JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28
29 import javax.swing.Action JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.ImageIcon JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JToolBar JavaDoc;
34
35 public class MyToolBar extends JToolBar JavaDoc {
36
37     public JButton JavaDoc add(Action JavaDoc a) {
38         JButton JavaDoc b = createActionComponent(a);
39     b.putClientProperty("hideActionText", Boolean.FALSE);
40         b.setAction(a);
41         
42         // MacOS user feeling
43
b.putClientProperty("JButton.buttonType", "icon");
44         
45         b.setMargin(new Insets JavaDoc(10,10,10,10));
46         b.setBorderPainted(false);
47         b.setFocusable(false);
48         
49         String JavaDoc longD = (String JavaDoc) a.getValue(Action.LONG_DESCRIPTION);
50         if (longD != null) {
51             b.setToolTipText(longD);
52         }
53         Icon JavaDoc icon = b.getIcon();
54         if(icon != null) {
55             BufferedImage JavaDoc bufIm = new BufferedImage JavaDoc(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
56             Graphics2D JavaDoc g2 = (Graphics2D JavaDoc)bufIm.getGraphics();
57             icon.paintIcon(this, g2, 0, 0);
58             g2.setColor(Color.BLACK);
59             g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.4f));
60             g2.fillRect(0,0,icon.getIconWidth(), icon.getIconHeight());
61             ImageIcon JavaDoc pressedIcon = new ImageIcon JavaDoc(bufIm);
62             b.setPressedIcon(pressedIcon);
63             
64             bufIm = new BufferedImage JavaDoc(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
65             g2 = (Graphics2D JavaDoc)bufIm.getGraphics();
66             icon.paintIcon(this, g2, 0, 0);
67             g2.setColor(Color.WHITE);
68             g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.4f));
69             g2.fillRect(0,0,icon.getIconWidth(), icon.getIconHeight());
70             ImageIcon JavaDoc hoverIcon = new ImageIcon JavaDoc(bufIm);
71             b.setRolloverIcon(hoverIcon);
72         }
73         
74         b.setOpaque(false);
75         b.setRolloverEnabled(false);
76         b.setFont(b.getFont().deriveFont(Font.PLAIN));
77         b.setContentAreaFilled(false);
78
79         add(b);
80         return b;
81     }
82
83 }
84
Popular Tags