001: /*
002: * RolloverButton.java - Class for buttons that implement rollovers
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002 Kris Kopicki
007: * Portions copyright (C) 2003 Slava Pestov
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit.gui;
025:
026: //{{{ Imports
027: import java.awt.*;
028: import java.awt.event.*;
029: import javax.swing.*;
030: import javax.swing.border.*;
031: import javax.swing.plaf.basic.BasicButtonUI;
032: import org.gjt.sp.jedit.OperatingSystem;
033:
034: //}}}
035:
036: /**
037: * If you wish to have rollovers on your buttons, use this class.
038: *
039: * Unlike the Swing rollover support, this class works outside of
040: * <code>JToolBar</code>s, and does not require undocumented client
041: * property hacks or JDK1.4-specific API calls.<p>
042: *
043: * Note: You should not call <code>setBorder()</code> on your buttons,
044: * as they probably won't work properly.
045: * @version $Id: RolloverButton.java 9481 2007-05-02 00:34:44Z k_satoda $
046: */
047: public class RolloverButton extends JButton {
048: //{{{ RolloverButton constructor
049: /**
050: * Setup the border (invisible initially)
051: */
052: public RolloverButton() {
053: setContentAreaFilled(false);
054:
055: addMouseListener(new MouseOverHandler());
056: } //}}}
057:
058: //{{{ RolloverButton constructor
059: /**
060: * Setup the border (invisible initially)
061: *
062: * @param icon the icon of this button
063: */
064: public RolloverButton(Icon icon) {
065: this ();
066:
067: setIcon(icon);
068: } //}}}
069:
070: //{{{ updateUI() method
071: public void updateUI() {
072: if (OperatingSystem.isWindows()) {
073: /* Workaround for uncooperative Windows L&F */
074: setUI(new BasicButtonUI());
075: } else
076: super .updateUI();
077:
078: setBorder(new EtchedBorder());
079: setBorderPainted(false);
080: setMargin(new Insets(1, 1, 1, 1));
081:
082: setRequestFocusEnabled(false);
083: } //}}}
084:
085: //{{{ isOpaque() method
086: public boolean isOpaque() {
087: return false;
088: } //}}}
089:
090: //{{{ setEnabled() method
091: public void setEnabled(boolean b) {
092: super .setEnabled(b);
093: setBorderPainted(false);
094: repaint();
095: } //}}}
096:
097: //{{{ setBorderPainted() method
098: public void setBorderPainted(boolean b) {
099: try {
100: revalidateBlocked = true;
101: super .setBorderPainted(b);
102: } finally {
103: revalidateBlocked = false;
104: }
105: } //}}}
106:
107: //{{{ revalidate() method
108: /**
109: * We block calls to revalidate() from a setBorderPainted(), for
110: * performance reasons.
111: */
112: public void revalidate() {
113: if (!revalidateBlocked)
114: super .revalidate();
115: } //}}}
116:
117: //{{{ paint() method
118: public void paint(Graphics g) {
119: if (isEnabled())
120: super .paint(g);
121: else {
122: Graphics2D g2 = (Graphics2D) g;
123: g2.setComposite(c);
124: super .paint(g2);
125: }
126: } //}}}
127:
128: //{{{ Private members
129: private static final AlphaComposite c = AlphaComposite.getInstance(
130: AlphaComposite.SRC_OVER, 0.5f);
131:
132: private boolean revalidateBlocked;
133:
134: //{{{ MouseHandler class
135: /**
136: * Make the border visible/invisible on rollovers
137: */
138: class MouseOverHandler extends MouseAdapter {
139: public void mouseEntered(MouseEvent e) {
140: if (isEnabled())
141: setBorderPainted(true);
142: }
143:
144: public void mouseExited(MouseEvent e) {
145: setBorderPainted(false);
146: }
147: } //}}}
148: //}}}
149: }
|