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 (+79 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.core.runtime.Platform;
7
import org.eclipse.jface.text.IRegion;
8
import org.eclipse.jface.text.Region;
9
import org.eclipse.jface.text.hyperlink.IHyperlink;
10
import org.eclipse.ui.PartInitException;
11
import org.eclipse.ui.PlatformUI;
12
import org.eclipse.ui.browser.IWebBrowser;
13
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
14
15
public class BugHyperlink implements IHyperlink {
16
	public static final String HYPERLINK_BUG_DEFAULT_URL = "https://bugs.eclipse.org/bugs/show_bug.cgi?id=<bug#>";
17
18
	public static final String BROWSER_BUG_TITLE = "Bug #";
19
20
	public static final String BUG_NUMBER = "<bug#>";
21
22
	private Region region;
23
24
	private String bugNumber;
25
26
	private String typeLabel;
27
28
	private String hyperlinkText;
29
30
	public BugHyperlink(Region region) {
31
	}
32
33
	public BugHyperlink(String bugNumber, Region region) {
34
		this.region = region;
35
		this.bugNumber = bugNumber;
36
	}
37
38
	public IRegion getHyperlinkRegion() {
39
		return this.region;
40
	}
41
42
	public String getHyperlinkText() {
43
		return this.hyperlinkText;
44
	}
45
46
	public String getTypeLabel() {
47
		return this.typeLabel;
48
	}
49
50
	public void open() {
51
		IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench()
52
				.getBrowserSupport();
53
54
		URL webUrl;
55
56
		try {
57
			String url = Platform.getPreferencesService().getString(
58
					"org.eclipse.ecf.presence.ui",
59
					"chatroom.hyperlink.bug.url", HYPERLINK_BUG_DEFAULT_URL,
60
					null).replaceAll(BUG_NUMBER, bugNumber);
61
			webUrl = new URL(url);
62
		} catch (MalformedURLException e) {
63
			return;
64
		}
65
66
		IWebBrowser browser;
67
		try {
68
			browser = browserSupport.createBrowser(
69
					IWorkbenchBrowserSupport.AS_EDITOR, BROWSER_BUG_TITLE
70
							+ bugNumber, BROWSER_BUG_TITLE + bugNumber,
71
					BROWSER_BUG_TITLE + bugNumber);
72
			browser.openURL(webUrl);
73
		} catch (PartInitException e) {
74
			return;
75
		}
76
77
	}
78
79
}

Return to bug 197991