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.ui.desktop;
014:
015: import java.util.HashSet;
016: import java.util.Set;
017:
018: import com.eviware.soapui.model.ModelItem;
019: import com.eviware.soapui.model.iface.Interface;
020: import com.eviware.soapui.model.iface.Operation;
021: import com.eviware.soapui.model.iface.Request;
022: import com.eviware.soapui.model.mock.MockOperation;
023: import com.eviware.soapui.model.mock.MockResponse;
024: import com.eviware.soapui.model.mock.MockService;
025: import com.eviware.soapui.model.project.Project;
026: import com.eviware.soapui.model.support.InterfaceListenerAdapter;
027: import com.eviware.soapui.model.support.MockServiceListenerAdapter;
028: import com.eviware.soapui.model.support.ProjectListenerAdapter;
029: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
030: import com.eviware.soapui.model.support.WorkspaceListenerAdapter;
031: import com.eviware.soapui.model.testsuite.LoadTest;
032: import com.eviware.soapui.model.testsuite.TestCase;
033: import com.eviware.soapui.model.testsuite.TestStep;
034: import com.eviware.soapui.model.testsuite.TestSuite;
035: import com.eviware.soapui.model.workspace.Workspace;
036: import com.eviware.soapui.support.action.swing.ActionList;
037:
038: /**
039: * Abstract SoapUIDesktop implementation for extension
040: *
041: * @author ole.matzura
042: */
043:
044: public abstract class AbstractSoapUIDesktop implements SoapUIDesktop {
045: private final Workspace workspace;
046: private final InternalProjectListener projectListener = new InternalProjectListener();
047: private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
048: private final InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
049: private final InternalMockServiceListener mockServiceListener = new InternalMockServiceListener();
050: private Set<DesktopListener> listeners = new HashSet<DesktopListener>();
051: private InternalWorkspaceListener workspaceListener = new InternalWorkspaceListener();
052:
053: public AbstractSoapUIDesktop(Workspace workspace) {
054: this .workspace = workspace;
055:
056: initListeners();
057: }
058:
059: private void initListeners() {
060: workspace.addWorkspaceListener(workspaceListener);
061:
062: for (int c = 0; c < workspace.getProjectCount(); c++) {
063: listenToProject(workspace.getProjectAt(c));
064: }
065: }
066:
067: public ActionList getActions() {
068: return null;
069: }
070:
071: private void listenToProject(Project project) {
072: project.addProjectListener(projectListener);
073:
074: for (int i = 0; i < project.getInterfaceCount(); i++) {
075: project.getInterfaceAt(i).addInterfaceListener(
076: interfaceListener);
077: }
078:
079: for (int i = 0; i < project.getTestSuiteCount(); i++) {
080: project.getTestSuiteAt(i).addTestSuiteListener(
081: testSuiteListener);
082: }
083:
084: for (int i = 0; i < project.getMockServiceCount(); i++) {
085: project.getMockServiceAt(i).addMockServiceListener(
086: mockServiceListener);
087: }
088: }
089:
090: public void addDesktopListener(DesktopListener listener) {
091: listeners.add(listener);
092: }
093:
094: public void removeDesktopListener(DesktopListener listener) {
095: listeners.remove(listener);
096: }
097:
098: public void closeDependantPanels(ModelItem modelItem) {
099: DesktopPanel[] panels = getDesktopPanels();
100:
101: for (int c = 0; c < panels.length; c++) {
102: if (panels[c].dependsOn(modelItem)) {
103: closeDesktopPanel(panels[c]);
104: }
105: }
106: }
107:
108: protected void fireDesktopPanelCreated(DesktopPanel desktopPanel) {
109: if (!listeners.isEmpty()) {
110: DesktopListener[] array = listeners
111: .toArray(new DesktopListener[listeners.size()]);
112: for (DesktopListener listener : array)
113: listener.desktopPanelCreated(desktopPanel);
114: }
115: }
116:
117: protected void fireDesktopPanelSelected(DesktopPanel desktopPanel) {
118: if (!listeners.isEmpty()) {
119: DesktopListener[] array = listeners
120: .toArray(new DesktopListener[listeners.size()]);
121: for (DesktopListener listener : array)
122: listener.desktopPanelSelected(desktopPanel);
123: }
124: }
125:
126: protected void fireDesktopPanelClosed(DesktopPanel desktopPanel) {
127: if (!listeners.isEmpty()) {
128: DesktopListener[] array = listeners
129: .toArray(new DesktopListener[listeners.size()]);
130: for (DesktopListener listener : array)
131: listener.desktopPanelClosed(desktopPanel);
132: }
133: }
134:
135: private class InternalWorkspaceListener extends
136: WorkspaceListenerAdapter {
137: public void projectRemoved(Project project) {
138: project.removeProjectListener(projectListener);
139: closeDependantPanels(project);
140: }
141:
142: public void projectAdded(Project project) {
143: listenToProject(project);
144: }
145: }
146:
147: private class InternalProjectListener extends
148: ProjectListenerAdapter {
149: public void interfaceRemoved(Interface iface) {
150: iface.removeInterfaceListener(interfaceListener);
151: closeDependantPanels(iface);
152: }
153:
154: public void testSuiteRemoved(TestSuite testSuite) {
155: testSuite.removeTestSuiteListener(testSuiteListener);
156: closeDependantPanels(testSuite);
157: }
158:
159: public void interfaceAdded(Interface iface) {
160: iface.addInterfaceListener(interfaceListener);
161: }
162:
163: public void testSuiteAdded(TestSuite testSuite) {
164: testSuite.addTestSuiteListener(testSuiteListener);
165: }
166:
167: public void mockServiceAdded(MockService mockService) {
168: mockService.addMockServiceListener(mockServiceListener);
169: }
170:
171: public void mockServiceRemoved(MockService mockService) {
172: mockService.removeMockServiceListener(mockServiceListener);
173: closeDependantPanels(mockService);
174: }
175: }
176:
177: private class InternalInterfaceListener extends
178: InterfaceListenerAdapter {
179: public void operationRemoved(Operation operation) {
180: closeDependantPanels(operation);
181: }
182:
183: public void requestRemoved(Request request) {
184: closeDependantPanels(request);
185: }
186: }
187:
188: private class InternalTestSuiteListener extends
189: TestSuiteListenerAdapter {
190: public void testCaseRemoved(TestCase testCase) {
191: closeDependantPanels(testCase);
192: }
193:
194: public void testStepRemoved(TestStep testStep, int index) {
195: closeDependantPanels(testStep);
196: }
197:
198: public void loadTestRemoved(LoadTest loadTest) {
199: closeDependantPanels(loadTest);
200: }
201: }
202:
203: private class InternalMockServiceListener extends
204: MockServiceListenerAdapter {
205: public void mockOperationRemoved(MockOperation operation) {
206: closeDependantPanels(operation);
207: }
208:
209: public void mockResponseRemoved(MockResponse request) {
210: closeDependantPanels(request);
211: }
212: }
213:
214: public void release() {
215: for (int c = 0; c < workspace.getProjectCount(); c++) {
216: Project project = workspace.getProjectAt(c);
217: project.removeProjectListener(projectListener);
218:
219: for (int i = 0; i < project.getInterfaceCount(); i++) {
220: project.getInterfaceAt(i).removeInterfaceListener(
221: interfaceListener);
222: }
223:
224: for (int i = 0; i < project.getTestSuiteCount(); i++) {
225: project.getTestSuiteAt(i).removeTestSuiteListener(
226: testSuiteListener);
227: }
228:
229: for (int i = 0; i < project.getMockServiceCount(); i++) {
230: project.getMockServiceAt(i).removeMockServiceListener(
231: mockServiceListener);
232: }
233: }
234:
235: workspace.removeWorkspaceListener(workspaceListener);
236: }
237: }
|