1
2
3
4
5
6
7
8
9
10
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 @SuppressWarnings( "hiding" )
75 public static <T extends ModelItem> ActionList buildActions( String actionGroup, T modelItem )
76 {
77 DefaultActionList actions = new DefaultActionList();
78
79 SoapUIActionGroup<T> group = SoapUI.getActionRegistry().getActionGroup( actionGroup );
80 if( group != null )
81 {
82 addActions( modelItem, actions, group );
83 }
84
85 return actions;
86 }
87
88 /***
89 * Adds the specified ActionMappings to the specified ActionList for the
90 * specified modelItem
91 */
92
93 @SuppressWarnings( { "hiding", "unchecked" } )
94 protected static <T extends ModelItem> void addActions( T modelItem, ActionList actions,
95 SoapUIActionGroup<T> actionGroup )
96 {
97 boolean prevWasSeparator = false;
98 for( SoapUIActionMapping<? extends ModelItem> mapping : actionGroup.getActionMappings( modelItem ) )
99 {
100 if( mapping == null )
101 continue;
102
103 SoapUIActionMapping<T> actionMapping = ( com.eviware.soapui.support.action.SoapUIActionMapping<T> )mapping;
104 SoapUIAction<T> action = ( SoapUIAction<T> )mapping.getAction();
105
106 if( action != null && !action.applies( modelItem ) )
107 {
108 System.out.println( action + " does not apply to " + modelItem );
109 }
110 else if( action instanceof SeperatorAction )
111 {
112 if( !prevWasSeparator )
113 {
114 actions.addAction( ActionSupport.SEPARATOR_ACTION );
115 }
116 prevWasSeparator = true;
117 }
118 else if( action instanceof SoapUIActionGroupAction )
119 {
120 DefaultActionList subActions = new DefaultActionList( mapping.getName() );
121 SoapUIActionGroup<T> subGroup = ( ( SoapUIActionGroupAction<T> )action ).getActionGroup();
122 addActions( modelItem, subActions, subGroup );
123 ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction( subActions );
124 actions.addAction( actionListAction );
125 actionListAction.setEnabled( mapping.isEnabled() );
126 prevWasSeparator = false;
127 }
128 else if( action != null )
129 {
130 SwingActionDelegate<T> actionDelegate = new SwingActionDelegate<T>( actionMapping, modelItem );
131 actions.addAction( actionDelegate );
132 if( mapping.isDefault() )
133 actions.setDefaultAction( actionDelegate );
134
135 actionDelegate.setEnabled( mapping.isEnabled() );
136 prevWasSeparator = false;
137 }
138 }
139 }
140
141 public static ActionList buildMultiActions( ModelItem[] modelItems )
142 {
143 DefaultActionList actions = new DefaultActionList();
144
145 SoapUIActionGroup<?> group = SoapUI.getActionRegistry().getActionGroup( "SoapUIMultiActions" );
146 if( group != null )
147 {
148 addMultiActions( modelItems, actions, group );
149 }
150
151 return actions;
152 }
153
154 /***
155 * Adds the specified ActionMappings to the specified ActionList for the
156 * specified modelItem
157 */
158
159 @SuppressWarnings( { "unchecked" } )
160 protected static void addMultiActions( ModelItem[] modelItems, ActionList actions, SoapUIActionGroup actionGroup )
161 {
162 boolean prevWasSeparator = false;
163 SoapUIActionMappingList actionMappings = actionGroup.getActionMappings( null );
164 for( int c = 0; c < actionMappings.size(); c++ )
165 {
166 SoapUIActionMapping mapping = ( SoapUIActionMapping )actionMappings.get( c );
167 if( mapping == null )
168 continue;
169
170 SoapUIAction action = mapping.getAction();
171
172 if( action instanceof SeperatorAction )
173 {
174 if( !prevWasSeparator )
175 {
176 actions.addAction( ActionSupport.SEPARATOR_ACTION );
177 }
178 prevWasSeparator = true;
179 }
180 else if( action instanceof SoapUIActionGroupAction )
181 {
182 DefaultActionList subActions = new DefaultActionList( mapping.getName() );
183 SoapUIActionGroup subGroup = ( ( SoapUIActionGroupAction )action ).getActionGroup();
184 addMultiActions( modelItems, subActions, subGroup );
185 ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction( subActions );
186 actions.addAction( actionListAction );
187 actionListAction.setEnabled( mapping.isEnabled() );
188 prevWasSeparator = false;
189 }
190 else if( action instanceof SoapUIMultiAction )
191 {
192 List<ModelItem> targets = new ArrayList<ModelItem>();
193 for( ModelItem target : modelItems )
194 {
195 if( action.applies( target ) )
196 {
197 targets.add( target );
198 }
199 }
200
201 if( targets.size() > 0 )
202 {
203 SwingMultiActionDelegate actionDelegate = new SwingMultiActionDelegate( mapping, modelItems );
204 actions.addAction( actionDelegate );
205 if( mapping.isDefault() )
206 actions.setDefaultAction( actionDelegate );
207
208 actionDelegate.setEnabled( mapping.isEnabled() );
209 prevWasSeparator = false;
210 }
211 }
212 }
213 }
214 }