Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8df14c550ada39195391c716fe5d41701019a9c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************************
 * Copyright (c) 2010 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.linuxtools.changelog.tests.helpers;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.linuxtools.changelog.core.ChangelogPlugin;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;

public class EditorHelper {

	/**
	 * Open file associated with <code>diskresource</code> in current
	 * workspace.
	 * 
	 * @param diskresource The file to be opened in the current workspace.
	 * @return The IEditorPart associated with the opened file in the current workspace
	 *         or null if opening fails.
	 */
	public static IEditorPart openEditor(IFile diskresource) {
		IWorkbench ws = ChangelogPlugin.getDefault().getWorkbench();
		try {
			return org.eclipse.ui.ide.IDE.openEditor(ws
					.getActiveWorkbenchWindow().getActivePage(), diskresource,
					true);
		} catch (PartInitException e) {
			e.printStackTrace();

			return null;
		}
	}
	
	/**
	 * Close editor if it is active.
	 */
	public static void closeEditor(final IEditorPart editor) {
		if (editor.getSite().getWorkbenchWindow().getActivePage() != null) {
			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
				public void run() {
					// close editor
					editor.getSite().getWorkbenchWindow().getActivePage()
							.closeEditor(editor, false);
				}
			});
		}
	}
	
	/**
	 * Return the content of the given IEditorPart as String.
	 * @param editorPart
	 * @return Content of editorPart.
	 */
	public static String getContent(IEditorPart editorPart) {
		AbstractTextEditor castEditor = (AbstractTextEditor) editorPart;
		IDocumentProvider provider = castEditor.getDocumentProvider();
		IDocument document = provider.getDocument(castEditor.getEditorInput());
		return document.get();
	}
}

Back to the top