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.model.tree.nodes;
    14
    15  import java.util.ArrayList;
    16  import java.util.List;
    17
    18  import com.eviware.soapui.model.iface.Interface;
    19  import com.eviware.soapui.model.mock.MockService;
    20  import com.eviware.soapui.model.project.Project;
    21  import com.eviware.soapui.model.project.ProjectListener;
    22  import com.eviware.soapui.model.testsuite.TestSuite;
    23  import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
    24  import com.eviware.soapui.model.tree.SoapUITreeNode;
    25  import com.eviware.soapui.settings.UISettings;
    26
    27  /***
    28   * SoapUITreeNode for Project implementations
    29   *
    30   * @author Ole.Matzura
    31   */
    32
    33  public class ProjectTreeNode extends AbstractModelItemTreeNode<Project>
    34  {
    35  	private InternalProjectListener internalProjectListener;
    36  	private List<InterfaceTreeNode> interfaceNodes = new ArrayList<InterfaceTreeNode>();
    37  	private List<TestSuiteTreeNode> testSuiteNodes = new ArrayList<TestSuiteTreeNode>();
    38  	private List<MockServiceTreeNode> mockServiceNodes = new ArrayList<MockServiceTreeNode>();
    39
    40     public ProjectTreeNode( Project project, WorkspaceTreeNode workspaceNode )
    41     {
    42        super( project, workspaceNode.getWorkspace(), workspaceNode.getTreeModel()  );
    43
    44        internalProjectListener = new InternalProjectListener();
    45  		project.addProjectListener( internalProjectListener);
    46
    47  		for( int c = 0; c < project.getInterfaceCount(); c++ )
    48  		{
    49  			interfaceNodes.add( new InterfaceTreeNode( project.getInterfaceAt( c ), getTreeModel() ));
    50  		}
    51
    52  		for( int c = 0; c < project.getTestSuiteCount(); c++ )
    53  		{
    54  			testSuiteNodes.add( new TestSuiteTreeNode( project.getTestSuiteAt( c ), getTreeModel() ));
    55  		}
    56
    57  		for( int c = 0; c < project.getMockServiceCount(); c++ )
    58  		{
    59  			mockServiceNodes.add( new MockServiceTreeNode( project.getMockServiceAt( c ), getTreeModel() ));
    60  		}
    61
    62  		getTreeModel().mapModelItems( interfaceNodes );
    63  		getTreeModel().mapModelItems( testSuiteNodes );
    64  		getTreeModel().mapModelItems( mockServiceNodes );
    65
    66  		initOrdering( testSuiteNodes, UISettings.ORDER_TESTSUITES );
    67     }
    68
    69     public void release()
    70     {
    71     	super.release();
    72
    73     	getProject().removeProjectListener( internalProjectListener );
    74
    75     	for( InterfaceTreeNode treeNode : interfaceNodes )
    76     		treeNode.release();
    77
    78     	for( TestSuiteTreeNode treeNode : testSuiteNodes )
    79     		treeNode.release();
    80
    81     	for( MockServiceTreeNode treeNode : mockServiceNodes )
    82     		treeNode.release();
    83     }
    84
    85     public int getChildCount()
    86     {
    87       return interfaceNodes.size() + testSuiteNodes.size() + mockServiceNodes.size();
    88     }
    89
    90     public int getIndexOfChild(Object child)
    91     {
    92     	int index = interfaceNodes.indexOf( child );
    93     	if( index >= 0 ) return index;
    94
    95     	index = testSuiteNodes.indexOf( child );
    96     	if( index >= 0 ) return index + interfaceNodes.size();
    97
    98     	index = mockServiceNodes.indexOf( child );
    99     	if( index >= 0 ) return index + interfaceNodes.size() + testSuiteNodes.size();
    100
    101    	return -1;
    102    }
    103
    104    public SoapUITreeNode getChildNode(int index)
    105    {
    106 		if( index < interfaceNodes.size() )
    107          return interfaceNodes.get( index );
    108       else if( index < testSuiteNodes.size()+interfaceNodes.size() )
    109          return testSuiteNodes.get( index-interfaceNodes.size() );
    110       else
    111          return mockServiceNodes.get( index- interfaceNodes.size() - testSuiteNodes.size() );
    112    }
    113
    114    public Project getProject()
    115    {
    116       return (Project) getModelItem();
    117    }
    118
    119    private class InternalProjectListener implements ProjectListener
    120    {
    121       public void interfaceAdded(Interface iface)
    122       {
    123       	InterfaceTreeNode interfaceTreeNode = new InterfaceTreeNode( iface, getTreeModel() );
    124 			interfaceNodes.add( interfaceTreeNode);
    125          getTreeModel().notifyNodeInserted( interfaceTreeNode );
    126       }
    127
    128       public void interfaceRemoved(Interface iface)
    129       {
    130       	SoapUITreeNode treeNode = getTreeModel().getTreeNode( iface );
    131       	if( interfaceNodes.contains( treeNode ))
    132       	{
    133       		getTreeModel().notifyNodeRemoved( treeNode );
    134       		interfaceNodes.remove( treeNode);
    135       	}
    136       	else throw new RuntimeException( "Removing unkown interface" );
    137       }
    138
    139       public void testSuiteAdded(TestSuite testSuite)
    140       {
    141       	TestSuiteTreeNode testSuiteNode = new TestSuiteTreeNode( testSuite, getTreeModel() );
    142       	testSuiteNodes.add( testSuiteNode );
    143       	reorder(false);
    144       	getTreeModel().notifyNodeInserted( testSuiteNode );
    145       }
    146
    147       public void testSuiteRemoved(TestSuite testSuite)
    148       {
    149       	SoapUITreeNode treeNode = getTreeModel().getTreeNode( testSuite );
    150       	if( testSuiteNodes.contains( treeNode ))
    151       	{
    152       		getTreeModel().notifyNodeRemoved( treeNode );
    153       		testSuiteNodes.remove( treeNode);
    154       	}
    155       	else throw new RuntimeException( "Removing unkown testSuite" );
    156       }
    157
    158 		public void mockServiceAdded( MockService mockService )
    159 		{
    160 			MockServiceTreeNode mockServiceNode = new MockServiceTreeNode( mockService, getTreeModel() );
    161       	mockServiceNodes.add( mockServiceNode );
    162       	getTreeModel().notifyNodeInserted( mockServiceNode );
    163 		}
    164
    165 		public void mockServiceRemoved( MockService mockService )
    166 		{
    167 			SoapUITreeNode treeNode = getTreeModel().getTreeNode( mockService );
    168       	if( mockServiceNodes.contains( treeNode ))
    169       	{
    170       		getTreeModel().notifyNodeRemoved( treeNode );
    171       		mockServiceNodes.remove( treeNode);
    172       	}
    173       	else throw new RuntimeException( "Removing unkown mockService" );
    174 		}
    175    }
    176
    177 }