PageRenderTime 65ms CodeModel.GetById 49ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/RolloverButton.java

#
Java | 155 lines | 78 code | 15 blank | 62 comment | 2 complexity | 0428a315378f63c01e729c71c647a249 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 * 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
 24package org.gjt.sp.jedit.gui;
 25
 26//{{{ Imports
 27import java.awt.*;
 28import java.awt.event.*;
 29import javax.swing.*;
 30import javax.swing.border.*;
 31import javax.swing.plaf.basic.BasicButtonUI;
 32import javax.swing.plaf.basic.BasicBorders.ButtonBorder;
 33import org.gjt.sp.jedit.OperatingSystem;
 34
 35//}}}
 36
 37/**
 38 * If you wish to have rollovers on your buttons, use this class.
 39 *
 40 * Unlike the Swing rollover support, this class works outside of
 41 * <code>JToolBar</code>s, and does not require undocumented client
 42 * property hacks or JDK1.4-specific API calls.<p>
 43 *
 44 * Note: You should not call <code>setBorder()</code> on your buttons,
 45 * as they probably won't work properly.
 46 * @version $Id: RolloverButton.java 12504 2008-04-22 23:12:43Z ezust $
 47 */
 48public class RolloverButton extends JButton
 49{
 50	//{{{ RolloverButton constructor
 51	/**
 52	 * Setup the border (invisible initially)
 53	 */
 54	public RolloverButton()
 55	{
 56		//setContentAreaFilled(true);
 57		addMouseListener(new MouseOverHandler());
 58	} //}}}
 59
 60	//{{{ RolloverButton constructor
 61	/**
 62	 * Setup the border (invisible initially)
 63	 *
 64	 * @param icon the icon of this button
 65	 */
 66	public RolloverButton(Icon icon)
 67	{
 68		this();
 69
 70		setIcon(icon);
 71	} //}}}
 72
 73	//{{{ updateUI() method
 74	public void updateUI()
 75	{
 76		super.updateUI();
 77		//setBorder(originalBorder);
 78		setBorderPainted(false);
 79		setRequestFocusEnabled(false);
 80		setMargin(new Insets(1,1,1,1));
 81	} //}}}
 82
 83	//{{{ setEnabled() method
 84	public void setEnabled(boolean b)
 85	{
 86		super.setEnabled(b);
 87		setBorderPainted(false);
 88		repaint();
 89	} //}}}
 90
 91	//{{{ setBorderPainted() method
 92	public void setBorderPainted(boolean b)
 93	{
 94		try
 95		{
 96			revalidateBlocked = true;
 97			super.setBorderPainted(b);
 98			setContentAreaFilled(b);
 99		}
100		finally
101		{
102			revalidateBlocked = false;
103		}
104	} //}}}
105
106	//{{{ revalidate() method
107	/**
108	 * We block calls to revalidate() from a setBorderPainted(), for
109	 * performance reasons.
110	 */
111	public void revalidate()
112	{
113		if(!revalidateBlocked)
114			super.revalidate();
115	} //}}}
116
117	//{{{ paint() method
118	public void paint(Graphics g)
119	{
120		if(isEnabled())
121			super.paint(g);
122		else
123		{
124			Graphics2D g2 = (Graphics2D)g;
125			g2.setComposite(c);
126			super.paint(g2);
127		}
128	} //}}}
129
130	//{{{ Private members
131	private static final AlphaComposite c = AlphaComposite.getInstance(
132		AlphaComposite.SRC_OVER, 0.5f);
133
134	private boolean revalidateBlocked;
135
136	//{{{ MouseHandler class
137	/**
138	 * Make the border visible/invisible on rollovers
139	 */
140	class MouseOverHandler extends MouseAdapter
141	{
142		public void mouseEntered(MouseEvent e)
143		{
144			setContentAreaFilled(true);
145			setBorderPainted(isEnabled());
146		}
147
148		public void mouseExited(MouseEvent e)
149		{
150			setContentAreaFilled(false);
151			setBorderPainted(false);
152		}
153	} //}}}
154	//}}}
155}