Search in sources :

Example 31 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project xtext-xtend by eclipse.

the class OpenEditorAction method run.

@Override
public void run() {
    if (inputFile == null) {
        return;
    }
    IWorkbenchPartSite workbenchPartSite = derivedSourceView.getSite();
    IWorkbenchPage workbenchPage = workbenchPartSite.getPage();
    try {
        IEditorPart editorPart = workbenchPage.openEditor(new FileEditorInput(inputFile), COMPILATION_UNIT_EDITOR_ID, true, IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT);
        if (selectedRegion != null) {
            ((ITextEditor) editorPart).selectAndReveal(selectedRegion.getOffset(), selectedRegion.getLength());
        }
    } catch (PartInitException partInitException) {
        throw new WrappedRuntimeException(partInitException);
    }
}
Also used : IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) WrappedRuntimeException(org.eclipse.core.internal.utils.WrappedRuntimeException)

Example 32 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project xtext-xtend by eclipse.

the class AbstractNewXtendElementWizard method performFinish.

@Override
public boolean performFinish() {
    final int size = this.page.createType();
    final IResource resource = page.getResource();
    if (resource != null) {
        selectAndReveal(resource);
        final Display display = getShell().getDisplay();
        display.asyncExec(new Runnable() {

            @Override
            public void run() {
                IEditorPart editor;
                try {
                    editor = IDE.openEditor(JavaPlugin.getActivePage(), (IFile) resource);
                    if (editor instanceof ITextEditor) {
                        ((ITextEditor) editor).selectAndReveal(size - 2, 0);
                    }
                } catch (PartInitException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return true;
    } else {
        return false;
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IResource(org.eclipse.core.resources.IResource) Display(org.eclipse.swt.widgets.Display)

Example 33 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.

the class STLink2SourceSupport method openFileImpl.

private static boolean openFileImpl(IProject project, IPath sourceLoc, int lineNumber) {
    if (sourceLoc == null || "??".equals(sourceLoc.toString())) {
        // $NON-NLS-1$
        return false;
    }
    try {
        IEditorInput editorInput = getEditorInput(sourceLoc, project);
        IWorkbenchPage p = CUIPlugin.getActivePage();
        if (p != null) {
            if (editorInput == null) {
                p.openEditor(new STCSourceNotFoundEditorInput(project, sourceLoc, lineNumber), STCSourceNotFoundEditor.ID, true);
            } else {
                IEditorPart editor = p.openEditor(editorInput, CUIPlugin.EDITOR_ID, true);
                if (lineNumber > 0 && editor instanceof ITextEditor) {
                    IDocumentProvider provider = ((ITextEditor) editor).getDocumentProvider();
                    IDocument document = provider.getDocument(editor.getEditorInput());
                    try {
                        int start = document.getLineOffset(lineNumber - 1);
                        ((ITextEditor) editor).selectAndReveal(start, 0);
                        IWorkbenchPage page = editor.getSite().getPage();
                        page.activate(editor);
                        return true;
                    } catch (BadLocationException x) {
                    // ignore
                    }
                }
            }
        }
    } catch (PartInitException e) {
    }
    return false;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 34 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.

the class ProfileUIUtils method openEditorAndSelect.

/**
 * Open a file in the Editor at the specified offset, highlighting the given length
 *
 * @param path : Absolute path pointing to the file which will be opened.
 * @param offset : Offset of the function to be highlighted.
 * @param length : Length of the function to be highlighted.
 * @throws PartInitException if the editor could not be initialized
 */
public static void openEditorAndSelect(String path, int offset, int length) throws PartInitException {
    Path p = new Path(path);
    if (p.toFile().exists()) {
        IWorkbenchPage activePage = ProfileUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IFileStore fileStore = EFS.getLocalFileSystem().getStore(p);
        IEditorPart editor = IDE.openEditorOnFileStore(activePage, fileStore);
        if (editor instanceof ITextEditor) {
            ITextEditor text = (ITextEditor) editor;
            text.selectAndReveal(offset, length);
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IFileStore(org.eclipse.core.filesystem.IFileStore) IEditorPart(org.eclipse.ui.IEditorPart)

Example 35 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.

the class ProfileUIUtils method openEditorAndSelect.

/**
 * Opens the specified file in an editor (or selects an already open
 * editor) and highlights the specified line.
 * @param result - result of performing source lookup with a org.eclipse.debug.core.model.ISourceLocator
 * @param line - line number to select, 0 to not select a line
 * @throws PartInitException - Failed to open editor
 * @throws BadLocationException - Line number not valid in file
 * @see DebugUITools#lookupSource(Object, org.eclipse.debug.core.model.ISourceLocator)
 */
public static void openEditorAndSelect(ISourceLookupResult result, int line) throws PartInitException, BadLocationException {
    IEditorInput input = result.getEditorInput();
    String editorID = result.getEditorId();
    if (input == null || editorID == null) {
        // Consult the CDT DebugModelPresentation
        Object sourceElement = result.getSourceElement();
        if (sourceElement != null) {
            // Resolve IResource in case we get a LocalFileStorage object
            if (sourceElement instanceof LocalFileStorage) {
                IPath filePath = ((LocalFileStorage) sourceElement).getFullPath();
                URI fileURI = URIUtil.toURI(filePath);
                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                IFile[] files = root.findFilesForLocationURI(fileURI);
                if (files.length > 0) {
                    // Take the first match
                    sourceElement = files[0];
                }
            }
            IDebugModelPresentation pres = DebugUITools.newDebugModelPresentation(CDebugCorePlugin.getUniqueIdentifier());
            input = pres.getEditorInput(sourceElement);
            editorID = pres.getEditorId(input, sourceElement);
            pres.dispose();
        }
    }
    if (input != null && editorID != null) {
        // Open the editor
        IWorkbenchPage activePage = ProfileUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorPart editor = IDE.openEditor(activePage, input, editorID);
        // Select the line
        if (editor instanceof ITextEditor) {
            ITextEditor textEditor = (ITextEditor) editor;
            if (line > 0) {
                IDocumentProvider provider = textEditor.getDocumentProvider();
                IDocument document = provider.getDocument(textEditor.getEditorInput());
                // zero-indexed
                IRegion lineRegion = document.getLineInformation(line - 1);
                textEditor.selectAndReveal(lineRegion.getOffset(), lineRegion.getLength());
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IPath(org.eclipse.core.runtime.IPath) LocalFileStorage(org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage) IDebugModelPresentation(org.eclipse.debug.ui.IDebugModelPresentation) IEditorPart(org.eclipse.ui.IEditorPart) URI(java.net.URI) IRegion(org.eclipse.jface.text.IRegion) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ITextEditor (org.eclipse.ui.texteditor.ITextEditor)233 IEditorPart (org.eclipse.ui.IEditorPart)91 IDocument (org.eclipse.jface.text.IDocument)73 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)39 ITextSelection (org.eclipse.jface.text.ITextSelection)34 IFile (org.eclipse.core.resources.IFile)33 Test (org.junit.Test)31 BadLocationException (org.eclipse.jface.text.BadLocationException)27 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)25 PartInitException (org.eclipse.ui.PartInitException)23 ISelection (org.eclipse.jface.viewers.ISelection)19 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)17 IRegion (org.eclipse.jface.text.IRegion)16 Annotation (org.eclipse.jface.text.source.Annotation)16 ArrayList (java.util.ArrayList)15 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)15 IResource (org.eclipse.core.resources.IResource)14 IEditorInput (org.eclipse.ui.IEditorInput)14 CoreException (org.eclipse.core.runtime.CoreException)13 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)12