Search in sources :

Example 56 with IAnnotationModel

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

the class ShowTranslationHandler method execute.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
	 * .ExecutionEvent)
	 */
public Object execute(final ExecutionEvent event) throws ExecutionException {
    // IDE.openEditor(event.getApplicationContext(), createEditorInput(),
    // JavaUI.ID_CU_EDITOR, true);
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        List list = ((IStructuredSelection) selection).toList();
        if (!list.isEmpty()) {
            if (list.get(0) instanceof IDOMNode) {
                final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
                INodeAdapter adapter = model.getDocument().getAdapterFor(IJsTranslation.class);
                if (adapter != null) {
                    Job opener = new UIJob("Opening JavaScript Translation") {

                        public IStatus runInUIThread(IProgressMonitor monitor) {
                            JsTranslationAdapter translationAdapter = (JsTranslationAdapter) model.getDocument().getAdapterFor(IJsTranslation.class);
                            final IJsTranslation translation = translationAdapter.getJsTranslation(false);
                            // create an IEditorInput for the Java editor
                            final IStorageEditorInput input = new JSTranslationEditorInput(translation, model.getBaseLocation());
                            try {
                                IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaScriptUI.ID_CU_EDITOR, true);
                                // Now add the problems we found
                                if (editor instanceof ITextEditor) {
                                    IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
                                    translation.reconcileCompilationUnit();
                                    List problemsList = translation.getProblems();
                                    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
                                    AnnotationTypeLookup lookup = new AnnotationTypeLookup();
                                    for (int i = 0; i < problems.length; i++) {
                                        int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
                                        Position position = new Position(problems[i].getSourceStart(), length);
                                        Annotation annotation = null;
                                        String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
                                        if (problems[i].isError()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
                                        } else if (problems[i].isWarning()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
                                        }
                                        annotation = new Annotation(type, false, problems[i].getMessage());
                                        if (annotation != null) {
                                            annotationModel.addAnnotation(annotation, position);
                                        }
                                    }
                                }
                            } catch (PartInitException e) {
                                e.printStackTrace();
                                Display.getCurrent().beep();
                            }
                            return Status.OK_STATUS;
                        }
                    };
                    opener.setSystem(false);
                    opener.setUser(true);
                    opener.schedule();
                }
            }
        }
    }
    return null;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Position(org.eclipse.jface.text.Position) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IEditorPart(org.eclipse.ui.IEditorPart) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem) Annotation(org.eclipse.jface.text.source.Annotation) AnnotationTypeLookup(org.eclipse.ui.texteditor.AnnotationTypeLookup) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ISelection(org.eclipse.jface.viewers.ISelection) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob)

Example 57 with IAnnotationModel

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

the class AnnotationHoverProcessor method getHoverInfo.

public String getHoverInfo(ITextViewer viewer, IRegion hoverRegion) {
    IAnnotationModel model = ((SourceViewer) viewer).getAnnotationModel();
    if (model != null) {
        List messages = new ArrayList();
        Iterator e = model.getAnnotationIterator();
        while (e.hasNext()) {
            Annotation a = (Annotation) e.next();
            if (!isAnnotationValid(a))
                continue;
            Position p = model.getPosition(a);
            // concerned with
            if (p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
                String msg = a.getText();
                if ((msg != null) && msg.trim().length() > 0) {
                    // dups
                    if (a instanceof ITemporaryAnnotation) {
                        boolean duplicated = false;
                        int j = 0;
                        while (j < messages.size() && !duplicated) {
                            duplicated = messages.get(j).equals(msg);
                            ++j;
                        }
                        if (!duplicated) {
                            messages.add(msg);
                        }
                    } else {
                        messages.add(msg);
                    }
                }
            }
        }
        if (messages.size() > 1) {
            return formatMessages(messages);
        } else if (messages.size() > 0) {
            return formatMessage(messages.get(0).toString());
        }
    }
    return null;
}
Also used : SourceViewer(org.eclipse.jface.text.source.SourceViewer) ITemporaryAnnotation(org.eclipse.wst.sse.ui.internal.ITemporaryAnnotation) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) ITemporaryAnnotation(org.eclipse.wst.sse.ui.internal.ITemporaryAnnotation)

Example 58 with IAnnotationModel

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

the class AnnotationHoverProcessor method getHoverRegion.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
	 *      int)
	 */
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    IAnnotationModel model = ((SourceViewer) textViewer).getAnnotationModel();
    Region hoverRegion = null;
    if (model != null) {
        Iterator e = model.getAnnotationIterator();
        while (e.hasNext()) {
            Annotation a = (Annotation) e.next();
            if (!isAnnotationValid(a))
                continue;
            Position p = model.getPosition(a);
            if (p != null && p.includes(offset)) {
                // find the smallest region containing offset
                if ((hoverRegion == null) || (hoverRegion.getLength() > p.getLength())) {
                    hoverRegion = new Region(p.getOffset(), p.getLength());
                }
            }
        }
    }
    return hoverRegion;
}
Also used : SourceViewer(org.eclipse.jface.text.source.SourceViewer) Position(org.eclipse.jface.text.Position) Iterator(java.util.Iterator) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) ITemporaryAnnotation(org.eclipse.wst.sse.ui.internal.ITemporaryAnnotation)

Example 59 with IAnnotationModel

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

the class SpellcheckStrategy method getSpellingAnnotationsToRemove.

private TemporaryAnnotation[] getSpellingAnnotationsToRemove(IRegion region) {
    List toRemove = new ArrayList();
    IAnnotationModel annotationModel = getAnnotationModel();
    // can be null when closing the editor
    if (annotationModel != null) {
        Iterator i = null;
        boolean annotationOverlaps = false;
        if (annotationModel instanceof IAnnotationModelExtension2) {
            i = ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(region.getOffset(), region.getLength(), true, true);
            annotationOverlaps = true;
        } else {
            i = annotationModel.getAnnotationIterator();
        }
        while (i.hasNext()) {
            Object obj = i.next();
            if (!(obj instanceof TemporaryAnnotation))
                continue;
            TemporaryAnnotation annotation = (TemporaryAnnotation) obj;
            ReconcileAnnotationKey key = (ReconcileAnnotationKey) annotation.getKey();
            // partition type
            if (key != null && key.equals(fReconcileAnnotationKey)) {
                if (key.getScope() == ReconcileAnnotationKey.PARTIAL && (annotationOverlaps || annotation.getPosition().overlapsWith(region.getOffset(), region.getLength()))) {
                    toRemove.add(annotation);
                } else if (key.getScope() == ReconcileAnnotationKey.TOTAL) {
                    toRemove.add(annotation);
                }
            }
        }
    }
    return (TemporaryAnnotation[]) toRemove.toArray(new TemporaryAnnotation[toRemove.size()]);
}
Also used : IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) ReconcileAnnotationKey(org.eclipse.wst.sse.ui.internal.reconcile.ReconcileAnnotationKey) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)

Example 60 with IAnnotationModel

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

the class AbstractStructuredTextReconcilingStrategy method getAllAnnotationsToRemove.

/**
 * Remove ALL temporary annotations that this strategy can handle.
 */
protected TemporaryAnnotation[] getAllAnnotationsToRemove() {
    List removals = new ArrayList();
    IAnnotationModel annotationModel = getAnnotationModel();
    if (annotationModel != null) {
        Iterator i = annotationModel.getAnnotationIterator();
        while (i.hasNext()) {
            Object obj = i.next();
            if (!(obj instanceof ITemporaryAnnotation))
                continue;
            ITemporaryAnnotation annotation = (ITemporaryAnnotation) obj;
            ReconcileAnnotationKey key = (ReconcileAnnotationKey) annotation.getKey();
            // partition type
            if (canHandlePartition(key.getPartitionType()))
                /*
																 * &&
																 * containsStep(key.getStep())
																 */
                removals.add(annotation);
        }
    }
    return (TemporaryAnnotation[]) removals.toArray(new TemporaryAnnotation[removals.size()]);
}
Also used : ITemporaryAnnotation(org.eclipse.wst.sse.ui.internal.ITemporaryAnnotation) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ITemporaryAnnotation(org.eclipse.wst.sse.ui.internal.ITemporaryAnnotation)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10