1 | 12 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 87 | public class WorkbenchContentsFinder { |
39 | |
|
40 | |
|
41 | |
|
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 | |
|
53 | |
|
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 | |
|
66 | |
|
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 | |
|
78 | |
|
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 | 334 | for (IViewReference viewReference : viewReferences) { |
95 | 285 | 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 | |
|
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 | |
|
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 | |
|
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 | |
} |