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.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  
18  import javax.swing.AbstractListModel;
19  import javax.swing.ComboBoxModel;
20  
21  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
23  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
24  import com.eviware.soapui.model.testsuite.TestStep;
25  
26  public class TestStepComboBoxModel extends AbstractListModel implements ComboBoxModel
27  {
28  	private final WsdlTestCase testCase;
29  	private WsdlTestStep selectedStep;
30  	private int selectedStepIndex = -1;
31  	private TestStepNameListener testStepNameListener = new TestStepNameListener();
32  
33  	public TestStepComboBoxModel( WsdlTestCase testCase )
34  	{
35  		this.testCase = testCase;
36  		
37  		testCase.getTestSuite().addTestSuiteListener( new InternalTestSuiteListener() );
38  	}
39  
40  	public Object getElementAt( int index )
41  	{
42  		return testCase.getTestStepAt( index ).getName();
43  	}
44  
45  	public int getSize()
46  	{
47  		return testCase.getTestStepCount();
48  	}
49  	
50  	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
51  	{
52  		@Override
53  		public void testStepAdded( TestStep testStep, int index )
54  		{
55  			if( testStep.getTestCase() == testCase )
56  				fireIntervalAdded( TestStepComboBoxModel.this, index, index );
57  		}
58  
59  		@Override
60  		public void testStepMoved( TestStep testStep, int fromIndex, int offset )
61  		{
62  			if( testStep.getTestCase() == testCase )
63  				fireContentsChanged( TestStepComboBoxModel.this, fromIndex, fromIndex+offset );
64  		}
65  
66  		@Override
67  		public void testStepRemoved( TestStep testStep, int index )
68  		{
69  			if( testStep.getTestCase() == testCase )
70  				fireIntervalRemoved( TestStepComboBoxModel.this, index, index );
71  			
72  			if( index == selectedStepIndex )
73  				setSelectedItem( null );
74  		}
75  	}
76  
77  	public Object getSelectedItem()
78  	{
79  		return selectedStep == null ? null : selectedStep.getName();
80  	}
81  
82  	public void setSelectedItem( Object anItem )
83  	{
84  		if( selectedStep != null )
85  			selectedStep.removePropertyChangeListener( testStepNameListener  );
86  		
87  		selectedStep = testCase.getTestStepByName( ( String ) anItem );
88  		if( selectedStep != null )
89  		{
90  			selectedStep.addPropertyChangeListener( WsdlTestStep.NAME_PROPERTY, testStepNameListener );
91  			selectedStepIndex = testCase.getIndexOfTestStep( selectedStep );
92  		}
93  		else selectedStepIndex = -1;
94  		
95  		fireContentsChanged( this, -1, -1 );
96  	}
97  	
98  	/***
99  	 * Listen for testStep name changes and modify comboBox model accordingly
100 	 */
101 	
102 	private final class TestStepNameListener implements PropertyChangeListener
103 	{
104 		public void propertyChange(PropertyChangeEvent evt)
105 		{
106 			Object oldItem = evt.getOldValue();
107 			int stepIndex = testCase.getTestStepIndexByName( ( String ) oldItem );
108 			if( stepIndex != -1 )
109 			{
110 				fireContentsChanged( TestStepComboBoxModel.this, stepIndex, stepIndex );
111 				
112 				if( selectedStep != null && testCase.getIndexOfTestStep( selectedStep ) == stepIndex )
113 					fireContentsChanged( this, -1, -1 );
114 			}
115 		}
116 	}
117 
118 	public WsdlTestStep getSelectedStep()
119 	{
120 		return selectedStep;
121 	}
122 }