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