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