PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/RolloverButton.java

#
Java | 124 lines | 53 code | 12 blank | 59 comment | 2 complexity | ec7b50567be061f4bffe1a944db52722 MD5 | raw file
  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 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import java.awt.*;
 27import java.awt.event.*;
 28import javax.swing.*;
 29import javax.swing.border.*;
 30import org.gjt.sp.jedit.OperatingSystem;
 31//}}}
 32
 33/**
 34 * If you wish to have rollovers on your buttons, use this class.
 35 *
 36 * Unlike the Swing rollover support, this class works outside of
 37 * <code>JToolBars</code>, and does not require undocumented client
 38 * property hacks or JDK1.4-specific API calls.<p>
 39 *
 40 * Note: You should not call setBorder() on your buttons, as they probably
 41 * won't work properly.
 42 */
 43public class RolloverButton extends JButton
 44{
 45	//{{{ RolloverButton constructor
 46	/**
 47	 * Setup the border (invisible initially)
 48	 */
 49	public RolloverButton()
 50	{
 51		setBorder(new EtchedBorder());
 52		setBorderPainted(false);
 53		setMargin(new Insets(0,0,0,0));
 54
 55		setRequestFocusEnabled(false);
 56
 57		addMouseListener(new MouseOverHandler());
 58	} //}}}
 59
 60	//{{{ RolloverButton constructor
 61	/**
 62	 * Setup the border (invisible initially)
 63	 */
 64	public RolloverButton(Icon icon)
 65	{
 66		this();
 67
 68		setIcon(icon);
 69	} //}}}
 70
 71	//{{{ setEnabled() method
 72	/**
 73	 * @overrides javax.swing.AbstractButton.setEnabled()
 74	 * @since jEdit 4.0pre3
 75	 * @param b true to enable the button, otherwise false
 76	 */
 77	public void setEnabled(boolean b)
 78	{
 79		super.setEnabled(b);
 80		setBorderPainted(false);
 81		repaint();
 82	} //}}}
 83
 84	//{{{ paint() method
 85	/**
 86	 * Custom paint method for fading the button when
 87	 * it is disabled
 88	 * @overrides javax.swing.JComponent.paint()
 89	 * @since jEdit 4.0pre3
 90	 * @param g the graphics context
 91	 */
 92	public void paint(Graphics g)
 93	{
 94		if (isEnabled())
 95			super.paint(g);
 96		else
 97		{
 98			Graphics2D g2 = (Graphics2D)g;
 99			g2.setComposite(c);
100			super.paint(g2);
101		}
102	} //}}}
103
104	private static AlphaComposite c = AlphaComposite.getInstance(
105		AlphaComposite.SRC_OVER, 0.5f);
106
107	//{{{ MouseHandler class
108	/**
109	 * Make the border visible/invisible on rollovers
110	 */
111	class MouseOverHandler extends MouseAdapter
112	{
113		public void mouseEntered(MouseEvent e)
114		{
115			if (isEnabled())
116				setBorderPainted(true);
117		}
118
119		public void mouseExited(MouseEvent e)
120		{
121			setBorderPainted(false);
122		}
123	} //}}}
124}