View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.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  	public static boolean switchClassloader;
    40
    41  	public SwingActionDelegate( SoapUIActionMapping<T> mapping, T target )
    42  	{
    43  		super( mapping.getName() );
    44  		this.mapping = mapping;
    45  		this.target = target;
    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  //		mapping.getAction().addPropertyChangeListener( this );
    59  	}
    60
    61  	public SoapUIActionMapping<T> getMapping()
    62  	{
    63  		return mapping;
    64  	}
    65
    66  	public void actionPerformed(ActionEvent e)
    67  	{
    68  		// required by IDE plugins
    69  		if( switchClassloader )
    70  		{
    71  			 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    72  			 Thread.currentThread().setContextClassLoader( SoapUI.class.getClassLoader() );
    73
    74  			 try
    75  			 {
    76  				 mapping.getAction().perform( target, mapping.getParam() );
    77  			 }
    78  			 finally
    79  			 {
    80  				 Thread.currentThread().setContextClassLoader( contextClassLoader );
    81  			 }
    82  		}
    83  		else
    84  		{
    85  			mapping.getAction().perform( target, mapping.getParam() );
    86  		}
    87  	}
    88
    89  	public void propertyChange(PropertyChangeEvent evt)
    90  	{
    91  		if( evt.getPropertyName().equals( SoapUIAction.ENABLED_PROPERTY ))
    92  			setEnabled( ((Boolean)evt.getNewValue()).booleanValue() );
    93  	}
    94
    95  	public SoapUIAction<T> getAction()
    96  	{
    97  		return mapping.getAction();
    98  	}
    99
    100 	public T getTarget()
    101 	{
    102 		return target;
    103 	}
    104
    105 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke, String iconPath )
    106 	{
    107 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke, iconPath ), target );
    108 	}
    109
    110 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke )
    111 	{
    112 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke ), target );
    113 	}
    114
    115 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target )
    116 	{
    117 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), target );
    118 	}
    119
    120 	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action )
    121 	{
    122 		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), null );
    123 	}
    124
    125 	public static AbstractAction createDelegate( String soapUIActionId )
    126 	{
    127 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ));
    128 	}
    129
    130 	public static <T extends ModelItem> AbstractAction createDelegate( String soapUIActionId, T target )
    131 	{
    132 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target );
    133 	}
    134
    135 	public static <T extends ModelItem> AbstractAction createDelegate( String soapUIActionId, T target, String keyStroke )
    136 	{
    137 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke );
    138 	}
    139
    140 	public static <T extends ModelItem> AbstractAction createDelegate( String soapUIActionId, T target, String keyStroke, String iconPath )
    141 	{
    142 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke, iconPath );
    143 	}
    144 }