1
/*******************************************************************************
2
* Copyright (c) 2010 Red Hat Inc. 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
package org.eclipse.linuxtools.changelog.tests.helpers;
10
import org.eclipse.core.resources.IFile;
11
import org.eclipse.jface.text.IDocument;
12
import org.eclipse.ui.IEditorPart;
13
import org.eclipse.ui.IWorkbench;
14
import org.eclipse.ui.PartInitException;
15
import org.eclipse.ui.PlatformUI;
16
import org.eclipse.ui.texteditor.AbstractTextEditor;
17
import org.eclipse.ui.texteditor.IDocumentProvider;
19
public class EditorHelper {
22
* Open file associated with <code>diskresource</code> in current
25
* @param diskresource The file to be opened in the current workspace.
26
* @return The IEditorPart associated with the opened file in the current workspace
27
* or null if opening fails.
29
public static IEditorPart openEditor(IFile diskresource) {
30
IWorkbench ws = PlatformUI.getWorkbench();
32
return org.eclipse.ui.ide.IDE.openEditor(ws
33
.getActiveWorkbenchWindow().getActivePage(), diskresource,
35
} catch (PartInitException e) {
43
* Close editor if it is active.
45
public static void closeEditor(final IEditorPart editor) {
46
if (editor.getSite().getWorkbenchWindow().getActivePage() != null) {
47
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
50
editor.getSite().getWorkbenchWindow().getActivePage()
51
.closeEditor(editor, false);
58
* Return the content of the given IEditorPart as String.
60
* @return Content of editorPart.
62
public static String getContent(IEditorPart editorPart) {
63
AbstractTextEditor castEditor = (AbstractTextEditor) editorPart;
64
IDocumentProvider provider = castEditor.getDocumentProvider();
65
IDocument document = provider.getDocument(castEditor.getEditorInput());
66
return document.get();