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.SoapUIMultiAction;
28  
29  /***
30   * Delegates a SwingAction to a SoapUIActionMapping containgin a
31   * SoapUIMultiAction
32   * 
33   * @author ole.matzura
34   */
35  
36  public class SwingMultiActionDelegate extends AbstractAction implements PropertyChangeListener, SoapUIActionMarker
37  {
38  	private final SoapUIActionMapping<?> mapping;
39  	private ModelItem[] targets;
40  
41  	public SwingMultiActionDelegate( SoapUIActionMapping<?> mapping, ModelItem[] targets )
42  	{
43  		super( mapping.getName() );
44  		this.mapping = mapping;
45  		this.targets = targets;
46  
47  		if( mapping.getDescription() != null )
48  			putValue( Action.SHORT_DESCRIPTION, mapping.getDescription() );
49  
50  		if( mapping.getIconPath() != null )
51  			putValue( Action.SMALL_ICON, UISupport.createImageIcon( mapping.getIconPath() ) );
52  
53  		if( mapping.getKeyStroke() != null )
54  			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( mapping.getKeyStroke() ) );
55  
56  		setEnabled( mapping.getAction().isEnabled() );
57  
58  		String name = mapping.getName();
59  		int ix = name.indexOf( '&' );
60  		if( ix >= 0 )
61  		{
62  			putValue( Action.NAME, name.substring( 0, ix ) + name.substring( ix + 1 ) );
63  			// This doesn't seem to work in Java 5:
64  			// putValue( Action.DISPLAYED_MNEMONIC_INDEX_KEY, new Integer( ix ));
65  			putValue( Action.MNEMONIC_KEY, new Integer( name.charAt( ix + 1 ) ) );
66  		}
67  	}
68  
69  	public SoapUIActionMapping<?> getMapping()
70  	{
71  		return mapping;
72  	}
73  
74  	public void actionPerformed( ActionEvent e )
75  	{
76  		// required by IDE plugins
77  		if( SwingActionDelegate.switchClassloader )
78  		{
79  			ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
80  			Thread.currentThread().setContextClassLoader( SoapUI.class.getClassLoader() );
81  
82  			try
83  			{
84  				( ( SoapUIMultiAction )mapping.getAction() ).perform( targets, mapping.getParam() );
85  			}
86  			catch( Throwable t )
87  			{
88  				SoapUI.logError( t );
89  			}
90  			finally
91  			{
92  				Thread.currentThread().setContextClassLoader( contextClassLoader );
93  			}
94  		}
95  		else
96  		{
97  			try
98  			{
99  				( ( SoapUIMultiAction )mapping.getAction() ).perform( targets, mapping.getParam() );
100 			}
101 			catch( Throwable t )
102 			{
103 				SoapUI.logError( t );
104 			}
105 		}
106 	}
107 
108 	public void propertyChange( PropertyChangeEvent evt )
109 	{
110 		if( evt.getPropertyName().equals( SoapUIAction.ENABLED_PROPERTY ) )
111 			setEnabled( ( ( Boolean )evt.getNewValue() ).booleanValue() );
112 	}
113 
114 	public ModelItem[] getTargets()
115 	{
116 		return targets;
117 	}
118 
119 	public SoapUIAction<?> getSoapUIAction()
120 	{
121 		return mapping.getAction();
122 	}
123 }