001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.testsuite;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Component;
018: import java.awt.Dimension;
019: import java.awt.Insets;
020: import java.awt.Point;
021: import java.awt.Rectangle;
022: import java.awt.dnd.Autoscroll;
023: import java.awt.event.KeyAdapter;
024: import java.awt.event.KeyEvent;
025: import java.awt.event.MouseAdapter;
026: import java.awt.event.MouseEvent;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029: import java.util.Arrays;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.Box;
035: import javax.swing.BoxLayout;
036: import javax.swing.JLabel;
037: import javax.swing.JPanel;
038: import javax.swing.JProgressBar;
039:
040: import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
041: import com.eviware.soapui.impl.wsdl.panels.support.ProgressBarAdapter;
042: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
043: import com.eviware.soapui.model.ModelItem;
044: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
045: import com.eviware.soapui.model.testsuite.TestCase;
046: import com.eviware.soapui.support.UISupport;
047: import com.eviware.soapui.support.action.swing.ActionList;
048: import com.eviware.soapui.support.action.swing.ActionListBuilder;
049: import com.eviware.soapui.support.action.swing.ActionSupport;
050: import com.eviware.soapui.support.swing.AutoscrollSupport;
051:
052: /**
053: * A panel showing a scrollable list of TestCases in a TestSuite.
054: *
055: * @author Ole.Matzura
056: */
057:
058: public class JTestCaseList extends JPanel {
059: private Map<TestCase, TestCaseListPanel> panels = new HashMap<TestCase, TestCaseListPanel>();
060: private final WsdlTestSuite testSuite;
061: private final InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
062:
063: public JTestCaseList(WsdlTestSuite testSuite) {
064: this .testSuite = testSuite;
065: setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
066:
067: for (int c = 0; c < testSuite.getTestCaseCount(); c++) {
068: TestCaseListPanel testCaseListPanel = createTestCaseListPanel(testSuite
069: .getTestCaseAt(c));
070: panels.put(testSuite.getTestCaseAt(c), testCaseListPanel);
071: add(testCaseListPanel);
072: }
073:
074: add(Box.createVerticalGlue());
075: setBackground(Color.WHITE);
076:
077: testSuite.addTestSuiteListener(testSuiteListener);
078: }
079:
080: @Override
081: public void addNotify() {
082: super .addNotify();
083: testSuite.addTestSuiteListener(testSuiteListener);
084:
085: }
086:
087: @Override
088: public void removeNotify() {
089: super .removeNotify();
090: testSuite.removeTestSuiteListener(testSuiteListener);
091: }
092:
093: private final class InternalTestSuiteListener extends
094: TestSuiteListenerAdapter {
095: public void testCaseAdded(TestCase testCase) {
096: TestCaseListPanel testCaseListPanel = createTestCaseListPanel(testCase);
097: panels.put(testCase, testCaseListPanel);
098: add(testCaseListPanel, testCase.getTestSuite()
099: .getIndexOfTestCase(testCase));
100: revalidate();
101: repaint();
102: }
103:
104: public void testCaseRemoved(TestCase testCase) {
105: TestCaseListPanel testCaseListPanel = panels.get(testCase);
106: if (testCaseListPanel != null) {
107: remove(testCaseListPanel);
108: panels.remove(testCase);
109: revalidate();
110: repaint();
111: }
112: }
113:
114: @Override
115: public void testCaseMoved(TestCase testCase, int index,
116: int offset) {
117: TestCaseListPanel testCaseListPanel = panels.get(testCase);
118: if (testCaseListPanel != null) {
119: boolean hadFocus = testCaseListPanel.hasFocus();
120:
121: remove(testCaseListPanel);
122: add(testCaseListPanel, index + offset);
123:
124: revalidate();
125: repaint();
126:
127: if (hadFocus)
128: testCaseListPanel.requestFocus();
129: }
130: }
131: }
132:
133: public final class TestCaseListPanel extends JPanel implements
134: Autoscroll {
135: private final WsdlTestCase testCase;
136: private JProgressBar progressBar;
137: private JLabel label;
138: private ProgressBarAdapter progressBarAdapter;
139: private boolean selected;
140: private TestCasePropertyChangeListener testCasePropertyChangeListener;
141: private AutoscrollSupport autoscrollSupport;
142:
143: public TestCaseListPanel(WsdlTestCase testCase) {
144: super (new BorderLayout());
145:
146: setFocusable(true);
147:
148: this .testCase = testCase;
149: autoscrollSupport = new AutoscrollSupport(this );
150:
151: progressBar = new JProgressBar(0, 100) {
152: protected void processMouseEvent(MouseEvent e) {
153: if (e.getID() == MouseEvent.MOUSE_PRESSED
154: || e.getID() == MouseEvent.MOUSE_RELEASED) {
155: TestCaseListPanel.this
156: .processMouseEvent(translateMouseEvent(e));
157: }
158: }
159:
160: protected void processMouseMotionEvent(MouseEvent e) {
161: TestCaseListPanel.this
162: .processMouseMotionEvent(translateMouseEvent(e));
163: }
164:
165: /**
166: * Translates the given mouse event to the enclosing map panel's
167: * coordinate space.
168: */
169: private MouseEvent translateMouseEvent(MouseEvent e) {
170: return new MouseEvent(TestCaseListPanel.this , e
171: .getID(), e.getWhen(), e.getModifiers(), e
172: .getX()
173: + getX(), e.getY() + getY(), e
174: .getClickCount(), e.isPopupTrigger(), e
175: .getButton());
176: }
177: };
178:
179: JPanel progressPanel = UISupport.createProgressBarPanel(
180: progressBar, 5, false);
181:
182: progressBar.setMinimumSize(new Dimension(0, 10));
183: progressBar.setBackground(Color.WHITE);
184: progressBar.setInheritsPopupMenu(true);
185:
186: label = new JLabel("TestCase: " + testCase.getName());
187: label
188: .setBorder(BorderFactory.createEmptyBorder(5, 5, 5,
189: 5));
190: label.setInheritsPopupMenu(true);
191:
192: add(progressPanel, BorderLayout.CENTER);
193: add(label, BorderLayout.NORTH);
194:
195: testCasePropertyChangeListener = new TestCasePropertyChangeListener();
196:
197: setComponentPopupMenu(ActionSupport
198: .buildPopup(ActionListBuilder
199: .buildActions(testCase)));
200:
201: addMouseListener(new MouseAdapter() {
202:
203: @Override
204: public void mousePressed(MouseEvent e) {
205: requestFocus();
206: }
207:
208: public void mouseClicked(MouseEvent e) {
209: if (e.getClickCount() < 2) {
210: setSelected(!selected);
211: return;
212: }
213:
214: UISupport
215: .selectAndShow(TestCaseListPanel.this .testCase);
216: }
217: });
218:
219: addKeyListener(new TestCaseListPanelKeyHandler());
220:
221: setSelected(false);
222: }
223:
224: public void addNotify() {
225: super .addNotify();
226: testCase.addPropertyChangeListener(TestCase.NAME_PROPERTY,
227: testCasePropertyChangeListener);
228: progressBarAdapter = new ProgressBarAdapter(progressBar,
229: testCase);
230: }
231:
232: public void removeNotify() {
233: super .removeNotify();
234: if (progressBarAdapter != null) {
235: testCase.removePropertyChangeListener(
236: TestCase.NAME_PROPERTY,
237: testCasePropertyChangeListener);
238: progressBarAdapter.release();
239:
240: progressBarAdapter = null;
241: }
242: }
243:
244: public Dimension getMaximumSize() {
245: Dimension size = super .getMaximumSize();
246: size.height = 50;
247: return size;
248: }
249:
250: public void setSelected(boolean selected) {
251: this .selected = selected;
252:
253: if (selected) {
254: setBackground(Color.YELLOW.brighter().brighter());
255: setBorder(BorderFactory.createLineBorder(Color.GRAY));
256: } else {
257: setBackground(Color.WHITE);
258: setBorder(BorderFactory.createLineBorder(Color.WHITE));
259: }
260: }
261:
262: public boolean isSelected() {
263: return selected;
264: }
265:
266: private final class TestCasePropertyChangeListener implements
267: PropertyChangeListener {
268: public void propertyChange(PropertyChangeEvent evt) {
269: label.setText("TestCase: "
270: + TestCaseListPanel.this .testCase.getName());
271: }
272: }
273:
274: protected TestCase getTestCase() {
275: return testCase;
276: }
277:
278: public ModelItem getModelItem() {
279: return testCase;
280: }
281:
282: public void autoscroll(Point pt) {
283: int ix = getIndexOf(this );
284: if (pt.getY() < 12 && ix > 0) {
285: Rectangle bounds = JTestCaseList.this .getComponent(
286: ix - 1).getBounds();
287: JTestCaseList.this .scrollRectToVisible(bounds);
288: } else if (pt.getY() > getHeight() - 12
289: && ix < testSuite.getTestCaseCount() - 1) {
290: Rectangle bounds = JTestCaseList.this .getComponent(
291: ix + 1).getBounds();
292: JTestCaseList.this .scrollRectToVisible(bounds);
293: }
294: }
295:
296: public Insets getAutoscrollInsets() {
297: return autoscrollSupport.getAutoscrollInsets();
298: }
299:
300: private final class TestCaseListPanelKeyHandler extends
301: KeyAdapter {
302: public void keyPressed(KeyEvent e) {
303: if (e.getKeyChar() == KeyEvent.VK_ENTER) {
304: UISupport.selectAndShow(testCase);
305: e.consume();
306: } else {
307: ActionList actions = ActionListBuilder
308: .buildActions(testCase);
309: if (actions != null)
310: actions.dispatchKeyEvent(e);
311: }
312: }
313: }
314: }
315:
316: public int[] getSelectedIndices() {
317: int cnt = 0;
318: for (TestCaseListPanel panel : panels.values()) {
319: if (panel.isSelected())
320: cnt++;
321: }
322:
323: int[] result = new int[cnt];
324: cnt = 0;
325:
326: for (int c = 0; c < getComponentCount(); c++) {
327: Component comp = getComponent(c);
328: if (comp instanceof TestCaseListPanel
329: && ((TestCaseListPanel) comp).isSelected()) {
330: result[cnt] = c;
331: cnt++;
332: }
333: }
334:
335: return result;
336: }
337:
338: public int getIndexOf(TestCaseListPanel panel) {
339: return Arrays.asList(getComponents()).indexOf(panel);
340: }
341:
342: protected TestCaseListPanel createTestCaseListPanel(
343: TestCase testCase) {
344: TestCaseListPanel testCaseListPanel = new TestCaseListPanel(
345: (WsdlTestCase) testCase);
346: return testCaseListPanel;
347: }
348: }
|