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.nodes;
    14
    15  import java.util.ArrayList;
    16  import java.util.List;
    17
    18  import javax.swing.event.TreeModelEvent;
    19
    20  import com.eviware.soapui.model.project.Project;
    21  import com.eviware.soapui.model.support.WorkspaceListenerAdapter;
    22  import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
    23  import com.eviware.soapui.model.tree.SoapUITreeModel;
    24  import com.eviware.soapui.model.tree.SoapUITreeNode;
    25  import com.eviware.soapui.model.workspace.Workspace;
    26  import com.eviware.soapui.settings.UISettings;
    27
    28  /***
    29   * SoapUITreeNode for Workspace implementations
    30   *
    31   * @author Ole.Matzura
    32   */
    33
    34  public class WorkspaceTreeNode extends AbstractModelItemTreeNode<Workspace>
    35  {
    36  	private InternalWorkspaceListener workspaceListener = new InternalWorkspaceListener();
    37  	private List<ProjectTreeNode> projectNodes = new ArrayList<ProjectTreeNode>();
    38  	private ReorderPropertyChangeListener propertyChangeListener = new ReorderPropertyChangeListener();
    39
    40  	public WorkspaceTreeNode( Workspace workspace, SoapUITreeModel treeModel  )
    41     {
    42        super( workspace, null, treeModel );
    43
    44  		workspace.addWorkspaceListener( workspaceListener );
    45
    46  		for( int c = 0; c < workspace.getProjectCount(); c++ )
    47  		{
    48  			Project project = workspace.getProjectAt( c );
    49  			project.addPropertyChangeListener( Project.NAME_PROPERTY, propertyChangeListener );
    50  			projectNodes.add( new ProjectTreeNode( project, this ));
    51  		}
    52
    53  		initOrdering( projectNodes, UISettings.ORDER_PROJECTS );
    54  		getTreeModel().mapModelItems( projectNodes );
    55     }
    56
    57     public void release()
    58     {
    59     	super.release();
    60     	getWorkspace().removeWorkspaceListener( workspaceListener );
    61
    62     	for( ProjectTreeNode treeNode : projectNodes )
    63     	{
    64     		treeNode.getModelItem().removePropertyChangeListener( propertyChangeListener );
    65     		treeNode.release();
    66     	}
    67     }
    68
    69     public Workspace getWorkspace()
    70     {
    71        return (Workspace) getModelItem();
    72     }
    73
    74     private class InternalWorkspaceListener extends WorkspaceListenerAdapter
    75     {
    76        public void projectAdded(Project project)
    77        {
    78        	ProjectTreeNode projectTreeNode = new ProjectTreeNode( project, WorkspaceTreeNode.this );
    79  			projectNodes.add( projectTreeNode);
    80  			project.addPropertyChangeListener( Project.NAME_PROPERTY, propertyChangeListener );
    81  			reorder( false );
    82        	getTreeModel().notifyNodeInserted( projectTreeNode );
    83        }
    84
    85        public void projectRemoved(Project project)
    86        {
    87        	SoapUITreeNode treeNode = getTreeModel().getTreeNode( project );
    88  			if( projectNodes.contains( treeNode))
    89        	{
    90        		getTreeModel().notifyNodeRemoved( treeNode );
    91        		projectNodes.remove( treeNode );
    92        		project.removePropertyChangeListener( propertyChangeListener );
    93        	}
    94  			else throw new RuntimeException( "Removing unkown project" );
    95        }
    96
    97        public void projectChanged( Project project )
    98        {
    99        	getTreeModel().notifyStructureChanged( new TreeModelEvent( WorkspaceTreeNode.this,
    100                new Object[] { getTreeModel().getPath( WorkspaceTreeNode.this ) } ));
    101       }
    102    }
    103 }