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.mock.MockOperation;
    19  import com.eviware.soapui.model.mock.MockResponse;
    20  import com.eviware.soapui.model.mock.MockService;
    21  import com.eviware.soapui.model.mock.MockServiceListener;
    22  import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
    23  import com.eviware.soapui.model.tree.SoapUITreeModel;
    24  import com.eviware.soapui.model.tree.SoapUITreeNode;
    25  import com.eviware.soapui.settings.UISettings;
    26
    27  /***
    28   * SoapUITreeNode for MockService implementations
    29   *
    30   * @author Ole.Matzura
    31   */
    32
    33  public class MockServiceTreeNode extends AbstractModelItemTreeNode<MockService>
    34  {
    35  	private ReorderPropertyChangeListener propertyChangeListener = new ReorderPropertyChangeListener();
    36  	private List<MockOperationTreeNode> mockOperationNodes = new ArrayList<MockOperationTreeNode>();
    37  	private InternalMockServiceListener mockServiceListener;
    38
    39  	public MockServiceTreeNode(MockService mockService, SoapUITreeModel treeModel )
    40     {
    41        super( mockService, mockService.getProject(), treeModel );
    42
    43        mockServiceListener = new InternalMockServiceListener();
    44  		mockService.addMockServiceListener( mockServiceListener );
    45
    46  		for( int c = 0; c < mockService.getMockOperationCount(); c++ )
    47  		{
    48  			MockOperation mockOperation = mockService.getMockOperationAt( c );
    49  			mockOperation.addPropertyChangeListener( MockService.NAME_PROPERTY, propertyChangeListener );
    50  			mockOperationNodes.add( new MockOperationTreeNode( mockOperation, getTreeModel() ));
    51  		}
    52
    53  		initOrdering( mockOperationNodes, UISettings.ORDER_MOCKOPERATION );
    54
    55  		getTreeModel().mapModelItems( mockOperationNodes );
    56     }
    57
    58  	public void release()
    59  	{
    60  		super.release();
    61
    62  		getModelItem().removeMockServiceListener( mockServiceListener );
    63
    64  		for( MockOperationTreeNode treeNode : mockOperationNodes )
    65  		{
    66  			treeNode.getModelItem().removePropertyChangeListener( propertyChangeListener );
    67  			treeNode.release();
    68  		}
    69  	}
    70
    71     public MockService getMockService()
    72     {
    73        return (MockService) getModelItem();
    74     }
    75
    76     public int getChildCount()
    77  	{
    78  		return mockOperationNodes.size();
    79  	}
    80
    81  	public int getIndexOfChild(Object child)
    82  	{
    83  		return mockOperationNodes.indexOf( child );
    84  	}
    85
    86  	public SoapUITreeNode getChildNode(int index)
    87  	{
    88  		return mockOperationNodes.get( index );
    89  	}
    90
    91  	private final class InternalMockServiceListener implements MockServiceListener
    92  	{
    93  		public void mockOperationAdded( MockOperation mockOperation )
    94  		{
    95  			MockOperationTreeNode mockOperationTreeNode = new MockOperationTreeNode( mockOperation, getTreeModel() );
    96  			mockOperationNodes.add( mockOperationTreeNode);
    97  			mockOperation.addPropertyChangeListener( propertyChangeListener );
    98  			getTreeModel().notifyNodeInserted( mockOperationTreeNode );
    99  		}
    100
    101 		public void mockOperationRemoved( MockOperation mockOperation )
    102 		{
    103 			SoapUITreeNode treeNode = getTreeModel().getTreeNode( mockOperation );
    104 	   	if( mockOperationNodes.contains( treeNode ))
    105 	   	{
    106 	   		getTreeModel().notifyNodeRemoved( treeNode );
    107 	   		mockOperation.removePropertyChangeListener( propertyChangeListener );
    108 	   		mockOperationNodes.remove( treeNode );
    109 	   	}
    110 	   	else throw new RuntimeException( "Removing unkown mockOperation" );
    111 		}
    112
    113 		public void mockResponseAdded( MockResponse mockResponse )
    114 		{
    115 			 MockOperationTreeNode operationTreeNode = (MockOperationTreeNode) getTreeModel().getTreeNode( mockResponse.getMockOperation() );
    116 	       if( operationTreeNode != null )
    117 	       	 operationTreeNode.mockResponseAdded( mockResponse );
    118 	       else
    119 	      	 throw new RuntimeException( "Adding mockResponse to unknwown MockOperation" );
    120 	   }
    121
    122 		public void mockResponseRemoved( MockResponse mockResponse )
    123 		{
    124 			MockOperationTreeNode operationTreeNode = (MockOperationTreeNode) getTreeModel().getTreeNode( mockResponse.getMockOperation() );
    125 	       if( operationTreeNode != null )
    126 	       	 operationTreeNode.mockResponseRemoved( mockResponse );
    127 	       else
    128 	      	 throw new RuntimeException( "Removing mockResponse from unknown MockOperation" );
    129
    130 		}
    131 	}
    132 }