KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBToolBarButton


1 /**
2  *
3  *
4  * Author: Van Bui
5  * Date: 15/09/2002 / 16:15:18
6  */

7 package com.ca.commons.cbutil;
8
9 import javax.swing.*;
10 import java.awt.*;
11 import java.awt.event.MouseAdapter JavaDoc;
12 import java.awt.event.MouseEvent JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14
15 /**
16  * A button with some pre-defined behaviours. The text, tool tip, icon,
17  * and mnemonic could be set in the constructor. The button has an optional
18  * icon on top and the text on bottom, center-aligned. The text is normally
19  * displayed in black. When the mouse is moved over the button the text turns
20  * blue. If alwaysPaintBorder is set to <b>false</b>, the border of the button
21  * is only highlighted when the mouse enters the button.
22  * <br>
23  *
24  * @author vbui
25  */

26 public class CBToolBarButton extends JButton
27 {
28
29     static Logger JavaDoc logger = Logger.getLogger(CBToolBarButton.class.getPackage().getName());
30
31     private boolean alwaysPaintBorder = true;
32     private int width = 75;
33     private int height = 40;
34
35     /**
36      * Constructor.
37      *
38      * @param label the text on the button, for example, if this parameter
39      * is set to "E&xit" the button will display the text "Exit" and will
40      * have "x" as its mnemonic.
41      * @param tooltip the tool tip text for the button.
42      */

43     public CBToolBarButton(String JavaDoc label, String JavaDoc tooltip)
44     {
45         super();
46         init(label, tooltip);
47     }
48
49     /**
50      * Constructor.
51      *
52      * @param label the text on the button, for example, if this parameter
53      * is set to "E&xit" the button will display the text "Exit" and will
54      * have "x" as its mnemonic.
55      * @param iconFileName specify where to find the icon for the button.
56      * @param tooltip the tool tip text for the button.
57      */

58     public CBToolBarButton(String JavaDoc label, String JavaDoc iconFileName, String JavaDoc tooltip)
59     {
60         this(label, iconFileName, tooltip, CENTER, BOTTOM);
61     }
62
63     /**
64      * Constructor.
65      *
66      * @param label the text on the button, for example, if this parameter
67      * is set to "E&xit" the button will display the text "Exit" and will
68      * have "x" as its mnemonic.
69      * @param iconFileName specify where to find the icon for the button.
70      * @param tooltip - the tool tip text for the button.
71      * @param horizPos - the horizontal position for the button text.
72      * @param vertPos - the vertical position for the button text.
73      */

74     public CBToolBarButton(String JavaDoc label, String JavaDoc iconFileName, String JavaDoc tooltip,
75                            int horizPos, int vertPos)
76     {
77         super();
78         init(label, tooltip);
79
80         ImageIcon buttonIcon = new ImageIcon(iconFileName);
81         setIcon(buttonIcon);
82         setHorizontalTextPosition(horizPos);
83         setVerticalTextPosition(vertPos);
84     }
85
86     /**
87      * Leave some space between the icon/text and the edges of the button.
88      */

89     public Insets getInsets()
90     {
91         return new Insets(1, 1, 1, 1);
92     }
93
94     /**
95      * To help the layout manager.
96      */

97     public Dimension getPreferredSize()
98     {
99         return new Dimension(width, height);
100     }
101
102     /**
103      * Initialization.
104      */

105     public void init(String JavaDoc label, String JavaDoc tooltip)
106     {
107         setForeground(Color.black);
108         if (!alwaysPaintBorder) setBorderPainted(false);
109
110         setText(label);
111
112         setToolTipText(tooltip);
113
114         addMouseListener(new MouseAdapter JavaDoc()
115         {
116             public void mouseEntered(MouseEvent JavaDoc e)
117             {
118                 setForeground(Color.blue);
119                 if (!alwaysPaintBorder && isEnabled())
120                     setBorderPainted(true);
121             }
122
123             public void mouseExited(MouseEvent JavaDoc e)
124             {
125                 setForeground(Color.black);
126                 if (!alwaysPaintBorder && isEnabled())
127                     setBorderPainted(false);
128             }
129         });
130
131         setMinimumSize(getPreferredSize());
132         setMaximumSize(getPreferredSize());
133     }
134
135     /**
136      * Sets the displayed text on a button. An optional ampersand
137      * character &amp; shows the location of the mnemonic (if any).
138      *
139      * @param label the button label, optionally including an
140      * ampersanded-mnemonic character
141      */

142
143     public void setText(String JavaDoc label)
144     {
145         if (label != null)
146         {
147             int pos = label.indexOf('&');
148             if (pos >= 0 && pos <= label.length() - 2)
149             {
150                 super.setText(label.substring(0, pos) + label.substring(pos + 1));
151                 setMnemonic(getText().charAt(pos));
152             }
153             else
154             {
155                 super.setText(label);
156             }
157         }
158     }
159
160     /**
161      * Set the parameter alwaysPaintBorder.
162      */

163     public void setAlwaysPaintBorder(boolean alwaysPaintBorder)
164     {
165         this.alwaysPaintBorder = alwaysPaintBorder;
166         if (alwaysPaintBorder)
167             setBorderPainted(true);
168         else
169             setBorderPainted(false);
170     }
171
172     /**
173      * Set preferredSize of the button.
174      *
175      * @param width preferred width of the button.
176      * @param height preferred height of the button.
177      */

178     public void setWidthHeight(int width, int height)
179     {
180         this.width = width;
181         this.height = height;
182         setMinimumSize(getPreferredSize());
183         setMaximumSize(getPreferredSize());
184     }
185 }
186
Popular Tags