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.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 {
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
60 }
61
62 public SoapUIActionMapping<T> getMapping()
63 {
64 return mapping;
65 }
66
67 public void actionPerformed(ActionEvent e)
68 {
69
70 if( switchClassloader )
71 {
72 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
73 Thread.currentThread().setContextClassLoader( SoapUI.class.getClassLoader() );
74
75 try
76 {
77 mapping.getAction().perform( target, param == null ? mapping.getParam() : param );
78 }
79 catch( Throwable t )
80 {
81 SoapUI.logError( t );
82 }
83 finally
84 {
85 Thread.currentThread().setContextClassLoader( contextClassLoader );
86 }
87 }
88 else
89 {
90 try
91 {
92 mapping.getAction().perform( target, mapping.getParam() );
93 }
94 catch( Throwable t )
95 {
96 SoapUI.logError( t );
97 }
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, String keyStroke, String iconPath )
128 {
129 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke, iconPath ), target );
130 }
131
132 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke )
133 {
134 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke ), target );
135 }
136
137 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target )
138 {
139 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), target );
140 }
141
142 public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action )
143 {
144 return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), null );
145 }
146
147 public static SwingActionDelegate createDelegate( String soapUIActionId )
148 {
149 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ));
150 }
151
152 public static <T extends ModelItem> SwingActionDelegate createDelegate( String soapUIActionId, T target )
153 {
154 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target );
155 }
156
157 public static <T extends ModelItem> SwingActionDelegate createDelegate( String soapUIActionId, T target, String keyStroke )
158 {
159 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke );
160 }
161
162 public static <T extends ModelItem> SwingActionDelegate createDelegate( String soapUIActionId, T target, String keyStroke, String iconPath )
163 {
164 return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke, iconPath );
165 }
166 }