]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/javaeditor/NLSKeyHyperlinkDetector.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / javaeditor / NLSKeyHyperlinkDetector.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2009 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.IEditorSite;
21
22 import org.eclipse.ui.texteditor.ITextEditor;
23
24 import org.eclipse.jdt.core.ITypeRoot;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27 import org.eclipse.jdt.core.dom.NodeFinder;
28 import org.eclipse.jdt.core.dom.QualifiedName;
29 import org.eclipse.jdt.core.dom.SimpleName;
30 import org.eclipse.jdt.core.dom.StringLiteral;
31
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.ui.JavaUI;
36 import org.eclipse.jdt.ui.SharedASTProvider;
37
38
39
40 /**
41  * NLS hyperlink detector.
42  *
43  * @since 3.1
44  */
45 public class NLSKeyHyperlinkDetector extends AbstractHyperlinkDetector {
46
47
48         /*
49          * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
50          */
51         public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
52                 ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class);
53                 if (region == null || textEditor == null)
54                         return null;
55
56                 IEditorSite site= textEditor.getEditorSite();
57                 if (site == null)
58                         return null;
59
60                 ITypeRoot javaElement= getInputJavaElement(textEditor);
61                 if (javaElement == null)
62                         return null;
63
64                 CompilationUnit ast= SharedASTProvider.getAST(javaElement, SharedASTProvider.WAIT_NO, null);
65                 if (ast == null)
66                         return null;
67
68                 ASTNode node= NodeFinder.perform(ast, region.getOffset(), 1);
69                 if (!(node instanceof StringLiteral)  && !(node instanceof SimpleName))
70                         return null;
71
72                 if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY)
73                         return null;
74
75                 IRegion nlsKeyRegion= new Region(node.getStartPosition(), node.getLength());
76                 AccessorClassReference ref= NLSHintHelper.getAccessorClassReference(ast, nlsKeyRegion);
77                 if (ref == null)
78                         return null;
79                 String keyName= null;
80                 if (node instanceof StringLiteral) {
81                         keyName= ((StringLiteral)node).getLiteralValue();
82                 } else {
83                         keyName= ((SimpleName)node).getIdentifier();
84                 }
85                 if (keyName != null)
86                         return new IHyperlink[] {new NLSKeyHyperlink(nlsKeyRegion, keyName, ref, textEditor)};
87
88                 return null;
89         }
90
91         private ITypeRoot getInputJavaElement(ITextEditor editor) {
92                 return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
93         }
94
95 }