KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > TestHierarchyTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids - sdavids@gmx.de bugs 26754, 41228
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.ui;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.ListIterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.eclipse.debug.core.ILaunchManager;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.junit.ITestRunListener;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IMenuListener;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.MenuManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.util.ListenerList;
33 import org.eclipse.jface.util.OpenStrategy;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.ISelectionProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.ui.IWorkbenchActionConstants;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.custom.CTabFolder;
42 import org.eclipse.swt.custom.CTabItem;
43 import org.eclipse.swt.dnd.Clipboard;
44 import org.eclipse.swt.events.DisposeEvent;
45 import org.eclipse.swt.events.DisposeListener;
46 import org.eclipse.swt.events.MouseEvent;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.events.SelectionListener;
50 import org.eclipse.swt.graphics.Image;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Menu;
55 import org.eclipse.swt.widgets.Tree;
56 import org.eclipse.swt.widgets.TreeItem;
57
58 /*
59  * A view that shows the contents of a test suite
60  * as a tree.
61  */

62 public class TestHierarchyTab extends TestRunTab implements IMenuListener, ISelectionProvider {
63      
64     private Tree fTree;
65     
66     private TreeItem fCachedParent;
67     private TreeItem[] fCachedItems;
68     
69     private TreeItem fLastParent;
70     private List JavaDoc fExecutionPath;
71     
72     private boolean fMoveSelection= false;
73     private ListenerList fSelectionListeners= new ListenerList();
74     
75     /**
76      * Helper used to resurrect test hierarchy
77      */

78     private static class SuiteInfo {
79         public int fTestCount;
80         public TreeItem fTreeItem;
81         
82         public SuiteInfo(TreeItem treeItem, int testCount){
83             fTreeItem= treeItem;
84             fTestCount= testCount;
85         }
86     }
87         
88     /**
89      * Vector of SuiteInfo items
90      */

91     private Vector JavaDoc fSuiteInfos= new Vector JavaDoc();
92     /**
93      * Maps test Ids to TreeItems.
94      */

95     private Map JavaDoc fTreeItemMap= new HashMap JavaDoc();
96     
97     private TestRunnerViewPart fTestRunnerPart;
98     
99     private final Image fOkIcon= TestRunnerViewPart.createImage("obj16/testok.gif"); //$NON-NLS-1$
100
private final Image fErrorIcon= TestRunnerViewPart.createImage("obj16/testerr.gif"); //$NON-NLS-1$
101
private final Image fFailureIcon= TestRunnerViewPart.createImage("obj16/testfail.gif"); //$NON-NLS-1$
102
private final Image fHierarchyIcon= TestRunnerViewPart.createImage("obj16/testhier.gif"); //$NON-NLS-1$
103
private final Image fSuiteIcon= TestRunnerViewPart.createImage("obj16/tsuite.gif"); //$NON-NLS-1$
104
private final Image fSuiteErrorIcon= TestRunnerViewPart.createImage("obj16/tsuiteerror.gif"); //$NON-NLS-1$
105
private final Image fSuiteFailIcon= TestRunnerViewPart.createImage("obj16/tsuitefail.gif"); //$NON-NLS-1$
106
private final Image fTestIcon= TestRunnerViewPart.createImage("obj16/test.gif"); //$NON-NLS-1$
107
private final Image fTestRunningIcon= TestRunnerViewPart.createImage("obj16/testrun.gif"); //$NON-NLS-1$
108
private final Image fSuiteRunningIcon= TestRunnerViewPart.createImage("obj16/tsuiterun.gif"); //$NON-NLS-1$
109

110     private class ExpandAllAction extends Action {
111         public ExpandAllAction() {
112             setText(JUnitMessages.ExpandAllAction_text);
113             setToolTipText(JUnitMessages.ExpandAllAction_tooltip);
114         }
115         
116         public void run(){
117             expandAll();
118         }
119     }
120     public TestHierarchyTab() {
121     }
122
123     public void createTabControl(CTabFolder tabFolder, Clipboard clipboard, TestRunnerViewPart runner) {
124         fTestRunnerPart= runner;
125         
126         CTabItem hierarchyTab= new CTabItem(tabFolder, SWT.NONE);
127         hierarchyTab.setText(getName());
128         hierarchyTab.setImage(fHierarchyIcon);
129         
130         Composite testTreePanel= new Composite(tabFolder, SWT.NONE);
131         GridLayout gridLayout= new GridLayout();
132         gridLayout.marginHeight= 0;
133         gridLayout.marginWidth= 0;
134         testTreePanel.setLayout(gridLayout);
135         
136         GridData gridData= new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
137         testTreePanel.setLayoutData(gridData);
138         
139         hierarchyTab.setControl(testTreePanel);
140         hierarchyTab.setToolTipText(JUnitMessages.HierarchyRunView_tab_tooltip);
141         
142         fTree= new Tree(testTreePanel, SWT.V_SCROLL | SWT.SINGLE);
143         gridData= new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
144         fTree.setLayoutData(gridData);
145         OpenStrategy handler = new OpenStrategy(fTree);
146         handler.addPostSelectionListener(new SelectionAdapter() {
147             public void widgetSelected(SelectionEvent e) {
148                 fireSelectionChanged();
149             }
150         });
151
152         
153         initMenu();
154         addListeners();
155     }
156
157     void disposeIcons() {
158         fErrorIcon.dispose();
159         fFailureIcon.dispose();
160         fOkIcon.dispose();
161         fHierarchyIcon.dispose();
162         fTestIcon.dispose();
163         fTestRunningIcon.dispose();
164         fSuiteRunningIcon.dispose();
165         fSuiteIcon.dispose();
166         fSuiteErrorIcon.dispose();
167         fSuiteFailIcon.dispose();
168     }
169     
170     private void initMenu() {
171         MenuManager menuMgr= new MenuManager("#PopupMenu"); //$NON-NLS-1$
172
menuMgr.setRemoveAllWhenShown(true);
173         menuMgr.addMenuListener(this);
174         fTestRunnerPart.getSite().registerContextMenu(menuMgr, this);
175         Menu menu= menuMgr.createContextMenu(fTree);
176         fTree.setMenu(menu);
177     }
178
179     private String JavaDoc getTestMethod() {
180         return getTestInfo().getTestMethodName();
181     }
182
183     private TestRunInfo getTestInfo() {
184         TreeItem[] treeItems= fTree.getSelection();
185         if(treeItems.length == 0)
186             return null;
187         return ((TestRunInfo)treeItems[0].getData());
188     }
189     
190     private boolean isSuiteSelected() {
191         TreeItem[] treeItems= fTree.getSelection();
192         if(treeItems.length != 1)
193             return false;
194         return treeItems[0].getItemCount() > 0;
195     }
196     
197     private String JavaDoc getClassName() {
198         return getTestInfo().getClassName();
199     }
200     
201     public String JavaDoc getSelectedTestId() {
202         TestRunInfo testInfo= getTestInfo();
203         if (testInfo == null)
204             return null;
205         return testInfo.getTestId();
206     }
207
208     public String JavaDoc getName() {
209         return JUnitMessages.HierarchyRunView_tab_title;
210     }
211     
212     public void setSelectedTest(String JavaDoc testId) {
213         TreeItem treeItem= findTreeItem(testId);
214         if (treeItem != null)
215             fTree.setSelection(new TreeItem[]{treeItem});
216     }
217     
218     public void startTest(String JavaDoc testId) {
219         TreeItem treeItem= findTreeItem(testId);
220         if (treeItem == null)
221             return;
222         TreeItem parent= treeItem.getParentItem();
223         if (fLastParent != parent) {
224             updatePath(parent);
225             fLastParent= parent;
226         }
227         setCurrentItem(treeItem);
228     }
229
230     public void rerunTest(String JavaDoc testId) {
231         TreeItem treeItem= findTreeItem(testId);
232         if (treeItem == null)
233             return;
234         setCurrentItem(treeItem);
235     }
236     
237     public void endRerunTest(String JavaDoc testId) {
238         TestRunInfo testInfo= fTestRunnerPart.getTestInfo(testId);
239         if (testInfo == null)
240             return;
241         
242         testStatusChanged(testInfo);
243     }
244     
245     private void updatePath(TreeItem parent) {
246         List JavaDoc newPath= new ArrayList JavaDoc();
247         while (parent != null) {
248             newPath.add(parent);
249             parent= parent.getParentItem();
250         }
251         Collections.reverse(newPath);
252         
253         // common path
254
ListIterator JavaDoc old= fExecutionPath.listIterator();
255         ListIterator JavaDoc np= newPath.listIterator();
256         int c= 0;
257         while (old.hasNext() && np.hasNext()) {
258             if (old.next() != np.next())
259                 break;
260             c++;
261         }
262         // clear old path
263
for (ListIterator JavaDoc iter= fExecutionPath.listIterator(c); iter.hasNext(); )
264             refreshItem((TreeItem) iter.next(), false);
265         // update new path
266
for (ListIterator JavaDoc iter= newPath.listIterator(c); iter.hasNext(); )
267             refreshItem((TreeItem) iter.next(), true);
268         fExecutionPath= newPath;
269     }
270     
271     private void refreshItem(TreeItem item, boolean onPath) {
272         if (onPath)
273             item.setImage(fSuiteRunningIcon);
274         else {
275             TestRunInfo info= getTestRunInfo(item);
276             switch (info.getStatus()) {
277             case ITestRunListener.STATUS_ERROR:
278                 item.setImage(fSuiteErrorIcon);
279                 break;
280             case ITestRunListener.STATUS_FAILURE:
281                 item.setImage(fSuiteFailIcon);
282                 break;
283             default:
284                 item.setImage(fSuiteIcon);
285             }
286         }
287     }
288     
289     private void setCurrentItem(TreeItem treeItem) {
290         treeItem.setImage(fTestRunningIcon);
291     
292         TreeItem parent= treeItem.getParentItem();
293         if (fTestRunnerPart.isAutoScroll()) {
294             fTree.showItem(treeItem);
295             while (parent != null) {
296                 if (parent.getExpanded())
297                     break;
298                 parent.setExpanded(true);
299                 parent= parent.getParentItem();
300             }
301         }
302     }
303
304     public void endTest(String JavaDoc testId) {
305         TreeItem treeItem= findTreeItem(testId);
306         if (treeItem == null)
307             return;
308             
309         TestRunInfo testInfo= fTestRunnerPart.getTestInfo(testId);
310         // fix for 61709 NPE in JUnit view plus strange behavior
311
// the testInfo map can already be destroyed at this point
312
if (testInfo == null)
313             return;
314         
315         updateItem(treeItem, testInfo);
316         
317         if (fTestRunnerPart.isAutoScroll()) {
318             fTree.showItem(treeItem);
319             cacheItems(treeItem);
320             collapsePassedTests(treeItem);
321         }
322     }
323
324     private void cacheItems(TreeItem treeItem) {
325         TreeItem parent= treeItem.getParentItem();
326         if (parent == fCachedParent)
327             return;
328         if (parent != null)
329             fCachedItems= parent.getItems();
330         else
331             fCachedItems= null;
332         fCachedParent= parent;
333     }
334
335     private void collapsePassedTests(TreeItem treeItem) {
336         TreeItem parent= treeItem.getParentItem();
337         if (parent != null) {
338             TreeItem[] items= null;
339             if (parent == fCachedParent)
340                 items= fCachedItems;
341             else
342                 items= parent.getItems();
343                 
344             if (isLast(treeItem, items)) {
345                 boolean ok= true;
346                 for (int i= 0; i < items.length; i++) {
347                     if (isFailure(items[i])) {
348                         ok= false;
349                         break;
350                     }
351                 }
352                 if (ok) {
353                     parent.setExpanded(false);
354                     collapsePassedTests(parent);
355                 }
356             }
357         }
358     }
359
360     private boolean isLast(TreeItem treeItem, TreeItem[] items) {
361         return items[items.length-1] == treeItem;
362     }
363
364     private void updateItem(TreeItem treeItem, TestRunInfo testInfo) {
365         treeItem.setData(testInfo);
366         if(testInfo.getStatus() == ITestRunListener.STATUS_OK) {
367             treeItem.setImage(fOkIcon);
368             return;
369         }
370         if (testInfo.getStatus() == ITestRunListener.STATUS_FAILURE)
371             treeItem.setImage(fFailureIcon);
372         else if (testInfo.getStatus() == ITestRunListener.STATUS_ERROR)
373             treeItem.setImage(fErrorIcon);
374         propagateStatus(treeItem, testInfo.getStatus());
375     }
376
377     private void propagateStatus(TreeItem item, int status) {
378         TreeItem parent= item.getParentItem();
379         TestRunInfo testRunInfo= getTestRunInfo(item);
380         
381         if (parent == null)
382             return;
383         TestRunInfo parentInfo= getTestRunInfo(parent);
384         int parentStatus= parentInfo.getStatus();
385         
386         if (status == ITestRunListener.STATUS_FAILURE) {
387             if (parentStatus == ITestRunListener.STATUS_ERROR || parentStatus == ITestRunListener.STATUS_FAILURE)
388                 return;
389             parentInfo.setStatus(ITestRunListener.STATUS_FAILURE);
390             testRunInfo.setStatus(ITestRunListener.STATUS_FAILURE);
391         } else {
392             if (parentStatus == ITestRunListener.STATUS_ERROR)
393                 return;
394             parentInfo.setStatus(ITestRunListener.STATUS_ERROR);
395             testRunInfo.setStatus(ITestRunListener.STATUS_ERROR);
396         }
397         propagateStatus(parent, status);
398     }
399     
400     private TestRunInfo getTestRunInfo(TreeItem item) {
401         return (TestRunInfo)item.getData();
402     }
403
404     public void activate() {
405         fMoveSelection= false;
406         testSelected();
407     }
408     
409     public void setFocus() {
410         fTree.setFocus();
411     }
412
413     public void aboutToStart() {
414         fTree.removeAll();
415         fSuiteInfos.removeAllElements();
416         fTreeItemMap= new HashMap JavaDoc();
417         fCachedParent= null;
418         fCachedItems= null;
419         fMoveSelection= false;
420         fExecutionPath= new ArrayList JavaDoc();
421     }
422     
423     private void testSelected() {
424         fTestRunnerPart.handleTestSelected(getTestInfo());
425     }
426     
427     private void addListeners() {
428         fTree.addSelectionListener(new SelectionListener() {
429             public void widgetSelected(SelectionEvent e) {
430                 activate();
431             }
432             public void widgetDefaultSelected(SelectionEvent e) {
433                 handleDefaultSelected(null);
434             }
435         });
436         
437         fTree.addDisposeListener(new DisposeListener() {
438             public void widgetDisposed(DisposeEvent e) {
439                 disposeIcons();
440             }
441         });
442     }
443     
444     void handleDefaultSelected(MouseEvent e) {
445         TestRunInfo testInfo= getTestInfo();
446         
447         if (testInfo == null)
448             return;
449             
450         String JavaDoc testLabel= testInfo.getTestName();
451         OpenTestAction action= null;
452         
453         if (isSuiteSelected())
454             action= new OpenTestAction(fTestRunnerPart, testLabel);
455         else
456             action= new OpenTestAction(fTestRunnerPart, getClassName(), getTestMethod());
457
458         if (action.isEnabled())
459             action.run();
460     }
461
462     public void menuAboutToShow(IMenuManager manager) {
463         if (fTree.getSelectionCount() > 0) {
464             TreeItem treeItem= fTree.getSelection()[0];
465             TestRunInfo testInfo= (TestRunInfo) treeItem.getData();
466             String JavaDoc testLabel= testInfo.getTestName();
467             if (isSuiteSelected()) {
468                 manager.add(new OpenTestAction(fTestRunnerPart, testLabel));
469                 manager.add(new Separator());
470                 if (testClassExists(getClassName()) && !fTestRunnerPart.lastLaunchIsKeptAlive()) {
471                     manager.add(new RerunAction(fTestRunnerPart, getSelectedTestId(), getClassName(), null, ILaunchManager.RUN_MODE));
472                     manager.add(new RerunAction(fTestRunnerPart, getSelectedTestId(), getClassName(), null, ILaunchManager.DEBUG_MODE));
473                 }
474             } else {
475                 manager.add(new OpenTestAction(fTestRunnerPart, getClassName(), getTestMethod()));
476                 manager.add(new Separator());
477                 if (fTestRunnerPart.lastLaunchIsKeptAlive()) {
478                     manager.add(new RerunAction(fTestRunnerPart, getSelectedTestId(), getClassName(), getTestMethod(), ILaunchManager.RUN_MODE));
479                     
480                 } else {
481                     manager.add(new RerunAction(fTestRunnerPart, getSelectedTestId(), getClassName(), getTestMethod(), ILaunchManager.RUN_MODE));
482                     manager.add(new RerunAction(fTestRunnerPart, getSelectedTestId(), getClassName(), getTestMethod(), ILaunchManager.DEBUG_MODE));
483                 }
484             }
485             manager.add(new Separator());
486             manager.add(new ExpandAllAction());
487         }
488         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
489         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
490
}
491     
492     private boolean testClassExists(String JavaDoc className) {
493         IJavaProject project= fTestRunnerPart.getLaunchedProject();
494         try {
495             IType type= project.findType(className);
496             return type != null;
497         } catch (JavaModelException e) {
498             // fall through
499
}
500         return false;
501     }
502
503     public void newTreeEntry(String JavaDoc treeEntry) {
504         // format: testId","testName","isSuite","testcount
505
int index0= treeEntry.indexOf(',');
506         StringBuffer JavaDoc testStringBuffer= new StringBuffer JavaDoc(100);
507         int index1= scanTestName(treeEntry, index0+1, testStringBuffer);
508         int index2= treeEntry.indexOf(',', index1+1);
509         String JavaDoc testString= testStringBuffer.toString().trim();
510         String JavaDoc id= treeEntry.substring(0, index0);
511         TestRunInfo testInfo= new TestRunInfo(id, testString);
512         
513         String JavaDoc isSuite= treeEntry.substring(index1+1, index2);
514         int testCount= Integer.parseInt(treeEntry.substring(index2+1));
515         TreeItem treeItem;
516
517         while((fSuiteInfos.size() > 0) && (((SuiteInfo) fSuiteInfos.lastElement()).fTestCount == 0)) {
518             fSuiteInfos.removeElementAt(fSuiteInfos.size()-1);
519         }
520     
521         if(fSuiteInfos.size() == 0){
522             treeItem= new TreeItem(fTree, SWT.NONE);
523             treeItem.setImage(fSuiteIcon);
524             fSuiteInfos.addElement(new SuiteInfo(treeItem, testCount));
525         } else if(isSuite.equals("true")) { //$NON-NLS-1$
526
treeItem= new TreeItem(((SuiteInfo) fSuiteInfos.lastElement()).fTreeItem, SWT.NONE);
527             treeItem.setImage(fSuiteIcon);
528             ((SuiteInfo)fSuiteInfos.lastElement()).fTestCount -= 1;
529             fSuiteInfos.addElement(new SuiteInfo(treeItem, testCount));
530         } else {
531             treeItem= new TreeItem(((SuiteInfo) fSuiteInfos.lastElement()).fTreeItem, SWT.NONE);
532             treeItem.setImage(fTestIcon);
533             ((SuiteInfo)fSuiteInfos.lastElement()).fTestCount -= 1;
534         }
535         mapTest(testInfo, treeItem);
536         treeItem.setText(testInfo.getTestMethodName());
537         treeItem.setData(testInfo);
538     }
539     
540     private int scanTestName(String JavaDoc s, int start, StringBuffer JavaDoc testName) {
541         boolean inQuote= false;
542         int i= start;
543         for (; i < s.length(); i++) {
544             char c= s.charAt(i);
545             if (c == '\\' && !inQuote) {
546                 inQuote= true;
547                 continue;
548             } else if (inQuote) {
549                 inQuote= false;
550                 testName.append(c);
551             } else if (c == ',')
552                 break;
553             else
554                 testName.append(c);
555         }
556         return i;
557     }
558
559
560     private void mapTest(TestRunInfo info, TreeItem item) {
561         fTreeItemMap.put(info.getTestId(), item);
562     }
563     
564     private TreeItem findTreeItem(String JavaDoc testId) {
565         Object JavaDoc o= fTreeItemMap.get(testId);
566         if (o instanceof TreeItem)
567             return (TreeItem)o;
568         return null;
569     }
570     
571     /*
572      * @see ITestRunView#testStatusChanged(TestRunInfo, int)
573      */

574     public void testStatusChanged(TestRunInfo newInfo) {
575         Object JavaDoc o= fTreeItemMap.get(newInfo.getTestId());
576         if (o instanceof TreeItem) {
577             updateItem((TreeItem)o, newInfo);
578             return;
579         }
580     }
581
582     /* (non-Javadoc)
583      * @see org.eclipse.jdt.internal.junit.ui.ITestRunView#selectNext()
584      */

585     public void selectNext() {
586         TreeItem selection= getInitialSearchSelection();
587         if (!moveSelection(selection))
588             return;
589             
590         TreeItem failure= findFailure(selection, true, !isLeafFailure(selection));
591         if (failure != null)
592             selectTest(failure);
593     }
594     
595     /* (non-Javadoc)
596      * @see org.eclipse.jdt.internal.junit.ui.ITestRunView#selectPrevious()
597      */

598     public void selectPrevious() {
599         TreeItem selection= getInitialSearchSelection();
600         if (!moveSelection(selection))
601             return;
602         
603         TreeItem failure= findFailure(selection, false, !isLeafFailure(selection));
604         if (failure != null)
605             selectTest(failure);
606     }
607
608     private boolean moveSelection(TreeItem selection) {
609         if (!fMoveSelection) {
610             fMoveSelection= true;
611             if (isLeafFailure(selection)) {
612                 selectTest(selection);
613                 return false;
614             }
615         }
616         return true;
617     }
618
619     private TreeItem getInitialSearchSelection() {
620         TreeItem[] treeItems= fTree.getSelection();
621         TreeItem selection= null;
622         
623         if (treeItems.length == 0)
624             selection= fTree.getItems()[0];
625         else
626             selection= treeItems[0];
627         return selection;
628     }
629
630     private boolean isFailure(TreeItem selection) {
631         return !(getTestRunInfo(selection).getStatus() == ITestRunListener.STATUS_OK);
632     }
633
634     private boolean isLeafFailure(TreeItem selection) {
635         boolean isLeaf= selection.getItemCount() == 0;
636         return isLeaf && isFailure(selection);
637     }
638
639     private void selectTest(TreeItem selection) {
640         fTestRunnerPart.showTest(getTestRunInfo(selection));
641     }
642
643     private TreeItem findFailure(TreeItem start, boolean next, boolean includeNode) {
644         TreeItem[] sib= findSiblings(start, next, includeNode);
645         if (next) {
646             for (int i= 0; i < sib.length; i++) {
647                 TreeItem failure= findFailureInTree(sib[i]);
648                 if (failure != null)
649                     return failure;
650             }
651         } else {
652             for (int i= sib.length-1; i >= 0; i--) {
653                 TreeItem failure= findFailureInTree(sib[i]);
654                 if (failure != null)
655                     return failure;
656             }
657         }
658         TreeItem parent= start.getParentItem();
659         if (parent == null)
660             return null;
661         return findFailure(parent, next, false);
662     }
663
664     private TreeItem[] findSiblings(TreeItem item, boolean next, boolean includeNode) {
665         TreeItem parent= item.getParentItem();
666         TreeItem[] children= null;
667         if (parent == null)
668             children= item.getParent().getItems();
669         else
670             children= parent.getItems();
671         
672         for (int i= 0; i < children.length; i++) {
673             TreeItem item2= children[i];
674             if (item2 == item) {
675                 TreeItem[] result= null;
676                 if (next) {
677                     if (!includeNode) {
678                         result= new TreeItem[children.length-i-1];
679                         System.arraycopy(children, i+1, result, 0, children.length-i-1);
680                     } else {
681                         result= new TreeItem[children.length-i];
682                         System.arraycopy(children, i, result, 0, children.length-i);
683                         
684                     }
685                 } else {
686                     if (!includeNode) {
687                         result= new TreeItem[i];
688                         System.arraycopy(children, 0, result, 0, i);
689                     } else {
690                         result= new TreeItem[i+1];
691                         System.arraycopy(children, 0, result, 0, i+1);
692                     }
693                 }
694                 return result;
695             }
696         }
697         return new TreeItem[0];
698     }
699
700     private TreeItem findFailureInTree(TreeItem item) {
701         if (item.getItemCount() == 0) {
702             if (isFailure(item))
703                 return item;
704         }
705         TreeItem[] children= item.getItems();
706         for (int i= 0; i < children.length; i++) {
707             TreeItem item2= findFailureInTree(children[i]);
708             if (item2 != null)
709                 return item2;
710         }
711         return null;
712     }
713     
714     protected void expandAll() {
715         TreeItem[] treeItems= fTree.getSelection();
716         fTree.setRedraw(false);
717         for (int i= 0; i < treeItems.length; i++) {
718             expandAll(treeItems[i]);
719         }
720         fTree.setRedraw(true);
721     }
722     
723     private void expandAll(TreeItem item) {
724         item.setExpanded(true);
725         TreeItem[] items= item.getItems();
726         for (int i= 0; i < items.length; i++) {
727             expandAll(items[i]);
728         }
729     }
730
731     public void aboutToEnd() {
732         for (int i= 0; i < fExecutionPath.size(); i++) {
733             refreshItem((TreeItem) fExecutionPath.get(i), false);
734         }
735     }
736     public void addSelectionChangedListener(ISelectionChangedListener listener) {
737         fSelectionListeners.add(listener);
738     }
739
740     public ISelection getSelection() {
741         TestRunInfo testInfo= getTestInfo();
742         if (testInfo == null)
743             return StructuredSelection.EMPTY;
744         return new StructuredSelection(testInfo);
745     }
746
747     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
748         fSelectionListeners.remove(listener);
749     }
750
751     public void setSelection(ISelection selection) {
752     }
753     
754     private void fireSelectionChanged() {
755         SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection());
756         Object JavaDoc[] listeners = fSelectionListeners.getListeners();
757         for (int i = 0; i < listeners.length; i++) {
758             ISelectionChangedListener listener = (ISelectionChangedListener)listeners[i];
759             listener.selectionChanged(event);
760         }
761     }
762 }
763
Popular Tags