001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.navigator;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014: import java.util.logging.Logger;
015:
016: import org.eclipse.core.resources.IContainer;
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.resources.ResourcesPlugin;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.Platform;
022: import org.eclipse.core.runtime.jobs.IJobManager;
023: import org.eclipse.core.runtime.jobs.Job;
024: import org.eclipse.swt.widgets.Display;
025: import org.eclipse.swt.widgets.Shell;
026: import org.eclipse.ui.IEditorPart;
027: import org.eclipse.ui.IEditorReference;
028: import org.eclipse.ui.IViewReference;
029: import org.eclipse.ui.IWorkbench;
030: import org.eclipse.ui.IWorkbenchPage;
031: import org.eclipse.ui.IWorkbenchPart;
032: import org.eclipse.ui.IWorkbenchPartSite;
033: import org.eclipse.ui.IWorkbenchWindow;
034: import org.eclipse.ui.PartInitException;
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.WorkbenchException;
037:
038: /**
039: * @since 3.1
040: */
041: public class EditorTestHelper {
042:
043: public static final String TEXT_EDITOR_ID = "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$
044:
045: public static final String COMPILATION_UNIT_EDITOR_ID = "org.eclipse.jdt.ui.CompilationUnitEditor"; //$NON-NLS-1$
046:
047: public static final String RESOURCE_PERSPECTIVE_ID = "org.eclipse.ui.resourcePerspective"; //$NON-NLS-1$
048:
049: public static final String JAVA_PERSPECTIVE_ID = "org.eclipse.jdt.ui.JavaPerspective"; //$NON-NLS-1$
050:
051: public static final String OUTLINE_VIEW_ID = "org.eclipse.ui.views.ContentOutline"; //$NON-NLS-1$
052:
053: public static final String PACKAGE_EXPLORER_VIEW_ID = "org.eclipse.jdt.ui.PackageExplorer"; //$NON-NLS-1$
054:
055: public static final String NAVIGATOR_VIEW_ID = "org.eclipse.ui.views.ResourceNavigator"; //$NON-NLS-1$
056:
057: public static final String INTRO_VIEW_ID = "org.eclipse.ui.internal.introview"; //$NON-NLS-1$
058:
059: public static void closeEditor(IEditorPart editor) {
060: IWorkbenchPartSite site;
061: IWorkbenchPage page;
062: if (editor != null && (site = editor.getSite()) != null
063: && (page = site.getPage()) != null)
064: page.closeEditor(editor, false);
065: }
066:
067: public static void closeAllEditors() {
068: IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
069: .getWorkbenchWindows();
070: for (int i = 0; i < windows.length; i++) {
071: IWorkbenchPage[] pages = windows[i].getPages();
072: for (int j = 0; j < pages.length; j++) {
073: IEditorReference[] editorReferences = pages[j]
074: .getEditorReferences();
075: for (int k = 0; k < editorReferences.length; k++)
076: closeEditor(editorReferences[k].getEditor(false));
077: }
078: }
079: }
080:
081: /**
082: * Runs the event queue on the current display until it is empty.
083: */
084: public static void runEventQueue() {
085: IWorkbenchWindow window = getActiveWorkbenchWindow();
086: if (window != null)
087: runEventQueue(window.getShell());
088: }
089:
090: public static void runEventQueue(IWorkbenchPart part) {
091: runEventQueue(part.getSite().getShell());
092: }
093:
094: public static void runEventQueue(Shell shell) {
095: runEventQueue(shell.getDisplay());
096: }
097:
098: public static void runEventQueue(Display display) {
099: while (display.readAndDispatch()) {
100: // do nothing
101: }
102: }
103:
104: /**
105: * Runs the event queue on the current display and lets it sleep until the
106: * timeout elapses.
107: *
108: * @param millis the timeout in milliseconds
109: */
110: public static void runEventQueue(long millis) {
111: runEventQueue(getActiveDisplay(), millis);
112: }
113:
114: public static void runEventQueue(IWorkbenchPart part, long millis) {
115: runEventQueue(part.getSite().getShell(), millis);
116: }
117:
118: public static void runEventQueue(Shell shell, long millis) {
119: runEventQueue(shell.getDisplay(), millis);
120: }
121:
122: public static void runEventQueue(Display display, long minTime) {
123: if (display != null) {
124: DisplayHelper.sleep(display, minTime);
125: } else {
126: sleep((int) minTime);
127: }
128: }
129:
130: public static IWorkbenchWindow getActiveWorkbenchWindow() {
131: return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
132: }
133:
134: public static void forceFocus() {
135: IWorkbenchWindow window = getActiveWorkbenchWindow();
136: if (window == null) {
137: IWorkbenchWindow[] wbWindows = PlatformUI.getWorkbench()
138: .getWorkbenchWindows();
139: if (wbWindows.length == 0)
140: return;
141: window = wbWindows[0];
142: }
143: Shell shell = window.getShell();
144: if (shell != null && !shell.isDisposed()) {
145: shell.forceActive();
146: shell.forceFocus();
147: }
148: }
149:
150: public static IWorkbenchPage getActivePage() {
151: IWorkbenchWindow window = getActiveWorkbenchWindow();
152: return window != null ? window.getActivePage() : null;
153: }
154:
155: public static Display getActiveDisplay() {
156: IWorkbenchWindow window = getActiveWorkbenchWindow();
157: return window != null ? window.getShell().getDisplay() : null;
158: }
159:
160: public static void joinBackgroundActivities() throws CoreException {
161: // Join Building
162: Logger.global.entering(
163: "EditorTestHelper", "joinBackgroundActivities"); //$NON-NLS-1$ //$NON-NLS-2$
164: Logger.global.finer("join builder"); //$NON-NLS-1$
165: boolean interrupted = true;
166: while (interrupted) {
167: try {
168: Platform.getJobManager().join(
169: ResourcesPlugin.FAMILY_AUTO_BUILD, null);
170: interrupted = false;
171: } catch (InterruptedException e) {
172: interrupted = true;
173: }
174: }
175: // Join jobs
176: joinJobs(0, 0, 500);
177: Logger.global.exiting(
178: "EditorTestHelper", "joinBackgroundActivities"); //$NON-NLS-1$ //$NON-NLS-2$
179: }
180:
181: public static boolean joinJobs(long minTime, long maxTime,
182: long intervalTime) {
183: Logger.global.entering("EditorTestHelper", "joinJobs"); //$NON-NLS-1$ //$NON-NLS-2$
184: runEventQueue(minTime);
185:
186: DisplayHelper helper = new DisplayHelper() {
187: public boolean condition() {
188: return allJobsQuiet();
189: }
190: };
191: boolean quiet = helper.waitForCondition(getActiveDisplay(),
192: maxTime > 0 ? maxTime : Long.MAX_VALUE, intervalTime);
193: Logger.global.exiting(
194: "EditorTestHelper", "joinJobs", new Boolean(quiet)); //$NON-NLS-1$ //$NON-NLS-2$
195: return quiet;
196: }
197:
198: public static void sleep(int intervalTime) {
199: try {
200: Thread.sleep(intervalTime);
201: } catch (InterruptedException e) {
202: e.printStackTrace();
203: }
204: }
205:
206: public static boolean allJobsQuiet() {
207: IJobManager jobManager = Platform.getJobManager();
208: Job[] jobs = jobManager.find(null);
209: for (int i = 0; i < jobs.length; i++) {
210: Job job = jobs[i];
211: int state = job.getState();
212: if (state == Job.RUNNING || state == Job.WAITING) {
213: Logger.global.finest(job.getName());
214: return false;
215: }
216: }
217: return true;
218: }
219:
220: public static boolean isViewShown(String viewId) {
221: return getActivePage().findViewReference(viewId) != null;
222: }
223:
224: public static boolean showView(String viewId, boolean show)
225: throws PartInitException {
226: IWorkbenchPage activePage = getActivePage();
227: IViewReference view = activePage.findViewReference(viewId);
228: boolean shown = view != null;
229: if (shown != show)
230: if (show)
231: activePage.showView(viewId);
232: else
233: activePage.hideView(view);
234: return shown;
235: }
236:
237: public static void bringToTop() {
238: getActiveWorkbenchWindow().getShell().forceActive();
239: }
240:
241: public static String showPerspective(String perspective)
242: throws WorkbenchException {
243: String shownPerspective = getActivePage().getPerspective()
244: .getId();
245: if (!perspective.equals(shownPerspective)) {
246: IWorkbench workbench = PlatformUI.getWorkbench();
247: IWorkbenchWindow activeWindow = workbench
248: .getActiveWorkbenchWindow();
249: workbench.showPerspective(perspective, activeWindow);
250: }
251: return shownPerspective;
252: }
253:
254: public static IFile[] findFiles(IResource resource)
255: throws CoreException {
256: List files = new ArrayList();
257: findFiles(resource, files);
258: return (IFile[]) files.toArray(new IFile[files.size()]);
259: }
260:
261: private static void findFiles(IResource resource, List files)
262: throws CoreException {
263: if (resource instanceof IFile) {
264: files.add(resource);
265: return;
266: }
267: if (resource instanceof IContainer) {
268: IResource[] resources = ((IContainer) resource).members();
269: for (int i = 0; i < resources.length; i++)
270: findFiles(resources[i], files);
271: }
272: }
273:
274: }
|