View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.impl.rest.RestResource;
16  import com.eviware.soapui.model.tree.SoapUITreeModel;
17  import com.eviware.soapui.model.tree.SoapUITreeNode;
18  import com.eviware.soapui.model.tree.TreeNodeFactory;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /***
24   * SoapUITreeNode for Operation implementations
25   *
26   * @author Ole.Matzura
27   */
28  
29  public class RestResourceTreeNode extends OperationTreeNode
30  {
31     private List<RestResourceTreeNode> resourceNodes = new ArrayList<RestResourceTreeNode>();
32     private final RestResource restResource;
33  
34     public RestResourceTreeNode( RestResource restResource, SoapUITreeModel treeModel )
35     {
36        super( restResource, treeModel );
37        this.restResource = restResource;
38  
39        for( int c = 0; c < restResource.getChildResourceCount(); c++ )
40        {
41           resourceNodes.add( new RestResourceTreeNode( restResource.getChildResourcetAt( c ), getTreeModel() ) );
42        }
43  
44        treeModel.mapModelItems( resourceNodes );
45     }
46  
47     @Override
48     public SoapUITreeNode getParentTreeNode()
49     {
50        return restResource.getParentResource() == null ? super.getParentTreeNode() :
51                getTreeModel().getTreeNode( restResource.getParentResource() );
52     }
53  
54     @Override
55     public String toString()
56     {
57        return restResource.getName() + " [" + restResource.getFullPath() + "]";
58     }
59  
60     @Override
61     public int getChildCount()
62     {
63        return super.getChildCount() + restResource.getChildResourceCount();
64     }
65  
66     @Override
67     public SoapUITreeNode getChildNode( int index )
68     {
69        int childCount = super.getChildCount();
70        if( index < childCount )
71           return super.getChildNode( index );
72        else
73           return resourceNodes.get( index - childCount );
74     }
75  
76     @Override
77     public int getIndexOfChild( Object child )
78     {
79        int result = super.getIndexOfChild( child );
80        if( result == -1 )
81        {
82           result = resourceNodes.indexOf( child );
83           if( result >= 0 )
84              result += super.getChildCount();
85        }
86  
87        return result;
88     }
89  
90     public void release()
91     {
92        super.release();
93     }
94  
95     public void addChildResource( RestResource restResource )
96     {
97        RestResourceTreeNode operationTreeNode = (RestResourceTreeNode) TreeNodeFactory.createTreeNode( restResource, getTreeModel() );
98  
99        resourceNodes.add( operationTreeNode );
100       getTreeModel().notifyNodeInserted( operationTreeNode );
101    }
102 
103    public void removeChildResource( RestResourceTreeNode childResource )
104    {
105       if( resourceNodes.contains( childResource ) )
106       {
107          getTreeModel().notifyNodeRemoved( childResource );
108          resourceNodes.remove( childResource );
109       }
110    }
111 }