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.util.HashMap;
16  import java.util.Map;
17  
18  import javax.swing.Action;
19  import javax.swing.Icon;
20  import javax.swing.ImageIcon;
21  
22  import com.eviware.soapui.support.UISupport;
23  
24  /***
25   * Registry of actions.. we could probably use a spring beanfactory instead..
26   * 
27   * @author Ole.Matzura
28   * @deprecated
29   */
30  
31  public class SwingActionRegistry
32  {
33     private Map<String,ActionMapping> actions = new HashMap<String,ActionMapping>();
34     private static SwingActionRegistry instance;  
35     
36     public SwingActionRegistry()
37     {
38     }
39     
40  	public <T> Action getAction(Class<? extends SoapUIAction<T> > actionClass, T target)
41  	{
42  		ActionMapping mapping = actions.get( actionClass.getName() );
43  		if( mapping == null )
44  			return null;
45  		
46  		return new SwingActionDelegate<T>( (SoapUIAction<T>) mapping.getAction(), target, 
47  				mapping.getIcon(), mapping.getKeyStroke() );
48  	}
49  	
50  	public <T> Action getAction(Class<? extends SoapUIAction<T> > actionClass, T target, String name, String description )
51  	{
52  		Action action = getAction( actionClass, target );
53  		if( action != null )
54  		{
55  			if( name != null )
56  				action.putValue( Action.NAME, name );
57  
58  			if( description != null )
59  				action.putValue( Action.SHORT_DESCRIPTION, description );
60  		}
61  		
62  		return action;
63  	}
64  	
65  	public void mapAction( SoapUIAction<?> action, String keyStroke, 
66  			String iconResource )
67  	{
68  		ActionMapping mapping = new ActionMapping( action, keyStroke, iconResource );
69  		actions.put( action.getClass().getName(), mapping );
70  	}
71  	
72  	public static SwingActionRegistry getInstance()
73  	{
74  		if( instance == null )
75  			instance = new SwingActionRegistry();
76  		
77  		return instance;
78  	}
79  	
80  	public static <T> Action get(Class<? extends SoapUIAction<T> > actionClass, T target)
81  	{
82  		return getInstance().getAction( actionClass, target );
83  	}
84  	
85  	public static <T> Action get(Class<? extends SoapUIAction<T> > actionClass, T target, String name, String description )
86  	{
87  		return getInstance().getAction( actionClass, target, name, description );
88  	}
89  	
90  	private final class ActionMapping
91  	{
92  		private final SoapUIAction<?> action;
93  		private final String keyStroke;
94  		private final String iconResource;
95  		private ImageIcon icon;
96  
97  		public ActionMapping(SoapUIAction<?> action, String keyStroke, String iconResource)
98  		{
99  			this.action = action;
100 			this.keyStroke = keyStroke;
101 			this.iconResource = iconResource;
102 		}
103 
104 		public SoapUIAction<?> getAction()
105 		{
106 			return action;
107 		}
108 
109 		public String getIconResource()
110 		{
111 			return iconResource;
112 		}
113 		
114 		public Icon getIcon()
115 		{
116 			if( iconResource == null )
117 				return null;
118 			
119 			if( icon == null )
120 				icon = UISupport.createImageIcon( iconResource );
121 			
122 			return icon;
123 		}
124 
125 		public String getKeyStroke()
126 		{
127 			return keyStroke;
128 		}
129 	}
130 }