Search in sources :

Example 41 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project linuxtools by eclipse.

the class SpecfileElementHyperlink method open.

@Override
public void open() {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
    HashMap<String, Object> map = new HashMap<>();
    // TODO don't increment the line number once the SpecfileSource reports
    // correct line
    map.put(IMarker.LINE_NUMBER, Integer.valueOf(getSource().getLineNumber() + 1));
    map.put(IDE.EDITOR_ID_ATTR, desc.getId());
    try {
        IMarker marker = file.createMarker(IMarker.TEXT);
        marker.setAttributes(map);
        IDE.openEditor(page, marker);
        marker.delete();
    } catch (CoreException e) {
        SpecfileLog.logError(e);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) HashMap(java.util.HashMap) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IMarker(org.eclipse.core.resources.IMarker)

Example 42 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project yamcs-studio by yamcs.

the class OpenWithMenu method createOtherMenuItem.

/**
 * Creates the Other... menu item
 *
 * @param menu
 *            the menu to add the item to
 */
private void createOtherMenuItem(final Menu menu) {
    IFile fileResource = getFileResource();
    if (fileResource == null) {
        return;
    }
    new MenuItem(menu, SWT.SEPARATOR);
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText("Other...");
    menuItem.addListener(SWT.Selection, evt -> {
        EditorSelectionDialog dialog = new EditorSelectionDialog(menu.getShell());
        dialog.setMessage("Choose the editor for opening " + fileResource.getName());
        if (dialog.open() == Window.OK) {
            IEditorDescriptor editor = dialog.getSelectedEditor();
            if (editor != null) {
                openEditor(editor, editor.isOpenExternal());
            }
        }
    });
}
Also used : IFile(org.eclipse.core.resources.IFile) EditorSelectionDialog(org.eclipse.ui.dialogs.EditorSelectionDialog) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) MenuItem(org.eclipse.swt.widgets.MenuItem)

Example 43 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project yamcs-studio by yamcs.

the class OpenWithMenu method fill.

/* (non-Javadoc)
     * Fills the menu with perspective items.
     */
@Override
public void fill(Menu menu, int index) {
    IFile file = getFileResource();
    if (file == null) {
        return;
    }
    // may be null
    IEditorDescriptor defaultEditor = registry.findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID);
    // may be null
    IEditorDescriptor preferredEditor = IDE.getDefaultEditor(file);
    IEditorDescriptor[] editors = registry.getEditors(file.getName(), IDE.getContentType(file));
    Collections.sort(Arrays.asList(editors), comparer);
    boolean defaultFound = false;
    // Check that we don't add it twice. This is possible
    // if the same editor goes to two mappings.
    ArrayList<IEditorDescriptor> alreadyMapped = new ArrayList<>();
    for (int i = 0; i < editors.length; i++) {
        IEditorDescriptor editor = editors[i];
        if (!alreadyMapped.contains(editor)) {
            createMenuItem(menu, editor, preferredEditor);
            if (defaultEditor != null && editor.getId().equals(defaultEditor.getId())) {
                defaultFound = true;
            }
            alreadyMapped.add(editor);
        }
    }
    // Only add a separator if there is something to separate
    if (editors.length > 0) {
        new MenuItem(menu, SWT.SEPARATOR);
    }
    // Add default editor. Check it if it is saved as the preference.
    if (!defaultFound && defaultEditor != null) {
        createMenuItem(menu, defaultEditor, preferredEditor);
    }
    // add Other... menu item
    createOtherMenuItem(menu);
}
Also used : IFile(org.eclipse.core.resources.IFile) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) ArrayList(java.util.ArrayList) MenuItem(org.eclipse.swt.widgets.MenuItem)

Example 44 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class JumpToRTSHandler method jump.

private void jump() {
    if (className != null) {
        IType cls = null;
        try {
            cls = project.findType(className);
        } catch (JavaModelException e) {
            return;
        }
        try {
            IFile file = project.getProject().getFile(getTestFileName(cls));
            IWorkbench workbench = PlatformUI.getWorkbench();
            IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
            FileEditorInput input = new FileEditorInput(file);
            IDocumentProvider provider = new TextFileDocumentProvider();
            provider.connect(input);
            IDocument document = provider.getDocument(input);
            TestFile testFile = new TestFile(input, provider);
            testFile.parse();
            IEditorDescriptor defaultEditor = workbench.getEditorRegistry().getDefaultEditor(file.getName());
            String editorId = defaultEditor.getId();
            if (!className.equals(testName)) {
                for (Test test : testFile.getTests()) {
                    if (test.toString().equals(testName)) {
                        int line = document.getLineOfOffset(test.getPosition().getOffset());
                        IMarker lineMarker = file.createMarker(IMarker.TEXT);
                        lineMarker.setAttribute(IMarker.LINE_NUMBER, line + 1);
                        lineMarker.setAttribute(IDE.EDITOR_ID_ATTR, editorId);
                        IDE.openEditor(page, lineMarker);
                        lineMarker.delete();
                        return;
                    }
                }
            } else {
                IDE.openEditor(page, file);
            }
        } catch (CoreException | BadLocationException | NullPointerException e) {
            e.printStackTrace();
            MessageDialog.openError(shell, "Jump to RTS", "Failed to find associated RTS file.");
        }
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) TextFileDocumentProvider(org.eclipse.ui.editors.text.TextFileDocumentProvider) IType(org.eclipse.jdt.core.IType) IWorkbench(org.eclipse.ui.IWorkbench) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) Test(name.graf.emanuel.testfileeditor.model.node.Test) FileEditorInput(org.eclipse.ui.part.FileEditorInput) TestFile(name.graf.emanuel.testfileeditor.model.TestFile) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IMarker(org.eclipse.core.resources.IMarker) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 45 with IEditorDescriptor

use of org.eclipse.ui.IEditorDescriptor in project webtools.sourceediting by eclipse.

the class ReferencedFileErrorUtility method openEditorAndGotoError.

public static void openEditorAndGotoError(String uristring, final int line, final int column) {
    if (uristring != null) {
        try {
            URL uri = new URL(uristring);
            if (uri != null) {
                if (// $NON-NLS-1$
                "file".equals(uri.getProtocol())) {
                    String pathString = uri.getPath();
                    IPath path = new Path(pathString);
                    String device = path.getDevice();
                    if (// $NON-NLS-1$
                    (device != null) && device.startsWith("/")) {
                        path = path.setDevice(device.substring(1));
                    }
                    final IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
                    if ((iFile != null) && iFile.exists()) {
                        // Open the editor for this file.
                        final IWorkbench workbench = PlatformUI.getWorkbench();
                        final IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
                        Display.getDefault().asyncExec(new Runnable() {

                            public void run() {
                                try {
                                    IContentType contentType = iFile.getContentDescription().getContentType();
                                    IEditorRegistry editorRegistry = workbench.getEditorRegistry();
                                    String fileName = iFile.getName();
                                    IEditorDescriptor descriptor = editorRegistry.getDefaultEditor(fileName, contentType);
                                    String editorId;
                                    if (descriptor != null) {
                                        editorId = descriptor.getId();
                                    } else {
                                        // $NON-NLS-1$
                                        descriptor = editorRegistry.getDefaultEditor(fileName + ".txt");
                                        editorId = descriptor.getId();
                                    }
                                    if (editorId != null) {
                                        FileEditorInput editorInput = new FileEditorInput(iFile);
                                        IWorkbenchPage activePage = workbenchWindow.getActivePage();
                                        activePage.openEditor(editorInput, editorId);
                                    }
                                } catch (Exception ex) {
                                    // $NON-NLS-1$ //$NON-NLS-2$
                                    LoggerFactory.getLoggerInstance().logError("Exception encountered when attempting to open file: " + iFile + "\n\n", ex);
                                }
                            }
                        });
                        Runnable runnable = new Runnable() {

                            public void run() {
                                IEditorPart editorPart = workbenchWindow.getActivePage().getActiveEditor();
                                gotoError(editorPart, line, column);
                            }
                        };
                        Display.getCurrent().asyncExec(runnable);
                    }
                }
            }
        } catch (Exception e) {
        // Do nothing.
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IContentType(org.eclipse.core.runtime.content.IContentType) IEditorPart(org.eclipse.ui.IEditorPart) URL(java.net.URL) BadLocationException(org.eclipse.jface.text.BadLocationException) IWorkbench(org.eclipse.ui.IWorkbench) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorRegistry(org.eclipse.ui.IEditorRegistry)

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