~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/helpers/EditorHelper.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
9
 
 
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;
 
18
 
 
19
public class EditorHelper {
 
20
 
 
21
        /**
 
22
         * Open file associated with <code>diskresource</code> in current
 
23
         * workspace.
 
24
         * 
 
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.
 
28
         */
 
29
        public static IEditorPart openEditor(IFile diskresource) {
 
30
                IWorkbench ws = PlatformUI.getWorkbench();
 
31
                try {
 
32
                        return org.eclipse.ui.ide.IDE.openEditor(ws
 
33
                                        .getActiveWorkbenchWindow().getActivePage(), diskresource,
 
34
                                        true);
 
35
                } catch (PartInitException e) {
 
36
                        e.printStackTrace();
 
37
 
 
38
                        return null;
 
39
                }
 
40
        }
 
41
        
 
42
        /**
 
43
         * Close editor if it is active.
 
44
         */
 
45
        public static void closeEditor(final IEditorPart editor) {
 
46
                if (editor.getSite().getWorkbenchWindow().getActivePage() != null) {
 
47
                        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
 
48
                                public void run() {
 
49
                                        // close editor
 
50
                                        editor.getSite().getWorkbenchWindow().getActivePage()
 
51
                                                        .closeEditor(editor, false);
 
52
                                }
 
53
                        });
 
54
                }
 
55
        }
 
56
        
 
57
        /**
 
58
         * Return the content of the given IEditorPart as String.
 
59
         * @param editorPart
 
60
         * @return Content of editorPart.
 
61
         */
 
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();
 
67
        }
 
68
}