/** * Use hyperlink detectors to find a text viewer's hyperlinks and return the style ranges to * render them. * * @param textViewer * @param hyperlinkDetectors * @return the style ranges to render the detected hyperlinks */ public static StyleRange[] getHyperlinkDetectorStyleRanges( ITextViewer textViewer, IHyperlinkDetector[] hyperlinkDetectors) { List<StyleRange> styleRangeList = new ArrayList<StyleRange>(); if (hyperlinkDetectors != null && hyperlinkDetectors.length > 0) { for (int i = 0; i < textViewer.getTextWidget().getText().length(); i++) { IRegion region = new Region(i, 0); for (IHyperlinkDetector hyperLinkDetector : hyperlinkDetectors) { IHyperlink[] hyperlinks = hyperLinkDetector.detectHyperlinks(textViewer, region, true); if (hyperlinks != null) { for (IHyperlink hyperlink : hyperlinks) { StyleRange hyperlinkStyleRange = new StyleRange( hyperlink.getHyperlinkRegion().getOffset(), hyperlink.getHyperlinkRegion().getLength(), Display.getDefault().getSystemColor(SWT.COLOR_BLUE), Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); hyperlinkStyleRange.underline = true; styleRangeList.add(hyperlinkStyleRange); } } } } } StyleRange[] styleRangeArray = new StyleRange[styleRangeList.size()]; styleRangeList.toArray(styleRangeArray); return styleRangeArray; }
private static Optional<IHyperlink> detect( final ITextViewer viewer, final IRegion hyperlinkRegion, final IHyperlinkDetector detector) { final IHyperlink[] hyperlinks = detector.detectHyperlinks(viewer, hyperlinkRegion, false); if (hyperlinks != null && hyperlinks.length > 0) { return Optional.of(hyperlinks[0]); } return Optional.absent(); }