KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > NLSKeyHyperlinkDetector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation 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  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
39  * NLS hyperlink detector.
40  *
41  * @since 3.1
42  */

43 public class NLSKeyHyperlinkDetector extends AbstractHyperlinkDetector {
44
45
46     /*
47      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
48      */

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