View Javadoc

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