View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.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.action.SoapUIAction;
28  import com.eviware.soapui.support.action.SoapUIActionMapping;
29  import com.eviware.soapui.support.action.SoapUIMultiAction;
30  
31  /***
32   * Delegates a SwingAction to a SoapUIActionMapping containgin a
33   * SoapUIMultiAction
34   * 
35   * @author ole.matzura
36   */
37  
38  public class SwingMultiActionDelegate extends AbstractAction implements PropertyChangeListener, SoapUIActionMarker
39  {
40  	private final SoapUIActionMapping<?> mapping;
41  	private ModelItem[] targets;
42  
43  	public SwingMultiActionDelegate( SoapUIActionMapping<?> mapping, ModelItem[] targets )
44  	{
45  		super( mapping.getName() );
46  		this.mapping = mapping;
47  		this.targets = targets;
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<?> getMapping()
72  	{
73  		return mapping;
74  	}
75  
76  	public void actionPerformed( ActionEvent e )
77  	{
78  		// required by IDE plugins
79  		if( SwingActionDelegate.switchClassloader )
80  		{
81  			SoapUIClassLoaderState state = SoapUIExtensionClassLoader.ensure();
82  
83  			try
84  			{
85  				( ( SoapUIMultiAction )mapping.getAction() ).perform( targets, mapping.getParam() );
86  			}
87  			catch( Throwable t )
88  			{
89  				SoapUI.logError( t );
90  			}
91  			finally
92  			{
93  				state.restore();
94  			}
95  		}
96  		else
97  		{
98  			try
99  			{
100 				( ( SoapUIMultiAction )mapping.getAction() ).perform( targets, 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 ModelItem[] getTargets()
116 	{
117 		return targets;
118 	}
119 
120 	public SoapUIAction<?> getSoapUIAction()
121 	{
122 		return mapping.getAction();
123 	}
124 }