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 java.awt.event.ActionEvent;
16  import java.beans.PropertyChangeEvent;
17  import java.beans.PropertyChangeListener;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  
22  import com.eviware.soapui.SoapUI;
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.support.UISupport;
25  import com.eviware.soapui.support.action.SoapUIAction;
26  import com.eviware.soapui.support.action.SoapUIActionMapping;
27  import com.eviware.soapui.support.action.support.StandaloneActionMapping;
28  
29  /***
30   * Delegates a SwingAction to a SoapUIActionMapping
31   *
32   * @author ole.matzura
33   */
34  
35  public class SwingActionDelegate<T extends ModelItem> extends AbstractAction implements PropertyChangeListener, SoapUIActionMarker
36  {
37  	private final T target;
38  	private final SoapUIActionMapping<T> mapping;
39  	private Object param;
40  	public static boolean switchClassloader;
41  
42  	public SwingActionDelegate( SoapUIActionMapping<T> mapping, T target )
43  	{
44  		super( mapping.getName() );
45  		this.mapping = mapping;
46  		this.target = target;
47  		
48  		if( mapping.getDescription() != null )
49  			putValue( Action.SHORT_DESCRIPTION, mapping.getDescription() );
50  
51  		if( mapping.getIconPath() != null )
52  			putValue( Action.SMALL_ICON, UISupport.createImageIcon( mapping.getIconPath() ) );
53  
54  		if( mapping.getKeyStroke() != null )
55  			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( mapping.getKeyStroke() ));
56  
57  		setEnabled( mapping.getAction().isEnabled() );
58  		
59  		String name = mapping.getName();
60  		int ix = name.indexOf( '&' );
61  		if( ix >= 0 )
62  		{
63  			putValue( Action.NAME, name.substring( 0, ix ) + name.substring( ix+1 ) );
64  // This doesn't seem to work in Java 5:
65  //			putValue( Action.DISPLAYED_MNEMONIC_INDEX_KEY, new Integer( ix ));
66  			putValue( Action.MNEMONIC_KEY, new Integer( name.charAt( ix+1 )) );
67  		}
68  	}
69  	
70  	public SoapUIActionMapping<T> getMapping()
71  	{
72  		return mapping;
73  	}
74  
75  	public void actionPerformed(ActionEvent e)
76  	{
77  		// required by IDE plugins
78  		if( switchClassloader )
79  		{
80  			 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
81  			 Thread.currentThread().setContextClassLoader( SoapUI.class.getClassLoader() );
82  		
83  			 try
84  			 {
85  				 mapping.getAction().perform( target, param == null ? mapping.getParam() : param );
86  			 }
87  			 catch( Throwable t )
88  			 {
89  				 SoapUI.logError( t );
90  			 }
91  			 finally
92  			 {
93  				 Thread.currentThread().setContextClassLoader( contextClassLoader );
94  			 }
95  		}
96  		else
97  		{
98  			try
99  			{
100 				mapping.getAction().perform( target, mapping.getParam() );
101 			}
102 			 catch( Throwable t )
103 			 {
104 				 SoapUI.logError( t );
105 			 }
106 		}
107 	}
108 
109 	public void propertyChange(PropertyChangeEvent evt)
110 	{
111 		if( evt.getPropertyName().equals( SoapUIAction.ENABLED_PROPERTY ))
112 			setEnabled( ((Boolean)evt.getNewValue()).booleanValue() );
113 	}
114 
115 	public SoapUIAction<T> getAction()
116 	{
117 		return mapping.getAction();
118 	}
119 
120 	public T getTarget()
121 	{
122 		return target;
123 	}
124 	
125 	protected Object getParam()
126 	{
127 		return param;
128 	}
129 
130 	protected void setParam( Object param )
131 	{
132 		this.param = param;
133 	}
134 
135 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke, String iconPath )
136 	{
137 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke, iconPath ), target );
138 	}
139 	
140 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke )
141 	{
142 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke ), target );
143 	}
144 	
145 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target )
146 	{
147 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), target );
148 	}
149 	
150 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action )
151 	{
152 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), null );
153 	}
154 
155 	public static SwingActionDelegate<?> createDelegate( String soapUIActionId )
156 	{
157 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ));
158 	}
159 	
160 	public static <T extends ModelItem> SwingActionDelegate<?> createDelegate( String soapUIActionId, T target )
161 	{
162 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target );
163 	}
164 	
165 	public static <T extends ModelItem> SwingActionDelegate<?> createDelegate( String soapUIActionId, T target, String keyStroke )
166 	{
167 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke );
168 	}
169 	
170 	public static <T extends ModelItem> SwingActionDelegate<?> createDelegate( String soapUIActionId, T target, String keyStroke, String iconPath )
171 	{
172 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke, iconPath );
173 	}
174 
175 	public SoapUIAction<?> getSoapUIAction()
176 	{
177 		return getAction();
178 	}
179 }