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