1 11 12 package org.eclipse.jdt.internal.ui.text.java.hover; 13 14 import java.io.IOException ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.widgets.Shell; 18 19 import org.eclipse.jface.text.Document; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.IInformationControl; 22 import org.eclipse.jface.text.IInformationControlCreator; 23 import org.eclipse.jface.text.ITextHoverExtension; 24 import org.eclipse.jface.text.information.IInformationProviderExtension2; 25 26 import org.eclipse.ui.IEditorPart; 27 import org.eclipse.ui.part.IWorkbenchPartOrientation; 28 29 import org.eclipse.ui.editors.text.EditorsUI; 30 31 import org.eclipse.jdt.core.IJavaElement; 32 import org.eclipse.jdt.core.ILocalVariable; 33 import org.eclipse.jdt.core.IMember; 34 import org.eclipse.jdt.core.ISourceReference; 35 import org.eclipse.jdt.core.ITypeParameter; 36 import org.eclipse.jdt.core.JavaModelException; 37 38 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 39 import org.eclipse.jdt.internal.corext.util.Strings; 40 41 import org.eclipse.jdt.internal.ui.JavaPlugin; 42 import org.eclipse.jdt.internal.ui.text.JavaCodeReader; 43 44 47 public class JavaSourceHover extends AbstractJavaEditorTextHover implements ITextHoverExtension, IInformationProviderExtension2 { 48 49 52 protected String getHoverInfo(IJavaElement[] result) { 53 int nResults= result.length; 54 55 if (nResults > 1) 56 return null; 57 58 IJavaElement curr= result[0]; 59 if ((curr instanceof IMember || curr instanceof ILocalVariable || curr instanceof ITypeParameter) && curr instanceof ISourceReference) { 60 try { 61 String source= ((ISourceReference) curr).getSource(); 62 if (source == null) 63 return null; 64 65 source= removeLeadingComments(source); 66 String delim= StubUtility.getLineDelimiterUsed(result[0]); 67 68 String [] sourceLines= Strings.convertIntoLines(source); 69 String firstLine= sourceLines[0]; 70 if (!Character.isWhitespace(firstLine.charAt(0))) 71 sourceLines[0]= ""; Strings.trimIndentation(sourceLines, curr.getJavaProject()); 73 74 if (!Character.isWhitespace(firstLine.charAt(0))) 75 sourceLines[0]= firstLine; 76 77 source= Strings.concatenate(sourceLines, delim); 78 79 return source; 80 81 } catch (JavaModelException ex) { 82 } 83 } 84 85 return null; 86 } 87 88 private String removeLeadingComments(String source) { 89 JavaCodeReader reader= new JavaCodeReader(); 90 IDocument document= new Document(source); 91 int i; 92 try { 93 reader.configureForwardReader(document, 0, document.getLength(), true, false); 94 int c= reader.read(); 95 while (c != -1 && (c == '\r' || c == '\n')) { 96 c= reader.read(); 97 } 98 i= reader.getOffset(); 99 reader.close(); 100 } catch (IOException ex) { 101 i= 0; 102 } finally { 103 try { 104 if (reader != null) 105 reader.close(); 106 } catch (IOException ex) { 107 JavaPlugin.log(ex); 108 } 109 } 110 111 if (i < 0) 112 return source; 113 return source.substring(i); 114 } 115 116 120 public IInformationControlCreator getHoverControlCreator() { 121 return new IInformationControlCreator() { 122 public IInformationControl createInformationControl(Shell parent) { 123 IEditorPart editor= getEditor(); 124 int shellStyle= SWT.TOOL | SWT.NO_TRIM; 125 if (editor instanceof IWorkbenchPartOrientation) 126 shellStyle |= ((IWorkbenchPartOrientation)editor).getOrientation(); 127 return new SourceViewerInformationControl(parent, shellStyle, SWT.NONE, EditorsUI.getTooltipAffordanceString()); 128 } 129 }; 130 } 131 132 136 public IInformationControlCreator getInformationPresenterControlCreator() { 137 return new IInformationControlCreator() { 138 public IInformationControl createInformationControl(Shell parent) { 139 int style= SWT.V_SCROLL | SWT.H_SCROLL; 140 int shellStyle= SWT.RESIZE | SWT.TOOL; 141 IEditorPart editor= getEditor(); 142 if (editor instanceof IWorkbenchPartOrientation) 143 shellStyle |= ((IWorkbenchPartOrientation)editor).getOrientation(); 144 return new SourceViewerInformationControl(parent, shellStyle, style); 145 } 146 }; 147 } 148 } 149 | Popular Tags |