Search in sources :

Example 11 with AnnotationModel

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

the class StorageModelProvider method createAnnotationModel.

// public boolean canSaveDocument(Object element) {
// return false;
// }
protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
    IAnnotationModel model = null;
    if (debugOperations) {
        if (element instanceof IStorageEditorInput)
            // $NON-NLS-1$
            System.out.println("StorageModelProvider: createAnnotationModel for " + ((IStorageEditorInput) element).getStorage().getFullPath());
        else
            // $NON-NLS-1$
            System.out.println("StorageModelProvider: createAnnotationModel for " + element);
    }
    if (element instanceof IStorageEditorInput) {
        IStorageEditorInput input = (IStorageEditorInput) element;
        String contentType = (getModel(input) != null ? getModel(input).getContentTypeIdentifier() : null);
        String ext = BreakpointRulerAction.getFileExtension((IEditorInput) element);
        IResource res = BreakpointProviderBuilder.getInstance().getResource(input, contentType, ext);
        String id = input.getName();
        if (input.getStorage() != null && input.getStorage().getFullPath() != null) {
            id = input.getStorage().getFullPath().toString();
        }
        // valid resource
        if (res != null)
            model = new StructuredResourceMarkerAnnotationModel(res, id);
        else
            model = new AnnotationModel();
    }
    if (model == null) {
        model = super.createAnnotationModel(element);
    }
    return model;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AnnotationModel(org.eclipse.jface.text.source.AnnotationModel) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IResource(org.eclipse.core.resources.IResource)

Example 12 with AnnotationModel

use of org.eclipse.jface.text.source.AnnotationModel in project mylyn.docs by eclipse.

the class ImageManager method updateImage.

@SuppressWarnings("unchecked")
private void updateImage(String imgSrc, ImageData imageData) {
    if (display.isDisposed() || viewer.getTextWidget().isDisposed()) {
        return;
    }
    Image image = imageData == null ? imageCache.getMissingImage() : ImageDescriptor.createFromImageData(imageData).createImage();
    imageCache.putImage(imgSrc, image);
    Set<ImageAnnotation> modifiedAnnotations = new HashSet<>();
    AnnotationModel annotationModel = (AnnotationModel) viewer.getAnnotationModel();
    Object annotationLockObject = annotationModel.getLockObject();
    if (annotationLockObject == null) {
        annotationLockObject = annotationModel;
    }
    synchronized (annotationLockObject) {
        Iterator<Annotation> iterator = annotationModel.getAnnotationIterator();
        while (iterator.hasNext()) {
            Annotation annotation = iterator.next();
            if (annotation instanceof ImageAnnotation) {
                ImageAnnotation imageAnnotation = (ImageAnnotation) annotation;
                if (imgSrc.equals(imageAnnotation.getUrl())) {
                    imageAnnotation.setImage(image);
                    modifiedAnnotations.add(imageAnnotation);
                }
            }
        }
    }
    if (!modifiedAnnotations.isEmpty()) {
        computingChanges = true;
        try {
            boolean rangesAdjusted = false;
            List<StyleRange> ranges = new ArrayList<>();
            Iterator<?> allStyleRangeIterator = viewer.getTextPresentation().getAllStyleRangeIterator();
            while (allStyleRangeIterator.hasNext()) {
                StyleRange range = (StyleRange) allStyleRangeIterator.next();
                ranges.add((StyleRange) range.clone());
            }
            GC gc = new GC(viewer.getTextWidget());
            try {
                viewer.getTextWidget().setRedraw(false);
                TextPresentation textPresentation = viewer.getTextPresentation();
                // textPresentation.
                for (ImageAnnotation annotation : modifiedAnnotations) {
                    int height = annotation.getImage().getBounds().height;
                    Position position = annotationModel.getPosition(annotation);
                    String widgetText = viewer.getTextWidget().getText();
                    Font font = null;
                    if (widgetText.length() > 0 && widgetText.length() > position.offset) {
                        StyleRange styleRange = viewer.getTextWidget().getStyleRangeAtOffset(position.offset);
                        if (styleRange != null) {
                            font = styleRange.font;
                        }
                    }
                    if (font == null) {
                        font = viewer.getTextWidget().getFont();
                    }
                    gc.setFont(font);
                    // $NON-NLS-1$
                    Point extent = gc.textExtent("\n");
                    if (extent.y > 0) {
                        int numNewlines = (int) Math.ceil(((double) height) / ((double) extent.y));
                        final int originalNewlines = numNewlines;
                        IDocument document = viewer.getDocument();
                        try {
                            for (int x = position.offset; x < document.getLength(); ++x) {
                                if (document.getChar(x) == '\n') {
                                    if (x != position.offset && Util.annotationsIncludeOffset(viewer.getAnnotationModel(), x)) {
                                        break;
                                    }
                                    --numNewlines;
                                } else {
                                    break;
                                }
                            }
                            if (numNewlines > 0) {
                                // $NON-NLS-1$
                                String newlines = "";
                                for (int x = 0; x < numNewlines; ++x) {
                                    // $NON-NLS-1$
                                    newlines += "\n";
                                }
                                document.replace(position.offset + 1, 0, newlines);
                            } else if (numNewlines < 0) {
                                // $NON-NLS-1$
                                document.replace(position.offset, -numNewlines, "");
                            }
                            if (numNewlines != 0) {
                                // fix up styles
                                for (StyleRange range : ranges) {
                                    if (range.start > position.offset) {
                                        range.start += numNewlines;
                                        rangesAdjusted = true;
                                    } else if (range.start + range.length > position.offset) {
                                        range.length += numNewlines;
                                        rangesAdjusted = true;
                                    }
                                }
                            }
                            // as a result of scrolling
                            if (position.getLength() != originalNewlines) {
                                annotationModel.modifyAnnotationPosition(annotation, new Position(position.offset, originalNewlines));
                            }
                        } catch (BadLocationException e) {
                        // ignore
                        }
                    }
                }
                if (rangesAdjusted) {
                    TextPresentation presentation = new TextPresentation();
                    if (textPresentation.getDefaultStyleRange() != null) {
                        StyleRange defaultStyleRange = (StyleRange) textPresentation.getDefaultStyleRange().clone();
                        if (viewer.getDocument() != null) {
                            if (defaultStyleRange.length < viewer.getDocument().getLength()) {
                                defaultStyleRange.length = viewer.getDocument().getLength();
                            }
                        }
                        presentation.setDefaultStyleRange(defaultStyleRange);
                    }
                    for (StyleRange range : ranges) {
                        presentation.addStyleRange(range);
                    }
                    viewer.setTextPresentation(presentation);
                    viewer.invalidateTextPresentation();
                }
            } finally {
                viewer.getTextWidget().setRedraw(true);
                gc.dispose();
            }
            viewer.getTextWidget().redraw();
        } finally {
            computingChanges = false;
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) StyleRange(org.eclipse.swt.custom.StyleRange) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) ImageAnnotation(org.eclipse.mylyn.internal.wikitext.ui.viewer.annotation.ImageAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) ImageAnnotation(org.eclipse.mylyn.internal.wikitext.ui.viewer.annotation.ImageAnnotation) AnnotationModel(org.eclipse.jface.text.source.AnnotationModel) GC(org.eclipse.swt.graphics.GC) TextPresentation(org.eclipse.jface.text.TextPresentation) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) HashSet(java.util.HashSet)

Example 13 with AnnotationModel

use of org.eclipse.jface.text.source.AnnotationModel in project statecharts by Yakindu.

the class StyledTextXtextAdapter method createXtextSourceViewer.

protected XtextSourceViewer createXtextSourceViewer() {
    final XtextSourceViewer result = new XtextSourceViewerEx(getStyledText(), getPreferenceStoreAccess().getPreferenceStore());
    result.configure(getXtextSourceViewerConfiguration());
    result.setDocument(getXtextDocument(), new AnnotationModel());
    return result;
}
Also used : XtextSourceViewer(org.eclipse.xtext.ui.editor.XtextSourceViewer) AnnotationModel(org.eclipse.jface.text.source.AnnotationModel)

Aggregations

AnnotationModel (org.eclipse.jface.text.source.AnnotationModel)13 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)7 Document (org.eclipse.jface.text.Document)5 IDocument (org.eclipse.jface.text.IDocument)5 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)3 SourceViewer (org.eclipse.jface.text.source.SourceViewer)3 HashSet (java.util.HashSet)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 IRegion (org.eclipse.jface.text.IRegion)2 TextPresentation (org.eclipse.jface.text.TextPresentation)2 DirtyRegion (org.eclipse.jface.text.reconciler.DirtyRegion)2 IReconcilingStrategy (org.eclipse.jface.text.reconciler.IReconcilingStrategy)2 MonoReconciler (org.eclipse.jface.text.reconciler.MonoReconciler)2 Annotation (org.eclipse.jface.text.source.Annotation)2 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)2 ILineDifferExtension (org.eclipse.jface.text.source.ILineDifferExtension)2 GC (org.eclipse.swt.graphics.GC)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 Display (org.eclipse.swt.widgets.Display)2 Shell (org.eclipse.swt.widgets.Shell)2