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