KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.internal.ui.text.java.hover;
12
13 import java.io.Reader JavaDoc;
14 import java.io.StringReader JavaDoc;
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 /**
47  * Provides Javadoc as hover info for Java elements.
48  *
49  * @since 2.1
50  */

51 public class JavadocHover extends AbstractJavaEditorTextHover implements IInformationProviderExtension2, ITextHoverExtension {
52
53     
54     /**
55      * Presenter control creator.
56      *
57      * @since 3.3
58      */

59     private static final class PresenterControlCreator extends AbstractReusableInformationControlCreator {
60         /*
61          * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
62          */

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     /**
75      * Hover control creator.
76      *
77      * @since 3.3
78      */

79     private static final class HoverControlCreator extends AbstractReusableInformationControlCreator {
80         /*
81          * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
82          */

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         /*
91          * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#canReuse(org.eclipse.jface.text.IInformationControl)
92          */

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     /**
112      * The hover control creator.
113      *
114      * @since 3.2
115      */

116     private IInformationControlCreator fHoverControlCreator;
117     /**
118      * The presentation control creator.
119      *
120      * @since 3.2
121      */

122     private IInformationControlCreator fPresenterControlCreator;
123
124
125     /*
126      * @see IInformationProviderExtension2#getInformationPresenterControlCreator()
127      * @since 3.1
128      */

129     public IInformationControlCreator getInformationPresenterControlCreator() {
130         if (fPresenterControlCreator == null)
131             fPresenterControlCreator= new PresenterControlCreator();
132         return fPresenterControlCreator;
133     }
134
135     /*
136      * @see ITextHoverExtension#getHoverControlCreator()
137      * @since 3.2
138      */

139     public IInformationControlCreator getHoverControlCreator() {
140         if (fHoverControlCreator == null)
141             fHoverControlCreator= new HoverControlCreator();
142         return fHoverControlCreator;
143     }
144
145     /*
146      * @see JavaElementHover
147      */

148     protected String JavaDoc getHoverInfo(IJavaElement[] result) {
149
150         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc reader;
175                 try {
176                     reader= JavadocContentAccess.getHTMLContentReader(member, true, true);
177                     
178                     // Provide hint why there's no Javadoc
179
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 JavaDoc(JavaHoverMessages.JavadocHover_noAttachments);
188                         else if (!hasAttachedJavadoc && !hasSource)
189                             reader= new StringReader JavaDoc(JavaHoverMessages.JavadocHover_noAttachedJavadoc);
190                         else if (!hasAttachedSource)
191                             reader= new StringReader JavaDoc(JavaHoverMessages.JavadocHover_noAttachedSource);
192                         else if (!hasSource)
193                             reader= new StringReader JavaDoc(JavaHoverMessages.JavadocHover_noInformation);
194                     }
195                     
196                 } catch (JavaModelException ex) {
197                     reader= new StringReader JavaDoc(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 JavaDoc getInfoText(IJavaElement member) {
224         long flags= member.getElementType() == IJavaElement.LOCAL_VARIABLE ? LOCAL_VARIABLE_FLAGS : LABEL_FLAGS;
225         String JavaDoc label= JavaElementLabels.getElementLabel(member, flags);
226         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
227         for (int i= 0; i < label.length(); i++) {
228             char ch= label.charAt(i);
229             if (ch == '<') {
230                 buf.append("&lt;"); //$NON-NLS-1$
231
} else if (ch == '>') {
232                 buf.append("&gt;"); //$NON-NLS-1$
233
} else {
234                 buf.append(ch);
235             }
236         }
237         return buf.toString();
238     }
239
240 }
241
Popular Tags