View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.util.ArrayList;
16  import java.util.List;
17  
18  import com.eviware.soapui.SoapUI;
19  import com.eviware.soapui.model.ModelItem;
20  import com.eviware.soapui.support.action.SoapUIAction;
21  import com.eviware.soapui.support.action.SoapUIActionGroup;
22  import com.eviware.soapui.support.action.SoapUIActionMapping;
23  import com.eviware.soapui.support.action.SoapUIMultiAction;
24  import com.eviware.soapui.support.action.SoapUIActionRegistry.SeperatorAction;
25  import com.eviware.soapui.support.action.SoapUIActionRegistry.SoapUIActionGroupAction;
26  import com.eviware.soapui.support.action.support.SoapUIActionMappingList;
27  
28  /***
29   * Builder for ActionLists for a variety of targets
30   * 
31   * @author ole.matzura
32   */
33  
34  public class ActionListBuilder
35  {
36  	/***
37  	 * Builds default ActionList for specified ModelItem
38  	 * 
39  	 * @param <T>
40  	 *           the type of ModelItem
41  	 * @param modelItem
42  	 *           the target ModelItem
43  	 * @return the ActionList
44  	 */
45  
46  	public static <T extends ModelItem> ActionList buildActions( T modelItem )
47  	{
48  		return buildActions( modelItem, "" );
49  	}
50  
51  	/***
52  	 * Creates an ActionList for the specified modelItem
53  	 */
54  
55  	public static <T extends ModelItem> ActionList buildActions( T modelItem, String suffix )
56  	{
57  		Class<?> clazz = modelItem.getClass();
58  		ActionList actions = buildActions( clazz.getSimpleName() + suffix + "Actions", modelItem );
59  
60  		if( actions.getActionCount() == 0 )
61  		{
62  			clazz = clazz.getSuperclass();
63  
64  			while( actions.getActionCount() == 0 && clazz != null && ModelItem.class.isAssignableFrom( clazz ) )
65  			{
66  				actions = buildActions( clazz.getSimpleName() + suffix + "Actions", modelItem );
67  				clazz = clazz.getSuperclass();
68  			}
69  		}
70  
71  		return actions;
72  	}
73  
74  	public static <T extends ModelItem> ActionList buildActions( String actionGroup, T modelItem )
75  	{
76  		DefaultActionList actions = new DefaultActionList();
77  
78  		SoapUIActionGroup<T> group = SoapUI.getActionRegistry().getActionGroup( actionGroup );
79  		if( group != null )
80  		{
81  			addActions( modelItem, actions, group );
82  		}
83  
84  		return actions;
85  	}
86  
87  	/***
88  	 * Adds the specified ActionMappings to the specified ActionList for the
89  	 * specified modelItem
90  	 */
91  
92  	@SuppressWarnings( { "unchecked" } )
93  	protected static <T extends ModelItem> void addActions( T modelItem, ActionList actions,
94  			SoapUIActionGroup<T> actionGroup )
95  	{
96  		boolean prevWasSeparator = false;
97  		for( SoapUIActionMapping<? extends ModelItem> mapping : actionGroup.getActionMappings( modelItem ) )
98  		{
99  			if( mapping == null )
100 				continue;
101 
102 			SoapUIActionMapping<T> actionMapping = ( com.eviware.soapui.support.action.SoapUIActionMapping<T> )mapping;
103 			SoapUIAction<T> action = ( SoapUIAction<T> )mapping.getAction();
104 
105 			if( action != null && !action.applies( modelItem ) )
106 			{
107 				System.out.println( action + " does not apply to " + modelItem );
108 			}
109 			else if( action instanceof SeperatorAction )
110 			{
111 				if( !prevWasSeparator )
112 				{
113 					actions.addAction( ActionSupport.SEPARATOR_ACTION );
114 				}
115 				prevWasSeparator = true;
116 			}
117 			else if( action instanceof SoapUIActionGroupAction )
118 			{
119 				DefaultActionList subActions = new DefaultActionList( mapping.getName() );
120 				SoapUIActionGroup<T> subGroup = ( ( SoapUIActionGroupAction<T> )action ).getActionGroup();
121 				addActions( modelItem, subActions, subGroup );
122 				ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction( subActions );
123 				actions.addAction( actionListAction );
124 				actionListAction.setEnabled( mapping.isEnabled() );
125 				prevWasSeparator = false;
126 			}
127 			else if( action != null )
128 			{
129 				SwingActionDelegate<T> actionDelegate = new SwingActionDelegate<T>( actionMapping, modelItem );
130 				actions.addAction( actionDelegate );
131 				if( mapping.isDefault() )
132 					actions.setDefaultAction( actionDelegate );
133 
134 				actionDelegate.setEnabled( mapping.isEnabled() );
135 				prevWasSeparator = false;
136 			}
137 		}
138 	}
139 	
140 	public static ActionList buildMultiActions( ModelItem[] modelItems )
141 	{
142 		DefaultActionList actions = new DefaultActionList();
143 
144 		SoapUIActionGroup<?> group = SoapUI.getActionRegistry().getActionGroup( "SoapUIMultiActions" );
145 		if( group != null )
146 		{
147 			addMultiActions( modelItems, actions, group );
148 		}
149 
150 		return actions;
151 	}
152 	
153 	/***
154 	 * Adds the specified ActionMappings to the specified ActionList for the
155 	 * specified modelItem
156 	 */
157 
158 	@SuppressWarnings( { "unchecked" } )
159 	protected static void addMultiActions( ModelItem[] modelItems, ActionList actions, SoapUIActionGroup actionGroup )
160 	{
161 		boolean prevWasSeparator = false;
162 		SoapUIActionMappingList actionMappings = actionGroup.getActionMappings( null );
163 		for( int c = 0; c < actionMappings.size(); c++ )
164 		{
165 			SoapUIActionMapping mapping = ( SoapUIActionMapping )actionMappings.get( c );
166 			if( mapping == null )
167 				continue;
168 
169 			SoapUIAction action = mapping.getAction();
170 
171 			if( action instanceof SeperatorAction )
172 			{
173 				if( !prevWasSeparator )
174 				{
175 					actions.addAction( ActionSupport.SEPARATOR_ACTION );
176 				}
177 				prevWasSeparator = true;
178 			}
179 			else if( action instanceof SoapUIActionGroupAction )
180 			{
181 				DefaultActionList subActions = new DefaultActionList( mapping.getName() );
182 				SoapUIActionGroup subGroup = ( ( SoapUIActionGroupAction )action ).getActionGroup();
183 				addMultiActions( modelItems, subActions, subGroup );
184 				ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction( subActions );
185 				actions.addAction( actionListAction );
186 				actionListAction.setEnabled( mapping.isEnabled() );
187 				prevWasSeparator = false;
188 			}
189 			else if( action instanceof SoapUIMultiAction )
190 			{
191 				List<ModelItem> targets = new ArrayList<ModelItem>();
192 				for( ModelItem target : modelItems )
193 				{
194 					if( action.applies( target ) )
195 					{
196 						targets.add( target );
197 					}
198 				}
199 
200 				if( targets.size() > 0 )
201 				{
202 					SwingMultiActionDelegate actionDelegate = new SwingMultiActionDelegate( mapping, modelItems );
203 					actions.addAction( actionDelegate );
204 					if( mapping.isDefault() )
205 						actions.setDefaultAction( actionDelegate );
206 
207 					actionDelegate.setEnabled( mapping.isEnabled() );
208 					prevWasSeparator = false;
209 				}
210 			}
211 		}
212 	}
213 }