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.model.support;
14  
15  import java.util.List;
16  import java.util.UUID;
17  
18  import com.eviware.soapui.model.ModelItem;
19  import com.eviware.soapui.model.project.Project;
20  import com.eviware.soapui.support.UISupport;
21  import com.eviware.soapui.support.types.StringList;
22  
23  /***
24   * Utility methods for soapui model-related interfaces
25   *  
26   * @author Ole.Matzura
27   */
28  
29  public class ModelSupport
30  {
31  	public static String[] getNames(List<? extends ModelItem> list)
32  	{
33  		String [] names = new String[list.size()];
34  		for( int c = 0; c < names.length; c++ )
35  		{
36  			names[c] = list.get( c ).getName();
37  		}
38  		
39  		return names;
40  	}
41  	
42  	public static String[] getNames( String [] firstItems, List<? extends ModelItem> list)
43  	{
44  		String [] names = new String[list.size()+firstItems.length];
45  		for( int c = 0; c < firstItems.length; c++ )
46  		{
47  			names[c] = firstItems[c];
48  		}
49  		
50  		for( int c = 0; c < list.size(); c++ )
51  		{
52  			names[c+firstItems.length] = list.get( c ).getName();
53  		}
54  		
55  		return names;
56  	}
57  
58  	public static String[] getNames( List<? extends ModelItem> list, String [] lastItems)
59  	{
60  		String [] names = new String[list.size()+lastItems.length];
61  		for( int c = 0; c < lastItems.length; c++ )
62  		{
63  			names[c+list.size()] = lastItems[c];
64  		}
65  		
66  		for( int c = 0; c < list.size(); c++ )
67  		{
68  			names[c] = list.get( c ).getName();
69  		}
70  		
71  		return names;
72  	}
73  	
74  	public static String generateModelItemID()
75  	{
76  		return UUID.randomUUID().toString();
77  	}
78  	
79  	@SuppressWarnings("unchecked")
80  	public static <T extends ModelItem> T findModelItemById( String id, ModelItem root )
81  	{
82  		for( ModelItem child : root.getChildren() )
83  		{
84  			if( child.getId().equals( id ))
85  				return ( T ) child;
86  			
87  			ModelItem result = findModelItemById( id, child );
88  			if( result != null )
89  				return ( T ) result;
90  		}
91  		
92  		return null;
93  	}
94  
95  	public static String promptForUniqueName( String typeName, ModelItem parent, String def )
96  	{
97  		String name = UISupport.prompt( "Specify name for new " + typeName, "New " + typeName, def );
98  		StringList names = new StringList();
99  		for( ModelItem item : parent.getChildren() )
100 			names.add( item.getName() );
101 	
102 		while( name != null && names.contains( name ) )
103 		{
104 			name = UISupport.prompt( "Specify unique name for new " + typeName, "New " + typeName, def );
105 		}
106 	
107 		return name;
108 	}
109 
110 	public static Project getModelItemProject( ModelItem modelItem )
111 	{
112 		if( modelItem == null )
113 			return null;
114 		
115 		while( !(modelItem instanceof Project) && modelItem != null )
116 		{
117 			modelItem = modelItem.getParent();
118 		}
119 		
120 		return ( Project ) modelItem;
121 	}
122 }