Search in sources :

Example 16 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project titan.EclipsePlug-ins by eclipse.

the class OpenSourceAction method openEditor.

/**
 * Opens the editor for the given source file
 *
 * This method is intended to use only by {@link org.eclipse.titan.log.viewer.actions.OpenSourceViewMenuAction}
 *
 * @param targetFile The source file
 * @param lineNumber The line number to select
 * @param logView The view which initiated this action (used only to report errors on the status line)
 * @param forceEditorOpening
 */
public static void openEditor(final IFile targetFile, final int lineNumber, final IViewPart logView, final boolean forceEditorOpening) {
    // search for the editor to open the file
    IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(targetFile.getName());
    if (desc == null) {
        desc = PlatformUI.getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
        if (desc == null) {
            logView.getViewSite().getActionBars().getStatusLineManager().setErrorMessage("The editor could not be found");
            return;
        }
    }
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null) {
        return;
    }
    IWorkbenchPage activePage = window.getActivePage();
    if (activePage == null) {
        return;
    }
    try {
        FileEditorInput editorInput = new FileEditorInput(targetFile);
        IEditorPart editorPart = activePage.findEditor(editorInput);
        if (editorPart == null) {
            if (!forceEditorOpening) {
                return;
            }
            editorPart = activePage.openEditor(editorInput, desc.getId());
        }
        if (!(editorPart instanceof AbstractTextEditor)) {
            logView.getViewSite().getActionBars().getStatusLineManager().setErrorMessage("Could not jump to the target position");
            return;
        }
        AbstractTextEditor textEditor = (AbstractTextEditor) editorPart;
        IDocument targetDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        if (targetDocument == null) {
            logView.getViewSite().getActionBars().getStatusLineManager().setErrorMessage("The target document does not exist");
            return;
        }
        if (lineNumber >= targetDocument.getNumberOfLines()) {
            logView.getViewSite().getActionBars().getStatusLineManager().setErrorMessage("The position is after the last line in the source code.");
            return;
        }
        // Line numbers are indexed from zero in the editors
        IRegion lineRegion = targetDocument.getLineInformation(lineNumber - 1);
        activePage.bringToTop(textEditor);
        textEditor.selectAndReveal(lineRegion.getOffset(), lineRegion.getLength());
    } catch (PartInitException e) {
        ErrorReporter.logExceptionStackTrace(e);
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 17 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project titan.EclipsePlug-ins by eclipse.

the class LocationHighlighter method jumpToLocation.

/**
 * Jump to the provided location and highlight it.
 */
public static void jumpToLocation(final Location location) {
    final IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(location.getFile().getName());
    if (desc == null) {
        return;
    }
    try {
        final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        final IEditorPart editorPart = page.openEditor(new FileEditorInput((IFile) location.getFile()), desc.getId());
        if (editorPart != null && editorPart instanceof AbstractTextEditor) {
            ((AbstractTextEditor) editorPart).setHighlightRange(location.getOffset(), 0, true);
        }
    } catch (final PartInitException e) {
        ErrorReporter.logExceptionStackTrace("Error while opening the editor", e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException)

Example 18 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project titan.EclipsePlug-ins by eclipse.

the class NewASN1ModuleWizard method selectAndRevealNewModule.

/**
 * Opens the new module in an editor, plus selects and reveals it in
 * every open windows.
 *
 * @param newModule
 *                the module to be revealed
 */
private void selectAndRevealNewModule(final IFile newModule) {
    final IWorkbench workbench = getWorkbench();
    final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
    if (window != null) {
        final IEditorDescriptor desc = ASN1Editor.findASN1Editor(workbench);
        final IWorkbenchPage page = window.getActivePage();
        try {
            page.openEditor(new FileEditorInput(newModule), desc.getId());
        } catch (PartInitException e) {
            ErrorReporter.logExceptionStackTrace(e);
        }
    }
    // select and reveal the new file in every window open
    selectAndReveal(newModule);
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException)

Example 19 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project titan.EclipsePlug-ins by eclipse.

the class NewTTCN3ModuleWizard method selectAndRevealNewModule.

/**
 * Opens the new module in an editor, plus selects and reveals it in
 * every open windows.
 *
 * @param newModule
 *                the module to be revealed
 */
private void selectAndRevealNewModule(final IFile newModule) {
    final IWorkbench workbench = getWorkbench();
    final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
    if (window != null) {
        final IEditorDescriptor desc = TTCN3Editor.findTTCN3Editor(workbench);
        final IWorkbenchPage page = window.getActivePage();
        try {
            page.openEditor(new FileEditorInput(newModule), desc.getId());
        } catch (PartInitException e) {
            ErrorReporter.logExceptionStackTrace(e);
        }
    }
    // select and reveal the new file in every window open
    selectAndReveal(newModule);
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException)

Example 20 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project titan.EclipsePlug-ins by eclipse.

the class OpenDeclaration method selectAndRevealDeclaration.

/**
 * Opens an editor for the provided declaration, and in this editor the
 * location of the declaration is revealed and selected.
 *
 * @param declaration
 *                the declaration to reveal
 */
private void selectAndRevealDeclaration(final Location location) {
    IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(location.getFile().getName());
    if (desc == null) {
        targetEditor.getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(TTCN3EDITORNOTFOUND);
        return;
    }
    try {
        IWorkbenchPage page = targetEditor.getSite().getPage();
        IEditorPart editorPart = page.openEditor(new FileEditorInput((IFile) location.getFile()), desc.getId());
        if (editorPart != null && (editorPart instanceof AbstractTextEditor)) {
            ((AbstractTextEditor) editorPart).selectAndReveal(location.getOffset(), location.getEndOffset() - location.getOffset());
        }
    } catch (PartInitException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) AbstractTextEditor(org.eclipse.ui.texteditor.AbstractTextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

IEditorDescriptor (org.eclipse.ui.IEditorDescriptor)74 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)34 PartInitException (org.eclipse.ui.PartInitException)34 FileEditorInput (org.eclipse.ui.part.FileEditorInput)27 IWorkbench (org.eclipse.ui.IWorkbench)25 IEditorPart (org.eclipse.ui.IEditorPart)21 IFile (org.eclipse.core.resources.IFile)19 IEditorRegistry (org.eclipse.ui.IEditorRegistry)18 CoreException (org.eclipse.core.runtime.CoreException)10 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)10 IEditorInput (org.eclipse.ui.IEditorInput)8 IContentType (org.eclipse.core.runtime.content.IContentType)7 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)7 AbstractTextEditor (org.eclipse.ui.texteditor.AbstractTextEditor)7 Image (org.eclipse.swt.graphics.Image)5 InputStream (java.io.InputStream)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 Path (org.eclipse.core.runtime.Path)3 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)3