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.impl.wsdl.panels.project;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ActionEvent;
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.Action;
24  import javax.swing.BorderFactory;
25  import javax.swing.JComponent;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTabbedPane;
29  import javax.swing.event.TreeModelEvent;
30  import javax.swing.event.TreeModelListener;
31  import javax.swing.text.Document;
32  
33  import com.eviware.soapui.SoapUI;
34  import com.eviware.soapui.impl.wsdl.WsdlProject;
35  import com.eviware.soapui.impl.wsdl.panels.iface.WSSTabPanel;
36  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.AbstractGroovyEditorModel;
37  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.PropertyHolderTable;
38  import com.eviware.soapui.model.ModelItem;
39  import com.eviware.soapui.model.iface.Interface;
40  import com.eviware.soapui.model.mock.MockOperation;
41  import com.eviware.soapui.model.mock.MockResponse;
42  import com.eviware.soapui.model.mock.MockService;
43  import com.eviware.soapui.model.project.Project;
44  import com.eviware.soapui.model.testsuite.Assertable;
45  import com.eviware.soapui.model.testsuite.LoadTest;
46  import com.eviware.soapui.model.testsuite.TestAssertion;
47  import com.eviware.soapui.model.testsuite.TestCase;
48  import com.eviware.soapui.model.testsuite.TestStep;
49  import com.eviware.soapui.model.testsuite.TestSuite;
50  import com.eviware.soapui.model.util.ModelItemIconFactory;
51  import com.eviware.soapui.support.DocumentListenerAdapter;
52  import com.eviware.soapui.support.UISupport;
53  import com.eviware.soapui.support.components.GroovyEditorComponent;
54  import com.eviware.soapui.support.components.GroovyEditorInspector;
55  import com.eviware.soapui.support.components.JComponentInspector;
56  import com.eviware.soapui.support.components.JFocusableComponentInspector;
57  import com.eviware.soapui.support.components.JInspectorPanel;
58  import com.eviware.soapui.support.components.JUndoableTextArea;
59  import com.eviware.soapui.support.components.MetricsPanel;
60  import com.eviware.soapui.support.components.MetricsPanel.MetricType;
61  import com.eviware.soapui.support.components.MetricsPanel.MetricsSection;
62  import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
63  
64  public class WsdlProjectDesktopPanel extends ModelItemDesktopPanel<WsdlProject>
65  {
66  	private static final String MOCKRESPONSES_STATISTICS = "MockResponses";
67  	private static final String MOCKOPERATIONS_STATISTICS = "MockOperations";
68  	private static final String MOCKSERVICES_STATISTICS = "MockServices";
69  	private static final String LOADTESTS_STATISTICS = "LoadTests";
70  	private static final String ASSERTIONS_STATISTICS = "Assertions";
71  	private static final String TESTSTEPS_STATISTICS = "TestSteps";
72  	private static final String TESTCASES_STATISTICS = "TestCases";
73  	private static final String TESTSUITES_STATISTICS = "TestSuites";
74  	private PropertyHolderTable propertiesTable;
75  	private JUndoableTextArea descriptionArea;
76  	private InternalTreeModelListener treeModelListener;
77  	private Set<String> interfaceNameSet = new HashSet<String>();
78  	private WSSTabPanel wssTabPanel;
79  	private MetricsPanel metrics;
80  	private GroovyEditorComponent loadScriptGroovyEditor;
81  	private GroovyEditorComponent saveScriptGroovyEditor;
82  
83  	public WsdlProjectDesktopPanel( WsdlProject modelItem )
84  	{
85  		super( modelItem );
86  
87  		add( buildTabbedPane(), BorderLayout.CENTER );
88  
89  		setPreferredSize( new Dimension( 600, 600 ) );
90  	}
91  
92  	private Component buildTabbedPane()
93  	{
94  		JTabbedPane mainTabs = new JTabbedPane();
95  		addTabs( mainTabs );
96  		return UISupport.createTabPanel( mainTabs, true );
97  	}
98  
99  	protected void addTabs( JTabbedPane mainTabs )
100 	{
101 		mainTabs.addTab( "Overview", null, buildOverviewTab(), "Shows General Project information and metrics" );
102 		mainTabs.addTab( "WS-Security Configurations", null, buildWSSTab(), "Manages WS-Security configurations");
103 	}
104 	
105 	private Component buildWSSTab()
106 	{
107 		wssTabPanel = new WSSTabPanel( getModelItem().getWssContainer() );
108 		return wssTabPanel;
109 	}
110 
111 	private Component buildOverviewTab()
112 	{
113 		JInspectorPanel inspectorPanel = new JInspectorPanel( buildProjectOverview() );
114 
115 		inspectorPanel.addInspector(  new JFocusableComponentInspector<JPanel>( buildDescriptionPanel(), 
116 					descriptionArea, "Description", "Project description", true ) );
117 		inspectorPanel.addInspector( new JComponentInspector( buildPropertiesPanel(), "Properties",
118 					"Project level properties", true ) );
119 		inspectorPanel.addInspector( new GroovyEditorInspector( buildLoadScriptPanel(), "Load Script",
120 					"Script to run after loading the project" ) );
121 		inspectorPanel.addInspector( new GroovyEditorInspector( buildSaveScriptPanel(), "Save Script",
122 					"Script to run before saving the project" ) );
123 
124 		inspectorPanel.setCurrentInspector( "Properties" );
125 
126 		treeModelListener = new InternalTreeModelListener();
127 		SoapUI.getNavigator().getMainTree().getModel().addTreeModelListener( treeModelListener );
128 
129 		updateStatistics();
130 
131 		return inspectorPanel;
132 	}
133 
134 	private void updateStatistics()
135 	{
136 		metrics.setMetric( "File Path", getModelItem().getPath() );
137 
138 		Set<String> newNames = new HashSet<String>();
139 		boolean rebuilt = false;
140 		for( Interface iface : getModelItem().getInterfaceList() )
141 		{
142 			if( !metrics.hasMetric( iface.getName() ) )
143 			{
144 				MetricsSection section = metrics.getSection( "Interface Summary" );
145 				buildInterfaceSummary( section.clear() );
146 				rebuilt = true;
147 				break;
148 			}
149 
150 			newNames.add( iface.getName() );
151 			interfaceNameSet.remove( iface.getName() );
152 		}
153 
154 		if( !rebuilt )
155 		{
156 			if( !interfaceNameSet.isEmpty() )
157 			{
158 				MetricsSection section = metrics.getSection( "Interface Summary" );
159 				buildInterfaceSummary( section.clear() );
160 			}
161 
162 			interfaceNameSet = newNames;
163 		}
164 
165 		metrics.setMetric( TESTSUITES_STATISTICS, getModelItem().getTestSuiteCount() );
166 
167 		int testCaseCount = 0;
168 		int testStepsCount = 0;
169 		int assertionsCount = 0;
170 		int loadTestsCount = 0;
171 
172 		for( TestSuite testSuite : getModelItem().getTestSuiteList() )
173 		{
174 			testCaseCount += testSuite.getTestCaseCount();
175 
176 			for( TestCase testCase : testSuite.getTestCaseList() )
177 			{
178 				testStepsCount += testCase.getTestStepCount();
179 				loadTestsCount += testCase.getLoadTestCount();
180 
181 				for( TestStep testStep : testCase.getTestStepList() )
182 				{
183 					if( testStep instanceof Assertable )
184 					{
185 						assertionsCount += ((Assertable)testStep).getAssertionCount();
186 					}
187 				}
188 			}
189 		}
190 
191 		metrics.setMetric( TESTCASES_STATISTICS, testCaseCount );
192 		metrics.setMetric( TESTSTEPS_STATISTICS, testStepsCount );
193 		metrics.setMetric( ASSERTIONS_STATISTICS, assertionsCount );
194 		metrics.setMetric( LOADTESTS_STATISTICS, loadTestsCount );
195 
196 		int mockOperationCount = 0;
197 		int mockResponseCount = 0;
198 
199 		for( MockService testSuite : getModelItem().getMockServiceList() )
200 		{
201 			mockOperationCount += testSuite.getMockOperationCount();
202 
203 			for( MockOperation testCase : testSuite.getMockOperationList() )
204 			{
205 				mockResponseCount += testCase.getMockResponseCount();
206 			}
207 		}
208 
209 		metrics.setMetric( MOCKSERVICES_STATISTICS, getModelItem().getMockServiceCount() );
210 		metrics.setMetric( MOCKOPERATIONS_STATISTICS, mockOperationCount );
211 		metrics.setMetric( MOCKRESPONSES_STATISTICS, mockResponseCount );
212 	}
213 
214 	private JComponent buildProjectOverview()
215 	{
216 		metrics = new MetricsPanel();
217 		
218 		MetricsSection section = metrics.addSection( "Project Summary" );
219 		section.addMetric( ModelItemIconFactory.getIcon( Project.class ), "File Path", MetricType.URL );
220 		section.finish();
221 		
222 		section = metrics.addSection( "Interface Summary" );
223 		buildInterfaceSummary( section );
224 		
225 		section = metrics.addSection( "Test Summary" );
226 		section.addMetric( ModelItemIconFactory.getIcon( TestSuite.class ), TESTSUITES_STATISTICS );
227 		section.addMetric( ModelItemIconFactory.getIcon( TestCase.class ), TESTCASES_STATISTICS  );
228 		section.addMetric( ModelItemIconFactory.getIcon( TestStep.class ), TESTSTEPS_STATISTICS );
229 		section.addMetric( ModelItemIconFactory.getIcon( TestAssertion.class ), ASSERTIONS_STATISTICS );
230 		section.addMetric( ModelItemIconFactory.getIcon( LoadTest.class ), LOADTESTS_STATISTICS );
231 		section.finish();
232 		
233 		section = metrics.addSection( "Mock Summary" );
234 		section.addMetric( ModelItemIconFactory.getIcon( MockService.class ), MOCKSERVICES_STATISTICS );
235 		section.addMetric( ModelItemIconFactory.getIcon( MockOperation.class ), MOCKOPERATIONS_STATISTICS );
236 		section.addMetric( ModelItemIconFactory.getIcon( MockResponse.class ), MOCKRESPONSES_STATISTICS );
237 		section.finish();
238 		
239 		return new JScrollPane( metrics );
240 	}
241 
242 	private void buildInterfaceSummary(MetricsSection section)
243 	{
244 		interfaceNameSet.clear();
245 		for( Interface iface : getModelItem().getInterfaceList() )
246 		{
247 			section.addMetric( iface.getIcon(), iface.getName(), MetricType.URL ).set( iface.getDefinition() );
248 			interfaceNameSet.add( iface.getName() );
249 		}
250 		
251 		section.finish();
252 	}
253 
254 	private JPanel buildDescriptionPanel()
255 	{
256 		JPanel panel = new JPanel( new BorderLayout() );
257 		descriptionArea = new JUndoableTextArea( getModelItem().getDescription() );
258 		descriptionArea.getDocument().addDocumentListener( new DocumentListenerAdapter()
259 		{
260 			@Override
261 			public void update( Document document )
262 			{
263 				getModelItem().setDescription( descriptionArea.getText() );
264 			}
265 		} );
266 
267 		panel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
268 		panel.add( new JScrollPane( descriptionArea ), BorderLayout.CENTER );
269 		UISupport.addTitledBorder( panel, "Project Description" );
270 
271 		return panel;
272 	}
273 
274 	protected GroovyEditorComponent buildLoadScriptPanel()
275 	{
276 		loadScriptGroovyEditor = new GroovyEditorComponent( new LoadScriptGroovyEditorModel(), null );
277 		return loadScriptGroovyEditor;
278 	}
279 
280 	protected GroovyEditorComponent buildSaveScriptPanel()
281 	{
282 		saveScriptGroovyEditor = new GroovyEditorComponent( new SaveScriptGroovyEditorModel(), null );
283 		return saveScriptGroovyEditor;
284 	}
285 
286 	private JComponent buildPropertiesPanel()
287 	{
288 		JPanel panel = new JPanel( new BorderLayout() );
289 		propertiesTable = new PropertyHolderTable( getModelItem() );
290 		panel.add( new JScrollPane( propertiesTable ), BorderLayout.CENTER );
291 		return panel;
292 	}
293 
294 	@Override
295 	public boolean dependsOn( ModelItem modelItem )
296 	{
297 		return modelItem == getModelItem();
298 	}
299 
300 	public boolean onClose( boolean canCancel )
301 	{
302 		propertiesTable.release();
303 		loadScriptGroovyEditor.getEditor().release();
304 		saveScriptGroovyEditor.getEditor().release();
305 		
306 		SoapUI.getNavigator().getMainTree().getModel().removeTreeModelListener( treeModelListener );
307 		wssTabPanel.release();
308 
309 		return release();
310 	}
311 
312 	private final class InternalTreeModelListener implements TreeModelListener
313 	{
314 		public void treeNodesChanged( TreeModelEvent e )
315 		{
316 			updateStatistics();
317 		}
318 
319 		public void treeNodesInserted( TreeModelEvent e )
320 		{
321 			updateStatistics();
322 		}
323 
324 		public void treeNodesRemoved( TreeModelEvent e )
325 		{
326 			updateStatistics();
327 		}
328 
329 		public void treeStructureChanged( TreeModelEvent e )
330 		{
331 			updateStatistics();
332 		}
333 	}
334 
335 	private class LoadScriptGroovyEditorModel extends AbstractGroovyEditorModel
336 	{
337 		public LoadScriptGroovyEditorModel()
338 		{
339 			super( new String[] { "log", "project" }, getModelItem().getSettings(), "Load" );
340 		}
341 
342 		@Override
343 		public String getScript()
344 		{
345 			return getModelItem().getAfterLoadScript();
346 		}
347 
348 		@Override
349 		public void setScript( String text )
350 		{
351 			getModelItem().setAfterLoadScript( text );
352 		}
353 
354 		@Override
355 		public Action getRunAction()
356 		{
357 			return new AfterLoadScriptRunAction();
358 		}
359 
360 		private final class AfterLoadScriptRunAction extends AbstractAction
361 		{
362 			public AfterLoadScriptRunAction()
363 			{
364 				putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ) );
365 				putValue( SHORT_DESCRIPTION, "Runs this script" );
366 			}
367 
368 			public void actionPerformed( ActionEvent e )
369 			{
370 				try
371 				{
372 					getModelItem().runAfterLoadScript();
373 				}
374 				catch( Exception e1 )
375 				{
376 					UISupport.showErrorMessage( e1 );
377 				}
378 			}
379 		}
380 	}
381 
382 	private class SaveScriptGroovyEditorModel extends AbstractGroovyEditorModel
383 	{
384 		public SaveScriptGroovyEditorModel()
385 		{
386 			super( new String[] { "log", "project" }, getModelItem().getSettings(), "Save" );
387 		}
388 
389 		@Override
390 		public String getScript()
391 		{
392 			return getModelItem().getBeforeSaveScript();
393 		}
394 
395 		@Override
396 		public void setScript( String text )
397 		{
398 			getModelItem().setBeforeSaveScript( text );
399 		}
400 
401 		@Override
402 		public Action getRunAction()
403 		{
404 			return new BeforeSaveScriptRunAction();
405 		}
406 
407 		private final class BeforeSaveScriptRunAction extends AbstractAction
408 		{
409 			public BeforeSaveScriptRunAction()
410 			{
411 				putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ) );
412 				putValue( SHORT_DESCRIPTION, "Runs this script" );
413 			}
414 
415 			public void actionPerformed( ActionEvent e )
416 			{
417 				try
418 				{
419 					getModelItem().runBeforeSaveScript();
420 				}
421 				catch( Exception e1 )
422 				{
423 					UISupport.showErrorMessage( e1 );
424 				}
425 			}
426 		}
427 	}
428 
429 	
430 }