View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.support;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import javax.swing.JComponent;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.model.testsuite.LoadTestRunner;
22  import com.eviware.soapui.model.testsuite.TestCase;
23  import com.eviware.soapui.model.testsuite.TestRunner;
24  import com.eviware.soapui.monitor.TestMonitorListener;
25  
26  /***
27   * ComponentEnabler for disabling components during TestCase runs
28   * 
29   * @author Ole.Matzura
30   */
31  
32  public class TestRunComponentEnabler implements TestMonitorListener
33  {
34  	private final List<JComponent> components = new ArrayList<JComponent>();
35  	private final List<Boolean> states = new ArrayList<Boolean>();
36  	private final TestCase testCase;
37  
38  	public TestRunComponentEnabler( TestCase testCase )
39  	{
40  		this.testCase = testCase;
41  		
42  		SoapUI.getTestMonitor().addTestMonitorListener( this );
43  	}
44  	
45  	public void release()
46  	{
47  		SoapUI.getTestMonitor().removeTestMonitorListener( this );
48  	}
49  
50  	public void loadTestStarted(LoadTestRunner runner)
51  	{
52  		disable();
53  	}
54  
55  	private void disable()
56  	{
57  		if( states.isEmpty() )
58  		{
59  			for( JComponent component : components )
60  			{
61  				states.add( component.isEnabled() );
62  				component.setEnabled( false );
63  			}
64  		}
65  	}
66  
67  	private void enable()
68  	{
69  		if( !states.isEmpty() )
70  		{
71  			for( int c = 0; c < components.size(); c++  )
72  			{
73  				JComponent component = components.get( c );
74  				component.setEnabled( states.get( c ));
75  			}
76  
77  			states.clear();
78  		}
79  	}
80  	
81  	public void loadTestFinished(LoadTestRunner runner)
82  	{
83  		if( !SoapUI.getTestMonitor().hasRunningTest( testCase ) )
84  			enable();
85  	}
86  
87  	public void testCaseStarted(TestRunner runner)
88  	{
89  		disable();
90  	}
91  
92  	public void testCaseFinished(TestRunner runner)
93  	{
94  		if( !SoapUI.getTestMonitor().hasRunningTest( testCase ) )
95  			enable();
96  	}
97  
98  	public void add(JComponent component)
99  	{
100 		components.add( component );
101 		
102 		if( SoapUI.getTestMonitor().hasRunningTest( testCase ))
103 		{
104 			states.add( component.isEnabled() );
105 			component.setEnabled( false );
106 		}
107 	}
108 }