Coverage Report - org.eclipse.swtbot.eclipse.finder.finders.WorkbenchContentsFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkbenchContentsFinder
86%
40/46
88%
16/18
1.455
WorkbenchContentsFinder$1
100%
3/3
N/A
1.455
WorkbenchContentsFinder$2
100%
3/3
N/A
1.455
WorkbenchContentsFinder$3
100%
3/3
N/A
1.455
WorkbenchContentsFinder$4
100%
3/3
N/A
1.455
WorkbenchContentsFinder$5
0%
0/3
N/A
1.455
WorkbenchContentsFinder$6
100%
3/3
N/A
1.455
WorkbenchContentsFinder$7
100%
3/3
N/A
1.455
 
 1  12
 /*******************************************************************************
 2  
  * Copyright (c) 2009 SWTBot Committers 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  
  *     Ralf Ebert www.ralfebert.de - (bug 271630) SWTBot Improved RCP / Workbench support
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.eclipse.finder.finders;
 12  
 
 13  
 import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
 14  
 
 15  
 import java.util.ArrayList;
 16  
 import java.util.List;
 17  
 
 18  
 import org.eclipse.swtbot.swt.finder.results.ListResult;
 19  
 import org.eclipse.swtbot.swt.finder.results.Result;
 20  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 21  
 import org.eclipse.ui.IEditorPart;
 22  
 import org.eclipse.ui.IEditorReference;
 23  
 import org.eclipse.ui.IPerspectiveDescriptor;
 24  
 import org.eclipse.ui.IViewReference;
 25  
 import org.eclipse.ui.IWorkbenchPage;
 26  
 import org.eclipse.ui.IWorkbenchPartReference;
 27  
 import org.eclipse.ui.IWorkbenchWindow;
 28  
 import org.eclipse.ui.PlatformUI;
 29  
 import org.hamcrest.Matcher;
 30  
 
 31  
 /**
 32  
  * WorkbenchContentsFinder allows to access the contents of a workbench window (views, editors, pages etc).
 33  
  * 
 34  
  * @author Ralf Ebert www.ralfebert.de (bug 271630)
 35  
  * @noextend This class is not intended to be subclassed by clients.
 36  
  * @noinstantiate This class is not intended to be instantiated by clients.
 37  
  */
 38  87
 public class WorkbenchContentsFinder {
 39  
 
 40  
         /**
 41  
          * @return the active workbench window.
 42  
          */
 43  
         public IWorkbenchWindow activeWorkbenchWindow() {
 44  84
                 return syncExec(new Result<IWorkbenchWindow>() {
 45  
                         public IWorkbenchWindow run() {
 46  84
                                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 47  
                         }
 48  
                 });
 49  
         }
 50  
 
 51  
         /**
 52  
          * @param matcher the matcher used to match editors.
 53  
          * @return the matching editors.
 54  
          */
 55  
         public List<IEditorReference> findEditors(final Matcher<?> matcher) {
 56  25
                 return syncExec(SWTUtils.display(), new ListResult<IEditorReference>() {
 57  
                         public List<IEditorReference> run() {
 58  25
                                 return findEditorsInternal(matcher);
 59  
                         }
 60  
 
 61  
                 });
 62  
         }
 63  
 
 64  
         /**
 65  
          * @param matcher the matcher used to match views
 66  
          * @return the list of matching views
 67  
          */
 68  
         public List<IViewReference> findViews(final Matcher<?> matcher) {
 69  49
                 return syncExec(SWTUtils.display(), new ListResult<IViewReference>() {
 70  
                         public List<IViewReference> run() {
 71  49
                                 return findViewsInternal(matcher);
 72  
                         }
 73  
                 });
 74  
         }
 75  
 
 76  
         /**
 77  
          * @param matcher the matcher used to match perspectives
 78  
          * @return the list of matching perspectives
 79  
          */
 80  
         public List<IPerspectiveDescriptor> findPerspectives(final Matcher<?> matcher) {
 81  2
                 return syncExec(SWTUtils.display(), new ListResult<IPerspectiveDescriptor>() {
 82  
                         public List<IPerspectiveDescriptor> run() {
 83  2
                                 return findPerspectivesInternal(matcher);
 84  
                         }
 85  
                 });
 86  
 
 87  
         }
 88  
 
 89  49
         private List<IViewReference> findViewsInternal(final Matcher<?> matcher) {
 90  49
                 List<IViewReference> result = new ArrayList<IViewReference>();
 91  49
                 IWorkbenchPage[] pages = workbenchPages();
 92  98
                 for (IWorkbenchPage page : pages) {
 93  49
                         IViewReference[] viewReferences = page.getViewReferences();
 94  381
                         for (IViewReference viewReference : viewReferences) {
 95  332
                                 if (matcher.matches(viewReference))
 96  18
                                         result.add(viewReference);
 97  
                         }
 98  
                 }
 99  49
                 return result;
 100  
         }
 101  
 
 102  2
         private List<IPerspectiveDescriptor> findPerspectivesInternal(final Matcher<?> matcher) {
 103  2
                 IPerspectiveDescriptor[] perspectives = activeWorkbenchWindow().getWorkbench().getPerspectiveRegistry().getPerspectives();
 104  2
                 List<IPerspectiveDescriptor> matchingPerspectives = new ArrayList<IPerspectiveDescriptor>();
 105  20
                 for (IPerspectiveDescriptor perspectiveDescriptor : perspectives)
 106  18
                         if (matcher.matches(perspectiveDescriptor))
 107  2
                                 matchingPerspectives.add(perspectiveDescriptor);
 108  2
                 return matchingPerspectives;
 109  
         }
 110  
 
 111  25
         private List<IEditorReference> findEditorsInternal(final Matcher<?> matcher) {
 112  25
                 List<IEditorReference> result = new ArrayList<IEditorReference>();
 113  25
                 IWorkbenchPage[] pages = workbenchPages();
 114  50
                 for (IWorkbenchPage page : pages) {
 115  25
                         IEditorReference[] editorReferences = page.getEditorReferences();
 116  60
                         for (IEditorReference editorReference : editorReferences) {
 117  35
                                 if (matcher.matches(editorReference))
 118  26
                                         result.add(editorReference);
 119  
                         }
 120  
                 }
 121  25
                 return result;
 122  
         }
 123  
 
 124  
         /**
 125  
          * @return the active view.
 126  
          */
 127  
         public IViewReference findActiveView() {
 128  0
                 return syncExec(new Result<IViewReference>() {
 129  
                         public IViewReference run() {
 130  0
                                 return findActiveViewInternal();
 131  
                         }
 132  
                 });
 133  
         }
 134  
 
 135  0
         private IViewReference findActiveViewInternal() {
 136  0
                 IWorkbenchPartReference partReference = activePageInternal().getActivePartReference();
 137  0
                 if (partReference instanceof IViewReference)
 138  0
                         return (IViewReference) partReference;
 139  0
                 return null;
 140  
         }
 141  
 
 142  
         /**
 143  
          * @return the active perspective.
 144  
          */
 145  
         public IPerspectiveDescriptor findActivePerspective() {
 146  2
                 return syncExec(new Result<IPerspectiveDescriptor>() {
 147  
                         public IPerspectiveDescriptor run() {
 148  2
                                 return findActivePerspectiveInternal();
 149  
                         }
 150  
                 });
 151  
         }
 152  
 
 153  
         /**
 154  
          * @return the active editor.
 155  
          */
 156  
         public IEditorReference findActiveEditor() {
 157  6
                 return syncExec(new Result<IEditorReference>() {
 158  
                         public IEditorReference run() {
 159  6
                                 return findActiveEditorInternal();
 160  
                         }
 161  
                 });
 162  
         }
 163  
 
 164  
         private IWorkbenchPage[] workbenchPages() {
 165  74
                 return activeWorkbenchWindow().getPages();
 166  
         }
 167  
 
 168  6
         private IEditorReference findActiveEditorInternal() {
 169  6
                 IWorkbenchPage page = activePageInternal();
 170  6
                 IEditorPart activeEditor = page.getActiveEditor();
 171  6
                 return (IEditorReference) page.getReference(activeEditor);
 172  
         }
 173  
 
 174  2
         private IPerspectiveDescriptor findActivePerspectiveInternal() {
 175  2
                 return activePageInternal().getPerspective();
 176  
         }
 177  
 
 178  
         private IWorkbenchPage activePageInternal() {
 179  8
                 return activeWorkbenchWindow().getActivePage();
 180  
         }
 181  
 }