View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui;
21  
22  import java.awt.Graphics;
23  import java.awt.Insets;
24  import java.awt.event.MouseEvent;
25  import javax.swing.Action;
26  import javax.swing.JButton;
27  
28  /***
29   * This class provides a toolbar button with an appropriately sized icon.
30   * The border of the button is only visible when the mouse hovers over the 
31   * button.
32   */
33  public class ToolBarButton extends JButton {
34  
35      private boolean showBorder = false;
36      private boolean showMenuHint;
37  
38      public ToolBarButton(Action action)
39      {
40  		super(action);
41  
42  		setContentAreaFilled(false);
43  		setText(null);
44  		setMargin(new Insets(1, 1, 1, 1));
45  
46  		putClientProperty("hideActionText", Boolean.TRUE);
47      }
48  
49  	/***
50  	 * Returns true, if the mouse is currently over the button.
51  	 */
52  	public boolean isMouseOver()
53  	{
54  		return showBorder;
55  	}
56  
57      @Override
58      protected void paintBorder(Graphics g)
59      {
60  		if (showBorder) {
61  			super.paintBorder(g);
62  		}
63      }
64  
65      @Override
66  	protected void processMouseEvent(MouseEvent e)
67  	{
68  		super.processMouseEvent(e);
69  		
70  		if (e.getID() == MouseEvent.MOUSE_ENTERED) {
71  			showBorder = true;
72  			setContentAreaFilled(true);
73  			repaint();
74  		}
75  		else if (e.getID() == MouseEvent.MOUSE_EXITED) {
76  			showBorder = false;
77  			setContentAreaFilled(false);
78  			repaint();
79  		}
80  	}
81  	
82  }