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.menu;
21  
22  import java.awt.Component;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  
26  import javax.swing.Action;
27  import javax.swing.JLabel;
28  import javax.swing.JMenu;
29  import javax.swing.event.PopupMenuEvent;
30  import javax.swing.event.PopupMenuListener;
31  
32  import org.xnap.gui.util.IconHelper;
33  
34  public abstract class AbstractDynamicMenu extends JMenu
35  {
36      
37      //--- Constant(s) ---
38      
39      //--- Data field(s) ---
40      
41      private LinkedList temporaries = new LinkedList();
42      
43      //--- Constructor(s) ---
44  
45      public AbstractDynamicMenu(String name)
46      {
47  		super(name);
48  
49  		initialize();
50      }
51  
52  	public AbstractDynamicMenu(Action action)
53  	{
54  		super(action);
55  
56  		String filename = (String)action.getValue(IconHelper.XNAP_ICON);
57  		setIcon(IconHelper.getMenuIcon(filename));
58  
59  		initialize();
60  	}
61  
62      //--- Method(s) ---
63  
64  	private void initialize()
65  	{
66  		getPopupMenu().addPopupMenuListener(new PopupListener());
67  	}
68  
69      /***
70       * Adds <code>c</code>. 
71       *
72       * @see #removeAllTemporaries()
73       */
74      public void addTemporary(Component c)
75      {
76  		add(c);
77  		temporaries.add(c);
78      }
79  
80      public void addTemporary(String s)
81  	{
82  		addTemporary(new JLabel(" " + s));
83  	}
84  
85      /***
86       * Adds <code>c</code>. 
87       *
88       * @see #removeAllTemporaries()
89       */
90      public void addTemporary(Component c, int index)
91      {
92  		add(c, index);
93  		temporaries.add(c);
94      }
95  
96      /***
97       * Removes all components that were added by
98       * {@link #addTemporary(Component)}.
99       */
100     public void removeAllTemporaries()
101     {
102 		for (Iterator i = temporaries.iterator(); i.hasNext();) {
103 			remove((Component)i.next());
104 		}
105 		temporaries.clear();
106     }
107 
108     protected abstract void willBecomeVisible();
109 
110     //--- Inner Class(es) ---
111     
112     protected class PopupListener implements PopupMenuListener {
113 
114 		public void popupMenuCanceled(PopupMenuEvent e) 
115 		{
116 		}
117 
118 		public void popupMenuWillBecomeInvisible(PopupMenuEvent e) 
119 		{
120 		}
121 
122 		public void popupMenuWillBecomeVisible(PopupMenuEvent e) 
123 		{
124 			willBecomeVisible();
125 		}
126     }
127 
128 }