|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2007 Mylyn project committers and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
*******************************************************************************/ |
| 8 |
|
| 9 |
package org.eclipse.mylyn.internal.java.tasks; |
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.List; |
| 13 |
import java.util.regex.Matcher; |
| 14 |
import java.util.regex.Pattern; |
| 15 |
|
| 16 |
import org.eclipse.jface.text.BadLocationException; |
| 17 |
import org.eclipse.jface.text.IDocument; |
| 18 |
import org.eclipse.jface.text.IRegion; |
| 19 |
import org.eclipse.jface.text.ITextViewer; |
| 20 |
import org.eclipse.jface.text.Region; |
| 21 |
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; |
| 22 |
import org.eclipse.jface.text.hyperlink.IHyperlink; |
| 23 |
|
| 24 |
/** |
| 25 |
* Java Class Hyperlink Dectector, e.g. detecting org.eclipse.mylyn.internal.java.tasks.JavaClassHyperlinkDetector |
| 26 |
* |
| 27 |
* @author Jingwen Ou |
| 28 |
*/ |
| 29 |
public class JavaClassHyperlinkDetector extends AbstractHyperlinkDetector { |
| 30 |
|
| 31 |
private static final Pattern javaClassPattern = Pattern.compile("(\\w+\\.)+\\w+", Pattern.CASE_INSENSITIVE); |
| 32 |
|
| 33 |
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { |
| 34 |
List<IHyperlink> hyperLinks = new ArrayList<IHyperlink>(); |
| 35 |
|
| 36 |
try { |
| 37 |
IDocument document = textViewer.getDocument(); |
| 38 |
if (document == null) { |
| 39 |
return null; |
| 40 |
} |
| 41 |
int offset = region.getOffset(); |
| 42 |
|
| 43 |
IRegion lineInfo = document.getLineInformationOfOffset(offset); |
| 44 |
String line = document.get(lineInfo.getOffset(), lineInfo.getLength()); |
| 45 |
|
| 46 |
Matcher m = javaClassPattern.matcher(line); |
| 47 |
|
| 48 |
while (m.find()) { |
| 49 |
// check if its a URL hyperlink |
| 50 |
if (lineInfo.getOffset() + m.start() - 1 >= 0 |
| 51 |
&& document.get(lineInfo.getOffset() + m.start() - 1, 1).equalsIgnoreCase("/")) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
IRegion urlRegion = new Region(lineInfo.getOffset() + m.start(), m.end() - m.start()); |
| 56 |
hyperLinks.add(new JavaClassFileHyperlink(urlRegion, m.group())); |
| 57 |
} |
| 58 |
|
| 59 |
} catch (BadLocationException e) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
return hyperLinks.size() > 0 ? hyperLinks.toArray(new IHyperlink[hyperLinks.size()]) : null; |
| 64 |
} |
| 65 |
|
| 66 |
public static void main(String str[]) { |
| 67 |
Pattern javaClassPattern = Pattern.compile("\\s(\\w+\\.)+\\w+", Pattern.CASE_INSENSITIVE); |
| 68 |
Matcher m = javaClassPattern.matcher(" http:// www.eclipse// \n ww.dd ff."); |
| 69 |
while (m.find()) { |
| 70 |
System.out.println(m.group()); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |