View Javadoc

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