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.event.MouseAdapter;
20  import java.awt.event.MouseEvent;
21  import java.beans.PropertyChangeEvent;
22  import java.beans.PropertyChangeListener;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.swing.BorderFactory;
27  import javax.swing.Box;
28  import javax.swing.BoxLayout;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JPopupMenu;
32  import javax.swing.JProgressBar;
33  
34  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
35  import com.eviware.soapui.impl.wsdl.panels.support.ProgressBarAdapter;
36  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
37  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
38  import com.eviware.soapui.model.testsuite.TestCase;
39  import com.eviware.soapui.support.UISupport;
40  import com.eviware.soapui.support.action.ActionSupport;
41  
42  /***
43   * A panel showing a scrollable list of TestCases in a TestSuite.
44   * 
45   * @author Ole.Matzura
46   */
47  
48  public class JTestCaseList extends JPanel
49  {
50  	private Map<TestCase,TestCaseListPanel> panels = new HashMap<TestCase,TestCaseListPanel>();
51  	private JPopupMenu popup;
52  	private final WsdlTestSuite testSuite;
53  	private final InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
54  
55  	public JTestCaseList(WsdlTestSuite testSuite)
56  	{
57  		this.testSuite = testSuite;
58  		setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ));
59  		
60  		for( int c = 0; c < testSuite.getTestCaseCount(); c++ )
61  		{
62  			TestCaseListPanel testCaseListPanel = new TestCaseListPanel(testSuite.getTestCaseAt(c));
63  			panels.put( testSuite.getTestCaseAt( c ), testCaseListPanel );
64  			add( testCaseListPanel );
65  		}
66  		
67  		add( Box.createVerticalGlue() );
68  		setBackground( Color.WHITE );
69  		
70  		testSuite.addTestSuiteListener( testSuiteListener );
71  	}
72  	
73  	@Override
74  	public void addNotify()
75  	{
76  		super.addNotify();
77  		testSuite.addTestSuiteListener( testSuiteListener );
78  	}
79  
80  	@Override
81  	public void removeNotify()
82  	{
83  		super.removeNotify();
84  		testSuite.removeTestSuiteListener( testSuiteListener );
85  	}
86  
87  	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
88  	{
89  		public void testCaseAdded(TestCase testCase)
90  		{
91  			TestCaseListPanel testCaseListPanel = new TestCaseListPanel(( WsdlTestCase ) testCase);
92  			panels.put( testCase, testCaseListPanel );
93  			add( testCaseListPanel, getComponentCount()-1 );
94  			revalidate();
95  			repaint();
96  		}
97  
98  		public void testCaseRemoved(TestCase testCase)
99  		{
100 			TestCaseListPanel testCaseListPanel = panels.get( testCase );
101 			if( testCaseListPanel != null )
102 			{
103 				remove( testCaseListPanel );
104 				panels.remove( testCase );
105 				revalidate();
106 				repaint();
107 			}
108 		}
109 	}
110 	
111 	private final static class TestCaseListPanel extends JPanel
112 	{
113 		private final TestCase testCase;
114 		private JProgressBar progressBar;
115 		private JLabel label;
116 		private ProgressBarAdapter progressBarAdapter;
117 		private boolean selected;
118 		private TestCasePropertyChangeListener testCasePropertyChangeListener;
119 
120 		public TestCaseListPanel( WsdlTestCase testCase )
121 		{
122 			super( new BorderLayout() );
123 			this.testCase = testCase;
124 			
125 			progressBar = new JProgressBar( 0, 100 );
126 			JPanel progressPanel = UISupport.createProgressBarPanel( progressBar, 5, false );
127 			
128 		   progressBar.setMinimumSize( new Dimension( 0, 10 ));
129 		   progressBar.setBackground( Color.WHITE );
130 		   progressBar.setInheritsPopupMenu( true );
131 		   progressBarAdapter = new ProgressBarAdapter( progressBar, testCase );
132 		   
133 			label = new JLabel( "TestCase: " + testCase.getName() );
134 			label.setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5));
135 		   label.setInheritsPopupMenu( true );
136 
137 			add( progressPanel, BorderLayout.CENTER );
138 			add( label, BorderLayout.NORTH );
139 			
140 			testCasePropertyChangeListener = new TestCasePropertyChangeListener();
141 			testCase.addPropertyChangeListener( TestCase.NAME_PROPERTY, testCasePropertyChangeListener );
142 			
143 			setComponentPopupMenu( ActionSupport.buildPopup( testCase.getActions() ) );
144 			
145 			addMouseListener( new MouseAdapter() {
146 				public void mouseClicked(MouseEvent e)
147 				{
148 					if (e.getClickCount() < 2)
149 					{
150 						setSelected( !selected );
151 						return;
152 					}
153 					
154 					UISupport.selectAndShow( TestCaseListPanel.this.testCase );
155 				}
156 			} );
157 			
158 			setSelected( false );
159 		}
160 		
161 		public void removeNotify()
162 		{
163 			super.removeNotify();
164 			testCase.removePropertyChangeListener( testCasePropertyChangeListener );
165 		}
166 
167 		public Dimension getMaximumSize() 
168 		{
169 		    Dimension size = super.getMaximumSize();
170 		    size.height = 50;
171 		    return size;
172 		}
173 		
174 		public void setSelected( boolean selected )
175 		{
176 			this.selected = selected;
177 			
178 			if( selected )
179 			{
180 				setBackground( Color.YELLOW.brighter().brighter() );
181 				setBorder( BorderFactory.createLineBorder( Color.GRAY ));
182 			}
183 			else
184 			{
185 				setBackground( Color.WHITE );
186 				setBorder( BorderFactory.createLineBorder( Color.WHITE ));
187 			}
188 		}
189 
190 		public boolean isSelected()
191 		{
192 			return selected;
193 		}
194 
195 		private final class TestCasePropertyChangeListener implements PropertyChangeListener
196 		{
197 			public void propertyChange(PropertyChangeEvent evt)
198 			{
199 				label.setText( "TestCase: " + TestCaseListPanel.this.testCase.getName() );
200 			}
201 		}
202 	}
203 	
204 	public int[] getSelectedIndices()
205 	{
206 		int cnt = 0;
207 		for( TestCaseListPanel panel : panels.values() )
208 		{
209 			if( panel.isSelected() ) cnt++;
210 		}
211 			
212 		int [] result = new int[cnt];
213 		cnt = 0;
214 		
215 		for( int c = 0; c < getComponentCount(); c++ )
216 		{
217 			Component comp = getComponent( c );
218 			if( comp instanceof TestCaseListPanel && ((TestCaseListPanel)comp).isSelected() )
219 			{
220 				result[cnt] = c;
221 				cnt++;
222 			}
223 		}
224 		
225 		return result;
226 	}
227 
228 }