001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.model.tree.nodes;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import javax.swing.event.TreeModelEvent;
019:
020: import com.eviware.soapui.model.project.Project;
021: import com.eviware.soapui.model.support.WorkspaceListenerAdapter;
022: import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
023: import com.eviware.soapui.model.tree.SoapUITreeModel;
024: import com.eviware.soapui.model.tree.SoapUITreeNode;
025: import com.eviware.soapui.model.workspace.Workspace;
026: import com.eviware.soapui.settings.UISettings;
027:
028: /**
029: * SoapUITreeNode for Workspace implementations
030: *
031: * @author Ole.Matzura
032: */
033:
034: public class WorkspaceTreeNode extends
035: AbstractModelItemTreeNode<Workspace> {
036: private InternalWorkspaceListener workspaceListener = new InternalWorkspaceListener();
037: private List<ProjectTreeNode> projectNodes = new ArrayList<ProjectTreeNode>();
038: private ReorderPropertyChangeListener propertyChangeListener = new ReorderPropertyChangeListener();
039:
040: public WorkspaceTreeNode(Workspace workspace,
041: SoapUITreeModel treeModel) {
042: super (workspace, null, treeModel);
043:
044: workspace.addWorkspaceListener(workspaceListener);
045:
046: for (int c = 0; c < workspace.getProjectCount(); c++) {
047: Project project = workspace.getProjectAt(c);
048: project.addPropertyChangeListener(Project.NAME_PROPERTY,
049: propertyChangeListener);
050: projectNodes.add(new ProjectTreeNode(project, this ));
051: }
052:
053: initOrdering(projectNodes, UISettings.ORDER_PROJECTS);
054: getTreeModel().mapModelItems(projectNodes);
055: }
056:
057: public void release() {
058: super .release();
059: getWorkspace().removeWorkspaceListener(workspaceListener);
060:
061: for (ProjectTreeNode treeNode : projectNodes) {
062: treeNode.getModelItem().removePropertyChangeListener(
063: propertyChangeListener);
064: treeNode.release();
065: }
066: }
067:
068: public Workspace getWorkspace() {
069: return (Workspace) getModelItem();
070: }
071:
072: private class InternalWorkspaceListener extends
073: WorkspaceListenerAdapter {
074: public void projectAdded(Project project) {
075: ProjectTreeNode projectTreeNode = new ProjectTreeNode(
076: project, WorkspaceTreeNode.this );
077: projectNodes.add(projectTreeNode);
078: project.addPropertyChangeListener(Project.NAME_PROPERTY,
079: propertyChangeListener);
080: reorder(false);
081: getTreeModel().notifyNodeInserted(projectTreeNode);
082: }
083:
084: public void projectRemoved(Project project) {
085: SoapUITreeNode treeNode = getTreeModel().getTreeNode(
086: project);
087: if (projectNodes.contains(treeNode)) {
088: getTreeModel().notifyNodeRemoved(treeNode);
089: projectNodes.remove(treeNode);
090: project
091: .removePropertyChangeListener(propertyChangeListener);
092: } else
093: throw new RuntimeException("Removing unkown project");
094: }
095:
096: public void projectChanged(Project project) {
097: getTreeModel().notifyStructureChanged(
098: new TreeModelEvent(WorkspaceTreeNode.this ,
099: new Object[] { getTreeModel().getPath(
100: WorkspaceTreeNode.this) }));
101: }
102: }
103: }
|