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