KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.ui.text.java.hover;
13
14 import java.io.IOException JavaDoc;
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 /**
45  * Provides source as hover info for Java elements.
46  */

47 public class JavaSourceHover extends AbstractJavaEditorTextHover implements ITextHoverExtension, IInformationProviderExtension2 {
48
49     /*
50      * @see JavaElementHover
51      */

52     protected String JavaDoc 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 JavaDoc source= ((ISourceReference) curr).getSource();
62                 if (source == null)
63                     return null;
64
65                 source= removeLeadingComments(source);
66                 String JavaDoc delim= StubUtility.getLineDelimiterUsed(result[0]);
67
68                 String JavaDoc[] sourceLines= Strings.convertIntoLines(source);
69                 String JavaDoc firstLine= sourceLines[0];
70                 if (!Character.isWhitespace(firstLine.charAt(0)))
71                     sourceLines[0]= ""; //$NON-NLS-1$
72
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 JavaDoc removeLeadingComments(String JavaDoc 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 JavaDoc ex) {
101             i= 0;
102         } finally {
103             try {
104                 if (reader != null)
105                     reader.close();
106             } catch (IOException JavaDoc ex) {
107                 JavaPlugin.log(ex);
108             }
109         }
110
111         if (i < 0)
112             return source;
113         return source.substring(i);
114     }
115
116     /*
117      * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
118      * @since 3.0
119      */

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     /*
133      * @see IInformationProviderExtension2#getInformationPresenterControlCreator()
134      * @since 3.0
135      */

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