View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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;
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  import javax.swing.Icon;
22  
23  import com.eviware.soapui.support.UISupport;
24  
25  public class SwingActionDelegate<T> extends AbstractAction implements PropertyChangeListener
26  {
27  	private final SoapUIAction<T> action;
28  	private final T target;
29  
30  	public SwingActionDelegate( SoapUIAction<T> action, T target )
31  	{
32  		this( action, target, null, null );
33  	}
34  	
35  	public SwingActionDelegate( SoapUIAction<T> action, T target, String keyStroke )
36  	{
37  		this( action, target, null, keyStroke );
38  	}
39  
40  	public SwingActionDelegate( SoapUIAction<T> action, T target, Icon icon )
41  	{
42  		this( action, target, icon, null );
43  	}
44  	
45  	public SwingActionDelegate( SoapUIAction<T> action, T target, Icon icon, String keyStroke )
46  	{
47  		super( action.getName() );
48  		this.action = action;
49  		this.target = target;
50  		
51  		if( action.getDescription() != null )
52  			putValue( Action.SHORT_DESCRIPTION, action.getDescription() );
53  
54  		if( icon != null )
55  			putValue( Action.SMALL_ICON, icon );
56  
57  		if( keyStroke != null )
58  			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( keyStroke ));
59  
60  		setEnabled( action.isEnabled() );
61  		
62  		action.addPropertyChangeListener( this );
63  	}
64  	
65  	public void actionPerformed(ActionEvent e)
66  	{
67  		action.perform( target );
68  	}
69  
70  	public void propertyChange(PropertyChangeEvent evt)
71  	{
72  		if( evt.getPropertyName().equals( SoapUIAction.ENABLED_PROPERTY ))
73  			setEnabled( ((Boolean)evt.getNewValue()).booleanValue() );
74  	}
75  
76  	public SoapUIAction<T> getAction()
77  	{
78  		return action;
79  	}
80  
81  	public T getTarget()
82  	{
83  		return target;
84  	}
85  	
86  	
87  }