1 11 package org.eclipse.jdt.internal.ui.javaeditor; 12 13 import org.eclipse.jface.action.IAction; 14 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.IRegion; 17 import org.eclipse.jface.text.ITextViewer; 18 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; 19 import org.eclipse.jface.text.hyperlink.IHyperlink; 20 21 import org.eclipse.ui.texteditor.ITextEditor; 22 23 import org.eclipse.jdt.core.ICodeAssist; 24 import org.eclipse.jdt.core.IJavaElement; 25 import org.eclipse.jdt.core.JavaModelException; 26 27 import org.eclipse.jdt.internal.ui.text.JavaWordFinder; 28 29 30 35 public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector { 36 37 40 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { 41 ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class); 42 if (region == null || canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor)) 43 return null; 44 45 IAction openAction= textEditor.getAction("OpenEditor"); if (openAction == null) 47 return null; 48 49 int offset= region.getOffset(); 50 51 IJavaElement input= EditorUtility.getEditorInputJavaElement(textEditor, false); 52 if (input == null) 53 return null; 54 55 try { 56 IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); 57 IRegion wordRegion= JavaWordFinder.findWord(document, offset); 58 if (wordRegion == null) 59 return null; 60 61 IJavaElement[] elements= null; 62 elements= ((ICodeAssist) input).codeSelect(wordRegion.getOffset(), wordRegion.getLength()); 63 if (elements != null && elements.length > 0) 64 return new IHyperlink[] {new JavaElementHyperlink(wordRegion, openAction)}; 65 } catch (JavaModelException e) { 66 return null; 67 } 68 69 return null; 70 } 71 72 } 73 | Popular Tags |