Search in sources :

Example 91 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor 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 92 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor 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 93 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor 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 94 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project pmd-eclipse-plugin by pmd.

the class CPDViewTooltipListener2 method highlight.

// open file and jump to the startline
private void highlight(Match match, Mark entry) {
    IPath path = Path.fromOSString(entry.getFilename());
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
    if (file == null) {
        return;
    }
    try {
        // open editor
        IWorkbenchPage page = view.getSite().getPage();
        IEditorPart part = IDE.openEditor(page, file);
        if (part instanceof ITextEditor) {
            // select text
            ITextEditor textEditor = (ITextEditor) part;
            IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
            int offset = document.getLineOffset(entry.getBeginLine() - 1);
            int length = document.getLineOffset(entry.getBeginLine() - 1 + match.getLineCount()) - offset - 1;
            textEditor.selectAndReveal(offset, length);
        }
    } catch (PartInitException pie) {
        PMDPlugin.getDefault().logError(getString(StringKeys.ERROR_VIEW_EXCEPTION), pie);
    } catch (BadLocationException ble) {
        PMDPlugin.getDefault().logError(getString(StringKeys.ERROR_VIEW_EXCEPTION), ble);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IPath(org.eclipse.core.runtime.IPath) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 95 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project pmd-eclipse-plugin by pmd.

the class MarkerContributionFactory method createContributionItems.

@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    ITextEditor editor = (ITextEditor) page.getActivePart();
    additions.addContributionItem(new MarkerMenuFiller(editor), null);
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Aggregations

ITextEditor (org.eclipse.ui.texteditor.ITextEditor)236 IEditorPart (org.eclipse.ui.IEditorPart)92 IDocument (org.eclipse.jface.text.IDocument)76 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)42 IFile (org.eclipse.core.resources.IFile)36 ITextSelection (org.eclipse.jface.text.ITextSelection)34 Test (org.junit.Test)33 BadLocationException (org.eclipse.jface.text.BadLocationException)28 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 CoreException (org.eclipse.core.runtime.CoreException)15 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)15 IResource (org.eclipse.core.resources.IResource)14 IEditorInput (org.eclipse.ui.IEditorInput)14 TextSelection (org.eclipse.jface.text.TextSelection)12