View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
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.swing;
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.eviware.soapui.support.actions.MarkerAction;
24  import com.eviware.soapui.support.components.JXToolBar;
25  import com.jgoodies.forms.builder.ButtonBarBuilder;
26  
27  /***
28   * ActionList-related utilities
29   * 
30   * @author Ole.Matzura
31   */
32  
33  public class ActionSupport
34  {
35  	public static JPopupMenu buildPopup( ActionList actions )
36  	{
37  		if( actions == null || actions.getActionCount() == 0 )
38  			return null;
39  		
40  		JPopupMenu popup = new JPopupMenu( actions.getLabel() );
41  	   
42  	   return ActionSupport.addActions(actions, popup);
43  	}
44  	
45  	public static JMenu buildMenu( ActionList actions )
46  	{
47  		if( actions == null || actions.getActionCount() == 0 )
48  			return null;
49  		
50  		JMenu menu = new JMenu( actions.getLabel() );
51  	   
52  	   return ActionSupport.addActions(actions, menu);
53  	}
54  
55  	public static JPopupMenu addActions(ActionList actions, JPopupMenu popup)
56  	{
57  		if( actions == null || actions.getActionCount() == 0 )
58  			return popup;
59  		
60  		for (int i = 0; i < actions.getActionCount(); i++)
61  	   {
62  	   	Action action = actions.getActionAt(i);
63  	   	if( action instanceof MarkerAction )
64  	   		continue;
65  	   	
66  	   	if( action == ActionSupport.SEPARATOR_ACTION )
67  	   		popup.addSeparator();
68  			else if( action instanceof ActionSupport.ActionListAction )
69  			{
70  				ActionList actionList = ((ActionListAction)action).getActionList();
71  				if( actionList == null || actionList.getActionCount() == 0 )
72  					System.err.println( "null/empty ActionList in action " + action.getValue( Action.NAME ));
73  				else
74  					popup.add( buildMenu( actionList ));
75  			}
76  	   	else 
77  	   		popup.add( action );
78  	   }
79  	   
80  	   return popup;
81  	}
82  
83  	public static JMenu addActions(ActionList actions, JMenu menu)
84  	{
85  		if( actions == null || menu == null )
86  			return menu;
87  		
88  		for (int i = 0; i < actions.getActionCount(); i++)
89  	   {
90  	   	Action action = actions.getActionAt(i);
91  	   	
92  	   	if( action instanceof MarkerAction )
93  	   		continue;
94  	   	
95  			if( action == ActionSupport.SEPARATOR_ACTION )
96  	   		menu.addSeparator();
97  			else if( action instanceof ActionSupport.ActionListAction )
98  				menu.add( buildMenu( ((ActionListAction)action).getActionList() ));
99  	   	else
100 	   		menu.add( action );
101 	   }
102 	   
103 	   return menu;
104 	}
105 
106 	public final static Action SEPARATOR_ACTION = new AbstractAction()
107 	{
108 		public void actionPerformed(ActionEvent e)
109 		{
110 		}
111 	};
112 	
113 	public static class ActionListAction extends AbstractAction
114 	{
115 		private final ActionList actionList;
116 
117 		public ActionListAction( ActionList actionList )
118 		{
119 			this.actionList = actionList;
120 		}
121 		
122 		public ActionList getActionList()
123 		{
124 			return actionList;
125 		}
126 
127 		public void actionPerformed(ActionEvent e)
128 		{
129 			Action defaultAction = actionList.getDefaultAction();
130 			if( defaultAction != null )
131 				defaultAction.actionPerformed(  e  );
132 		}
133 	};
134 
135 	public static JPopupMenu insertActions(ActionList actions, JPopupMenu popup, int index)
136 	{
137 		for (int i = 0; i < actions.getActionCount(); i++)
138 	   {
139 	   	Action action = actions.getActionAt(i);
140 	   	if( action instanceof MarkerAction )
141 	   		continue;
142 	   	
143 			if( action == ActionSupport.SEPARATOR_ACTION )
144 	   		popup.insert( new JPopupMenu.Separator(), index+i );
145 	   	else if( action instanceof ActionSupport.ActionListAction )
146 		   	popup.insert( buildMenu( ((ActionSupport.ActionListAction)action).getActionList() ), index+i );
147 	   	else
148 	   		popup.insert( action, index+i );
149 	   }
150 	   
151 	   return popup;
152 	}
153 
154 	public static void addActions( ActionList actionList, ButtonBarBuilder builder )
155 	{
156 		for( int c = 0; c < actionList.getActionCount(); c++ )
157 		{
158 			Action action = actionList.getActionAt( c );
159 			if( action == SEPARATOR_ACTION )
160 			{
161 				builder.addUnrelatedGap();
162 			}
163 			else
164 			{
165 				if( c > 0 )
166 					builder.addRelatedGap();
167 				
168 				builder.addFixed( new JButton( action ));
169 			}
170 		}
171 	}
172 
173 	public static void addActions( ActionList actionList, JXToolBar toolbar )
174 	{
175 		for( int c = 0; c < actionList.getActionCount(); c++ )
176 		{
177 			Action action = actionList.getActionAt( c );
178 			if( action == SEPARATOR_ACTION )
179 			{
180 				toolbar.addUnrelatedGap();
181 			}
182 			else
183 			{
184 				if( c > 0 )
185 					toolbar.addRelatedGap();
186 				
187 				toolbar.addFixed( new JButton( action ));
188 			}
189 		}
190 	}
191 }