View Javadoc
1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.component;
21  
22  import java.awt.Color;
23  import java.awt.Graphics;
24  import java.awt.Insets;
25  import java.awt.event.MouseEvent;
26  import java.beans.PropertyChangeEvent;
27  import java.beans.PropertyChangeListener;
28  
29  import javax.swing.JToggleButton;
30  
31  import org.xnap.action.ToggleAction;
32  import org.xnap.gui.action.PanelAction;
33  import org.xnap.gui.util.IconHelper;
34  
35  /***
36   * This class provides a toolbar button with an appropriate sized icon.
37   */
38  public class XNapToolBarToggleButton extends JToggleButton
39  {
40  
41  	//--- Constant(s) ---
42  
43  	//--- Data field(s) ---
44  
45  	private boolean showBorder = false;
46  	private boolean showMenuHint;
47  
48  	//--- Constructor(s) ---
49  
50  	public XNapToolBarToggleButton(ToggleAction action)
51  	{
52  		super(action);
53  
54  		String filename = (String)action.getValue(IconHelper.XNAP_ICON);
55  		setIcon(IconHelper.getToolBarIcon(filename));
56  
57  		setText(null);
58  		setMargin(new Insets(1, 1, 1, 1));
59  
60  		setSelected(action.isSelected());
61  		action.addPropertyChangeListener(new SelectionListener());
62  
63  		showMenuHint = (action instanceof PanelAction);
64  	}
65  
66  	//--- Method(s) ---
67  
68  	/***
69  	 * Needed by {@link PanelAction}
70  	 */
71  	public boolean isMouseOver()
72  	{
73  		return showBorder;
74  	}
75  
76  	protected void paintBorder(Graphics g)
77  	{
78  		if (showBorder || isSelected()) {
79  			super.paintBorder(g);
80  		}
81  	}
82  
83  	protected void paintComponent(Graphics g)
84  	{
85  		super.paintComponent(g);
86  
87  		if (showMenuHint) {
88  			int w = getWidth();
89  			int h = getHeight();
90  
91  			g.setColor(Color.black);
92  
93  			// draw bottom up: yoff = -3, xoff = -8
94  			for (int i = 0; i < 4; i++) {
95  				g.drawLine(w - 8 - i, h - 3 - i, w - 8 + i, h - 3 - i);
96  			}
97  			g.drawLine(w - 8 - 4, h - 3 - 4, w - 8 + 4, h - 3 - 4);
98  		}
99  	}
100 
101 	protected void processMouseEvent(MouseEvent e)
102 	{
103 		super.processMouseEvent(e);
104 		
105 		if (e.getID() == MouseEvent.MOUSE_ENTERED) {
106 			showBorder = true;
107 					repaint();
108 		}
109 		else if (e.getID() == MouseEvent.MOUSE_EXITED) {
110 			showBorder = false;
111 			repaint();
112 		}
113 	}
114 
115 	//--- Inner Class(es) ---
116 
117 	private class SelectionListener implements PropertyChangeListener
118 	{
119 	
120 		public void propertyChange(PropertyChangeEvent e) 
121 		{
122 			if (e.getPropertyName().equals("selected")) {
123 				setSelected(((Boolean)e.getNewValue()).booleanValue());
124 			}
125 		}
126 	 
127 	}
128 	
129 }