KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.io.StringReader JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMember;
22 import org.eclipse.jdt.core.JavaModelException;
23
24 import org.eclipse.jdt.ui.JavadocContentAccess;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDoc2HTMLTextReader;
28
29
30 public class ProposalInfo {
31
32     private boolean fJavadocResolved= false;
33     private String JavaDoc fJavadoc= null;
34
35     protected IJavaElement fElement;
36
37     public ProposalInfo(IMember member) {
38         fElement= member;
39     }
40     
41     protected ProposalInfo() {
42         fElement= null;
43     }
44
45     public IJavaElement getJavaElement() throws JavaModelException {
46         return fElement;
47     }
48
49     /**
50      * Gets the text for this proposal info formatted as HTML, or
51      * <code>null</code> if no text is available.
52      *
53      * @param monitor a progress monitor
54      * @return the additional info text
55      */

56     public final String JavaDoc getInfo(IProgressMonitor monitor) {
57         if (!fJavadocResolved) {
58             fJavadocResolved= true;
59             fJavadoc= computeInfo(monitor);
60         }
61         return fJavadoc;
62     }
63
64     /**
65      * Gets the text for this proposal info formatted as HTML, or
66      * <code>null</code> if no text is available.
67      *
68      * @param monitor a progress monitor
69      * @return the additional info text
70      */

71     private String JavaDoc computeInfo(IProgressMonitor monitor) {
72         try {
73             final IJavaElement javaElement= getJavaElement();
74             if (javaElement instanceof IMember) {
75                 IMember member= (IMember) javaElement;
76                 return extractJavadoc(member, monitor);
77             }
78         } catch (JavaModelException e) {
79             JavaPlugin.log(e);
80         } catch (IOException JavaDoc e) {
81             JavaPlugin.log(e);
82         }
83         return null;
84     }
85
86     /**
87      * Extracts the javadoc for the given <code>IMember</code> and returns it
88      * as HTML.
89      *
90      * @param member the member to get the documentation for
91      * @param monitor a progress monitor
92      * @return the javadoc for <code>member</code> or <code>null</code> if
93      * it is not available
94      * @throws JavaModelException if accessing the javadoc fails
95      * @throws IOException if reading the javadoc fails
96      */

97     private String JavaDoc extractJavadoc(IMember member, IProgressMonitor monitor) throws JavaModelException, IOException JavaDoc {
98         if (member != null) {
99             Reader JavaDoc reader= getHTMLContentReader(member, monitor);
100             if (reader != null)
101                 return getString(reader);
102         }
103         return null;
104     }
105
106     private Reader JavaDoc getHTMLContentReader(IMember member, IProgressMonitor monitor) throws JavaModelException {
107         Reader JavaDoc contentReader= JavadocContentAccess.getContentReader(member, true);
108         if (contentReader != null)
109             return new JavaDoc2HTMLTextReader(contentReader);
110         
111         if (true && member.getOpenable().getBuffer() == null) { // only if no source available
112
String JavaDoc s= member.getAttachedJavadoc(monitor);
113             if (s != null)
114                 return new StringReader JavaDoc(s);
115         }
116         return null;
117     }
118     
119     /**
120      * Gets the reader content as a String
121      */

122     private static String JavaDoc getString(Reader JavaDoc reader) {
123         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
124         char[] buffer= new char[1024];
125         int count;
126         try {
127             while ((count= reader.read(buffer)) != -1)
128                 buf.append(buffer, 0, count);
129         } catch (IOException JavaDoc e) {
130             return null;
131         }
132         return buf.toString();
133     }
134 }
135
Popular Tags