Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 9994523a7b36ee8e6db4b438b9e5ae51417d9a7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.eclipse.wst.sse.ui.internal.hyperlink;

import java.util.ResourceBundle;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.TextEditorAction;

/**
 * Open hyperlink action
 */
public class OpenHyperlinkAction extends TextEditorAction {
	private IHyperlinkDetector[] fHyperlinkDetectors;
	private ITextViewer fTextViewer;

	public OpenHyperlinkAction(ResourceBundle bundle, String prefix, ITextEditor editor, ITextViewer viewer) {
		super(bundle, prefix, editor);
		fTextViewer = viewer;
	}

	public void setHyperlinkDetectors(IHyperlinkDetector[] detectors) {
		fHyperlinkDetectors = detectors;
	}

	public void run() {
		if (fHyperlinkDetectors == null)
			return;
		ISelection selection = getTextEditor().getSelectionProvider().getSelection();
		if (selection == null || !(selection instanceof ITextSelection)) {
			return;
		}

		ITextSelection textSelection = (ITextSelection) selection;
		IRegion region = new Region(textSelection.getOffset(), textSelection.getLength());
		IHyperlink hyperlink = null;

		synchronized (fHyperlinkDetectors) {
			for (int i = 0, length = fHyperlinkDetectors.length; i < length && hyperlink == null; i++) {
				IHyperlinkDetector detector = fHyperlinkDetectors[i];
				if (detector == null)
					continue;

				IHyperlink[] hyperlinks = detector.detectHyperlinks(fTextViewer, region, false);
				if (hyperlinks == null)
					continue;

				if (hyperlinks.length > 0)
					hyperlink = hyperlinks[0];
			}
		}
		if (hyperlink != null) {
			/**
			 * Force the highlight range to change when the hyperlink is
			 * opened by altering the highlighted range beforehand.
			 */
			getTextEditor().setHighlightRange(Math.max(0, region.getOffset() - 1), 0, false);
			hyperlink.open();
		}
	}
}

Back to the top