View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.beans.PropertyChangeEvent;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import com.eviware.soapui.impl.rest.RestMethod;
20  import com.eviware.soapui.impl.rest.RestRequest;
21  import com.eviware.soapui.model.iface.Request;
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  
26  /***
27   * SoapUITreeNode for RestRequest implementations
28   * 
29   * @author dain.nilsson
30   */
31  
32  public class RestMethodTreeNode extends AbstractModelItemTreeNode<RestMethod>
33  {
34  	private List<RequestTreeNode> requestNodes = new ArrayList<RequestTreeNode>();
35  	private ReorderPropertyChangeListener propertyChangeListener = new ReorderPropertyChangeListener();
36  
37  	public RestMethodTreeNode( RestMethod method, SoapUITreeModel treeModel )
38  	{
39  		super( method, method.getParent(), treeModel );
40  		treeModel.mapModelItem( this );
41  
42  		for( RestRequest request : method.getRequestList() )
43  		{
44  			requestAdded( request );
45  		}
46  	}
47  
48  	@Override
49  	public int getChildCount()
50  	{
51  		return requestNodes.size();
52  	}
53  
54  	@Override
55  	public SoapUITreeNode getChildNode( int index )
56  	{
57  		return requestNodes.get( index );
58  	}
59  
60  	@Override
61  	public int getIndexOfChild( Object child )
62  	{
63  		return requestNodes.indexOf( child );
64  	}
65  
66  	public void requestAdded( Request request )
67  	{
68  		RequestTreeNode requestTreeNode = new RequestTreeNode( request, getTreeModel() );
69  		requestNodes.add( requestTreeNode );
70  		reorder( false );
71  		request.addPropertyChangeListener( Request.NAME_PROPERTY, propertyChangeListener );
72  		getTreeModel().notifyNodeInserted( requestTreeNode );
73  	}
74  
75  	public void requestRemoved( Request request )
76  	{
77  		SoapUITreeNode requestTreeNode = getTreeModel().getTreeNode( request );
78  		if( requestNodes.contains( requestTreeNode ) )
79  		{
80  			getTreeModel().notifyNodeRemoved( requestTreeNode );
81  			requestNodes.remove( requestTreeNode );
82  			request.removePropertyChangeListener( propertyChangeListener );
83  		}
84  		else
85  			throw new RuntimeException( "Removing unknown request" );
86  	}
87  	
88  	public void release()
89  	{
90  		super.release();
91  
92  		for( RequestTreeNode treeNode : requestNodes )
93  		{
94  			treeNode.getModelItem().removePropertyChangeListener( Request.NAME_PROPERTY, propertyChangeListener );
95  			treeNode.release();
96  		}
97  	}
98  
99  	public void propertyChange( PropertyChangeEvent evt )
100 	{
101 		super.propertyChange( evt );
102 		if( evt.getPropertyName().equals( "childRequests" ) )
103 		{
104 			if( evt.getNewValue() != null )
105 			{
106 				requestAdded( ( RestRequest )evt.getNewValue() );
107 			}
108 			else
109 			{
110 				requestRemoved( ( RestRequest )evt.getOldValue() );
111 			}
112 		}
113 	}
114 }