View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 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.HashMap;
17  import java.util.HashSet;
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import javax.swing.event.TreeModelEvent;
24  import javax.swing.event.TreeModelListener;
25  import javax.swing.tree.TreeModel;
26  import javax.swing.tree.TreePath;
27  
28  import org.apache.log4j.Logger;
29  
30  import com.eviware.soapui.model.ModelItem;
31  import com.eviware.soapui.model.tree.nodes.WorkspaceTreeNode;
32  import com.eviware.soapui.model.workspace.Workspace;
33  
34  /***
35   * The Navigator TreeModel
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class SoapUITreeModel implements TreeModel
41  {
42     private Set<TreeModelListener> listeners = new HashSet<TreeModelListener>();
43     private SoapUITreeNode workspaceNode;
44     private final static Logger logger = Logger.getLogger( SoapUITreeModel.class );
45     private Map<ModelItem,SoapUITreeNode> modelItemMap = new HashMap<ModelItem,SoapUITreeNode>();
46  
47     public SoapUITreeModel(Workspace workspace)
48     {
49        workspaceNode = new WorkspaceTreeNode( workspace, this );
50        mapModelItem( workspaceNode );
51     }
52  
53     public Object getRoot()
54     {
55        return workspaceNode;
56     }
57  
58     public Object getChild(Object parent, int index)
59     {
60        SoapUITreeNode treeNode = (SoapUITreeNode) parent;
61        return treeNode.getChildNode( index );
62     }
63  
64     public int getChildCount(Object parent)
65     {
66        SoapUITreeNode treeNode = (SoapUITreeNode) parent;
67        return treeNode.getChildCount();
68     }
69  
70     public boolean isLeaf(Object node)
71     {
72        SoapUITreeNode treeNode = (SoapUITreeNode) node;
73        return treeNode.isLeaf();
74     }
75  
76     public void valueForPathChanged(TreePath path, Object newValue)
77     {
78        SoapUITreeNode treeNode = (SoapUITreeNode) path.getLastPathComponent();
79        if( treeNode.valueChanged( newValue ))
80        {
81        	// not implemented.. need to expose setName in ModelItem
82        }
83     }
84  
85     public int getIndexOfChild(Object parent, Object child)
86     {
87        SoapUITreeNode treeNode = (SoapUITreeNode) parent;
88        return treeNode.getIndexOfChild( child );
89     }
90  
91     public void addTreeModelListener(TreeModelListener l)
92     {
93        listeners.add( l );
94     }
95  
96     public void removeTreeModelListener(TreeModelListener l)
97     {
98        listeners.remove( l );
99     }
100    
101    public void mapModelItem( SoapUITreeNode soapUITreeNode )
102    {
103    	modelItemMap.put( soapUITreeNode.getModelItem(), soapUITreeNode );
104    }
105    
106    public void unmapModelItem( ModelItem modelItem )
107    {
108    	if( modelItemMap.containsKey( modelItem ))
109    		modelItemMap.remove( modelItem );
110    	else
111    		logger.error( "Failed to unmap model item [" + modelItem.getName() + "]" );
112    }
113    
114    public void notifyNodesInserted( TreeModelEvent e )
115    {
116       Iterator<TreeModelListener> i = listeners.iterator();
117       while( i.hasNext() )
118       {
119          i.next().treeNodesInserted( e );
120       }
121    }
122    
123    public void notifyNodesRemoved( TreeModelEvent e )
124    {
125       Iterator<TreeModelListener> i = listeners.iterator();
126       while( i.hasNext() )
127       {
128          i.next().treeNodesRemoved( e );
129       }
130    }
131    public void notifyStructureChanged( TreeModelEvent e )
132    {
133       Iterator<TreeModelListener> i = listeners.iterator();
134       while( i.hasNext() )
135       {
136          i.next().treeStructureChanged( e );
137       }
138    }
139    
140    public void notifyNodesChanged(  TreeModelEvent e )
141    {
142       Iterator<TreeModelListener> i = listeners.iterator();
143       while( i.hasNext() )
144       {
145          i.next().treeNodesChanged( e );
146       }
147    }
148 
149    public TreePath getPath(SoapUITreeNode treeNode)
150    {
151       //SoapUITreeNode treeNode = modelItemMap.get( modelItem );
152       //if( treeNode == null )
153       //	throw new RuntimeException( "Missing mapping for modelItem " + modelItem.getName() );
154 
155       List<Object> nodes = new ArrayList<Object>();
156       nodes.add( treeNode );
157       
158       treeNode = treeNode.getParentTreeNode();
159       while( treeNode != null )
160       {
161          nodes.add( 0, treeNode );
162          treeNode = treeNode.getParentTreeNode();
163       }
164       
165       return new TreePath( nodes.toArray() );
166       
167    }
168    
169    public void notifyNodeChanged( SoapUITreeNode treeNode )
170    {
171       SoapUITreeNode parent = treeNode.getParentTreeNode();
172       int ix = parent.getIndexOfChild( treeNode );
173       
174       if( ix == -1 )
175       {
176          logger.error( "Changed node [" + treeNode + "] not found in parent [" + parent + "]" );
177          return;
178       }
179       
180       notifyNodesChanged( new TreeModelEvent( 
181             this, getPath( parent ), 
182             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
183    }
184 
185    public void notifyNodeInserted( SoapUITreeNode treeNode )
186    {
187       SoapUITreeNode parent = treeNode.getParentTreeNode();
188       int ix = parent.getIndexOfChild( treeNode );
189       
190       if( ix == -1 )
191       {
192          logger.error( "Inserted node [" + treeNode + "] not found in parent [" + parent + "]" );
193          return;
194       }
195       
196       mapModelItem( treeNode );
197       
198       notifyNodesInserted( new TreeModelEvent( 
199             this, getPath( parent ), 
200             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
201    }
202 
203    public void notifyNodeRemoved( SoapUITreeNode treeNode )
204    {
205       SoapUITreeNode parent = treeNode.getParentTreeNode();
206       int ix = parent.getIndexOfChild( treeNode);
207       
208       if( ix == -1 )
209       {
210          logger.error( "Removed node [" + treeNode + "] not found in parent [" + parent + "]" );
211          return;
212       }
213       
214       unmapModelItem( treeNode.getModelItem() );
215 
216       notifyNodesRemoved( new TreeModelEvent( 
217             this, getPath( parent ), 
218             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
219       
220       treeNode.release();
221    }
222 
223 	public SoapUITreeNode getTreeNode(ModelItem parentItem)
224 	{
225 		return modelItemMap.get( parentItem );
226 	}
227 
228 	public TreePath getPath(ModelItem modelItem)
229 	{
230 		return getPath( modelItemMap.get( modelItem ));
231 	}
232 
233 	public void mapModelItems(List<? extends SoapUITreeNode> treeNodes)
234 	{
235 		Iterator<? extends SoapUITreeNode> iterator = treeNodes.iterator();
236 		while( iterator.hasNext() )
237 		{
238 			SoapUITreeNode item = iterator.next();
239 			mapModelItem( item );
240 		}
241 	}
242 
243 }