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.Color;
23  import java.awt.Graphics;
24  import java.awt.Insets;
25  import java.awt.event.MouseEvent;
26  import javax.swing.JToggleButton;
27  import org.xnap.commons.gui.action.ToggleAction;
28  
29  /***
30   * This class provides a toggelable toolbar button with an appropriate sized
31   * icon that can display a little arrow as a hint for a menu. The border of the 
32   * button is only visible when the mouse hovers over the button.
33   */
34  public class ToolBarToggleButton extends JToggleButton {
35  
36  	private boolean showBorder;
37  	private boolean showMenuHint;
38  
39  	public ToolBarToggleButton(ToggleAction action, boolean showMenuHint)
40  	{
41  		super(action);
42  
43  		this.showMenuHint = showMenuHint;
44  		
45  		setContentAreaFilled(false);
46  		setText(null);
47  		setMargin(new Insets(1, 1, 1, 1));
48  
49  		putClientProperty("hideActionText", Boolean.TRUE);
50  	}
51  
52  	/***
53  	 * Returns true, if the mouse is currently over the button.
54  	 */
55  	public boolean isMouseOver()
56  	{
57  		return showBorder;
58  	}
59  
60  	@Override
61  	protected void paintBorder(Graphics g)
62  	{
63  		if (showBorder || isSelected()) {
64  			super.paintBorder(g);
65  		}
66  	}
67  
68  	@Override
69  	protected void paintComponent(Graphics g)
70  	{
71  		super.paintComponent(g);
72  
73  		if (showMenuHint) {
74  			int w = getWidth();
75  			int h = getHeight();
76  
77  			g.setColor(Color.black);
78  
79  			// draw bottom up: yoff = -3, xoff = -8
80  			for (int i = 0; i < 4; i++) {
81  				g.drawLine(w - 8 - i, h - 3 - i, w - 8 + i, h - 3 - i);
82  			}
83  			g.drawLine(w - 8 - 4, h - 3 - 4, w - 8 + 4, h - 3 - 4);
84  		}
85  	}
86  
87  	@Override
88  	protected void processMouseEvent(MouseEvent e)
89  	{
90  		super.processMouseEvent(e);
91  		
92  		if (e.getID() == MouseEvent.MOUSE_ENTERED) {
93  			showBorder = true;
94  			setContentAreaFilled(true);
95  			repaint();
96  		}
97  		else if (e.getID() == MouseEvent.MOUSE_EXITED) {
98  			showBorder = false;
99  			setContentAreaFilled(false);
100 			repaint();
101 		}
102 	}
103 	
104 }