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.action;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import javax.swing.Action;
25  import javax.swing.JPopupMenu;
26  import org.xnap.commons.gui.Builder;
27  
28  /***
29   * 
30   * @author Steffen Pingel
31   */
32  public class ActionCollection {
33  	
34  	List<Action> actions = new ArrayList<Action>();
35  	
36  	public ActionCollection() 
37  	{
38  	}
39  
40  	public void add(Action action)
41  	{
42  		actions.add(action);
43  	}
44  
45  	public void addSeparator()
46  	{
47  		actions.add(null);
48  	}
49  
50  	public JPopupMenu createPopupMenu()
51  	{
52  		JPopupMenu menu = new JPopupMenu();
53  		for (Action action : actions) {
54  			if (action != null) {
55  				menu.add(Builder.createMenuItem(action));
56  			}
57  			else {
58  				menu.addSeparator();
59  			}
60  		}
61  		return menu;
62  	}
63  	
64  	public Action[] getActions()
65  	{
66  		List<Action> result = new ArrayList<Action>(actions.size());
67  		for (Action action : result) {
68  			if (action != null) {
69  				result.add(action);
70  			}
71  		}
72  		return result.toArray(new Action[0]);
73  	}
74  	
75  	public void setSelectedItems(Object[] items)
76  	{
77  		for (Action action : actions) {
78  			if (action instanceof SelectionAction) {
79  				((SelectionAction)action).setSelectedItems(items);
80  			}
81  		}
82  	}
83  	
84  	public boolean isEmpty()
85  	{
86  		return actions.isEmpty();
87  	}
88  	
89  }