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.action;
21  
22  import java.awt.event.ActionEvent;
23  
24  import javax.swing.Action;
25  import javax.swing.JComponent;
26  import javax.swing.JPopupMenu;
27  import javax.swing.event.PopupMenuEvent;
28  import javax.swing.event.PopupMenuListener;
29  
30  import org.xnap.action.*;
31  import org.xnap.gui.component.XNapToolBarToggleButton;
32  import org.xnap.gui.util.GUIHelper;
33  import org.xnap.gui.util.IconHelper;
34  
35  /***
36   * Provides an action that shows a panel below the button that triggered this
37   * action.
38   */
39  public class PanelAction extends AbstractToggleAction 
40  	implements ToggleAction
41  {
42      
43      //--- Constant(s) ---
44      
45      //--- Data field(s) ---
46      
47      private JComponent jc;
48  	private Object source;
49  
50      //--- Constructor(s) ---
51  
52      public PanelAction(JComponent jc, String iconFilename)
53      {
54  		this.jc = jc;
55  		
56  		putValue(Action.SHORT_DESCRIPTION, "Shows a settings panel.");
57  		putValue(IconHelper.XNAP_ICON, iconFilename);
58      }
59  
60      //--- Method(s) ---
61  
62      public void actionPerformed(ActionEvent event)
63      {
64  		source = event.getSource();
65  		super.actionPerformed(event);
66      }
67  
68      public void toggled(boolean selected)
69  	{
70  		if (selected && source instanceof JComponent) {
71  			JComponent src = (JComponent)source;
72  			JPopupMenu jp;
73  			if (jc instanceof JPopupMenu) {
74  				jp = (JPopupMenu)jc;
75  			}
76  			else {
77  				jp = new JPopupMenu();
78  				jp.add(jc);
79  			}
80  			jp.addPopupMenuListener(new PopupListener());
81  			GUIHelper.showPopupMenu(jp, src, 0, src.getHeight(), -src.getHeight());
82  		}
83  		
84  	}
85  
86      //--- Inner Class(es) ---
87      
88      private class PopupListener implements PopupMenuListener {
89  
90  		public PopupListener()
91  		{
92  		}
93  
94  		public void popupMenuCanceled(PopupMenuEvent e) 
95  		{
96  		}
97  
98  		public void popupMenuWillBecomeInvisible(PopupMenuEvent e) 
99  		{
100 			if (source instanceof XNapToolBarToggleButton
101 				&& ((XNapToolBarToggleButton)source).isMouseOver()) {
102 				// the setSelected(false) will be triggered by the button
103 				// if we call it, the button will call setSelected(true)
104 				// instead and a new PopupMenu would be shown
105 				return;
106 			}
107 			((JPopupMenu)e.getSource()).removePopupMenuListener(this);
108 			PanelAction.this.setSelected(false);
109 		}
110 
111 		public void popupMenuWillBecomeVisible(PopupMenuEvent e) 
112 		{
113 			PanelAction.this.setSelected(true);
114 		}
115     }
116 
117 }
118