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.beans.PropertyChangeEvent;
23  import java.beans.PropertyChangeListener;
24  
25  import javax.swing.JToggleButton;
26  
27  import org.xnap.action.ToggleAction;
28  import org.xnap.gui.util.IconHelper;
29  
30  /***
31   * This class provides a toolbar button with an appropriate sized icon.
32   */
33  public class XNapToggleButton extends JToggleButton
34  {
35  
36  	//--- Constant(s) ---
37  
38  	//--- Data field(s) ---
39  
40  	//--- Constructor(s) ---
41  
42  	public XNapToggleButton(ToggleAction action)
43  	{
44  		super(action);
45  
46  		String filename = (String)action.getValue(IconHelper.XNAP_ICON);
47  		setIcon(IconHelper.getButtonIcon(filename));
48  
49  		setSelected(action.isSelected());
50  		action.addPropertyChangeListener(new SelectionListener());
51  	}
52  
53  	//--- Method(s) ---
54  
55  	//--- Inner Class(es) ---
56  
57  	private class SelectionListener implements PropertyChangeListener
58  	{
59  	
60  		public void propertyChange(PropertyChangeEvent e) 
61  		{
62  			if (e.getPropertyName().equals("selected")) {
63  				setSelected(((Boolean)e.getNewValue()).booleanValue());
64  			}
65  			else if (e.getPropertyName().equals(IconHelper.XNAP_ICON)) {
66  				String filename = (String)e.getNewValue();
67  				setIcon(IconHelper.getButtonIcon(filename));
68  				
69  			}
70  		}
71  	 
72  	}
73  	
74  }