PageRenderTime 124ms CodeModel.GetById 117ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 159 lines | 80 code | 15 blank | 64 comment | 8 complexity | ba6b1ccbf4078a34443015e15d7a5042 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 java.lang.reflect.Method;
 29import javax.swing.*;
 30import javax.swing.border.*;
 31import org.gjt.sp.jedit.OperatingSystem;
 32import org.gjt.sp.util.Log;
 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 */
 45public class RolloverButton extends JButton
 46{
 47	//{{{ RolloverButton constructor
 48	/**
 49	 * Setup the border (invisible initially)
 50	 */
 51	public RolloverButton()
 52	{
 53		if(method != null)
 54		{
 55			try
 56			{
 57				method.invoke(this,new Boolean[] { Boolean.TRUE });
 58			}
 59			catch(Exception e)
 60			{
 61				Log.log(Log.ERROR,this,e);
 62			}
 63		}
 64		else
 65		{
 66			setBorder(new EtchedBorder());
 67			setBorderPainted(false);
 68			setMargin(new Insets(0,0,0,0));
 69
 70			setRequestFocusEnabled(false);
 71
 72			addMouseListener(new MouseOverHandler());
 73		}
 74	} //}}}
 75
 76	//{{{ RolloverButton constructor
 77	/**
 78	 * Setup the border (invisible initially)
 79	 */
 80	public RolloverButton(Icon icon)
 81	{
 82		this();
 83
 84		setIcon(icon);
 85	} //}}}
 86
 87	//{{{ isOpaque() method
 88	public boolean isOpaque()
 89	{
 90		return false;
 91	} //}}}
 92
 93	//{{{ setEnabled() method
 94	public void setEnabled(boolean b)
 95	{
 96		super.setEnabled(b);
 97		if(method == null)
 98		{
 99			setBorderPainted(false);
100			repaint();
101		}
102	} //}}}
103
104	//{{{ paint() method
105	public void paint(Graphics g)
106	{
107		if(method != null || isEnabled())
108			super.paint(g);
109		else
110		{
111			Graphics2D g2 = (Graphics2D)g;
112			g2.setComposite(c);
113			super.paint(g2);
114		}
115	} //}}}
116
117	//{{{ Private members
118	private static AlphaComposite c = AlphaComposite.getInstance(
119		AlphaComposite.SRC_OVER, 0.5f);
120
121	private static Method method;
122
123	static
124	{
125		/* if(OperatingSystem.hasJava14())
126		{
127			try
128			{
129				method = RolloverButton.class
130					.getMethod("setRolloverEnabled",new Class[]
131					{ boolean.class });
132				Log.log(Log.DEBUG,RolloverButton.class,
133					"Java 1.4 setRolloverEnabled() method enabled");
134			}
135			catch(Exception e)
136			{
137				Log.log(Log.ERROR,RolloverButton.class,e);
138			}
139		} */
140	} //}}}
141
142	//{{{ MouseHandler class
143	/**
144	 * Make the border visible/invisible on rollovers
145	 */
146	class MouseOverHandler extends MouseAdapter
147	{
148		public void mouseEntered(MouseEvent e)
149		{
150			if (isEnabled())
151				setBorderPainted(true);
152		}
153
154		public void mouseExited(MouseEvent e)
155		{
156			setBorderPainted(false);
157		}
158	} //}}}
159}