View | Details | Raw Unified | Return to bug 197991 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.xml (+7 lines)
Lines 19-24 Link Here
19
            name="%IRCHyperlinkDetector"
19
            name="%IRCHyperlinkDetector"
20
            targetId="org.eclipse.ui.DefaultTextEditor">
20
            targetId="org.eclipse.ui.DefaultTextEditor">
21
      </hyperlinkDetector>
21
      </hyperlinkDetector>
22
      <hyperlinkDetector
23
            activate="true"
24
            class="org.eclipse.ecf.internal.irc.ui.hyperlink.BugHyperlinkDetector"
25
            id="org.eclipse.ecf.internal.irc.ui.hyperlink.BugHyperlinkDetector"
26
            name="Bug"
27
            targetId="org.eclipse.ui.DefaultTextEditor">
28
      </hyperlinkDetector>
22
   </extension>
29
   </extension>
23
   <extension
30
   <extension
24
         point="org.eclipse.ui.popupMenus">
31
         point="org.eclipse.ui.popupMenus">
(-)src/org/eclipse/ecf/internal/irc/ui/hyperlink/BugHyperlinkDetector.java (+98 lines)
Added Link Here
1
package org.eclipse.ecf.internal.irc.ui.hyperlink;
2
3
import java.util.StringTokenizer;
4
5
import org.eclipse.jface.text.BadLocationException;
6
import org.eclipse.jface.text.IDocument;
7
import org.eclipse.jface.text.IRegion;
8
import org.eclipse.jface.text.ITextViewer;
9
import org.eclipse.jface.text.Region;
10
import org.eclipse.jface.text.hyperlink.IHyperlink;
11
12
public class BugHyperlinkDetector extends
13
		org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector {
14
15
	public static final String DEFAULT_PREFIX = "bug "; //$NON-NLS-1$
16
	public static final String DEFAULT_ENDDELIMITERS = " \t\n\r\f<>"; //$NON-NLS-1$
17
18
	/*
19
	 * (non-Javadoc)
20
	 * 
21
	 * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer,
22
	 *      org.eclipse.jface.text.IRegion, boolean)
23
	 */
24
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer,
25
			IRegion region, boolean canShowMultipleHyperlinks) {
26
		if (region == null || textViewer == null)
27
			return null;
28
29
		IDocument document = textViewer.getDocument();
30
		if (document == null)
31
			return null;
32
33
		int offset = region.getOffset();
34
35
		IRegion lineInfo;
36
		String line;
37
		try {
38
			lineInfo = document.getLineInformationOfOffset(offset);
39
			line = document.get(lineInfo.getOffset(), lineInfo.getLength());
40
		} catch (BadLocationException ex) {
41
			return null;
42
		}
43
44
		String detectedSubstring = detectSubstring(line, offset
45
				- lineInfo.getOffset());
46
		String bugNumber = detectedSubstring.substring(DEFAULT_PREFIX.length());
47
48
		if (detectedSubstring != null && isBugNumber(bugNumber))
49
			return createHyperLinksForBug(bugNumber, new Region(
50
					lineInfo.getOffset() + line.indexOf(detectedSubstring),
51
					detectedSubstring.length()));
52
53
		return null;
54
	}
55
56
	private boolean isBugNumber(String bug) {
57
		try {
58
			Integer.parseInt(bug);
59
			return true;
60
		} catch (NumberFormatException e) {
61
		}
62
		
63
		return false;
64
	}
65
66
	private IHyperlink[] createHyperLinksForBug(String bugNumber, Region region) {
67
		return new IHyperlink[] { new BugHyperlink(bugNumber, region) };
68
	}
69
70
	private String detectSubstring(String fromLine, int offsetInLine) {
71
		int resultLength = 0;
72
73
		int separatorOffset = fromLine.indexOf(DEFAULT_PREFIX);
74
		while (separatorOffset >= 0) {
75
			StringTokenizer tokenizer = new StringTokenizer(fromLine
76
					.substring(separatorOffset + DEFAULT_PREFIX.length()),
77
					DEFAULT_ENDDELIMITERS, false);
78
			if (!tokenizer.hasMoreTokens())
79
				return null;
80
81
			resultLength = tokenizer.nextToken().length()
82
					+ DEFAULT_PREFIX.length();
83
			if (offsetInLine >= separatorOffset
84
					&& offsetInLine <= separatorOffset + resultLength)
85
				break;
86
87
			separatorOffset = fromLine.indexOf(DEFAULT_PREFIX,
88
					separatorOffset + 1);
89
		}
90
91
		if (separatorOffset < 0)
92
			return null;
93
94
		return fromLine.substring(separatorOffset, separatorOffset
95
				+ resultLength);
96
	}
97
98
}
(-)src/org/eclipse/ecf/internal/irc/ui/hyperlink/BugHyperlink.java (+71 lines)
Added Link Here
1
package org.eclipse.ecf.internal.irc.ui.hyperlink;
2
3
import java.net.MalformedURLException;
4
import java.net.URL;
5
6
import org.eclipse.jface.text.IRegion;
7
import org.eclipse.jface.text.Region;
8
import org.eclipse.jface.text.hyperlink.IHyperlink;
9
import org.eclipse.ui.PartInitException;
10
import org.eclipse.ui.PlatformUI;
11
import org.eclipse.ui.browser.IWebBrowser;
12
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
13
14
public class BugHyperlink implements IHyperlink {
15
	public static final String ECLIPSE_BUGZILLA_URL = "https://bugs.eclipse.org/bugs/show_bug.cgi?id=";
16
	
17
	public static final String BROWSER_BUG_TITLE = "Bug #";
18
19
	private Region region;
20
21
	private String bugNumber;
22
23
	private String typeLabel;
24
25
	private String hyperlinkText;
26
27
	public BugHyperlink(Region region) {
28
	}
29
30
	public BugHyperlink(String bugNumber, Region region) {
31
		this.region = region;
32
		this.bugNumber = bugNumber;
33
	}
34
35
	public IRegion getHyperlinkRegion() {
36
		return this.region;
37
	}
38
39
	public String getHyperlinkText() {
40
		return this.hyperlinkText;
41
	}
42
43
	public String getTypeLabel() {
44
		return this.typeLabel;
45
	}
46
47
	public void open() {
48
		IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench()
49
				.getBrowserSupport();
50
51
		URL webUrl;
52
53
		try {
54
			webUrl = new URL(ECLIPSE_BUGZILLA_URL + bugNumber);
55
		} catch (MalformedURLException e) {
56
			return;
57
		}
58
59
		IWebBrowser browser;
60
		try {
61
			browser = browserSupport.createBrowser(
62
					IWorkbenchBrowserSupport.AS_EDITOR, BROWSER_BUG_TITLE+bugNumber, BROWSER_BUG_TITLE+bugNumber,
63
					BROWSER_BUG_TITLE+bugNumber);
64
			browser.openURL(webUrl);
65
		} catch (PartInitException e) {
66
			return;
67
		}
68
69
	}
70
71
}

Return to bug 197991