KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > JavadocHelpContext


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.util;
12
13 import com.ibm.icu.text.BreakIterator;
14
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.Reader JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24
25 import org.eclipse.help.HelpSystem;
26 import org.eclipse.help.IContext;
27 import org.eclipse.help.IContext2;
28 import org.eclipse.help.IHelpResource;
29
30 import org.eclipse.jface.internal.text.html.HTML2TextReader;
31
32 import org.eclipse.ui.PlatformUI;
33
34 import org.eclipse.jdt.core.IJavaElement;
35 import org.eclipse.jdt.core.IMember;
36 import org.eclipse.jdt.core.IPackageFragmentRoot;
37 import org.eclipse.jdt.core.JavaModelException;
38
39 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
40 import org.eclipse.jdt.internal.corext.util.Messages;
41
42 import org.eclipse.jdt.ui.JavaElementLabels;
43 import org.eclipse.jdt.ui.JavaUI;
44 import org.eclipse.jdt.ui.JavadocContentAccess;
45
46 import org.eclipse.jdt.internal.ui.JavaUIMessages;
47 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
48
49 public class JavadocHelpContext implements IContext2 {
50     
51     
52     public static void displayHelp(String JavaDoc contextId, Object JavaDoc[] selected) throws CoreException {
53         IContext context= HelpSystem.getContext(contextId);
54         if (context != null) {
55             if (selected != null && selected.length > 0) {
56                 context= new JavadocHelpContext(context, selected);
57             }
58             PlatformUI.getWorkbench().getHelpSystem().displayHelp(context);
59         }
60     }
61     
62     
63     private static class JavaUIHelpResource implements IHelpResource {
64
65         private IJavaElement fElement;
66         private String JavaDoc fUrl;
67
68         public JavaUIHelpResource(IJavaElement element, String JavaDoc url) {
69             fElement= element;
70             fUrl= url;
71         }
72
73         public String JavaDoc getHref() {
74             return fUrl;
75         }
76
77         public String JavaDoc getLabel() {
78             String JavaDoc label= JavaElementLabels.getTextLabel(fElement, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_FULLY_QUALIFIED);
79             return Messages.format(JavaUIMessages.JavaUIHelp_link_label, label);
80         }
81     }
82     
83
84     private IHelpResource[] fHelpResources;
85     private String JavaDoc fText;
86     private String JavaDoc fTitle;
87     
88     
89     // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=85719
90
private static final boolean BUG_85719_FIXED= false;
91
92     public JavadocHelpContext(IContext context, Object JavaDoc[] elements) throws JavaModelException {
93         Assert.isNotNull(elements);
94         if (context instanceof IContext2)
95             fTitle= ((IContext2)context).getTitle();
96
97         List JavaDoc helpResources= new ArrayList JavaDoc();
98
99         String JavaDoc javadocSummary= null;
100         for (int i= 0; i < elements.length; i++) {
101             if (elements[i] instanceof IJavaElement) {
102                 IJavaElement element= (IJavaElement) elements[i];
103                 // if element isn't on the build path skip it
104
if (!ActionUtil.isOnBuildPath(element))
105                     continue;
106                 
107                 // Create Javadoc summary
108
if (BUG_85719_FIXED) {
109                     if (javadocSummary == null) {
110                         javadocSummary= retrieveText(element);
111                         if (javadocSummary != null) {
112                             String JavaDoc elementLabel= JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_DEFAULT);
113                             
114                             // FIXME: needs to be NLSed once the code becomes active
115
javadocSummary= "<b>Javadoc for " + elementLabel + ":</b><br>" + javadocSummary; //$NON-NLS-1$//$NON-NLS-2$
116
}
117                     } else {
118                         javadocSummary= ""; // no Javadoc summary for multiple selection //$NON-NLS-1$
119
}
120                 }
121                 
122                 URL JavaDoc url= JavaUI.getJavadocLocation(element, true);
123                 if (url == null || doesNotExist(url)) {
124                     IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
125                     if (root != null) {
126                         url= JavaUI.getJavadocBaseLocation(element);
127                         if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
128                             element= element.getJavaProject();
129                         } else {
130                             element= root;
131                         }
132                         url= JavaUI.getJavadocLocation(element, false);
133                     }
134                 }
135                 if (url != null) {
136                     IHelpResource javaResource= new JavaUIHelpResource(element, getURLString(url));
137                     helpResources.add(javaResource);
138                 }
139             }
140         }
141
142         // Add static help topics
143
if (context != null) {
144             IHelpResource[] resources= context.getRelatedTopics();
145             if (resources != null) {
146                 for (int j= 0; j < resources.length; j++) {
147                     helpResources.add(resources[j]);
148                 }
149             }
150         }
151         
152         fHelpResources= (IHelpResource[]) helpResources.toArray(new IHelpResource[helpResources.size()]);
153
154         if (context != null)
155             fText= context.getText();
156         
157         if (BUG_85719_FIXED) {
158             if (javadocSummary != null && javadocSummary.length() > 0) {
159                 if (fText != null)
160                     fText= context.getText() + "<br><br>" + javadocSummary; //$NON-NLS-1$
161
else
162                     fText= javadocSummary;
163             }
164         }
165         
166         if (fText == null)
167             fText= ""; //$NON-NLS-1$
168

169     }
170     
171     private String JavaDoc getURLString(URL JavaDoc url) {
172         String JavaDoc location= url.toExternalForm();
173         if (url.getRef() != null) {
174             int anchorIdx= location.lastIndexOf('#');
175             if (anchorIdx != -1) {
176                 return location.substring(0, anchorIdx) + "?noframes=true" + location.substring(anchorIdx); //$NON-NLS-1$
177
}
178         }
179         return location + "?noframes=true"; //$NON-NLS-1$
180
}
181
182     private boolean doesNotExist(URL JavaDoc url) {
183         if (url.getProtocol().equals("file")) { //$NON-NLS-1$
184
File JavaDoc file= new File JavaDoc(url.getFile());
185             return !file.exists();
186         }
187         return false;
188     }
189
190     private String JavaDoc retrieveText(IJavaElement elem) throws JavaModelException {
191         if (elem instanceof IMember) {
192             Reader JavaDoc reader= JavadocContentAccess.getHTMLContentReader((IMember)elem, true, true);
193             if (reader != null)
194                 reader= new HTML2TextReader(reader, null);
195             if (reader != null) {
196                 String JavaDoc str= getString(reader);
197                 BreakIterator breakIterator= BreakIterator.getSentenceInstance();
198                 breakIterator.setText(str);
199                 return str.substring(0, breakIterator.next());
200             }
201         }
202         return ""; //$NON-NLS-1$
203
}
204     
205     /**
206      * Gets the reader content as a String
207      */

208     private static String JavaDoc getString(Reader JavaDoc reader) {
209         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
210         char[] buffer= new char[1024];
211         int count;
212         try {
213             while ((count= reader.read(buffer)) != -1)
214                 buf.append(buffer, 0, count);
215         } catch (IOException JavaDoc e) {
216             return null;
217         }
218         return buf.toString();
219     }
220
221     public IHelpResource[] getRelatedTopics() {
222         return fHelpResources;
223     }
224
225     public String JavaDoc getText() {
226         return fText;
227     }
228
229     public String JavaDoc getStyledText() {
230         return fText;
231     }
232
233     public String JavaDoc getCategory(IHelpResource topic) {
234         if (topic instanceof JavaUIHelpResource)
235             return JavaUIMessages.JavaUIHelpContext_javaHelpCategory_label;
236
237         return null;
238     }
239     
240     /*
241      * @see org.eclipse.help.IContext2#getTitle()
242      * @since 3.1
243      */

244     public String JavaDoc getTitle() {
245         return fTitle;
246     }
247
248 }
249
250
Popular Tags