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        if( !project.isOpen() )
    45        	return;
    46
    47        internalProjectListener = new InternalProjectListener();
    48  		project.addProjectListener( internalProjectListener);
    49
    50  		for( int c = 0; c < project.getInterfaceCount(); c++ )
    51  		{
    52  			interfaceNodes.add( new InterfaceTreeNode( project.getInterfaceAt( c ), getTreeModel() ));
    53  		}
    54
    55  		for( int c = 0; c < project.getTestSuiteCount(); c++ )
    56  		{
    57  			testSuiteNodes.add( new TestSuiteTreeNode( project.getTestSuiteAt( c ), getTreeModel() ));
    58  		}
    59
    60  		for( int c = 0; c < project.getMockServiceCount(); c++ )
    61  		{
    62  			mockServiceNodes.add( new MockServiceTreeNode( project.getMockServiceAt( c ), getTreeModel() ));
    63  		}
    64
    65  		getTreeModel().mapModelItems( interfaceNodes );
    66  		getTreeModel().mapModelItems( testSuiteNodes );
    67  		getTreeModel().mapModelItems( mockServiceNodes );
    68
    69  		initOrdering( testSuiteNodes, UISettings.ORDER_TESTSUITES );
    70     }
    71
    72     public void release()
    73     {
    74     	super.release();
    75
    76     	getProject().removeProjectListener( internalProjectListener );
    77
    78     	for( InterfaceTreeNode treeNode : interfaceNodes )
    79     		treeNode.release();
    80
    81     	for( TestSuiteTreeNode treeNode : testSuiteNodes )
    82     		treeNode.release();
    83
    84     	for( MockServiceTreeNode treeNode : mockServiceNodes )
    85     		treeNode.release();
    86     }
    87
    88  	public int getChildCount()
    89     {
    90  		return interfaceNodes.size() + testSuiteNodes.size() + mockServiceNodes.size();
    91     }
    92
    93     public int getIndexOfChild(Object child)
    94     {
    95     	int index = interfaceNodes.indexOf( child );
    96     	if( index >= 0 ) return index;
    97
    98     	index = testSuiteNodes.indexOf( child );
    99     	if( index >= 0 ) return index + interfaceNodes.size();
    100
    101    	index = mockServiceNodes.indexOf( child );
    102    	if( index >= 0 ) return index + interfaceNodes.size() + testSuiteNodes.size();
    103
    104    	return -1;
    105    }
    106
    107    public SoapUITreeNode getChildNode(int index)
    108    {
    109 		if( index < interfaceNodes.size() )
    110          return interfaceNodes.get( index );
    111       else if( index < testSuiteNodes.size()+interfaceNodes.size() )
    112          return testSuiteNodes.get( index-interfaceNodes.size() );
    113       else
    114          return mockServiceNodes.get( index- interfaceNodes.size() - testSuiteNodes.size() );
    115    }
    116
    117    public Project getProject()
    118    {
    119       return (Project) getModelItem();
    120    }
    121
    122 //	public boolean isLeaf()
    123 //	{
    124 //		return getModelItem().isOpen() ? super.isLeaf() : false;
    125 //	}
    126
    127 	private class InternalProjectListener implements ProjectListener
    128    {
    129       public void interfaceAdded(Interface iface)
    130       {
    131       	InterfaceTreeNode interfaceTreeNode = new InterfaceTreeNode( iface, getTreeModel() );
    132 			interfaceNodes.add( interfaceTreeNode);
    133          getTreeModel().notifyNodeInserted( interfaceTreeNode );
    134       }
    135
    136       public void interfaceRemoved(Interface iface)
    137       {
    138       	SoapUITreeNode treeNode = getTreeModel().getTreeNode( iface );
    139       	if( interfaceNodes.contains( treeNode ))
    140       	{
    141       		getTreeModel().notifyNodeRemoved( treeNode );
    142       		interfaceNodes.remove( treeNode);
    143       	}
    144       	else throw new RuntimeException( "Removing unkown interface" );
    145       }
    146
    147       public void testSuiteAdded(TestSuite testSuite)
    148       {
    149       	TestSuiteTreeNode testSuiteNode = new TestSuiteTreeNode( testSuite, getTreeModel() );
    150       	testSuiteNodes.add( testSuiteNode );
    151       	reorder(false);
    152       	getTreeModel().notifyNodeInserted( testSuiteNode );
    153       }
    154
    155       public void testSuiteRemoved(TestSuite testSuite)
    156       {
    157       	SoapUITreeNode treeNode = getTreeModel().getTreeNode( testSuite );
    158       	if( testSuiteNodes.contains( treeNode ))
    159       	{
    160       		getTreeModel().notifyNodeRemoved( treeNode );
    161       		testSuiteNodes.remove( treeNode);
    162       	}
    163       	else throw new RuntimeException( "Removing unkown testSuite" );
    164       }
    165
    166 		public void mockServiceAdded( MockService mockService )
    167 		{
    168 			MockServiceTreeNode mockServiceNode = new MockServiceTreeNode( mockService, getTreeModel() );
    169       	mockServiceNodes.add( mockServiceNode );
    170       	getTreeModel().notifyNodeInserted( mockServiceNode );
    171 		}
    172
    173 		public void mockServiceRemoved( MockService mockService )
    174 		{
    175 			SoapUITreeNode treeNode = getTreeModel().getTreeNode( mockService );
    176       	if( mockServiceNodes.contains( treeNode ))
    177       	{
    178       		getTreeModel().notifyNodeRemoved( treeNode );
    179       		mockServiceNodes.remove( treeNode);
    180       	}
    181       	else throw new RuntimeException( "Removing unkown mockService" );
    182 		}
    183
    184 		public void interfaceUpdated( Interface iface )
    185 		{
    186 		}
    187    }
    188
    189 }