1 11 package org.eclipse.jdt.internal.ui.text.java.hover; 12 13 import java.util.Properties ; 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 49 public class NLSStringHover extends AbstractJavaEditorTextHover { 50 51 52 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 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 83 public String 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, ""); } catch (JavaModelException ex) { 112 return null; 113 } 114 115 final String propertiesFileName= propertiesFile.getName(); 116 Properties 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 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 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 toHtml(String header, String string) { 141 142 StringBuffer buffer= new StringBuffer (); 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 |