1 11 package org.eclipse.jdt.internal.ui.text.java.hover; 12 13 import java.io.Reader ; 14 import java.io.StringReader ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.widgets.Shell; 18 19 import org.eclipse.jface.internal.text.html.BrowserInformationControl; 20 import org.eclipse.jface.internal.text.html.HTMLPrinter; 21 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; 22 23 import org.eclipse.jface.text.AbstractReusableInformationControlCreator; 24 import org.eclipse.jface.text.DefaultInformationControl; 25 import org.eclipse.jface.text.IInformationControl; 26 import org.eclipse.jface.text.IInformationControlCreator; 27 import org.eclipse.jface.text.IInformationControlExtension4; 28 import org.eclipse.jface.text.ITextHoverExtension; 29 import org.eclipse.jface.text.information.IInformationProviderExtension2; 30 31 import org.eclipse.ui.editors.text.EditorsUI; 32 33 import org.eclipse.jdt.core.IJavaElement; 34 import org.eclipse.jdt.core.IMember; 35 import org.eclipse.jdt.core.IOpenable; 36 import org.eclipse.jdt.core.IPackageFragmentRoot; 37 import org.eclipse.jdt.core.JavaModelException; 38 39 import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations; 40 41 import org.eclipse.jdt.ui.JavaElementLabels; 42 import org.eclipse.jdt.ui.JavadocContentAccess; 43 44 import org.eclipse.jdt.internal.ui.JavaPlugin; 45 46 51 public class JavadocHover extends AbstractJavaEditorTextHover implements IInformationProviderExtension2, ITextHoverExtension { 52 53 54 59 private static final class PresenterControlCreator extends AbstractReusableInformationControlCreator { 60 63 public IInformationControl doCreateInformationControl(Shell parent) { 64 int shellStyle= SWT.RESIZE | SWT.TOOL; 65 int style= SWT.V_SCROLL | SWT.H_SCROLL; 66 if (BrowserInformationControl.isAvailable(parent)) 67 return new BrowserInformationControl(parent, shellStyle, style); 68 else 69 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false)); 70 } 71 } 72 73 74 79 private static final class HoverControlCreator extends AbstractReusableInformationControlCreator { 80 83 public IInformationControl doCreateInformationControl(Shell parent) { 84 if (BrowserInformationControl.isAvailable(parent)) 85 return new BrowserInformationControl(parent, SWT.TOOL | SWT.NO_TRIM, SWT.NONE, EditorsUI.getTooltipAffordanceString()); 86 else 87 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true), EditorsUI.getTooltipAffordanceString()); 88 } 89 90 93 public boolean canReuse(IInformationControl control) { 94 if (!super.canReuse(control)) 95 return false; 96 97 if (control instanceof IInformationControlExtension4) 98 ((IInformationControlExtension4)control).setStatusText(EditorsUI.getTooltipAffordanceString()); 99 100 return true; 101 } 102 } 103 104 private final long LABEL_FLAGS= JavaElementLabels.ALL_FULLY_QUALIFIED 105 | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS 106 | JavaElementLabels.F_PRE_TYPE_SIGNATURE | JavaElementLabels.M_PRE_TYPE_PARAMETERS | JavaElementLabels.T_TYPE_PARAMETERS 107 | JavaElementLabels.USE_RESOLVED; 108 private final long LOCAL_VARIABLE_FLAGS= LABEL_FLAGS & ~JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.F_POST_QUALIFIED; 109 110 111 116 private IInformationControlCreator fHoverControlCreator; 117 122 private IInformationControlCreator fPresenterControlCreator; 123 124 125 129 public IInformationControlCreator getInformationPresenterControlCreator() { 130 if (fPresenterControlCreator == null) 131 fPresenterControlCreator= new PresenterControlCreator(); 132 return fPresenterControlCreator; 133 } 134 135 139 public IInformationControlCreator getHoverControlCreator() { 140 if (fHoverControlCreator == null) 141 fHoverControlCreator= new HoverControlCreator(); 142 return fHoverControlCreator; 143 } 144 145 148 protected String getHoverInfo(IJavaElement[] result) { 149 150 StringBuffer buffer= new StringBuffer (); 151 int nResults= result.length; 152 if (nResults == 0) 153 return null; 154 155 boolean hasContents= false; 156 if (nResults > 1) { 157 158 for (int i= 0; i < result.length; i++) { 159 HTMLPrinter.startBulletList(buffer); 160 IJavaElement curr= result[i]; 161 if (curr instanceof IMember || curr.getElementType() == IJavaElement.LOCAL_VARIABLE) { 162 HTMLPrinter.addBullet(buffer, getInfoText(curr)); 163 hasContents= true; 164 } 165 HTMLPrinter.endBulletList(buffer); 166 } 167 168 } else { 169 170 IJavaElement curr= result[0]; 171 if (curr instanceof IMember) { 172 IMember member= (IMember) curr; 173 HTMLPrinter.addSmallHeader(buffer, getInfoText(member)); 174 Reader reader; 175 try { 176 reader= JavadocContentAccess.getHTMLContentReader(member, true, true); 177 178 if (reader == null && member.isBinary()) { 180 boolean hasAttachedJavadoc= JavaDocLocations.getJavadocBaseLocation(member) != null; 181 IPackageFragmentRoot root= (IPackageFragmentRoot)member.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 182 boolean hasAttachedSource= root != null && root.getSourceAttachmentPath() != null; 183 IOpenable openable= member.getOpenable(); 184 boolean hasSource= openable.getBuffer() != null; 185 186 if (!hasAttachedSource && !hasAttachedJavadoc) 187 reader= new StringReader (JavaHoverMessages.JavadocHover_noAttachments); 188 else if (!hasAttachedJavadoc && !hasSource) 189 reader= new StringReader (JavaHoverMessages.JavadocHover_noAttachedJavadoc); 190 else if (!hasAttachedSource) 191 reader= new StringReader (JavaHoverMessages.JavadocHover_noAttachedSource); 192 else if (!hasSource) 193 reader= new StringReader (JavaHoverMessages.JavadocHover_noInformation); 194 } 195 196 } catch (JavaModelException ex) { 197 reader= new StringReader (JavaHoverMessages.JavadocHover_error_gettingJavadoc); 198 JavaPlugin.log(ex.getStatus()); 199 } 200 201 if (reader != null) { 202 HTMLPrinter.addParagraph(buffer, reader); 203 } 204 hasContents= true; 205 } else if (curr.getElementType() == IJavaElement.LOCAL_VARIABLE || curr.getElementType() == IJavaElement.TYPE_PARAMETER) { 206 HTMLPrinter.addSmallHeader(buffer, getInfoText(curr)); 207 hasContents= true; 208 } 209 } 210 211 if (!hasContents) 212 return null; 213 214 if (buffer.length() > 0) { 215 HTMLPrinter.insertPageProlog(buffer, 0, getStyleSheet()); 216 HTMLPrinter.addPageEpilog(buffer); 217 return buffer.toString(); 218 } 219 220 return null; 221 } 222 223 private String getInfoText(IJavaElement member) { 224 long flags= member.getElementType() == IJavaElement.LOCAL_VARIABLE ? LOCAL_VARIABLE_FLAGS : LABEL_FLAGS; 225 String label= JavaElementLabels.getElementLabel(member, flags); 226 StringBuffer buf= new StringBuffer (); 227 for (int i= 0; i < label.length(); i++) { 228 char ch= label.charAt(i); 229 if (ch == '<') { 230 buf.append("<"); } else if (ch == '>') { 232 buf.append(">"); } else { 234 buf.append(ch); 235 } 236 } 237 return buf.toString(); 238 } 239 240 } 241 | Popular Tags |