KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > RolloverButton


1 /*
2  * RolloverButton.java - Class for buttons that implement rollovers
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Kris Kopicki
7  * Portions copyright (C) 2003 Slava Pestov
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */

23
24 package org.gjt.sp.jedit.gui;
25
26 //{{{ Imports
27
import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 import javax.swing.border.*;
31 import javax.swing.plaf.basic.BasicButtonUI JavaDoc;
32 import org.gjt.sp.jedit.OperatingSystem;
33 //}}}
34

35 /**
36  * If you wish to have rollovers on your buttons, use this class.
37  *
38  * Unlike the Swing rollover support, this class works outside of
39  * <code>JToolBar</code>s, and does not require undocumented client
40  * property hacks or JDK1.4-specific API calls.<p>
41  *
42  * Note: You should not call <code>setBorder()</code> on your buttons,
43  * as they probably won't work properly.
44  * @version $Id: RolloverButton.java 8125 2006-11-24 15:07:17Z kpouer $
45  */

46 public class RolloverButton extends JButton
47 {
48     //{{{ RolloverButton constructor
49
/**
50      * Setup the border (invisible initially)
51      */

52     public RolloverButton()
53     {
54         setContentAreaFilled(false);
55
56         addMouseListener(new MouseOverHandler());
57     } //}}}
58

59     //{{{ RolloverButton constructor
60
/**
61      * Setup the border (invisible initially)
62      *
63      * @param icon the icon of this button
64      */

65     public RolloverButton(Icon icon)
66     {
67         this();
68
69         setIcon(icon);
70     } //}}}
71

72     //{{{ updateUI() method
73
public void updateUI()
74     {
75         if(OperatingSystem.isWindows())
76         {
77             /* Workaround for uncooperative Windows L&F */
78             setUI(new BasicButtonUI JavaDoc());
79         }
80         else
81             super.updateUI();
82
83         setBorder(new EtchedBorder());
84         setBorderPainted(false);
85         setMargin(new Insets(1,1,1,1));
86
87         setRequestFocusEnabled(false);
88     } //}}}
89

90     //{{{ isOpaque() method
91
public boolean isOpaque()
92     {
93         return false;
94     } //}}}
95

96     //{{{ setEnabled() method
97
public void setEnabled(boolean b)
98     {
99         super.setEnabled(b);
100         setBorderPainted(false);
101         repaint();
102     } //}}}
103

104     //{{{ setBorderPainted() method
105
public void setBorderPainted(boolean b)
106     {
107         try
108         {
109             revalidateBlocked = true;
110             super.setBorderPainted(b);
111         }
112         finally
113         {
114             revalidateBlocked = false;
115         }
116     } //}}}
117

118     //{{{ revalidate() method
119
/**
120      * We block calls to revalidate() from a setBorderPainted(), for
121      * performance reasons.
122      */

123     public void revalidate()
124     {
125         if(!revalidateBlocked)
126             super.revalidate();
127     } //}}}
128

129     //{{{ paint() method
130
public void paint(Graphics g)
131     {
132         if(isEnabled())
133             super.paint(g);
134         else
135         {
136             Graphics2D g2 = (Graphics2D)g;
137             g2.setComposite(c);
138             super.paint(g2);
139         }
140     } //}}}
141

142     //{{{ Private members
143
private static final AlphaComposite c = AlphaComposite.getInstance(
144         AlphaComposite.SRC_OVER, 0.5f);
145
146     private boolean revalidateBlocked;
147
148     //{{{ MouseHandler class
149
/**
150      * Make the border visible/invisible on rollovers
151      */

152     class MouseOverHandler extends MouseAdapter
153     {
154         public void mouseEntered(MouseEvent e)
155         {
156             if (isEnabled())
157                 setBorderPainted(true);
158         }
159
160         public void mouseExited(MouseEvent e)
161         {
162             setBorderPainted(false);
163         }
164     } //}}}
165
}
166
Popular Tags