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 com.eviware.soapui.model.iface.Interface;
019: import com.eviware.soapui.model.mock.MockService;
020: import com.eviware.soapui.model.project.Project;
021: import com.eviware.soapui.model.project.ProjectListener;
022: import com.eviware.soapui.model.testsuite.TestSuite;
023: import com.eviware.soapui.model.tree.AbstractModelItemTreeNode;
024: import com.eviware.soapui.model.tree.SoapUITreeNode;
025: import com.eviware.soapui.settings.UISettings;
026:
027: /**
028: * SoapUITreeNode for Project implementations
029: *
030: * @author Ole.Matzura
031: */
032:
033: public class ProjectTreeNode extends AbstractModelItemTreeNode<Project> {
034: private InternalProjectListener internalProjectListener;
035: private List<InterfaceTreeNode> interfaceNodes = new ArrayList<InterfaceTreeNode>();
036: private List<TestSuiteTreeNode> testSuiteNodes = new ArrayList<TestSuiteTreeNode>();
037: private List<MockServiceTreeNode> mockServiceNodes = new ArrayList<MockServiceTreeNode>();
038:
039: public ProjectTreeNode(Project project,
040: WorkspaceTreeNode workspaceNode) {
041: super (project, workspaceNode.getWorkspace(), workspaceNode
042: .getTreeModel());
043:
044: internalProjectListener = new InternalProjectListener();
045: project.addProjectListener(internalProjectListener);
046:
047: for (int c = 0; c < project.getInterfaceCount(); c++) {
048: interfaceNodes.add(new InterfaceTreeNode(project
049: .getInterfaceAt(c), getTreeModel()));
050: }
051:
052: for (int c = 0; c < project.getTestSuiteCount(); c++) {
053: testSuiteNodes.add(new TestSuiteTreeNode(project
054: .getTestSuiteAt(c), getTreeModel()));
055: }
056:
057: for (int c = 0; c < project.getMockServiceCount(); c++) {
058: mockServiceNodes.add(new MockServiceTreeNode(project
059: .getMockServiceAt(c), getTreeModel()));
060: }
061:
062: getTreeModel().mapModelItems(interfaceNodes);
063: getTreeModel().mapModelItems(testSuiteNodes);
064: getTreeModel().mapModelItems(mockServiceNodes);
065:
066: initOrdering(testSuiteNodes, UISettings.ORDER_TESTSUITES);
067: }
068:
069: public void release() {
070: super .release();
071:
072: getProject().removeProjectListener(internalProjectListener);
073:
074: for (InterfaceTreeNode treeNode : interfaceNodes)
075: treeNode.release();
076:
077: for (TestSuiteTreeNode treeNode : testSuiteNodes)
078: treeNode.release();
079:
080: for (MockServiceTreeNode treeNode : mockServiceNodes)
081: treeNode.release();
082: }
083:
084: public int getChildCount() {
085: return interfaceNodes.size() + testSuiteNodes.size()
086: + mockServiceNodes.size();
087: }
088:
089: public int getIndexOfChild(Object child) {
090: int index = interfaceNodes.indexOf(child);
091: if (index >= 0)
092: return index;
093:
094: index = testSuiteNodes.indexOf(child);
095: if (index >= 0)
096: return index + interfaceNodes.size();
097:
098: index = mockServiceNodes.indexOf(child);
099: if (index >= 0)
100: return index + interfaceNodes.size()
101: + testSuiteNodes.size();
102:
103: return -1;
104: }
105:
106: public SoapUITreeNode getChildNode(int index) {
107: if (index < interfaceNodes.size())
108: return interfaceNodes.get(index);
109: else if (index < testSuiteNodes.size() + interfaceNodes.size())
110: return testSuiteNodes.get(index - interfaceNodes.size());
111: else
112: return mockServiceNodes.get(index - interfaceNodes.size()
113: - testSuiteNodes.size());
114: }
115:
116: public Project getProject() {
117: return (Project) getModelItem();
118: }
119:
120: private class InternalProjectListener implements ProjectListener {
121: public void interfaceAdded(Interface iface) {
122: InterfaceTreeNode interfaceTreeNode = new InterfaceTreeNode(
123: iface, getTreeModel());
124: interfaceNodes.add(interfaceTreeNode);
125: getTreeModel().notifyNodeInserted(interfaceTreeNode);
126: }
127:
128: public void interfaceRemoved(Interface iface) {
129: SoapUITreeNode treeNode = getTreeModel().getTreeNode(iface);
130: if (interfaceNodes.contains(treeNode)) {
131: getTreeModel().notifyNodeRemoved(treeNode);
132: interfaceNodes.remove(treeNode);
133: } else
134: throw new RuntimeException("Removing unkown interface");
135: }
136:
137: public void testSuiteAdded(TestSuite testSuite) {
138: TestSuiteTreeNode testSuiteNode = new TestSuiteTreeNode(
139: testSuite, getTreeModel());
140: testSuiteNodes.add(testSuiteNode);
141: reorder(false);
142: getTreeModel().notifyNodeInserted(testSuiteNode);
143: }
144:
145: public void testSuiteRemoved(TestSuite testSuite) {
146: SoapUITreeNode treeNode = getTreeModel().getTreeNode(
147: testSuite);
148: if (testSuiteNodes.contains(treeNode)) {
149: getTreeModel().notifyNodeRemoved(treeNode);
150: testSuiteNodes.remove(treeNode);
151: } else
152: throw new RuntimeException("Removing unkown testSuite");
153: }
154:
155: public void mockServiceAdded(MockService mockService) {
156: MockServiceTreeNode mockServiceNode = new MockServiceTreeNode(
157: mockService, getTreeModel());
158: mockServiceNodes.add(mockServiceNode);
159: getTreeModel().notifyNodeInserted(mockServiceNode);
160: }
161:
162: public void mockServiceRemoved(MockService mockService) {
163: SoapUITreeNode treeNode = getTreeModel().getTreeNode(
164: mockService);
165: if (mockServiceNodes.contains(treeNode)) {
166: getTreeModel().notifyNodeRemoved(treeNode);
167: mockServiceNodes.remove(treeNode);
168: } else
169: throw new RuntimeException(
170: "Removing unkown mockService");
171: }
172: }
173:
174: }
|