Search in sources :

Example 66 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.

the class GoToMatchingTagAction method updateFor.

void updateFor(ISelection selection) {
    ITextEditor textEditor = getTextEditor();
    if (textEditor == null) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("no editor");
        }
        return;
    }
    IDocumentProvider documentProvider = textEditor.getDocumentProvider();
    if (documentProvider == null) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("no document provider");
        }
        return;
    }
    IAnnotationModel annotationModel = documentProvider.getAnnotationModel(textEditor.getEditorInput());
    if (annotationModel == null || !(annotationModel instanceof IAnnotationModelExtension)) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("no annotation model");
        }
        return;
    }
    List oldAnnotations = new ArrayList(2);
    Iterator annotationIterator = annotationModel.getAnnotationIterator();
    while (annotationIterator.hasNext()) {
        Annotation annotation = (Annotation) annotationIterator.next();
        if (ANNOTATION_TYPE.equals(annotation.getType())) {
            annotation.markDeleted(true);
            if (DEBUG) {
                // $NON-NLS-1$
                System.out.println("removing " + annotation);
            }
            oldAnnotations.add(annotation);
        }
    }
    Map newAnnotations = new HashMap();
    if (!selection.isEmpty() && selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
        Object o = ((IStructuredSelection) selection).getFirstElement();
        if (o instanceof IDOMNode) {
            int offset = ((ITextSelection) selection).getOffset();
            IStructuredDocumentRegion matchRegion = null;
            if (((Node) o).getNodeType() == Node.ATTRIBUTE_NODE) {
                o = ((Attr) o).getOwnerElement();
            }
            Position pStart = null;
            Position pEnd = null;
            // $NON-NLS-1$
            String tag = "";
            if (o instanceof IDOMNode) {
                IDOMNode node = (IDOMNode) o;
                IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
                if (startStructuredDocumentRegion != null && startStructuredDocumentRegion.containsOffset(offset)) {
                    if (startStructuredDocumentRegion.getNumberOfRegions() > 1) {
                        ITextRegion nameRegion = startStructuredDocumentRegion.getRegions().get(1);
                        pStart = new Position(startStructuredDocumentRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                        tag = startStructuredDocumentRegion.getText(nameRegion);
                    }
                    matchRegion = ((IDOMNode) o).getEndStructuredDocumentRegion();
                    if (matchRegion != null && matchRegion.getNumberOfRegions() > 1) {
                        ITextRegion nameRegion = matchRegion.getRegions().get(1);
                        pEnd = new Position(matchRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                    }
                } else {
                    IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
                    if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.containsOffset(offset)) {
                        if (endStructuredDocumentRegion.getNumberOfRegions() > 1) {
                            ITextRegion nameRegion = endStructuredDocumentRegion.getRegions().get(1);
                            pEnd = new Position(endStructuredDocumentRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                            tag = endStructuredDocumentRegion.getText(nameRegion);
                        }
                        matchRegion = ((IDOMNode) o).getStartStructuredDocumentRegion();
                        if (matchRegion != null && matchRegion.getNumberOfRegions() > 1) {
                            ITextRegion nameRegion = matchRegion.getRegions().get(1);
                            pStart = new Position(matchRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                        }
                    }
                }
            }
            if (pStart != null && pEnd != null) {
                Annotation annotation = new Annotation(ANNOTATION_TYPE, false, NLS.bind(XMLUIMessages.gotoMatchingTag_start, tag));
                newAnnotations.put(annotation, pStart);
                if (DEBUG) {
                    // $NON-NLS-1$
                    System.out.println("adding " + annotation);
                }
                annotation = new Annotation(ANNOTATION_TYPE, false, NLS.bind(XMLUIMessages.gotoMatchingTag_end, tag));
                newAnnotations.put(annotation, pEnd);
                if (DEBUG) {
                    // $NON-NLS-1$
                    System.out.println("adding " + annotation);
                }
            }
        }
    }
    ((IAnnotationModelExtension) annotationModel).replaceAnnotations((Annotation[]) oldAnnotations.toArray(new Annotation[oldAnnotations.size()]), newAnnotations);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Annotation(org.eclipse.jface.text.source.Annotation) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 67 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.

the class CleanupActionXMLDelegate method run.

public void run(IAction action) {
    if (fEditor instanceof ITextEditor) {
        final ITextEditor editor = (ITextEditor) fEditor;
        Dialog cleanupDialog = new CleanupDialogXML(editor.getSite().getShell());
        if (cleanupDialog.open() == Window.OK) {
            // setup runnable
            Runnable runnable = new Runnable() {

                public void run() {
                    IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
                    if (cleanupProcessor != null) {
                        IStructuredModel model = null;
                        try {
                            model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                            if (model != null) {
                                cleanupProcessor.cleanupModel(model);
                            }
                        } finally {
                            if (model != null) {
                                model.releaseFromEdit();
                            }
                        }
                    }
                }
            };
            // TODO: make independent of 'model'.
            IStructuredModel model = null;
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                if (model != null) {
                    // begin recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength());
                    // tell the model that we are about to make a big
                    // model change
                    model.aboutToChangeModel();
                    // run
                    BusyIndicator.showWhile(fEditor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable);
                }
            } finally {
                if (model != null) {
                    // tell the model that we are done with the big
                    // model
                    // change
                    model.changedModel();
                    // end recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    model.endRecording(this, selection.getOffset(), selection.getLength());
                    model.releaseFromEdit();
                }
            }
        }
    }
}
Also used : IStructuredCleanupProcessor(org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Dialog(org.eclipse.jface.dialogs.Dialog) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 68 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.

the class ToggleCommentActionXMLDelegate method updateCurrentSelection.

private void updateCurrentSelection(Position selectionPosition, IDocument document, boolean updateStartOffset) {
    if (fEditor instanceof ITextEditor) {
        // update the selection if text selection changed
        if (selectionPosition != null) {
            ITextSelection selection = null;
            if (updateStartOffset) {
                selection = new TextSelection(document, selectionPosition.getOffset() - OPEN_COMMENT.length(), selectionPosition.getLength() + OPEN_COMMENT.length());
            } else {
                selection = new TextSelection(document, selectionPosition.getOffset(), selectionPosition.getLength());
            }
            ISelectionProvider provider = ((ITextEditor) fEditor).getSelectionProvider();
            if (provider != null) {
                provider.setSelection(selection);
            }
            document.removePosition(selectionPosition);
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 69 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project pmd-eclipse-plugin by pmd.

the class ReviewCodeHandler method computeSelectedResources.

/**
 * Computes the selected resources.
 */
protected final void computeSelectedResources() {
    if (fResources != null || fLocation != null) {
        return;
    }
    ISelection selection = getSelection();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        List resources = new ArrayList(structuredSelection.size());
        Iterator<?> e = structuredSelection.iterator();
        while (e.hasNext()) {
            Object element = e.next();
            if (element instanceof IResource) {
                resources.add(element);
            } else if (element instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) element;
                Object adapter = adaptable.getAdapter(IResource.class);
                if (adapter instanceof IResource) {
                    resources.add(adapter);
                }
            }
        }
        if (!resources.isEmpty()) {
            fResources = (IResource[]) resources.toArray(new IResource[resources.size()]);
        }
    } else if (selection instanceof ITextSelection) {
        IWorkbenchWindow window = getWorkbenchWindow();
        if (window != null) {
            IWorkbenchPart workbenchPart = window.getPartService().getActivePart();
            if (workbenchPart instanceof IEditorPart) {
                IEditorPart editorPart = (IEditorPart) workbenchPart;
                IEditorInput input = editorPart.getEditorInput();
                Object adapter = input.getAdapter(IResource.class);
                if (adapter instanceof IResource) {
                    fResources = new IResource[] { (IResource) adapter };
                } else {
                    adapter = input.getAdapter(ILocationProvider.class);
                    if (adapter instanceof ILocationProvider) {
                        ILocationProvider provider = (ILocationProvider) adapter;
                        fLocation = provider.getPath(input);
                    }
                }
            }
        }
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) ILocationProvider(org.eclipse.ui.editors.text.ILocationProvider) ArrayList(java.util.ArrayList) List(java.util.List) IResource(org.eclipse.core.resources.IResource) IEditorInput(org.eclipse.ui.IEditorInput)

Example 70 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project dbeaver by serge-rider.

the class AbstractCommentHandler method execute.

public final Object execute(ExecutionEvent event) throws ExecutionException {
    BaseTextEditor textEditor = BaseTextEditor.getTextEditor(HandlerUtil.getActiveEditor(event));
    if (textEditor != null) {
        ICommentsSupport commentsSupport = textEditor.getCommentsSupport();
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        if (document != null && commentsSupport != null) {
            // get current text selection
            ISelectionProvider provider = textEditor.getSelectionProvider();
            if (provider != null) {
                ISelection selection = provider.getSelection();
                if (selection instanceof ITextSelection) {
                    ITextSelection textSelection = (ITextSelection) selection;
                    if (!textSelection.isEmpty()) {
                        try {
                            processAction(textEditor.getSelectionProvider(), commentsSupport, document, textSelection);
                        } catch (BadLocationException e) {
                            log.warn(e);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ICommentsSupport(org.jkiss.dbeaver.ui.ICommentsSupport) ISelection(org.eclipse.jface.viewers.ISelection) BaseTextEditor(org.jkiss.dbeaver.ui.editors.text.BaseTextEditor) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10