View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.impl.wsdl.panels.teststeps.support;
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.teststeps.WsdlGotoTestStep.GotoCondition;
22  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
23  import com.eviware.soapui.model.testsuite.TestCase;
24  import com.eviware.soapui.model.testsuite.TestStep;
25  
26  /***
27   * ComboBox-model used by combo in the WsdlGotoTestStep desktop panel for
28   * selecting a conditions target teststep
29   * 
30   * @author Ole.Matzura
31   */
32  
33  public class GotoTestStepsComboBoxModel extends AbstractListModel implements ComboBoxModel
34  {
35  	private final TestCase testCase;
36  	private GotoCondition condition;
37  	private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();;
38  	private InternalPropertyChangeListener propertyChangeListener = new InternalPropertyChangeListener();
39  
40  	public GotoTestStepsComboBoxModel( TestCase testCase, GotoCondition condition )
41  	{
42  		super();
43  		this.testCase = testCase;
44  		this.condition = condition;
45  
46  		testCase.getTestSuite().addTestSuiteListener( testSuiteListener );
47  
48  		if( condition != null )
49  			condition.addPropertyChangeListener( GotoCondition.TARGET_STEP_PROPERTY, propertyChangeListener );
50  
51  		for( int c = 0; c < testCase.getTestStepCount(); c++ )
52  		{
53  			testCase.getTestStepAt( c ).addPropertyChangeListener( TestStep.NAME_PROPERTY, propertyChangeListener );
54  		}
55  	}
56  
57  	public GotoCondition getCondition()
58  	{
59  		return condition;
60  	}
61  
62  	public void setCondition( GotoCondition condition )
63  	{
64  		if( this.condition != null )
65  			this.condition.removePropertyChangeListener( GotoCondition.TARGET_STEP_PROPERTY, propertyChangeListener );
66  
67  		this.condition = condition;
68  
69  		if( condition != null )
70  			condition.addPropertyChangeListener( GotoCondition.TARGET_STEP_PROPERTY, propertyChangeListener );
71  
72  		fireContentsChanged( this, 0, getSize() );
73  	}
74  
75  	public void setSelectedItem( Object anItem )
76  	{
77  		if( condition != null )
78  			condition.setTargetStep( anItem == null ? null : anItem.toString() );
79  	}
80  
81  	public Object getSelectedItem()
82  	{
83  		return condition == null ? null : condition.getTargetStep();
84  	}
85  
86  	public int getSize()
87  	{
88  		return testCase.getTestStepCount();
89  	}
90  
91  	public Object getElementAt( int index )
92  	{
93  		return testCase.getTestStepAt( index ).getName();
94  	}
95  
96  	private class InternalTestSuiteListener extends TestSuiteListenerAdapter
97  	{
98  		public void testStepAdded( TestStep testStep, int index )
99  		{
100 			if( testStep.getTestCase() == testCase )
101 			{
102 				fireContentsChanged( GotoTestStepsComboBoxModel.this, 0, getSize() );
103 			}
104 		}
105 
106 		public void testStepRemoved( TestStep testStep, int index )
107 		{
108 			if( testStep.getTestCase() == testCase )
109 			{
110 				fireContentsChanged( GotoTestStepsComboBoxModel.this, 0, getSize() );
111 			}
112 		}
113 	}
114 
115 	private class InternalPropertyChangeListener implements PropertyChangeListener
116 	{
117 		public void propertyChange( PropertyChangeEvent evt )
118 		{
119 			fireContentsChanged( GotoTestStepsComboBoxModel.this, 0, getSize() );
120 		}
121 	}
122 
123 	public void release()
124 	{
125 		testCase.getTestSuite().removeTestSuiteListener( testSuiteListener );
126 
127 		if( condition != null )
128 			condition.removePropertyChangeListener( GotoCondition.TARGET_STEP_PROPERTY, propertyChangeListener );
129 
130 		for( int c = 0; c < testCase.getTestStepCount(); c++ )
131 		{
132 			testCase.getTestStepAt( c ).removePropertyChangeListener( TestStep.NAME_PROPERTY, propertyChangeListener );
133 		}
134 	}
135 }