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.components;
14  
15  import java.util.ArrayList;
16  import java.util.Arrays;
17  import java.util.List;
18  
19  import javax.swing.AbstractListModel;
20  import javax.swing.ComboBoxModel;
21  
22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepListenerAdapter;
24  
25  public class TestStepPropertyComboBoxModel extends AbstractListModel implements ComboBoxModel
26  {
27  	private WsdlTestStep testStep;
28  	private List<String> names;
29  	private String selectedName;
30  	private InternalTestStepListener testStepListener = new InternalTestStepListener();
31  
32  	public TestStepPropertyComboBoxModel( WsdlTestStep testStep )
33  	{
34  		this.testStep = testStep;
35  	
36  		names = new ArrayList<String>();
37  		
38  		if( testStep != null ) 
39  		{
40  			names.addAll( Arrays.asList( testStep.getPropertyNames() ));
41  			testStep.addTestStepListener( testStepListener );
42  		}
43  	}
44  	
45  	public WsdlTestStep getTestStep()
46  	{
47  		return testStep;
48  	}
49  
50  	public void setTestStep( WsdlTestStep testStep )
51  	{
52  		if( this.testStep != null )
53  		{
54  			this.testStep.removeTestStepListener( testStepListener );
55  		}
56  		
57  		int sz = names.size();
58  		if( sz > 0 )
59  		{
60  			names.clear();
61  			fireIntervalRemoved( this, 0, sz-1 );
62  		}
63  		
64  		this.testStep = testStep;
65  		if( testStep != null )
66  		{
67  			testStep.addTestStepListener( testStepListener );
68  			names.addAll( Arrays.asList( testStep.getPropertyNames() ));
69  			if( !names.isEmpty())
70  			{
71  				fireIntervalAdded( this, 0, names.size()-1 );
72  			}
73  		}
74  		
75  		setSelectedItem( null );
76  	}
77  
78  	public Object getElementAt( int index )
79  	{
80  		return names.get( index );
81  	}
82  
83  	public int getSize()
84  	{
85  		return names.size();
86  	}
87  	
88  	private final class InternalTestStepListener extends WsdlTestStepListenerAdapter
89  	{
90  		@Override
91  		public void propertyAdded( String name )
92  		{
93  			names.add( name );
94  			fireIntervalAdded( TestStepPropertyComboBoxModel.this, names.size()-1, names.size()-1 );
95  		}
96  
97  		@Override
98  		public void propertyRemoved( String name )
99  		{
100 			int ix = names.indexOf( name );
101 			if( ix >= 0 )
102 			{
103 				names.remove( ix );
104 				fireIntervalRemoved( TestStepPropertyComboBoxModel.this, ix, ix );
105 				
106 				if( name.equals( selectedName ))
107 					setSelectedItem( null );
108 			}
109 		}
110 
111 		@Override
112 		public void propertyRenamed( String oldName, String newName )
113 		{
114 			int ix = names.indexOf( oldName );
115 			fireContentsChanged( TestStepPropertyComboBoxModel.this, ix, ix );
116 			
117 			if( oldName.equals( selectedName ))
118 				setSelectedItem( newName );
119 		}
120 	}
121 
122 	public Object getSelectedItem()
123 	{
124 		return selectedName;
125 	}
126 
127 	public void setSelectedItem( Object anItem )
128 	{
129 		if( anItem == null && selectedName == null )
130 			return;
131 		
132 		if( anItem != null && selectedName != null && anItem.equals( selectedName))
133 			return;
134 		
135 		selectedName = anItem == null ? null : anItem.toString();
136 		
137 		fireContentsChanged( this, -1, -1 );
138 	}
139 }