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;
}
Aggregations