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;
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    public void notifyNodeChanged( SoapUITreeNode treeNode )
169    {
170       SoapUITreeNode parent = treeNode.getParentTreeNode();
171       if( parent == null )
172       {
173          notifyNodesChanged( new TreeModelEvent( this, new Object[] {treeNode} ));
174          return;
175       }
176       
177       int ix = parent.getIndexOfChild( treeNode );
178       
179       if( ix == -1 )
180       {
181          logger.error( "Changed node [" + treeNode + "] not found in parent [" + parent + "]" );
182          return;
183       }
184       
185       notifyNodesChanged( new TreeModelEvent( 
186             this, getPath( parent ), 
187             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
188    }
189 
190    public void notifyNodeInserted( SoapUITreeNode treeNode )
191    {
192       SoapUITreeNode parent = treeNode.getParentTreeNode();
193       int ix = parent.getIndexOfChild( treeNode );
194       
195       if( ix == -1 )
196       {
197          logger.error( "Inserted node [" + treeNode + "] not found in parent [" + parent + "]" );
198          return;
199       }
200       
201       mapModelItem( treeNode );
202       
203       notifyNodesInserted( new TreeModelEvent( 
204             this, getPath( parent ), 
205             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
206    }
207 
208    public void notifyNodeRemoved( SoapUITreeNode treeNode )
209    {
210       SoapUITreeNode parent = treeNode.getParentTreeNode();
211       int ix = parent.getIndexOfChild( treeNode);
212       
213       if( ix == -1 )
214       {
215          logger.error( "Removed node [" + treeNode + "] not found in parent [" + parent + "]" );
216          return;
217       }
218       
219       notifyNodesRemoved( new TreeModelEvent( 
220             this, getPath( parent ), 
221             new int[] {ix}, new Object[]{parent.getChildNode( ix )}));
222       
223       treeNode.release();
224    }
225 
226 	public SoapUITreeNode getTreeNode(ModelItem parentItem)
227 	{
228 		return modelItemMap.get( parentItem );
229 	}
230 
231 	public TreePath getPath(ModelItem modelItem)
232 	{
233 		return getPath( modelItemMap.get( modelItem ));
234 	}
235 
236 	public void mapModelItems(List<? extends SoapUITreeNode> treeNodes)
237 	{
238 		Iterator<? extends SoapUITreeNode> iterator = treeNodes.iterator();
239 		while( iterator.hasNext() )
240 		{
241 			SoapUITreeNode item = iterator.next();
242 			mapModelItem( item );
243 		}
244 	}
245 
246 }