1 11 12 package org.eclipse.jdt.internal.ui.javaeditor; 13 14 import org.eclipse.jface.text.IRegion; 15 import org.eclipse.jface.text.ITextViewer; 16 import org.eclipse.jface.text.Region; 17 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; 18 import org.eclipse.jface.text.hyperlink.IHyperlink; 19 20 import org.eclipse.ui.IEditorInput; 21 import org.eclipse.ui.IEditorSite; 22 import org.eclipse.ui.texteditor.ITextEditor; 23 24 import org.eclipse.jdt.core.IJavaElement; 25 import org.eclipse.jdt.core.dom.ASTNode; 26 import org.eclipse.jdt.core.dom.CompilationUnit; 27 import org.eclipse.jdt.core.dom.QualifiedName; 28 import org.eclipse.jdt.core.dom.SimpleName; 29 import org.eclipse.jdt.core.dom.StringLiteral; 30 31 import org.eclipse.jdt.internal.corext.dom.NodeFinder; 32 import org.eclipse.jdt.internal.corext.refactoring.nls.AccessorClassReference; 33 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSHintHelper; 34 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 37 38 43 public class NLSKeyHyperlinkDetector extends AbstractHyperlinkDetector { 44 45 46 49 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { 50 ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class); 51 if (region == null || textEditor == null || canShowMultipleHyperlinks) 52 return null; 53 54 IEditorSite site= textEditor.getEditorSite(); 55 if (site == null) 56 return null; 57 58 IJavaElement javaElement= getInputJavaElement(textEditor); 59 if (javaElement == null) 60 return null; 61 62 CompilationUnit ast= JavaPlugin.getDefault().getASTProvider().getAST(javaElement, ASTProvider.WAIT_NO, null); 63 if (ast == null) 64 return null; 65 66 ASTNode node= NodeFinder.perform(ast, region.getOffset(), 1); 67 if (!(node instanceof StringLiteral) && !(node instanceof SimpleName)) 68 return null; 69 70 if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) 71 return null; 72 73 IRegion nlsKeyRegion= new Region(node.getStartPosition(), node.getLength()); 74 AccessorClassReference ref= NLSHintHelper.getAccessorClassReference(ast, nlsKeyRegion); 75 if (ref == null) 76 return null; 77 String keyName= null; 78 if (node instanceof StringLiteral) { 79 keyName= ((StringLiteral)node).getLiteralValue(); 80 } else { 81 keyName= ((SimpleName)node).getIdentifier(); 82 } 83 if (keyName != null) 84 return new IHyperlink[] {new NLSKeyHyperlink(nlsKeyRegion, keyName, ref, textEditor)}; 85 86 return null; 87 } 88 89 private IJavaElement getInputJavaElement(ITextEditor editor) { 90 IEditorInput editorInput= editor.getEditorInput(); 91 if (editorInput instanceof IClassFileEditorInput) 92 return ((IClassFileEditorInput)editorInput).getClassFile(); 93 94 if (editor instanceof CompilationUnitEditor) 95 return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput); 96 97 return null; 98 } 99 100 } 101 | Popular Tags |