View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.action;
14  
15  import java.awt.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  import javax.swing.JButton;
20  import javax.swing.JMenu;
21  import javax.swing.JPopupMenu;
22  
23  import com.jgoodies.forms.builder.ButtonBarBuilder;
24  
25  /***
26   * ActionList-related utilities
27   * 
28   * @author Ole.Matzura
29   */
30  
31  public class ActionSupport
32  {
33  	public static JPopupMenu buildPopup( ActionList actions )
34  	{
35  		JPopupMenu popup = new JPopupMenu( actions.getLabel() );
36  	   
37  	   return ActionSupport.addActions(actions, popup);
38  	}
39  	
40  	public static JMenu buildMenu( ActionList actions )
41  	{
42  		JMenu menu = new JMenu( actions.getLabel() );
43  	   
44  	   return ActionSupport.addActions(actions, menu);
45  	}
46  
47  	public static JPopupMenu addActions(ActionList actions, JPopupMenu popup)
48  	{
49  		for (int i = 0; i < actions.getActionCount(); i++)
50  	   {
51  	   	Action action = actions.getActionAt(i);
52  	   	if( action instanceof MarkerAction )
53  	   		continue;
54  	   	
55  	   	if( action == ActionSupport.SEPARATOR_ACTION )
56  	   		popup.addSeparator();
57  			else if( action instanceof ActionSupport.ActionListAction )
58  				popup.add( buildMenu( ((ActionListAction)action).getActionList() ));
59  	   	else 
60  	   		popup.add( action );
61  	   }
62  	   
63  	   return popup;
64  	}
65  
66  	public static JMenu addActions(ActionList actions, JMenu menu)
67  	{
68  		if( actions == null || menu == null )
69  			return menu;
70  		
71  		for (int i = 0; i < actions.getActionCount(); i++)
72  	   {
73  	   	Action action = actions.getActionAt(i);
74  	   	
75  	   	if( action instanceof MarkerAction )
76  	   		continue;
77  	   	
78  			if( action == ActionSupport.SEPARATOR_ACTION )
79  	   		menu.addSeparator();
80  			else if( action instanceof ActionSupport.ActionListAction )
81  				menu.add( buildMenu( ((ActionListAction)action).getActionList() ));
82  	   	else
83  	   		menu.add( action );
84  	   }
85  	   
86  	   return menu;
87  	}
88  
89  	public final static Action SEPARATOR_ACTION = new AbstractAction()
90  	{
91  		public void actionPerformed(ActionEvent e)
92  		{
93  		}
94  	};
95  	
96  	public static class ActionListAction extends AbstractAction
97  	{
98  		private final ActionList actionList;
99  
100 		public ActionListAction( ActionList actionList )
101 		{
102 			this.actionList = actionList;
103 		}
104 		
105 		public ActionList getActionList()
106 		{
107 			return actionList;
108 		}
109 
110 		public void actionPerformed(ActionEvent e)
111 		{
112 			Action defaultAction = actionList.getDefaultAction();
113 			if( defaultAction != null )
114 				defaultAction.actionPerformed(  e  );
115 		}
116 	};
117 
118 	public static JPopupMenu insertActions(ActionList actions, JPopupMenu popup, int index)
119 	{
120 		for (int i = 0; i < actions.getActionCount(); i++)
121 	   {
122 	   	Action action = actions.getActionAt(i);
123 	   	if( action instanceof MarkerAction )
124 	   		continue;
125 	   	
126 			if( action == ActionSupport.SEPARATOR_ACTION )
127 	   		popup.insert( new JPopupMenu.Separator(), index+i );
128 	   	else if( action instanceof ActionSupport.ActionListAction )
129 		   	popup.insert( buildMenu( ((ActionSupport.ActionListAction)action).getActionList() ), index+i );
130 	   	else
131 	   		popup.insert( action, index+i );
132 	   }
133 	   
134 	   return popup;
135 	}
136 
137 	public static void addActions( ActionList actionList, ButtonBarBuilder builder )
138 	{
139 		for( int c = 0; c < actionList.getActionCount(); c++ )
140 		{
141 			Action action = actionList.getActionAt( c );
142 			if( action == SEPARATOR_ACTION )
143 			{
144 				builder.addUnrelatedGap();
145 			}
146 			else
147 			{
148 				if( c > 0 )
149 					builder.addRelatedGap();
150 				
151 				builder.addFixed( new JButton( action ));
152 			}
153 		}
154 	}
155 }