View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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;
14  
15  import java.util.ArrayList;
16  import java.util.HashSet;
17  import java.util.Iterator;
18  import java.util.List;
19  import java.util.Set;
20  
21  import javax.swing.event.TreeModelEvent;
22  import javax.swing.event.TreeModelListener;
23  import javax.swing.tree.TreeModel;
24  import javax.swing.tree.TreePath;
25  
26  import org.apache.log4j.Logger;
27  
28  import com.eviware.soapui.model.ModelItem;
29  import com.eviware.soapui.model.workspace.Workspace;
30  
31  /***
32   * The Navigator TreeModel
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public class SoapUITreeModel implements TreeModel
38  {
39     private Workspace workspace;
40     private Set<TreeModelListener> listeners = new HashSet<TreeModelListener>();
41     private SoapUITreeNode workspaceNode;
42     private final static Logger logger = Logger.getLogger( SoapUITreeModel.class );
43  
44     public SoapUITreeModel(Workspace workspace)
45     {
46        super();
47        this.workspace = workspace;
48        workspaceNode = workspace.getTreeNode();
49     }
50  
51     public Object getRoot()
52     {
53        return workspaceNode;
54     }
55  
56     public Object getChild(Object parent, int index)
57     {
58        SoapUITreeNode treeNode = (SoapUITreeNode) parent;
59        return treeNode.getChildNode( index );
60     }
61  
62     public int getChildCount(Object parent)
63     {
64        SoapUITreeNode treeNode = (SoapUITreeNode) parent;
65        return treeNode.getChildCount();
66     }
67  
68     public boolean isLeaf(Object node)
69     {
70        SoapUITreeNode treeNode = (SoapUITreeNode) node;
71        return treeNode.isLeaf();
72     }
73  
74     public void valueForPathChanged(TreePath path, Object newValue)
75     {
76        SoapUITreeNode treeNode = (SoapUITreeNode) path.getLastPathComponent();
77        if( treeNode.valueChanged( newValue ))
78        {}
79     }
80  
81     public int getIndexOfChild(Object parent, Object child)
82     {
83        SoapUITreeNode treeNode = (SoapUITreeNode) parent;
84        return treeNode.getIndexOfChild( child );
85     }
86  
87     public void addTreeModelListener(TreeModelListener l)
88     {
89        listeners.add( l );
90     }
91  
92     public void removeTreeModelListener(TreeModelListener l)
93     {
94        listeners.remove( l );
95     }
96     
97     public void notifyNodesInserted( TreeModelEvent e )
98     {
99        Iterator<TreeModelListener> i = listeners.iterator();
100       while( i.hasNext() )
101       {
102          i.next().treeNodesInserted( e );
103       }
104    }
105    
106    public void notifyNodesRemoved( TreeModelEvent e )
107    {
108       Iterator<TreeModelListener> i = listeners.iterator();
109       while( i.hasNext() )
110       {
111          i.next().treeNodesRemoved( e );
112       }
113    }
114    public void notifyStructureChanged( TreeModelEvent e )
115    {
116       Iterator<TreeModelListener> i = listeners.iterator();
117       while( i.hasNext() )
118       {
119          i.next().treeStructureChanged( e );
120       }
121    }
122    
123    public void notifyNodesChanged(  TreeModelEvent e )
124    {
125       Iterator<TreeModelListener> i = listeners.iterator();
126       while( i.hasNext() )
127       {
128          i.next().treeNodesChanged( e );
129       }
130    }
131 
132 /*
133 
134 /   public SoapUITreeNode getTreeNode(ModelItem modelItem)
135    {
136       if( !nodeCache.containsKey( modelItem )) 
137          nodeCache.put( modelItem, TreeNodeFactory.getInstance().createTreeNode( this, modelItem ) );
138       
139       return nodeCache.get( modelItem );
140    }
141 */
142    public TreePath getPath(ModelItem modelItem)
143    {
144       SoapUITreeNode treeNode = modelItem.getTreeNode();
145 
146       List<Object> nodes = new ArrayList<Object>();
147       nodes.add( treeNode );
148       
149       treeNode = treeNode.getParent();
150       while( treeNode != null )
151       {
152          nodes.add( 0, treeNode );
153          treeNode = treeNode.getParent();
154       }
155       
156       return new TreePath( nodes.toArray() );
157       
158    }
159    
160    public void notifyNodeChanged( SoapUITreeNode treeNode )
161    {
162       SoapUITreeNode parent = treeNode.getParent();
163       int ix = parent.getIndexOfChild( treeNode );
164       
165       if( ix == -1 )
166       {
167          logger.error( "Changed node [" + treeNode + "] not found in parent [" + parent + "]" );
168          return;
169       }
170       
171       notifyNodesChanged( new TreeModelEvent( 
172             this, getPath( parent.getModelItem() ), 
173             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
174    }
175 
176    public void notifyNodeInserted( SoapUITreeNode treeNode )
177    {
178       SoapUITreeNode parent = treeNode.getParent();
179       int ix = parent.getIndexOfChild( treeNode );
180       
181       if( ix == -1 )
182       {
183          logger.error( "Inserted node [" + treeNode + "] not found in parent [" + parent + "]" );
184          return;
185       }
186       
187       notifyNodesInserted( new TreeModelEvent( 
188             this, getPath( parent.getModelItem() ), 
189             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
190    }
191 
192    public void notifyNodeRemoved( SoapUITreeNode treeNode )
193    {
194       SoapUITreeNode parent = treeNode.getParent();
195       int ix = parent.getIndexOfChild( treeNode);
196       
197       if( ix == -1 )
198       {
199          logger.error( "Removed node [" + treeNode + "] not found in parent [" + parent + "]" );
200          return;
201       }
202       
203       notifyNodesRemoved( new TreeModelEvent( 
204             this, getPath( parent.getModelItem() ), 
205             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
206    }
207 
208 }