PageRenderTime 136ms CodeModel.GetById 94ms app.highlight 24ms RepoModel.GetById 16ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/RolloverButton.java

#
Java | 190 lines | 100 code | 19 blank | 71 comment | 10 complexity | 5f286b30b7a8055bc6aae3f99dfd15a5 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 java.lang.reflect.Method;
 30import javax.swing.*;
 31import javax.swing.border.*;
 32import org.gjt.sp.jedit.OperatingSystem;
 33import org.gjt.sp.util.Log;
 34//}}}
 35
 36/**
 37 * If you wish to have rollovers on your buttons, use this class.
 38 *
 39 * Unlike the Swing rollover support, this class works outside of
 40 * <code>JToolBar</code>s, and does not require undocumented client
 41 * property hacks or JDK1.4-specific API calls.<p>
 42 *
 43 * Note: You should not call <code>setBorder()</code> on your buttons,
 44 * as they probably won't work properly.
 45 */
 46public class RolloverButton extends JButton
 47{
 48	//{{{ RolloverButton constructor
 49	/**
 50	 * Setup the border (invisible initially)
 51	 */
 52	public RolloverButton()
 53	{
 54		if(OperatingSystem.hasJava15())
 55			setContentAreaFilled(false);
 56
 57		if(method != null)
 58		{
 59			try
 60			{
 61				method.invoke(this,new Boolean[] { Boolean.TRUE });
 62			}
 63			catch(Exception e)
 64			{
 65				Log.log(Log.ERROR,this,e);
 66			}
 67		}
 68		else
 69		{
 70			setBorder(new EtchedBorder());
 71			setBorderPainted(false);
 72			setMargin(new Insets(1,1,1,1));
 73
 74			setRequestFocusEnabled(false);
 75
 76			addMouseListener(new MouseOverHandler());
 77		}
 78	} //}}}
 79
 80	//{{{ RolloverButton constructor
 81	/**
 82	 * Setup the border (invisible initially)
 83	 */
 84	public RolloverButton(Icon icon)
 85	{
 86		this();
 87
 88		setIcon(icon);
 89	} //}}}
 90
 91	//{{{ isOpaque() method
 92	public boolean isOpaque()
 93	{
 94		return false;
 95	} //}}}
 96
 97	//{{{ setEnabled() method
 98	public void setEnabled(boolean b)
 99	{
100		super.setEnabled(b);
101		if(method == null)
102		{
103			setBorderPainted(false);
104			repaint();
105		}
106	} //}}}
107
108	//{{{ setBorderPainted() method
109	public void setBorderPainted(boolean b)
110	{
111		try
112		{
113			revalidateBlocked = true;
114			super.setBorderPainted(b);
115		}
116		finally
117		{
118			revalidateBlocked = false;
119		}
120	} //}}}
121
122	//{{{ revalidate() method
123	/**
124	 * We block calls to revalidate() from a setBorderPainted(), for
125	 * performance reasons.
126	 */
127	public void revalidate()
128	{
129		if(!revalidateBlocked)
130			super.revalidate();
131	} //}}}
132
133	//{{{ paint() method
134	public void paint(Graphics g)
135	{
136		if(method != null || isEnabled())
137			super.paint(g);
138		else
139		{
140			Graphics2D g2 = (Graphics2D)g;
141			g2.setComposite(c);
142			super.paint(g2);
143		}
144	} //}}}
145
146	//{{{ Private members
147	private static AlphaComposite c = AlphaComposite.getInstance(
148		AlphaComposite.SRC_OVER, 0.5f);
149
150	private static Method method;
151
152	private boolean revalidateBlocked;
153
154	static
155	{
156		/* if(OperatingSystem.hasJava14())
157		{
158			try
159			{
160				method = RolloverButton.class
161					.getMethod("setRolloverEnabled",new Class[]
162					{ boolean.class });
163				Log.log(Log.DEBUG,RolloverButton.class,
164					"Java 1.4 setRolloverEnabled() method enabled");
165			}
166			catch(Exception e)
167			{
168				Log.log(Log.ERROR,RolloverButton.class,e);
169			}
170		} */
171	} //}}}
172
173	//{{{ MouseHandler class
174	/**
175	 * Make the border visible/invisible on rollovers
176	 */
177	class MouseOverHandler extends MouseAdapter
178	{
179		public void mouseEntered(MouseEvent e)
180		{
181			if (isEnabled())
182				setBorderPainted(true);
183		}
184
185		public void mouseExited(MouseEvent e)
186		{
187			setBorderPainted(false);
188		}
189	} //}}}
190}