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.testsuite;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.Dimension;
19  import java.awt.Insets;
20  import java.awt.Point;
21  import java.awt.Rectangle;
22  import java.awt.dnd.Autoscroll;
23  import java.awt.event.KeyAdapter;
24  import java.awt.event.KeyEvent;
25  import java.awt.event.MouseAdapter;
26  import java.awt.event.MouseEvent;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  import java.util.Arrays;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.swing.BorderFactory;
34  import javax.swing.Box;
35  import javax.swing.BoxLayout;
36  import javax.swing.JLabel;
37  import javax.swing.JPanel;
38  import javax.swing.JProgressBar;
39  
40  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
41  import com.eviware.soapui.impl.wsdl.panels.support.ProgressBarAdapter;
42  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
43  import com.eviware.soapui.model.ModelItem;
44  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
45  import com.eviware.soapui.model.testsuite.TestCase;
46  import com.eviware.soapui.support.UISupport;
47  import com.eviware.soapui.support.action.swing.ActionList;
48  import com.eviware.soapui.support.action.swing.ActionListBuilder;
49  import com.eviware.soapui.support.action.swing.ActionSupport;
50  import com.eviware.soapui.support.swing.AutoscrollSupport;
51  
52  /***
53   * A panel showing a scrollable list of TestCases in a TestSuite.
54   * 
55   * @author Ole.Matzura
56   */
57  
58  public class JTestCaseList extends JPanel 
59  {
60  	private Map<TestCase,TestCaseListPanel> panels = new HashMap<TestCase,TestCaseListPanel>();
61  	private final WsdlTestSuite testSuite;
62  	private final InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
63  
64  	public JTestCaseList(WsdlTestSuite testSuite)
65  	{
66  		this.testSuite = testSuite;
67  		setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ));
68  		
69  		for( int c = 0; c < testSuite.getTestCaseCount(); c++ )
70  		{
71  			TestCaseListPanel testCaseListPanel = createTestCaseListPanel( testSuite.getTestCaseAt( c ) );
72  			panels.put( testSuite.getTestCaseAt( c ), testCaseListPanel );
73  			add( testCaseListPanel );
74  		}
75  		
76  		add( Box.createVerticalGlue() );
77  		setBackground( Color.WHITE );
78  		
79  		testSuite.addTestSuiteListener( testSuiteListener );
80  	}
81  	
82  	@Override
83  	public void addNotify()
84  	{
85  		super.addNotify();
86  		testSuite.addTestSuiteListener( testSuiteListener );
87  		
88  	}
89  
90  	@Override
91  	public void removeNotify()
92  	{
93  		super.removeNotify();
94  		testSuite.removeTestSuiteListener( testSuiteListener );
95  	}
96  
97  	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
98  	{
99  		public void testCaseAdded(TestCase testCase)
100 		{
101 			TestCaseListPanel testCaseListPanel = createTestCaseListPanel( testCase );
102 			panels.put( testCase, testCaseListPanel );
103 			add( testCaseListPanel, testCase.getTestSuite().getIndexOfTestCase( testCase ) );
104 			revalidate();
105 			repaint();
106 		}
107 
108 		public void testCaseRemoved(TestCase testCase)
109 		{
110 			TestCaseListPanel testCaseListPanel = panels.get( testCase );
111 			if( testCaseListPanel != null )
112 			{
113 				remove( testCaseListPanel );
114 				panels.remove( testCase );
115 				revalidate();
116 				repaint();
117 			}
118 		}
119 
120 		@Override
121 		public void testCaseMoved( TestCase testCase, int index, int offset )
122 		{
123 			TestCaseListPanel testCaseListPanel = panels.get( testCase );
124 			if( testCaseListPanel != null )
125 			{
126 				boolean hadFocus = testCaseListPanel.hasFocus();
127 				
128 				remove( testCaseListPanel );
129 				add( testCaseListPanel, index+offset );
130 				
131 				revalidate();
132 				repaint();
133 				
134 				if( hadFocus )
135 					testCaseListPanel.requestFocus();
136 			}
137 		}
138 	}
139 	
140 	public final class TestCaseListPanel extends JPanel implements Autoscroll
141 	{
142 		private final WsdlTestCase testCase;
143 		private JProgressBar progressBar;
144 		private JLabel label;
145 		private ProgressBarAdapter progressBarAdapter;
146 		private boolean selected;
147 		private TestCasePropertyChangeListener testCasePropertyChangeListener;
148 		private AutoscrollSupport autoscrollSupport;
149 
150 		public TestCaseListPanel( WsdlTestCase testCase )
151 		{
152 			super( new BorderLayout() );
153 			
154 			setFocusable( true );
155 			
156 			this.testCase = testCase;
157 			autoscrollSupport = new AutoscrollSupport( this );
158 			
159 			progressBar = new JProgressBar( 0, 100 )
160 			{
161 				protected void processMouseEvent(MouseEvent e) {
162 			      if (e.getID() == MouseEvent.MOUSE_PRESSED ||
163 			        e.getID() == MouseEvent.MOUSE_RELEASED) {
164 			      	TestCaseListPanel.this.processMouseEvent(translateMouseEvent(e));
165 			      }
166 			    }
167 			    
168 			    protected void processMouseMotionEvent(MouseEvent e) {
169 			   	 TestCaseListPanel.this.processMouseMotionEvent(translateMouseEvent(e));
170 			    }
171 			    
172 			    /***
173 			     * Translates the given mouse event to the enclosing map panel's
174 			     * coordinate space.
175 			     */
176 			    private MouseEvent translateMouseEvent(MouseEvent e) {
177 			      return new MouseEvent(TestCaseListPanel.this, e.getID(), e.getWhen(), 
178 			        e.getModifiers(), e.getX() + getX(), e.getY() + getY(), 
179 			        e.getClickCount(), e.isPopupTrigger(), e.getButton());
180 			    }
181 			};
182 			
183 			JPanel progressPanel = UISupport.createProgressBarPanel( progressBar, 5, false );
184 			
185 		   progressBar.setMinimumSize( new Dimension( 0, 10 ));
186 		   progressBar.setBackground( Color.WHITE );
187 		   progressBar.setInheritsPopupMenu( true );
188 		   
189 			label = new JLabel( "TestCase: " + testCase.getName() );
190 			label.setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5));
191 		   label.setInheritsPopupMenu( true );
192 
193 			add( progressPanel, BorderLayout.CENTER );
194 			add( label, BorderLayout.NORTH );
195 			
196 			testCasePropertyChangeListener = new TestCasePropertyChangeListener();
197 			
198 			setComponentPopupMenu( ActionSupport.buildPopup( 
199 						ActionListBuilder.buildActions( testCase )));
200 			
201 			addMouseListener( new MouseAdapter() {
202 				
203 				@Override
204 				public void mousePressed( MouseEvent e )
205 				{
206 					requestFocus();
207 				}
208 
209 				public void mouseClicked(MouseEvent e)
210 				{
211 					if (e.getClickCount() < 2)
212 					{
213 						setSelected( !selected );
214 						return;
215 					}
216 					
217 					UISupport.selectAndShow( TestCaseListPanel.this.testCase );
218 				}
219 			} );
220 			
221 			addKeyListener( new TestCaseListPanelKeyHandler() );
222 			
223 			setSelected( false );
224 		}
225 		
226 		public void addNotify()
227 		{
228 			super.addNotify();
229 			testCase.addPropertyChangeListener( TestCase.NAME_PROPERTY, testCasePropertyChangeListener );
230 			progressBarAdapter = new ProgressBarAdapter( progressBar, testCase );
231 		}
232 
233 		public void removeNotify()
234 		{
235 			super.removeNotify();
236 			if( progressBarAdapter != null )
237 			{
238 				testCase.removePropertyChangeListener( TestCase.NAME_PROPERTY, testCasePropertyChangeListener );
239 				progressBarAdapter.release();
240 				
241 				progressBarAdapter = null;
242 			}
243 		}
244 
245 		public Dimension getMaximumSize() 
246 		{
247 		    Dimension size = super.getMaximumSize();
248 		    size.height = 50;
249 		    return size;
250 		}
251 		
252 		public void setSelected( boolean selected )
253 		{
254 			this.selected = selected;
255 			
256 			if( selected )
257 			{
258 				setBackground( Color.YELLOW.brighter().brighter() );
259 				setBorder( BorderFactory.createLineBorder( Color.GRAY ));
260 			}
261 			else
262 			{
263 				setBackground( Color.WHITE );
264 				setBorder( BorderFactory.createLineBorder( Color.WHITE ));
265 			}
266 		}
267 
268 		public boolean isSelected()
269 		{
270 			return selected;
271 		}
272 		
273 		private final class TestCasePropertyChangeListener implements PropertyChangeListener
274 		{
275 			public void propertyChange(PropertyChangeEvent evt)
276 			{
277 				label.setText( "TestCase: " + TestCaseListPanel.this.testCase.getName() );
278 			}
279 		}
280 
281 		protected TestCase getTestCase()
282 		{
283 			return testCase;
284 		}
285 
286 		public ModelItem getModelItem()
287 		{
288 			return testCase;
289 		}
290 
291 		public void autoscroll( Point pt )
292 		{
293 			int ix = getIndexOf( this );
294 			if( pt.getY() < 12 && ix > 0 ) 
295 			{
296 				Rectangle bounds = JTestCaseList.this.getComponent( ix-1 ).getBounds();
297 				JTestCaseList.this.scrollRectToVisible( bounds );
298 			}
299 			else if( pt.getY() > getHeight()-12 && ix < testSuite.getTestCaseCount()-1 )
300 			{
301 				Rectangle bounds = JTestCaseList.this.getComponent( ix+1 ).getBounds();
302 				JTestCaseList.this.scrollRectToVisible( bounds );
303 			}
304 		}
305 
306 		public Insets getAutoscrollInsets()
307 		{
308 			return autoscrollSupport.getAutoscrollInsets();
309 		}
310 		
311 		private final class TestCaseListPanelKeyHandler extends KeyAdapter
312 		{
313 			public void keyPressed(KeyEvent e)
314 			{
315 				if (e.getKeyChar() == KeyEvent.VK_ENTER)
316 				{
317 					UISupport.selectAndShow( testCase );
318 					e.consume();
319 				}
320 				else
321 				{
322 					ActionList actions = ActionListBuilder.buildActions( testCase );
323 					if( actions != null )
324 						actions.dispatchKeyEvent( e );
325 				}
326 			}
327 		}
328 	}
329 	
330 	public int[] getSelectedIndices()
331 	{
332 		int cnt = 0;
333 		for( TestCaseListPanel panel : panels.values() )
334 		{
335 			if( panel.isSelected() ) cnt++;
336 		}
337 			
338 		int [] result = new int[cnt];
339 		cnt = 0;
340 		
341 		for( int c = 0; c < getComponentCount(); c++ )
342 		{
343 			Component comp = getComponent( c );
344 			if( comp instanceof TestCaseListPanel && ((TestCaseListPanel)comp).isSelected() )
345 			{
346 				result[cnt] = c;
347 				cnt++;
348 			}
349 		}
350 		
351 		return result;
352 	}
353 
354 	public int getIndexOf( TestCaseListPanel panel )
355 	{
356 		return Arrays.asList( getComponents() ).indexOf( panel );
357 	}
358 
359 	protected TestCaseListPanel createTestCaseListPanel( TestCase testCase )
360 	{
361 		TestCaseListPanel testCaseListPanel = new TestCaseListPanel(( WsdlTestCase ) testCase);
362 		return testCaseListPanel;
363 	}
364 }