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.model.tree.nodes;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import com.eviware.soapui.model.testsuite.LoadTest;
19  import com.eviware.soapui.model.testsuite.TestCase;
20  import com.eviware.soapui.model.testsuite.TestStep;
21  import com.eviware.soapui.model.testsuite.TestSuite;
22  import com.eviware.soapui.model.testsuite.TestSuiteListener;
23  import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
24  import com.eviware.soapui.model.tree.SoapUITreeModel;
25  import com.eviware.soapui.model.tree.SoapUITreeNode;
26  
27  /***
28   * SoapUITreeNode for TestSuite implementations
29   * 
30   * @author Ole.Matzura
31   */
32  
33  public class TestSuiteTreeNode extends AbstractModelItemTreeNode<TestSuite>
34  {
35  	private InternalTestSuiteListener internalTestSuiteListener = new InternalTestSuiteListener();;
36  	private ReorderPropertyChangeListener propertyChangeListener = new ReorderPropertyChangeListener();
37  	private List<TestCaseTreeNode> testCaseNodes = new ArrayList<TestCaseTreeNode>();
38  	private PropertiesTreeNode<?> propertiesTreeNode;
39  
40  	public TestSuiteTreeNode( TestSuite testSuite, SoapUITreeModel treeModel )
41  	{
42  		super( testSuite, testSuite.getProject(), treeModel );
43  
44  		testSuite.addTestSuiteListener( internalTestSuiteListener );
45  
46  		for( int c = 0; c < testSuite.getTestCaseCount(); c++ )
47  		{
48  			TestCase testCase = testSuite.getTestCaseAt( c );
49  			testCase.addPropertyChangeListener( TestCase.NAME_PROPERTY, propertyChangeListener );
50  			testCaseNodes.add( new TestCaseTreeNode( testCase, getTreeModel() ) );
51  		}
52  
53  		getTreeModel().mapModelItems( testCaseNodes );
54  
55  		propertiesTreeNode = PropertiesTreeNode.createDefaultPropertiesNode( testSuite, getTreeModel() );
56  		getTreeModel().mapModelItem( propertiesTreeNode );
57  	}
58  
59  	public void release()
60  	{
61  		super.release();
62  
63  		getTestSuite().removeTestSuiteListener( internalTestSuiteListener );
64  
65  		for( TestCaseTreeNode treeNode : testCaseNodes )
66  		{
67  			treeNode.getModelItem().removePropertyChangeListener( TestCase.NAME_PROPERTY, propertyChangeListener );
68  			treeNode.release();
69  		}
70  
71  		propertiesTreeNode.release();
72  	}
73  
74  	@Override
75  	public int getChildCount()
76  	{
77  		int propMod = getTreeModel().isShowProperties() ? 1 : 0;
78  		return testCaseNodes.size() + propMod;
79  	}
80  
81  	@Override
82  	public SoapUITreeNode getChildNode( int index )
83  	{
84  		int propMod = getTreeModel().isShowProperties() ? 1 : 0;
85  		return index == 0 && propMod == 1 ? propertiesTreeNode : testCaseNodes.get( index - propMod );
86  	}
87  
88  	@Override
89  	public int getIndexOfChild( Object child )
90  	{
91  		int propMod = getTreeModel().isShowProperties() ? 1 : 0;
92  		if( propMod == 1 && child == propertiesTreeNode )
93  			return 0;
94  
95  		int ix = testCaseNodes.indexOf( child );
96  		return ix == -1 ? ix : ix + propMod;
97  	}
98  
99  	public TestSuite getTestSuite()
100 	{
101 		return ( TestSuite )getModelItem();
102 	}
103 
104 	private class InternalTestSuiteListener implements TestSuiteListener
105 	{
106 		public void testCaseAdded( TestCase testCase )
107 		{
108 			TestCaseTreeNode testCaseTreeNode = new TestCaseTreeNode( testCase, getTreeModel() );
109 			testCaseNodes.add( testCase.getTestSuite().getIndexOfTestCase( testCase ), testCaseTreeNode );
110 
111 			testCase.addPropertyChangeListener( TestCase.NAME_PROPERTY, propertyChangeListener );
112 			getTreeModel().notifyNodeInserted( testCaseTreeNode );
113 		}
114 
115 		public void testCaseRemoved( TestCase testCase )
116 		{
117 			SoapUITreeNode treeNode = getTreeModel().getTreeNode( testCase );
118 			if( testCaseNodes.contains( treeNode ) )
119 			{
120 				getTreeModel().notifyNodeRemoved( treeNode );
121 				testCaseNodes.remove( treeNode );
122 				testCase.removePropertyChangeListener( propertyChangeListener );
123 			}
124 			else
125 				throw new RuntimeException( "Removing unknown TestCase" );
126 		}
127 
128 		public void testStepAdded( TestStep testStep, int index )
129 		{
130 			TestCaseTreeNode testCaseTreeNode = ( TestCaseTreeNode )getTreeModel().getTreeNode( testStep.getTestCase() );
131 			testCaseTreeNode.testStepInserted( testStep, index );
132 		}
133 
134 		public void testStepRemoved( TestStep testStep, int index )
135 		{
136 			TestCaseTreeNode testCaseTreeNode = ( TestCaseTreeNode )getTreeModel().getTreeNode( testStep.getTestCase() );
137 			testCaseTreeNode.testStepRemoved( testStep, index );
138 		}
139 
140 		public void loadTestAdded( LoadTest loadTest )
141 		{
142 			TestCaseTreeNode testCaseTreeNode = ( TestCaseTreeNode )getTreeModel().getTreeNode( loadTest.getTestCase() );
143 			testCaseTreeNode.loadTestInserted( loadTest );
144 		}
145 
146 		public void loadTestRemoved( LoadTest loadTest )
147 		{
148 			TestCaseTreeNode testCaseTreeNode = ( TestCaseTreeNode )getTreeModel().getTreeNode( loadTest.getTestCase() );
149 			testCaseTreeNode.loadTestRemoved( loadTest );
150 		}
151 
152 		public void testStepMoved( TestStep testStep, int fromIndex, int offset )
153 		{
154 			TestCaseTreeNode testCaseTreeNode = ( TestCaseTreeNode )getTreeModel().getTreeNode( testStep.getTestCase() );
155 			testCaseTreeNode.testStepMoved( testStep, fromIndex, offset );
156 		}
157 
158 		public void testCaseMoved( TestCase testCase, int index, int offset )
159 		{
160 			testCaseRemoved( testCase );
161 			testCaseAdded( testCase );
162 		}
163 	}
164 }