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  
40  	public SwingActionDelegate( SoapUIActionMapping<T> mapping, T target )
41  	{
42  		super( mapping.getName() );
43  		this.mapping = mapping;
44  		this.target = target;
45  		
46  		if( mapping.getDescription() != null )
47  			putValue( Action.SHORT_DESCRIPTION, mapping.getDescription() );
48  
49  		if( mapping.getIconPath() != null )
50  			putValue( Action.SMALL_ICON, UISupport.createImageIcon( mapping.getIconPath() ) );
51  
52  		if( mapping.getKeyStroke() != null )
53  			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( mapping.getKeyStroke() ));
54  
55  		setEnabled( mapping.getAction().isEnabled() );
56  		
57  //		mapping.getAction().addPropertyChangeListener( this );
58  	}
59  	
60  	public void actionPerformed(ActionEvent e)
61  	{
62  		mapping.getAction().perform( target, mapping.getParam() );
63  	}
64  
65  	public void propertyChange(PropertyChangeEvent evt)
66  	{
67  		if( evt.getPropertyName().equals( SoapUIAction.ENABLED_PROPERTY ))
68  			setEnabled( ((Boolean)evt.getNewValue()).booleanValue() );
69  	}
70  
71  	public SoapUIAction<T> getAction()
72  	{
73  		return mapping.getAction();
74  	}
75  
76  	public T getTarget()
77  	{
78  		return target;
79  	}
80  
81  	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke, String iconPath )
82  	{
83  		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke, iconPath ), target );
84  	}
85  	
86  	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target, String keyStroke )
87  	{
88  		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action, keyStroke ), target );
89  	}
90  	
91  	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action, T target )
92  	{
93  		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), target );
94  	}
95  	
96  	public static <T extends ModelItem> SwingActionDelegate<T> createDelegate( SoapUIAction<T> action )
97  	{
98  		return new SwingActionDelegate<T>( new StandaloneActionMapping<T>( action ), null );
99  	}
100 
101 	public static AbstractAction createDelegate( String soapUIActionId )
102 	{
103 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ));
104 	}
105 	
106 	public static <T extends ModelItem> AbstractAction createDelegate( String soapUIActionId, T target )
107 	{
108 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target );
109 	}
110 	
111 	public static <T extends ModelItem> AbstractAction createDelegate( String soapUIActionId, T target, String keyStroke )
112 	{
113 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke );
114 	}
115 	
116 	public static <T extends ModelItem> AbstractAction createDelegate( String soapUIActionId, T target, String keyStroke, String iconPath )
117 	{
118 		return createDelegate( SoapUI.getActionRegistry().getAction( soapUIActionId ), target, keyStroke, iconPath );
119 	}
120 }