Search in sources :

Example 1 with AnchorHrefAnnotation

use of org.eclipse.mylyn.wikitext.ui.annotation.AnchorHrefAnnotation in project mylyn.docs by eclipse.

the class AnnotationHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (textViewer instanceof ISourceViewer) {
        // Note: only find hyperlinks that include the region end point, otherwise we may get hyperlinks that
        // are not even relevant.
        int interestingOffset = region.getOffset() + region.getLength();
        ISourceViewer sourceViewer = (ISourceViewer) textViewer;
        IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
        if (annotationModel != null) {
            List<AnchorHrefAnnotation> hrefs = null;
            Iterator<?> iterator = annotationModel.getAnnotationIterator();
            while (iterator.hasNext()) {
                Annotation annotation = (Annotation) iterator.next();
                if (annotation instanceof AnchorHrefAnnotation) {
                    AnchorHrefAnnotation href = (AnchorHrefAnnotation) annotation;
                    Position position = annotationModel.getPosition(href);
                    if (position.getOffset() <= interestingOffset && position.getOffset() + position.getLength() >= interestingOffset) {
                        if (hrefs == null) {
                            hrefs = new ArrayList<>();
                        }
                        hrefs.add(href);
                    }
                }
            }
            if (hrefs != null) {
                if (hrefs.size() > 1) {
                    // put greatest offset annotations first.
                    Collections.sort(hrefs, new OffsetComparator(annotationModel));
                }
                return new IHyperlink[] { createHyperlink(sourceViewer, annotationModel, hrefs.get(0)) };
            }
        }
    }
    return null;
}
Also used : AnchorHrefAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.AnchorHrefAnnotation) Position(org.eclipse.jface.text.Position) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Annotation(org.eclipse.jface.text.source.Annotation) AnchorNameAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.AnchorNameAnnotation) AnchorHrefAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.AnchorHrefAnnotation) IdAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.IdAnnotation)

Aggregations

Position (org.eclipse.jface.text.Position)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 AnchorHrefAnnotation (org.eclipse.mylyn.wikitext.ui.annotation.AnchorHrefAnnotation)1 AnchorNameAnnotation (org.eclipse.mylyn.wikitext.ui.annotation.AnchorNameAnnotation)1 IdAnnotation (org.eclipse.mylyn.wikitext.ui.annotation.IdAnnotation)1