Search in sources :

Example 1 with IGotoMarker

use of org.eclipse.ui.ide.IGotoMarker in project eclipse.platform.text by eclipse.

the class SelectMarkerRulerAction method gotoMarker.

private void gotoMarker(IMarker marker) {
    // Use the provided adapter if any
    IGotoMarker gotoMarkerAdapter = fTextEditor.getAdapter(IGotoMarker.class);
    if (gotoMarkerAdapter != null) {
        gotoMarkerAdapter.gotoMarker(marker);
        return;
    }
    int start = MarkerUtilities.getCharStart(marker);
    int end = MarkerUtilities.getCharEnd(marker);
    boolean selectLine = start < 0 || end < 0;
    IDocumentProvider documentProvider = fTextEditor.getDocumentProvider();
    IEditorInput editorInput = fTextEditor.getEditorInput();
    // look up the current range of the marker when the document has been edited
    IAnnotationModel model = documentProvider.getAnnotationModel(editorInput);
    if (model instanceof AbstractMarkerAnnotationModel) {
        AbstractMarkerAnnotationModel markerModel = (AbstractMarkerAnnotationModel) model;
        Position pos = markerModel.getMarkerPosition(marker);
        if (pos != null && !pos.isDeleted()) {
            // use position instead of marker values
            start = pos.getOffset();
            end = pos.getOffset() + pos.getLength();
        }
        if (pos != null && pos.isDeleted()) {
            // do nothing if position has been deleted
            return;
        }
    }
    IDocument document = documentProvider.getDocument(editorInput);
    if (selectLine) {
        int line;
        try {
            if (start >= 0)
                line = document.getLineOfOffset(start);
            else {
                line = MarkerUtilities.getLineNumber(marker);
                // Marker line numbers are 1-based
                --line;
            }
            end = start + document.getLineLength(line) - 1;
        } catch (BadLocationException e) {
            return;
        }
    }
    int length = document.getLength();
    if (end - 1 < length && start < length)
        fTextEditor.selectAndReveal(start, end - start);
}
Also used : Position(org.eclipse.jface.text.Position) IGotoMarker(org.eclipse.ui.ide.IGotoMarker) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with IGotoMarker

use of org.eclipse.ui.ide.IGotoMarker in project eclipse.platform.text by eclipse.

the class AbstractDecoratedTextEditor method revealInEditor.

/**
 * Selects and reveals the given offset and length in the given editor part.
 *
 * @param editor the editor part
 * @param offset the offset
 * @param length the length
 * @since 3.7
 */
private static void revealInEditor(IEditorPart editor, final int offset, final int length) {
    if (editor instanceof ITextEditor) {
        ((ITextEditor) editor).selectAndReveal(offset, length);
        return;
    }
    // Support for non-text editor - try IGotoMarker interface
    final IGotoMarker gotoMarkerTarget;
    if (editor instanceof IGotoMarker)
        gotoMarkerTarget = (IGotoMarker) editor;
    else
        gotoMarkerTarget = editor != null ? editor.getAdapter(IGotoMarker.class) : null;
    if (gotoMarkerTarget != null) {
        final IEditorInput input = editor.getEditorInput();
        if (input instanceof IFileEditorInput) {
            WorkspaceModifyOperation op = new WorkspaceModifyOperation() {

                @Override
                protected void execute(IProgressMonitor monitor) throws CoreException {
                    IMarker marker = null;
                    try {
                        marker = ((IFileEditorInput) input).getFile().createMarker(IMarker.TEXT);
                        marker.setAttribute(IMarker.CHAR_START, offset);
                        marker.setAttribute(IMarker.CHAR_END, offset + length);
                        gotoMarkerTarget.gotoMarker(marker);
                    } finally {
                        if (marker != null)
                            marker.delete();
                    }
                }
            };
            try {
                op.run(null);
            } catch (InvocationTargetException ex) {
            // reveal failed
            } catch (InterruptedException e) {
                // $NON-NLS-1$
                Assert.isTrue(false, "this operation can not be canceled");
            }
        }
        return;
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFileEditorInput(org.eclipse.ui.IFileEditorInput) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) IGotoMarker(org.eclipse.ui.ide.IGotoMarker) IMarker(org.eclipse.core.resources.IMarker) IEditorInput(org.eclipse.ui.IEditorInput) IURIEditorInput(org.eclipse.ui.IURIEditorInput) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with IGotoMarker

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

the class OPIEditor method getAdapter.

@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class type) {
    if (type == IPropertySheetPage.class)
        return getPropertySheetPage();
    else if (type == ZoomManager.class)
        return ((ScalableFreeformRootEditPart) getGraphicalViewer().getRootEditPart()).getZoomManager();
    else // return getOverviewOutlinePage();
    if (type == IContentOutlinePage.class) {
        outlinePage = new OutlinePage(new TreeViewer());
        return outlinePage;
    } else if (type.equals(IContextProvider.class)) {
        if (helpContextProvider == null)
            helpContextProvider = new OPIHelpContextProvider(getGraphicalViewer());
        return helpContextProvider;
    } else if (type.equals(IGotoMarker.class)) {
        return new IGotoMarker() {

            @Override
            public void gotoMarker(IMarker marker) {
                try {
                    String wuid = (String) marker.getAttribute(AbstractWidgetModel.PROP_WIDGET_UID);
                    if (wuid == null) {
                        // if wuid is not stored in the marker try to find it based on character
                        Integer charStart = (Integer) marker.getAttribute(IMarker.CHAR_START);
                        if (charStart == null) {
                            return;
                        }
                        // Get the closest widget to charStart position
                        wuid = XMLUtil.findClosestWidgetUid(getInputStream(), charStart);
                        if (wuid == null) {
                            return;
                        }
                    }
                    AbstractWidgetModel widget = getDisplayModel().getWidgetFromWUID(wuid);
                    if (widget == null) {
                        return;
                    }
                    // Get the widget editPart
                    Object obj = getGraphicalViewer().getEditPartRegistry().get(widget);
                    if (obj != null && obj instanceof AbstractBaseEditPart) {
                        EditPart widgetEditPart = (AbstractBaseEditPart) obj;
                        // Reveal the widget
                        getGraphicalViewer().reveal(widgetEditPart);
                        // Find the closest selectable part
                        while (widgetEditPart != null && !widgetEditPart.isSelectable()) {
                            widgetEditPart = widgetEditPart.getParent();
                        }
                        if (widgetEditPart != null) {
                            // Select the widget in OPI
                            SelectionManager selectionManager = getGraphicalViewer().getSelectionManager();
                            selectionManager.deselectAll();
                            selectionManager.appendSelection(widgetEditPart);
                        }
                    }
                } catch (IOException e) {
                    MessageDialog.openError(getSite().getShell(), "IO Error", e.getMessage());
                    OPIBuilderPlugin.getLogger().log(Level.WARNING, "File open error", // $NON-NLS-1$
                    e);
                } catch (CoreException e) {
                    MessageDialog.openError(getSite().getShell(), "Core Error", e.getMessage());
                    OPIBuilderPlugin.getLogger().log(Level.WARNING, "File open error", // $NON-NLS-1$
                    e);
                }
            }
        };
    }
    return super.getAdapter(type);
}
Also used : SelectionManager(org.eclipse.gef.SelectionManager) AbstractBaseEditPart(org.csstudio.opibuilder.editparts.AbstractBaseEditPart) TreeViewer(org.eclipse.gef.ui.parts.TreeViewer) RootEditPart(org.eclipse.gef.RootEditPart) EditPart(org.eclipse.gef.EditPart) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) AbstractBaseEditPart(org.csstudio.opibuilder.editparts.AbstractBaseEditPart) IGotoMarker(org.eclipse.ui.ide.IGotoMarker) IOException(java.io.IOException) IContentOutlinePage(org.eclipse.ui.views.contentoutline.IContentOutlinePage) ContentOutlinePage(org.eclipse.gef.ui.parts.ContentOutlinePage) AbstractWidgetModel(org.csstudio.opibuilder.model.AbstractWidgetModel) IContextProvider(org.eclipse.help.IContextProvider) CoreException(org.eclipse.core.runtime.CoreException) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) ZoomManager(org.eclipse.gef.editparts.ZoomManager) EventObject(java.util.EventObject) IMarker(org.eclipse.core.resources.IMarker)

Aggregations

IGotoMarker (org.eclipse.ui.ide.IGotoMarker)3 IMarker (org.eclipse.core.resources.IMarker)2 IEditorInput (org.eclipse.ui.IEditorInput)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 EventObject (java.util.EventObject)1 AbstractBaseEditPart (org.csstudio.opibuilder.editparts.AbstractBaseEditPart)1 AbstractWidgetModel (org.csstudio.opibuilder.model.AbstractWidgetModel)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 EditPart (org.eclipse.gef.EditPart)1 RootEditPart (org.eclipse.gef.RootEditPart)1 SelectionManager (org.eclipse.gef.SelectionManager)1 ScalableFreeformRootEditPart (org.eclipse.gef.editparts.ScalableFreeformRootEditPart)1 ZoomManager (org.eclipse.gef.editparts.ZoomManager)1 ContentOutlinePage (org.eclipse.gef.ui.parts.ContentOutlinePage)1 TreeViewer (org.eclipse.gef.ui.parts.TreeViewer)1 IContextProvider (org.eclipse.help.IContextProvider)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1