View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  	 * Builds default ActionList for specified ModelItem
33  	 * 
34  	 * @param <T> the type of ModelItem
35  	 * @param modelItem the target ModelItem
36  	 * @return the ActionList
37  	 */
38  	
39  	public static <T extends ModelItem> ActionList buildActions( T modelItem)
40  	{
41  		return buildActions( modelItem, "" );
42  	}
43  	
44  	/***
45  	 * Creates an ActionList for the specified modelItem
46  	 */
47  	
48  	public static <T extends ModelItem> ActionList buildActions( T modelItem, String suffix )
49  	{
50  		Class<?> clazz = modelItem.getClass();
51  		ActionList actions = buildActions( clazz.getSimpleName() + suffix + "Actions", modelItem);
52  		
53  		if( actions.getActionCount() == 0 )
54  		{
55  			clazz = clazz.getSuperclass();
56  			
57  			while( actions.getActionCount() == 0 && clazz != null && ModelItem.class.isAssignableFrom(clazz) )
58  			{
59  				actions = buildActions( clazz.getSimpleName() + suffix +  "Actions", modelItem);
60  				clazz = clazz.getSuperclass();
61  			}
62  		}
63  		
64  		return actions;
65  	}
66  	
67  	@SuppressWarnings( "hiding" )
68  	public static <T extends ModelItem> ActionList buildActions( String actionGroup, T modelItem )
69  	{
70  		DefaultActionList actions = new DefaultActionList();
71  
72  		SoapUIActionGroup<T> group = SoapUI.getActionRegistry().getActionGroup( actionGroup );
73  		if( group != null )
74  		{
75  			addActions( modelItem, actions, group );
76  		}
77  
78  		return actions;
79  	}
80  	
81  	/***
82  	 * Adds the specified ActionMappings to the specified ActionList for the specified modelItem 
83  	 */
84  
85  	@SuppressWarnings({ "hiding", "unchecked" })
86  	protected static <T extends ModelItem> void addActions( T modelItem, ActionList actions, SoapUIActionGroup<T> actionGroup )
87  	{
88        boolean prevWasSeparator = false;
89  		for( SoapUIActionMapping<? extends ModelItem> mapping : actionGroup.getActionMappings( modelItem ) )
90  		{
91  			if( mapping == null )
92  				continue;
93  			
94  			SoapUIActionMapping<T> actionMapping = (com.eviware.soapui.support.action.SoapUIActionMapping<T> ) mapping;
95  			SoapUIAction<T> action = ( SoapUIAction<T> ) mapping.getAction();
96  			
97           if( action != null && !action.applies(modelItem) )
98           {
99              System.out.println(action + " does not apply to " + modelItem);
100          }
101          else if( action instanceof SeperatorAction )
102 			{
103             if( !prevWasSeparator )
104             {
105                actions.addAction( ActionSupport.SEPARATOR_ACTION );
106             }
107             prevWasSeparator = true;
108 			}
109 			else if( action instanceof SoapUIActionGroupAction )
110 			{
111 				DefaultActionList subActions = new DefaultActionList( mapping.getName() );
112 				SoapUIActionGroup<T> subGroup = ((SoapUIActionGroupAction<T>)action).getActionGroup();
113 				addActions( modelItem, subActions, subGroup );
114 				ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction( subActions );
115 				actions.addAction( actionListAction);
116 				actionListAction.setEnabled( mapping.isEnabled() );
117             prevWasSeparator = false;
118 			}
119 			else if( action != null )
120 			{
121 				SwingActionDelegate<T> actionDelegate = new SwingActionDelegate<T>( actionMapping, modelItem );
122 				actions.addAction( actionDelegate );
123 				if( mapping.isDefault() )
124 					actions.setDefaultAction( actionDelegate );
125 				
126 				actionDelegate.setEnabled( mapping.isEnabled() );
127             prevWasSeparator = false;
128 			}
129 		}
130 	}
131 }