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.ui.desktop;
    14
    15  import java.util.HashSet;
    16  import java.util.Set;
    17
    18  import com.eviware.soapui.model.ModelItem;
    19  import com.eviware.soapui.model.iface.Interface;
    20  import com.eviware.soapui.model.iface.Operation;
    21  import com.eviware.soapui.model.iface.Request;
    22  import com.eviware.soapui.model.mock.MockOperation;
    23  import com.eviware.soapui.model.mock.MockResponse;
    24  import com.eviware.soapui.model.mock.MockService;
    25  import com.eviware.soapui.model.project.Project;
    26  import com.eviware.soapui.model.support.InterfaceListenerAdapter;
    27  import com.eviware.soapui.model.support.MockServiceListenerAdapter;
    28  import com.eviware.soapui.model.support.ProjectListenerAdapter;
    29  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
    30  import com.eviware.soapui.model.support.WorkspaceListenerAdapter;
    31  import com.eviware.soapui.model.testsuite.LoadTest;
    32  import com.eviware.soapui.model.testsuite.TestCase;
    33  import com.eviware.soapui.model.testsuite.TestStep;
    34  import com.eviware.soapui.model.testsuite.TestSuite;
    35  import com.eviware.soapui.model.workspace.Workspace;
    36  import com.eviware.soapui.support.action.swing.ActionList;
    37
    38  /***
    39   * Abstract SoapUIDesktop implementation for extension
    40   *
    41   * @author ole.matzura
    42   */
    43
    44  public abstract class AbstractSoapUIDesktop implements SoapUIDesktop
    45  {
    46  	private final Workspace workspace;
    47  	private final InternalProjectListener projectListener = new InternalProjectListener();
    48  	private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
    49  	private final InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
    50  	private final InternalMockServiceListener mockServiceListener = new InternalMockServiceListener();
    51  	private Set<DesktopListener> listeners = new HashSet<DesktopListener>();
    52  	private InternalWorkspaceListener workspaceListener = new InternalWorkspaceListener();
    53
    54  	public AbstractSoapUIDesktop(Workspace workspace)
    55  	{
    56  		this.workspace = workspace;
    57
    58  		initListeners();
    59  	}
    60
    61  	private void initListeners()
    62  	{
    63  		workspace.addWorkspaceListener(workspaceListener);
    64
    65  		for (int c = 0; c < workspace.getProjectCount(); c++)
    66  		{
    67  			listenToProject(workspace.getProjectAt(c));
    68  		}
    69  	}
    70
    71  	public ActionList getActions()
    72  	{
    73  		return null;
    74  	}
    75
    76  	private void listenToProject(Project project)
    77  	{
    78  		project.addProjectListener(projectListener);
    79
    80  		for (int i = 0; i < project.getInterfaceCount(); i++)
    81  		{
    82  			project.getInterfaceAt(i).addInterfaceListener(interfaceListener);
    83  		}
    84
    85  		for (int i = 0; i < project.getTestSuiteCount(); i++)
    86  		{
    87  			project.getTestSuiteAt(i).addTestSuiteListener(testSuiteListener);
    88  		}
    89
    90  		for (int i = 0; i < project.getMockServiceCount(); i++)
    91  		{
    92  			project.getMockServiceAt(i).addMockServiceListener(mockServiceListener);
    93  		}
    94  	}
    95
    96  	public void addDesktopListener(DesktopListener listener)
    97  	{
    98  		listeners.add(listener);
    99  	}
    100
    101 	public void removeDesktopListener(DesktopListener listener)
    102 	{
    103 		listeners.remove(listener);
    104 	}
    105
    106 	public void closeDependantPanels(ModelItem modelItem)
    107 	{
    108 		DesktopPanel[] panels = getDesktopPanels();
    109
    110 		for (int c = 0; c < panels.length; c++)
    111 		{
    112 			if (panels[c].dependsOn(modelItem))
    113 			{
    114 				closeDesktopPanel(panels[c]);
    115 			}
    116 		}
    117 	}
    118
    119 	protected void fireDesktopPanelCreated(DesktopPanel desktopPanel)
    120 	{
    121 		if (!listeners.isEmpty())
    122 		{
    123 			DesktopListener[] array = listeners.toArray(new DesktopListener[listeners.size()]);
    124 			for (DesktopListener listener : array)
    125 				listener.desktopPanelCreated(desktopPanel);
    126 		}
    127 	}
    128
    129 	protected void fireDesktopPanelSelected(DesktopPanel desktopPanel)
    130 	{
    131 		if (!listeners.isEmpty())
    132 		{
    133 			DesktopListener[] array = listeners.toArray(new DesktopListener[listeners.size()]);
    134 			for (DesktopListener listener : array)
    135 				listener.desktopPanelSelected(desktopPanel);
    136 		}
    137 	}
    138
    139 	protected void fireDesktopPanelClosed(DesktopPanel desktopPanel)
    140 	{
    141 		if (!listeners.isEmpty())
    142 		{
    143 			DesktopListener[] array = listeners.toArray(new DesktopListener[listeners.size()]);
    144 			for (DesktopListener listener : array)
    145 				listener.desktopPanelClosed(desktopPanel);
    146 		}
    147 	}
    148
    149 	private class InternalWorkspaceListener extends WorkspaceListenerAdapter
    150 	{
    151 		public void projectRemoved(Project project)
    152 		{
    153 			project.removeProjectListener(projectListener);
    154 			closeDependantPanels(project);
    155 		}
    156
    157 		public void projectAdded(Project project)
    158 		{
    159 			listenToProject( project );
    160 		}
    161 	}
    162
    163 	private class InternalProjectListener extends ProjectListenerAdapter
    164 	{
    165 		public void interfaceRemoved(Interface iface)
    166 		{
    167 			iface.removeInterfaceListener(interfaceListener);
    168 			closeDependantPanels(iface);
    169 		}
    170
    171 		public void testSuiteRemoved(TestSuite testSuite)
    172 		{
    173 			testSuite.removeTestSuiteListener(testSuiteListener);
    174 			closeDependantPanels(testSuite);
    175 		}
    176
    177 		public void interfaceAdded(Interface iface)
    178 		{
    179 			iface.addInterfaceListener(interfaceListener);
    180 		}
    181
    182 		public void testSuiteAdded(TestSuite testSuite)
    183 		{
    184 			testSuite.addTestSuiteListener(testSuiteListener);
    185 		}
    186
    187 		public void mockServiceAdded( MockService mockService )
    188 		{
    189 			mockService.addMockServiceListener( mockServiceListener );
    190 		}
    191
    192 		public void mockServiceRemoved( MockService mockService )
    193 		{
    194 			mockService.removeMockServiceListener( mockServiceListener );
    195 			closeDependantPanels( mockService );
    196 		}
    197 	}
    198
    199 	private class InternalInterfaceListener extends InterfaceListenerAdapter
    200 	{
    201 		public void operationRemoved(Operation operation)
    202 		{
    203 			closeDependantPanels(operation);
    204 		}
    205
    206 		public void requestRemoved(Request request)
    207 		{
    208 			closeDependantPanels(request);
    209 		}
    210 	}
    211
    212 	private class InternalTestSuiteListener extends TestSuiteListenerAdapter
    213 	{
    214 		public void testCaseRemoved(TestCase testCase)
    215 		{
    216 			closeDependantPanels(testCase);
    217 		}
    218
    219 		public void testStepRemoved(TestStep testStep, int index)
    220 		{
    221 			closeDependantPanels(testStep);
    222 		}
    223
    224 		public void loadTestRemoved(LoadTest loadTest)
    225 		{
    226 			closeDependantPanels(loadTest);
    227 		}
    228 	}
    229
    230 	private class InternalMockServiceListener extends MockServiceListenerAdapter
    231 	{
    232 		public void mockOperationRemoved( MockOperation operation )
    233 		{
    234 			closeDependantPanels( operation );
    235 		}
    236
    237 		public void mockResponseRemoved( MockResponse request )
    238 		{
    239 			closeDependantPanels( request );
    240 		}
    241 	}
    242
    243 	public void release()
    244 	{
    245 		for (int c = 0; c < workspace.getProjectCount(); c++)
    246 		{
    247 			Project project = workspace.getProjectAt( c );
    248 			project.removeProjectListener(projectListener);
    249
    250 			for (int i = 0; i < project.getInterfaceCount(); i++)
    251 			{
    252 				project.getInterfaceAt(i).removeInterfaceListener(interfaceListener);
    253 			}
    254
    255 			for (int i = 0; i < project.getTestSuiteCount(); i++)
    256 			{
    257 				project.getTestSuiteAt(i).removeTestSuiteListener(testSuiteListener);
    258 			}
    259
    260 			for (int i = 0; i < project.getMockServiceCount(); i++)
    261 			{
    262 				project.getMockServiceAt(i).removeMockServiceListener(mockServiceListener);
    263 			}
    264 		}
    265
    266 		workspace.removeWorkspaceListener( workspaceListener );
    267 	}
    268
    269 	public void init()
    270 	{
    271 	}
    272 }