KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.junit.ui;
13
14 import java.util.AbstractList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.LinkedHashSet JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.dnd.Clipboard;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Menu;
33 import org.eclipse.swt.widgets.TableItem;
34
35 import org.eclipse.jface.action.Action;
36 import org.eclipse.jface.action.IMenuListener;
37 import org.eclipse.jface.action.IMenuManager;
38 import org.eclipse.jface.action.MenuManager;
39 import org.eclipse.jface.action.Separator;
40 import org.eclipse.jface.viewers.AbstractTreeViewer;
41 import org.eclipse.jface.viewers.ISelectionChangedListener;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.viewers.SelectionChangedEvent;
44 import org.eclipse.jface.viewers.StructuredSelection;
45 import org.eclipse.jface.viewers.StructuredViewer;
46 import org.eclipse.jface.viewers.TableViewer;
47 import org.eclipse.jface.viewers.TreeViewer;
48 import org.eclipse.jface.viewers.Viewer;
49 import org.eclipse.jface.viewers.ViewerFilter;
50
51 import org.eclipse.ui.IWorkbenchActionConstants;
52 import org.eclipse.ui.part.PageBook;
53
54 import org.eclipse.debug.core.ILaunchManager;
55
56 import org.eclipse.jdt.core.IJavaProject;
57 import org.eclipse.jdt.core.IType;
58 import org.eclipse.jdt.core.JavaModelException;
59
60 import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
61 import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
62
63 import org.eclipse.jdt.internal.junit.model.TestCaseElement;
64 import org.eclipse.jdt.internal.junit.model.TestElement;
65 import org.eclipse.jdt.internal.junit.model.TestRoot;
66 import org.eclipse.jdt.internal.junit.model.TestRunSession;
67 import org.eclipse.jdt.internal.junit.model.TestSuiteElement;
68 import org.eclipse.jdt.internal.junit.model.TestElement.Status;
69
70
71 public class TestViewer {
72     private final class TestSelectionListener implements ISelectionChangedListener {
73         public void selectionChanged(SelectionChangedEvent event) {
74             handleSelected();
75         }
76     }
77     
78     private final class TestOpenListener extends SelectionAdapter {
79         public void widgetDefaultSelected(SelectionEvent e) {
80             handleDefaultSelected();
81         }
82     }
83     
84     private final class FailuresOnlyFilter extends ViewerFilter {
85         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
86             return select(((TestElement) element));
87         }
88
89         public boolean select(TestElement testElement) {
90             Status status= testElement.getStatus();
91             if (status.isErrorOrFailure())
92                 return true;
93             else
94                 return ! fTestRunSession.isRunning() && status == Status.RUNNING; // rerunning
95
}
96     }
97
98     private static class ReverseList extends AbstractList JavaDoc {
99         private final List JavaDoc fList;
100         public ReverseList(List JavaDoc list) {
101             fList= list;
102         }
103         public Object JavaDoc get(int index) {
104             return fList.get(fList.size() - index - 1);
105         }
106         public int size() {
107             return fList.size();
108         }
109     }
110     
111     private class ExpandAllAction extends Action {
112         public ExpandAllAction() {
113             setText(JUnitMessages.ExpandAllAction_text);
114             setToolTipText(JUnitMessages.ExpandAllAction_tooltip);
115         }
116         
117         public void run(){
118             fTreeViewer.expandAll();
119         }
120     }
121
122     private final FailuresOnlyFilter fFailuresOnlyFilter= new FailuresOnlyFilter();
123     
124     private final TestRunnerViewPart fTestRunnerPart;
125     private final Clipboard fClipboard;
126     
127     private PageBook fViewerbook;
128     private TreeViewer fTreeViewer;
129     private TestSessionTreeContentProvider fTreeContentProvider;
130     private TestSessionLabelProvider fTreeLabelProvider;
131     private TableViewer fTableViewer;
132     private TestSessionTableContentProvider fTableContentProvider;
133     private TestSessionLabelProvider fTableLabelProvider;
134     private SelectionProviderMediator fSelectionProvider;
135     
136     private final Image fHierarchyIcon;
137     
138     private int fLayoutMode;
139     private boolean fTreeHasFilter;
140     private boolean fTableHasFilter;
141     
142     private TestRunSession fTestRunSession;
143     
144     private boolean fTreeNeedsRefresh;
145     private boolean fTableNeedsRefresh;
146     private HashSet JavaDoc/*<TestElement>*/ fNeedUpdate;
147     private TestCaseElement fAutoScrollTarget;
148     
149     private LinkedList JavaDoc/*<TestSuiteElement>*/ fAutoClose;
150     private HashSet JavaDoc/*<TestSuite>*/ fAutoExpand;
151
152     
153     public TestViewer(Composite parent, Clipboard clipboard, TestRunnerViewPart runner) {
154         fTestRunnerPart= runner;
155         fClipboard= clipboard;
156         
157         fHierarchyIcon= TestRunnerViewPart.createImage("obj16/testhier.gif"); //$NON-NLS-1$
158
parent.addDisposeListener(new DisposeListener() {
159             public void widgetDisposed(DisposeEvent e) {
160                 disposeIcons();
161             }
162         });
163         
164         fLayoutMode= TestRunnerViewPart.LAYOUT_HIERARCHICAL;
165         
166         createTestViewers(parent);
167         
168         registerViewersRefresh();
169         
170         initContextMenu();
171     }
172
173     private void createTestViewers(Composite parent) {
174         fViewerbook= new PageBook(parent, SWT.NULL);
175         
176         fTreeViewer= new TreeViewer(fViewerbook, SWT.V_SCROLL | SWT.SINGLE);
177         ColoredViewersManager.install(fTreeViewer);
178         fTreeViewer.setUseHashlookup(true);
179         fTreeContentProvider= new TestSessionTreeContentProvider();
180         fTreeViewer.setContentProvider(fTreeContentProvider);
181         fTreeLabelProvider= new TestSessionLabelProvider(fTestRunnerPart, TestRunnerViewPart.LAYOUT_HIERARCHICAL);
182         fTreeViewer.setLabelProvider(fTreeLabelProvider);
183         
184         fTableViewer= new TableViewer(fViewerbook, SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
185         ColoredViewersManager.install(fTableViewer);
186         fTableViewer.setUseHashlookup(true);
187         fTableContentProvider= new TestSessionTableContentProvider();
188         fTableViewer.setContentProvider(fTableContentProvider);
189         fTableLabelProvider= new TestSessionLabelProvider(fTestRunnerPart, TestRunnerViewPart.LAYOUT_FLAT);
190         fTableViewer.setLabelProvider(fTableLabelProvider);
191         
192         fSelectionProvider= new SelectionProviderMediator(new StructuredViewer[] { fTreeViewer, fTableViewer }, fTreeViewer);
193         fSelectionProvider.addSelectionChangedListener(new TestSelectionListener());
194         TestOpenListener testOpenListener= new TestOpenListener();
195         fTreeViewer.getTree().addSelectionListener(testOpenListener);
196         fTableViewer.getTable().addSelectionListener(testOpenListener);
197         
198         fViewerbook.showPage(fTreeViewer.getTree());
199     }
200     
201     private void initContextMenu() {
202         MenuManager menuMgr= new MenuManager("#PopupMenu"); //$NON-NLS-1$
203
menuMgr.setRemoveAllWhenShown(true);
204         menuMgr.addMenuListener(new IMenuListener() {
205             public void menuAboutToShow(IMenuManager manager) {
206                 handleMenuAboutToShow(manager);
207             }
208         });
209         fTestRunnerPart.getSite().registerContextMenu(menuMgr, fSelectionProvider);
210         Menu menu= menuMgr.createContextMenu(fViewerbook);
211         fTreeViewer.getTree().setMenu(menu);
212         fTableViewer.getTable().setMenu(menu);
213     }
214
215     
216     void handleMenuAboutToShow(IMenuManager manager) {
217         IStructuredSelection selection= (IStructuredSelection) fSelectionProvider.getSelection();
218         if (! selection.isEmpty()) {
219             TestElement testElement= (TestElement) selection.getFirstElement();
220             
221             String JavaDoc testLabel= testElement.getTestName();
222             String JavaDoc className= testElement.getClassName();
223             if (testElement instanceof TestSuiteElement) {
224                 manager.add(new OpenTestAction(fTestRunnerPart, testLabel));
225                 manager.add(new Separator());
226                 if (testClassExists(className) && !fTestRunnerPart.lastLaunchIsKeptAlive()) {
227                     manager.add(new RerunAction(JUnitMessages.RerunAction_label_run, fTestRunnerPart, testElement.getId(), className, null, ILaunchManager.RUN_MODE));
228                     manager.add(new RerunAction(JUnitMessages.RerunAction_label_debug, fTestRunnerPart, testElement.getId(), className, null, ILaunchManager.DEBUG_MODE));
229                 }
230             } else {
231                 TestCaseElement testCaseElement= (TestCaseElement) testElement;
232                 String JavaDoc testMethodName= testCaseElement.getTestMethodName();
233                 manager.add(new OpenTestAction(fTestRunnerPart, className, testMethodName));
234                 manager.add(new Separator());
235                 if (fTestRunnerPart.lastLaunchIsKeptAlive()) {
236                     manager.add(new RerunAction(JUnitMessages.RerunAction_label_rerun, fTestRunnerPart, testElement.getId(), className, testMethodName, ILaunchManager.RUN_MODE));
237                     
238                 } else {
239                     manager.add(new RerunAction(JUnitMessages.RerunAction_label_run, fTestRunnerPart, testElement.getId(), className, testMethodName, ILaunchManager.RUN_MODE));
240                     manager.add(new RerunAction(JUnitMessages.RerunAction_label_debug, fTestRunnerPart, testElement.getId(), className, testMethodName, ILaunchManager.DEBUG_MODE));
241                 }
242             }
243             if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL) {
244                 manager.add(new Separator());
245                 manager.add(new ExpandAllAction());
246             }
247
248         }
249         if (fTestRunSession != null && fTestRunSession.getFailureCount() + fTestRunSession.getErrorCount() > 0) {
250             if (fLayoutMode != TestRunnerViewPart.LAYOUT_HIERARCHICAL)
251                 manager.add(new Separator());
252             manager.add(new CopyFailureListAction(fTestRunnerPart, fClipboard));
253         }
254         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
255         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
256
}
257     
258     private boolean testClassExists(String JavaDoc className) {
259         IJavaProject project= fTestRunnerPart.getLaunchedProject();
260         if (project == null)
261             return false;
262         try {
263             IType type= project.findType(className);
264             return type != null;
265         } catch (JavaModelException e) {
266             // fall through
267
}
268         return false;
269     }
270
271     public Control getTestViewerControl() {
272         return fViewerbook;
273     }
274
275     public synchronized void registerActiveSession(TestRunSession testRunSession) {
276         fTestRunSession= testRunSession;
277         registerAutoScrollTarget(null);
278         registerViewersRefresh();
279     }
280
281     void handleDefaultSelected() {
282         IStructuredSelection selection= (IStructuredSelection) fSelectionProvider.getSelection();
283         if (selection.size() != 1)
284             return;
285
286         TestElement testElement= (TestElement) selection.getFirstElement();
287
288         OpenTestAction action;
289         if (testElement instanceof TestSuiteElement) {
290             action= new OpenTestAction(fTestRunnerPart, testElement.getTestName());
291         } else if (testElement instanceof TestCaseElement){
292             TestCaseElement testCase= (TestCaseElement) testElement;
293             action= new OpenTestAction(fTestRunnerPart, testCase.getClassName(), testCase.getTestMethodName());
294         } else {
295             throw new IllegalStateException JavaDoc(String.valueOf(testElement));
296         }
297
298         if (action.isEnabled())
299             action.run();
300     }
301     
302     private void handleSelected() {
303         IStructuredSelection selection= (IStructuredSelection) fSelectionProvider.getSelection();
304         TestElement testElement= null;
305         if (selection.size() == 1) {
306             testElement= (TestElement) selection.getFirstElement();
307         }
308         fTestRunnerPart.handleTestSelected(testElement);
309     }
310     
311     void disposeIcons() {
312         fHierarchyIcon.dispose();
313     }
314
315     public synchronized void setShowFailuresOnly(boolean failuresOnly, int layoutMode) {
316         /*
317          * Management of fTreeViewer and fTableViewer
318          * ******************************************
319          * - invisible viewer is updated on registerViewerUpdate unless its f*NeedsRefresh is true
320          * - invisible viewer is not refreshed upfront
321          * - on layout change, new viewer is refreshed if necessary
322          * - filter only applies to "current" layout mode / viewer
323          */

324         try {
325             fViewerbook.setRedraw(false);
326             
327             IStructuredSelection selection= null;
328             boolean switchLayout= layoutMode != fLayoutMode;
329             if (switchLayout) {
330                 selection= (IStructuredSelection) fSelectionProvider.getSelection();
331                 if (layoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL) {
332                     if (fTreeNeedsRefresh) {
333                         clearUpdateAndExpansion();
334                     }
335                 } else {
336                     if (fTableNeedsRefresh) {
337                         clearUpdateAndExpansion();
338                     }
339                 }
340                 fLayoutMode= layoutMode;
341                 fViewerbook.showPage(getActiveViewer().getControl());
342             }
343             
344             //avoid realizing all TableItems, especially in flat mode!
345
StructuredViewer viewer= getActiveViewer();
346             if (failuresOnly) {
347                 if (! getActiveViewerHasFilter()) {
348                     setActiveViewerHasFilter(true);
349                     if (getActiveViewerNeedsRefresh())
350                         viewer.setInput(null);
351                     viewer.addFilter(fFailuresOnlyFilter);
352                 }
353                 
354             } else {
355                 if (getActiveViewerHasFilter()) {
356                     setActiveViewerHasFilter(false);
357                     if (getActiveViewerNeedsRefresh())
358                         viewer.setInput(null);
359                     viewer.removeFilter(fFailuresOnlyFilter);
360                 }
361             }
362             processChangesInUI();
363             
364             if (selection != null) {
365                 // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=125708
366
// (ITreeSelection not adapted if TreePaths changed):
367
StructuredSelection flatSelection= new StructuredSelection(selection.toList());
368                 fSelectionProvider.setSelection(flatSelection, true);
369             }
370             
371         } finally {
372             fViewerbook.setRedraw(true);
373         }
374     }
375
376     private boolean getActiveViewerHasFilter() {
377         if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
378             return fTreeHasFilter;
379         else
380             return fTableHasFilter;
381     }
382     
383     private void setActiveViewerHasFilter(boolean filter) {
384         if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
385             fTreeHasFilter= filter;
386         else
387             fTableHasFilter= filter;
388     }
389     
390     private StructuredViewer getActiveViewer() {
391         if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
392             return fTreeViewer;
393         else
394             return fTableViewer;
395     }
396
397     private boolean getActiveViewerNeedsRefresh() {
398         if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
399             return fTreeNeedsRefresh;
400         else
401             return fTableNeedsRefresh;
402     }
403     
404     private void setActiveViewerRefreshed() {
405         if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
406             fTreeNeedsRefresh= false;
407         else
408             fTableNeedsRefresh= false;
409     }
410     
411     /**
412      * To be called periodically by the TestRunnerViewPart (in the UI thread).
413      */

414     public void processChangesInUI() {
415         TestRoot testRoot;
416         if (fTestRunSession == null) {
417             registerViewersRefresh();
418             fTreeNeedsRefresh= false;
419             fTableNeedsRefresh= false;
420             fTreeViewer.setInput(null);
421             fTableViewer.setInput(null);
422             return;
423         }
424         
425         testRoot= fTestRunSession.getTestRoot();
426         
427         StructuredViewer viewer= getActiveViewer();
428         if (getActiveViewerNeedsRefresh()) {
429             clearUpdateAndExpansion();
430             setActiveViewerRefreshed();
431             viewer.setInput(testRoot);
432             
433         } else {
434             Object JavaDoc[] toUpdate;
435             synchronized (this) {
436                 toUpdate= fNeedUpdate.toArray();
437                 fNeedUpdate.clear();
438             }
439             if (! fTreeNeedsRefresh && toUpdate.length > 0) {
440                 if (fTreeHasFilter)
441                     for (int i= 0; i < toUpdate.length; i++)
442                         updateElementInTree((TestElement) toUpdate[i]);
443                 else {
444                     HashSet JavaDoc toUpdateWithParents= new HashSet JavaDoc();
445                     toUpdateWithParents.addAll(Arrays.asList(toUpdate));
446                     for (int i= 0; i < toUpdate.length; i++) {
447                         TestElement parent= ((TestElement) toUpdate[i]).getParent();
448                         while (parent != null) {
449                             toUpdateWithParents.add(parent);
450                             parent= parent.getParent();
451                         }
452                     }
453                     fTreeViewer.update(toUpdateWithParents.toArray(), null);
454                 }
455             }
456             if (! fTableNeedsRefresh && toUpdate.length > 0) {
457                 if (fTableHasFilter)
458                     for (int i= 0; i < toUpdate.length; i++)
459                         updateElementInTable((TestElement) toUpdate[i]);
460                 else
461                     fTableViewer.update(toUpdate, null);
462             }
463         }
464         autoScrollInUI();
465     }
466
467     private void updateElementInTree(final TestElement testElement) {
468         if (isShown(testElement)) {
469             updateShownElementInTree(testElement);
470         } else {
471             TestElement current= testElement;
472             do {
473                 if (fTreeViewer.testFindItem(current) != null)
474                     fTreeViewer.remove(current);
475                 current= current.getParent();
476             } while (! (current instanceof TestRoot) && ! isShown(current));
477             
478             while (current != null && ! (current instanceof TestRoot)) {
479                 fTreeViewer.update(current, null);
480                 current= current.getParent();
481             }
482         }
483     }
484
485     private void updateShownElementInTree(TestElement testElement) {
486         if (testElement == null || testElement instanceof TestRoot) // paranoia null check
487
return;
488         
489         TestSuiteElement parent= testElement.getParent();
490         updateShownElementInTree(parent); // make sure parent is shown and up-to-date
491

492         if (fTreeViewer.testFindItem(testElement) == null) {
493             fTreeViewer.add(parent, testElement); // if not yet in tree: add
494
} else {
495             fTreeViewer.update(testElement, null); // if in tree: update
496
}
497     }
498
499     private void updateElementInTable(TestElement element) {
500         if (isShown(element)) {
501             if (fTableViewer.testFindItem(element) == null) {
502                 TestElement previous= getNextFailure(element, false);
503                 int insertionIndex= -1;
504                 if (previous != null) {
505                     TableItem item= (TableItem) fTableViewer.testFindItem(previous);
506                     if (item != null)
507                         insertionIndex= fTableViewer.getTable().indexOf(item);
508                 }
509                 fTableViewer.insert(element, insertionIndex);
510             } else {
511                 fTableViewer.update(element, null);
512             }
513         } else {
514             fTableViewer.remove(element);
515         }
516     }
517
518     private boolean isShown(TestElement current) {
519         return fFailuresOnlyFilter.select(current);
520     }
521
522     private void autoScrollInUI() {
523         if (! fTestRunnerPart.isAutoScroll()) {
524             clearAutoExpand();
525             fAutoClose.clear();
526             return;
527         }
528         
529         if (fLayoutMode == TestRunnerViewPart.LAYOUT_FLAT) {
530             if (fAutoScrollTarget != null)
531                 fTableViewer.reveal(fAutoScrollTarget);
532             return;
533         }
534         
535         synchronized (this) {
536             for (Iterator JavaDoc iter= fAutoExpand.iterator(); iter.hasNext();) {
537                 TestSuiteElement suite= (TestSuiteElement) iter.next();
538                 fTreeViewer.setExpandedState(suite, true);
539             }
540             clearAutoExpand();
541         }
542         
543         TestCaseElement current= fAutoScrollTarget;
544         fAutoScrollTarget= null;
545         
546         TestSuiteElement parent= current == null ? null : (TestSuiteElement) fTreeContentProvider.getParent(current);
547         if (fAutoClose.isEmpty() || ! fAutoClose.getLast().equals(parent)) {
548             // we're in a new branch, so let's close old OK branches:
549
for (ListIterator JavaDoc iter= fAutoClose.listIterator(fAutoClose.size()); iter.hasPrevious();) {
550                 TestSuiteElement previousAutoOpened= (TestSuiteElement) iter.previous();
551                 if (previousAutoOpened.equals(parent))
552                     break;
553                 
554                 if (previousAutoOpened.getStatus() == TestElement.Status.OK) {
555                     // auto-opened the element, and all children are OK -> auto close
556
iter.remove();
557                     fTreeViewer.collapseToLevel(previousAutoOpened, AbstractTreeViewer.ALL_LEVELS);
558                 }
559             }
560             
561             while (parent != null && ! fTestRunSession.getTestRoot().equals(parent) && fTreeViewer.getExpandedState(parent) == false) {
562                 fAutoClose.add(parent); // add to auto-opened elements -> close later if STATUS_OK
563
parent= (TestSuiteElement) fTreeContentProvider.getParent(parent);
564             }
565         }
566         if (current != null)
567             fTreeViewer.reveal(current);
568     }
569
570     public void selectFirstFailure() {
571         TestCaseElement firstFailure= getNextChildFailure(fTestRunSession.getTestRoot(), true);
572         if (firstFailure != null)
573             getActiveViewer().setSelection(new StructuredSelection(firstFailure), true);
574     }
575     
576     public void selectFailure(boolean showNext) {
577         IStructuredSelection selection= (IStructuredSelection) getActiveViewer().getSelection();
578         TestElement selected= (TestElement) selection.getFirstElement();
579         TestElement next;
580         
581         if (selected == null) {
582             next= getNextChildFailure(fTestRunSession.getTestRoot(), showNext);
583         } else {
584             next= getNextFailure(selected, showNext);
585         }
586         
587         if (next != null)
588             getActiveViewer().setSelection(new StructuredSelection(next), true);
589     }
590
591     private TestElement getNextFailure(TestElement selected, boolean showNext) {
592         if (selected instanceof TestSuiteElement) {
593             TestElement nextChild= getNextChildFailure((TestSuiteElement) selected, showNext);
594             if (nextChild != null)
595                 return nextChild;
596         }
597         return getNextFailureSibling(selected, showNext);
598     }
599     
600     private TestCaseElement getNextFailureSibling(TestElement current, boolean showNext) {
601         TestSuiteElement parent= current.getParent();
602         if (parent == null)
603             return null;
604         
605         List JavaDoc siblings= Arrays.asList(parent.getChildren());
606         if (! showNext)
607             siblings= new ReverseList(siblings);
608         
609         int nextIndex= siblings.indexOf(current) + 1;
610         for (int i= nextIndex; i < siblings.size(); i++) {
611             TestElement sibling= (TestElement) siblings.get(i);
612             if (sibling.getStatus().isErrorOrFailure()) {
613                 if (sibling instanceof TestCaseElement) {
614                     return (TestCaseElement) sibling;
615                 } else {
616                     return getNextChildFailure((TestSuiteElement) sibling, showNext);
617                 }
618             }
619         }
620         return getNextFailureSibling(parent, showNext);
621     }
622
623     private TestCaseElement getNextChildFailure(TestSuiteElement root, boolean showNext) {
624         List JavaDoc children= Arrays.asList(root.getChildren());
625         if (! showNext)
626             children= new ReverseList(children);
627         for (int i= 0; i < children.size(); i++) {
628             TestElement child= (TestElement) children.get(i);
629             if (child.getStatus().isErrorOrFailure()) {
630                 if (child instanceof TestCaseElement) {
631                     return (TestCaseElement) child;
632                 } else {
633                     return getNextChildFailure((TestSuiteElement) child, showNext);
634                 }
635             }
636         }
637         return null;
638     }
639
640     public synchronized void registerViewersRefresh() {
641         fTreeNeedsRefresh= true;
642         fTableNeedsRefresh= true;
643         clearUpdateAndExpansion();
644     }
645
646     private void clearUpdateAndExpansion() {
647         fNeedUpdate= new LinkedHashSet JavaDoc();
648         fAutoClose= new LinkedList JavaDoc();
649         fAutoExpand= new HashSet JavaDoc();
650     }
651     
652     public synchronized void registerTestAdded(TestElement testElement) {
653         //TODO: performance: would only need to refresh parent of added element
654
fTreeNeedsRefresh= true;
655         fTableNeedsRefresh= true;
656     }
657     
658     public synchronized void registerViewerUpdate(final TestElement testElement) {
659         fNeedUpdate.add(testElement);
660     }
661
662     private synchronized void clearAutoExpand() {
663         fAutoExpand.clear();
664     }
665     
666     public void registerAutoScrollTarget(TestCaseElement testCaseElement) {
667         fAutoScrollTarget= testCaseElement;
668     }
669
670     public synchronized void registerFailedForAutoScroll(TestElement testElement) {
671         Object JavaDoc parent= fTreeContentProvider.getParent(testElement);
672         if (parent != null)
673             fAutoExpand.add(parent);
674     }
675
676     public void expandFirstLevel() {
677         fTreeViewer.expandToLevel(2);
678     }
679     
680 }
681  
682
Popular Tags