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