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.model.util;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import javax.swing.ImageIcon;
19  
20  import com.eviware.soapui.model.ModelItem;
21  import com.eviware.soapui.model.mock.MockOperation;
22  import com.eviware.soapui.model.mock.MockResponse;
23  import com.eviware.soapui.model.mock.MockService;
24  import com.eviware.soapui.model.project.Project;
25  import com.eviware.soapui.model.testsuite.LoadTest;
26  import com.eviware.soapui.model.testsuite.TestAssertion;
27  import com.eviware.soapui.model.testsuite.TestCase;
28  import com.eviware.soapui.model.testsuite.TestStep;
29  import com.eviware.soapui.model.testsuite.TestSuite;
30  import com.eviware.soapui.support.UISupport;
31  
32  public class ModelItemIconFactory
33  {
34  	private static Map<Class<? extends ModelItem>, String> modelItemIcons = new HashMap<Class<? extends ModelItem>, String>();
35  
36  	static
37  	{
38  		modelItemIcons.put( Project.class, "/project.gif" );
39  		modelItemIcons.put( TestSuite.class, "/testSuite.gif" );
40  		modelItemIcons.put( TestCase.class, "/testCase.gif" );
41  		modelItemIcons.put( TestStep.class, "/teststeps.gif" );
42  		modelItemIcons.put( TestAssertion.class, "/assertion.gif" );
43  		modelItemIcons.put( LoadTest.class, "/loadTest.gif" );
44  		modelItemIcons.put( MockService.class, "/mockService.gif" );
45  		modelItemIcons.put( MockResponse.class, "/mockResponse.gif" );
46  		modelItemIcons.put( MockOperation.class, "/mockOperation.gif" );
47  	}
48  
49  	public static ImageIcon getIcon( Class<? extends ModelItem> clazz )
50  	{
51  		if( modelItemIcons.containsKey( clazz ) )
52  			return UISupport.createImageIcon( modelItemIcons.get( clazz ) );
53  
54  		for( Class<?> iface : clazz.getInterfaces() )
55  		{
56  			if( modelItemIcons.containsKey( iface ) )
57  				return UISupport.createImageIcon( modelItemIcons.get( iface ) );
58  		}
59  
60  		return null;
61  	}
62  
63  	@SuppressWarnings( "unchecked" )
64  	public static String getIconPath( Class<? extends ModelItem> clazz )
65  	{
66  		if( modelItemIcons.containsKey( clazz ) )
67  			return modelItemIcons.get( clazz );
68  
69  		for( Class<?> iface : clazz.getInterfaces() )
70  		{
71  			if( modelItemIcons.containsKey( iface ) )
72  				return modelItemIcons.get( iface );
73  		}
74  
75  		while( clazz.getSuperclass() != null && ModelItem.class.isAssignableFrom( clazz.getSuperclass() ) )
76  		{
77  			return getIconPath( ( Class<? extends ModelItem> )clazz.getSuperclass() );
78  		}
79  
80  		return null;
81  	}
82  
83  	public static <T extends ModelItem> String getIconPath( T modelItem )
84  	{
85  		return getIconPath( modelItem.getClass() );
86  	}
87  
88  	public static <T extends ModelItem> ImageIcon getIcon( T modelItem )
89  	{
90  		return getIcon( modelItem.getClass() );
91  	}
92  }