1 11 package org.eclipse.jdt.internal.ui.text.java.hover; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStreamReader ; 16 import java.net.URL ; 17 18 import org.eclipse.core.runtime.FileLocator; 19 import org.eclipse.core.runtime.Platform; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.FontData; 23 import org.eclipse.swt.widgets.Shell; 24 25 import org.eclipse.jface.internal.text.html.HTMLPrinter; 26 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; 27 import org.eclipse.jface.resource.JFaceResources; 28 29 import org.eclipse.jface.text.DefaultInformationControl; 30 import org.eclipse.jface.text.IInformationControl; 31 import org.eclipse.jface.text.IInformationControlCreator; 32 import org.eclipse.jface.text.IRegion; 33 import org.eclipse.jface.text.ITextHoverExtension; 34 import org.eclipse.jface.text.ITextViewer; 35 36 import org.eclipse.ui.IEditorInput; 37 import org.eclipse.ui.IEditorPart; 38 39 import org.eclipse.ui.editors.text.EditorsUI; 40 41 import org.eclipse.jdt.core.ICodeAssist; 42 import org.eclipse.jdt.core.IJavaElement; 43 import org.eclipse.jdt.core.JavaModelException; 44 45 import org.eclipse.jdt.ui.PreferenceConstants; 46 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover; 47 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 50 import org.eclipse.jdt.internal.ui.javaeditor.WorkingCopyManager; 51 import org.eclipse.jdt.internal.ui.text.JavaWordFinder; 52 53 import org.osgi.framework.Bundle; 54 55 60 public abstract class AbstractJavaEditorTextHover implements IJavaEditorTextHover, ITextHoverExtension { 61 65 private static String fgStyleSheet; 66 private IEditorPart fEditor; 67 68 71 public void setEditor(IEditorPart editor) { 72 fEditor= editor; 73 } 74 75 protected IEditorPart getEditor() { 76 return fEditor; 77 } 78 79 protected ICodeAssist getCodeAssist() { 80 if (fEditor != null) { 81 IEditorInput input= fEditor.getEditorInput(); 82 if (input instanceof IClassFileEditorInput) { 83 IClassFileEditorInput cfeInput= (IClassFileEditorInput) input; 84 return cfeInput.getClassFile(); 85 } 86 87 WorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager(); 88 return manager.getWorkingCopy(input, false); 89 } 90 91 return null; 92 } 93 94 97 public IRegion getHoverRegion(ITextViewer textViewer, int offset) { 98 return JavaWordFinder.findWord(textViewer.getDocument(), offset); 99 } 100 101 104 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { 105 106 111 if (hoverRegion.getLength() == 0) 112 return null; 113 114 ICodeAssist resolve= getCodeAssist(); 115 if (resolve != null) { 116 try { 117 IJavaElement[] result= resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength()); 118 if (result == null) 119 return null; 120 121 int nResults= result.length; 122 if (nResults == 0) 123 return null; 124 125 return getHoverInfo(result); 126 127 } catch (JavaModelException x) { 128 return null; 129 } 130 } 131 return null; 132 } 133 134 141 protected String getHoverInfo(IJavaElement[] javaElements) { 142 return null; 143 } 144 145 149 public IInformationControlCreator getHoverControlCreator() { 150 return new IInformationControlCreator() { 151 public IInformationControl createInformationControl(Shell parent) { 152 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), EditorsUI.getTooltipAffordanceString()); 153 } 154 }; 155 } 156 157 protected static String getStyleSheet() { 158 if (fgStyleSheet == null) 159 fgStyleSheet= loadStyleSheet(); 160 String css= fgStyleSheet; 161 if (css != null) { 162 FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0]; 163 css= HTMLPrinter.convertTopLevelFont(css, fontData); 164 } 165 166 return css; 167 } 168 169 private static String loadStyleSheet() { 170 Bundle bundle= Platform.getBundle(JavaPlugin.getPluginId()); 171 URL styleSheetURL= bundle.getEntry("/JavadocHoverStyleSheet.css"); if (styleSheetURL != null) { 173 try { 174 styleSheetURL= FileLocator.toFileURL(styleSheetURL); 175 BufferedReader reader= new BufferedReader (new InputStreamReader (styleSheetURL.openStream())); 176 StringBuffer buffer= new StringBuffer (200); 177 String line= reader.readLine(); 178 while (line != null) { 179 buffer.append(line); 180 buffer.append('\n'); 181 line= reader.readLine(); 182 } 183 return buffer.toString(); 184 } catch (IOException ex) { 185 JavaPlugin.log(ex); 186 return ""; } 188 } 189 return null; 190 } 191 } 192 | Popular Tags |