KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > hover > NLSStringHover


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 package org.eclipse.jdt.internal.ui.text.java.hover;
12
13 import java.util.Properties JavaDoc;
14
15 import org.eclipse.core.resources.IStorage;
16
17 import org.eclipse.jface.internal.text.html.HTMLPrinter;
18
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.Region;
22
23 import org.eclipse.ui.IEditorInput;
24
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.core.dom.ASTNode;
28 import org.eclipse.jdt.core.dom.CompilationUnit;
29 import org.eclipse.jdt.core.dom.QualifiedName;
30 import org.eclipse.jdt.core.dom.SimpleName;
31 import org.eclipse.jdt.core.dom.StringLiteral;
32
33 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
34 import org.eclipse.jdt.internal.corext.refactoring.nls.AccessorClassReference;
35 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSHintHelper;
36
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
39 import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditor;
40 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
41 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
42 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
43
44 /**
45  * Provides externalized string as hover info for NLS key.
46  *
47  * @since 3.1
48  */

49 public class NLSStringHover extends AbstractJavaEditorTextHover {
50
51
52     /*
53      * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractJavaEditorTextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
54      */

55     public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
56         if (!(getEditor() instanceof JavaEditor))
57             return null;
58
59         IJavaElement je= getEditorInputJavaElement();
60         if (je == null)
61             return null;
62
63         // Never wait for an AST in UI thread.
64
CompilationUnit ast= JavaPlugin.getDefault().getASTProvider().getAST(je, ASTProvider.WAIT_NO, null);
65         if (ast == null)
66             return null;
67
68         ASTNode node= NodeFinder.perform(ast, offset, 1);
69         if (node instanceof StringLiteral) {
70             StringLiteral stringLiteral= (StringLiteral)node;
71             return new Region(stringLiteral.getStartPosition(), stringLiteral.getLength());
72         } else if (node instanceof SimpleName) {
73             SimpleName simpleName= (SimpleName)node;
74             return new Region(simpleName.getStartPosition(), simpleName.getLength());
75         }
76
77         return null;
78     }
79
80     /*
81      * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractJavaEditorTextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
82      */

83     public String JavaDoc getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
84         if (!(getEditor() instanceof JavaEditor))
85             return null;
86
87         IJavaElement je= getEditorInputJavaElement();
88         if (je == null)
89             return null;
90
91         CompilationUnit ast= JavaPlugin.getDefault().getASTProvider().getAST(je, ASTProvider.WAIT_ACTIVE_ONLY, null);
92         if (ast == null)
93             return null;
94
95         ASTNode node= NodeFinder.perform(ast, hoverRegion.getOffset(), hoverRegion.getLength());
96         if (!(node instanceof StringLiteral) && !(node instanceof SimpleName))
97             return null;
98         
99         if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY)
100             return null;
101
102         AccessorClassReference ref= NLSHintHelper.getAccessorClassReference(ast, hoverRegion);
103         if (ref == null)
104             return null;
105
106         IStorage propertiesFile;
107         try {
108             propertiesFile= NLSHintHelper.getResourceBundle(je.getJavaProject(), ref);
109             if (propertiesFile == null)
110                 return toHtml(JavaHoverMessages.NLSStringHover_NLSStringHover_PropertiesFileNotDetectedWarning, ""); //$NON-NLS-1$
111
} catch (JavaModelException ex) {
112             return null;
113         }
114
115         final String JavaDoc propertiesFileName= propertiesFile.getName();
116         Properties JavaDoc properties= NLSHintHelper.getProperties(propertiesFile);
117         if (properties == null)
118             return null;
119         if (properties.isEmpty())
120             return toHtml(propertiesFileName, JavaHoverMessages.NLSStringHover_NLSStringHover_missingKeyWarning);
121
122         String JavaDoc identifier= null;
123         if (node instanceof StringLiteral) {
124             identifier= ((StringLiteral)node).getLiteralValue();
125         } else {
126             identifier= ((SimpleName)node).getIdentifier();
127         }
128         if (identifier == null)
129             return null;
130         
131         String JavaDoc value= properties.getProperty(identifier, null);
132         if (value != null)
133             value= HTMLPrinter.convertToHTMLContent(value);
134         else
135             value= JavaHoverMessages.NLSStringHover_NLSStringHover_missingKeyWarning;
136
137         return toHtml(propertiesFileName, value);
138     }
139
140     private String JavaDoc toHtml(String JavaDoc header, String JavaDoc string) {
141
142         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
143
144         HTMLPrinter.addSmallHeader(buffer, header);
145         HTMLPrinter.addParagraph(buffer, string);
146         HTMLPrinter.insertPageProlog(buffer, 0);
147         HTMLPrinter.addPageEpilog(buffer);
148         return buffer.toString();
149     }
150
151     private IJavaElement getEditorInputJavaElement() {
152         if (getEditor() instanceof CompilationUnitEditor)
153             return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(getEditor().getEditorInput());
154         else if (getEditor() instanceof ClassFileEditor) {
155             IEditorInput editorInput= getEditor().getEditorInput();
156             if (editorInput instanceof IClassFileEditorInput)
157                 return ((IClassFileEditorInput)editorInput).getClassFile();
158
159         }
160         return null;
161     }
162
163 }
164
Popular Tags