1
2
3
4
5
6
7
8
9
10
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.WeakPropertyChangeListener;
26 import com.eviware.soapui.support.action.SoapUIAction;
27 import com.eviware.soapui.support.action.SoapUIActionMapping;
28 import com.eviware.soapui.support.action.support.StandaloneActionMapping;
29
30 /***
31 * Delegates a SwingAction to a SoapUIActionMapping
32 *
33 * @author ole.matzura
34 */
35
36 public class SwingActionDelegate<T extends ModelItem> extends AbstractAction implements PropertyChangeListener,
37 SoapUIActionMarker
38 {
39 private final T target;
40 private final SoapUIActionMapping<T> mapping;
41 private Object param;
42 public static boolean switchClassloader;
43
44 public SwingActionDelegate( SoapUIActionMapping<T> mapping, T target )
45 {
46 super( mapping.getName() );
47 this.mapping = mapping;
48 this.target = target;
49
50 if( mapping.getDescription() != null )
51 putValue( Action.SHORT_DESCRIPTION, mapping.getDescription() );
52
53 if( mapping.getIconPath() != null )
54 putValue( Action.SMALL_ICON, UISupport.createImageIcon( mapping.getIconPath() ) );
55
56 if( mapping.getKeyStroke() != null )
57 putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( mapping.getKeyStroke() ) );
58
59 setEnabled( mapping.getAction().isEnabled() );
60
61 mapping.getAction().addPropertyChangeListener( new WeakPropertyChangeListener( this, mapping.getAction() ));
62
63 String name = mapping.getName();
64 int ix = name.indexOf( '&' );
65 if( ix >= 0 )
66 {
67 putValue( Action.NAME, name.substring( 0, ix ) + name.substring( ix + 1 ) );
68
69
70 putValue( Action.MNEMONIC_KEY, new Integer( name.charAt( ix + 1 ) ) );
71 }
72 }
73
74 public SoapUIActionMapping<T> getMapping()
75 {
76 return mapping;
77 }
78
79 public void actionPerformed( ActionEvent e )
80 {
81
82 if( switchClassloader )
83 {
84 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
85 Thread.currentThread().setContextClassLoader( SoapUI.class.getClassLoader() );
86
87 try
88 {
89 mapping.getAction().perform( target, param == null ? mapping.getParam() : param );
90 }
91 catch( Throwable t )
92 {
93 SoapUI.logError( t );
94 }
95 finally
96 {
97 Thread.currentThread().setContextClassLoader( contextClassLoader );
98 }
99 }
100 else
101 {
102 try
103 {
104 mapping.getAction().perform( target, mapping.getParam() );
105 }
106 catch( Throwable t )
107 {
108 SoapUI.logError( t );
109 }
110 }
111 }
112
113 public void propertyChange( PropertyChangeEvent evt )
114 {
115 if( evt.getPropertyName().equals( SoapUIAction.ENABLED_PROPERTY ) )
116 setEnabled( ( ( Boolean )evt.getNewValue() ).booleanValue() );
117 }
118
119 public SoapUIAction<T> getAction()
120 {
121 return mapping.getAction();
122 }
123
124 public T getTarget()
125 {
126 return target;
127 }
128
129 protected Object getParam()
130 {
131 return param;
132 }
133
134 protected void setParam( Object param )
135 {
136 this.param = param;
137 }
138
139 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target,
140 String keyStroke, String iconPath )
141 {
142 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke, iconPath ), target );
143 }
144
145 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target,
146 String keyStroke )
147 {
148 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke ), target );
149 }
150
151 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target )
152 {
153 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), target );
154 }
155
156 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action )
157 {
158 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), null );
159 }
160
161 public static SwingActionDelegate<?> createDelegate( String soapUIActionId )
162 {
163 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ) );
164 }
165
166 public static <T extends ModelItem> SwingActionDelegate<?> createDelegate( String soapUIActionId, T target )
167 {
168 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target );
169 }
170
171 public static <T extends ModelItem> SwingActionDelegate<?> createDelegate( String soapUIActionId, T target,
172 String keyStroke )
173 {
174 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke );
175 }
176
177 public static <T extends ModelItem> SwingActionDelegate<?> createDelegate( String soapUIActionId, T target,
178 String keyStroke, String iconPath )
179 {
180 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke, iconPath );
181 }
182
183 public SoapUIAction<?> getSoapUIAction()
184 {
185 return getAction();
186 }
187
188 public static void invoke( Runnable action )
189 {
190
191 if( switchClassloader )
192 {
193 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
194 Thread.currentThread().setContextClassLoader( SoapUI.class.getClassLoader() );
195
196 try
197 {
198 action.run();
199 }
200 catch( Throwable t )
201 {
202 SoapUI.logError( t );
203 }
204 finally
205 {
206 Thread.currentThread().setContextClassLoader( contextClassLoader );
207 }
208 }
209 else
210 {
211 try
212 {
213 action.run();
214 }
215 catch( Throwable t )
216 {
217 SoapUI.logError( t );
218 }
219 }
220
221 }
222 }