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 com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.model.ModelItem;
17  import com.eviware.soapui.support.action.SoapUIAction;
18  import com.eviware.soapui.support.action.SoapUIActionGroup;
19  import com.eviware.soapui.support.action.SoapUIActionMapping;
20  import com.eviware.soapui.support.action.SoapUIActionRegistry.SeperatorAction;
21  import com.eviware.soapui.support.action.SoapUIActionRegistry.SoapUIActionGroupAction;
22  
23  /***
24   * Builder for ActionLists for a variety of targets
25   *  
26   * @author ole.matzura
27   */
28  
29  public class ActionListBuilder
30  {
31  	/***
32  	 * Creates an ActionList for the specified modelItem
33  	 */
34  	
35  	public static <T extends ModelItem> ActionList buildActions( T modelItem )
36  	{
37  		Class<?> clazz = modelItem.getClass();
38  		ActionList actions = buildActions( clazz.getSimpleName() + "Actions", modelItem);
39  		
40  		if( actions.getActionCount() == 0 )
41  		{
42  			clazz = clazz.getSuperclass();
43  			
44  			while( actions.getActionCount() == 0 && clazz != null && ModelItem.class.isAssignableFrom(clazz) )
45  			{
46  				actions = buildActions( clazz.getSimpleName() + "Actions", modelItem);
47  				clazz = clazz.getSuperclass();
48  			}
49  		}
50  		
51  		return actions;
52  	}
53  	
54  	@SuppressWarnings( "hiding" )
55  	public static <T extends ModelItem> ActionList buildActions( String actionGroup, T modelItem )
56  	{
57  		DefaultActionList actions = new DefaultActionList();
58  
59  		SoapUIActionGroup<T> group = SoapUI.getActionRegistry().getActionGroup( actionGroup );
60  		if( group != null )
61  		{
62  			addActions( modelItem, actions, group );
63  		}
64  
65  		return actions;
66  	}
67  	
68  	/***
69  	 * Adds the specified ActionMappings to the specified ActionList for the specified modelItem 
70  	 */
71  
72  	@SuppressWarnings({ "hiding", "unchecked" })
73  	protected static <T extends ModelItem> void addActions( T modelItem, ActionList actions, SoapUIActionGroup<T> actionGroup )
74  	{
75        boolean prevWasSeparator = false;
76  		for( SoapUIActionMapping<? extends ModelItem> mapping : actionGroup.getActionMappings( modelItem ) )
77  		{
78  			SoapUIActionMapping<T> actionMapping = (com.eviware.soapui.support.action.SoapUIActionMapping<T> ) mapping;
79  			SoapUIAction<T> action = ( SoapUIAction<T> ) mapping.getAction();
80  			
81           if( !action.applies(modelItem) )
82           {
83              System.out.println(action + " does not apply to " + modelItem);
84           }
85           else if( action instanceof SeperatorAction )
86  			{
87              if( !prevWasSeparator )
88              {
89                 actions.addAction( ActionSupport.SEPARATOR_ACTION );
90              }
91              prevWasSeparator = true;
92  			}
93  			else if( action instanceof SoapUIActionGroupAction )
94  			{
95  				DefaultActionList subActions = new DefaultActionList( mapping.getName() );
96  				SoapUIActionGroup<T> subGroup = ((SoapUIActionGroupAction<T>)action).getActionGroup();
97  				addActions( modelItem, subActions, subGroup );
98  				ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction( subActions );
99  				actions.addAction( actionListAction);
100 				actionListAction.setEnabled( mapping.isEnabled() );
101             prevWasSeparator = false;
102 			}
103 			else if( action != null )
104 			{
105 				SwingActionDelegate<T> actionDelegate = new SwingActionDelegate<T>( actionMapping, modelItem );
106 				actions.addAction( actionDelegate );
107 				if( mapping.isDefault() )
108 					actions.setDefaultAction( actionDelegate );
109 				
110 				actionDelegate.setEnabled( mapping.isEnabled() );
111             prevWasSeparator = false;
112 			}
113 		}
114 	}
115 }