1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import com.ibm.icu.text.BreakIterator; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.io.Reader ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.List ; 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 contextId, Object [] 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 fUrl; 67 68 public JavaUIHelpResource(IJavaElement element, String url) { 69 fElement= element; 70 fUrl= url; 71 } 72 73 public String getHref() { 74 return fUrl; 75 } 76 77 public String getLabel() { 78 String 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 fText; 86 private String fTitle; 87 88 89 private static final boolean BUG_85719_FIXED= false; 91 92 public JavadocHelpContext(IContext context, Object [] elements) throws JavaModelException { 93 Assert.isNotNull(elements); 94 if (context instanceof IContext2) 95 fTitle= ((IContext2)context).getTitle(); 96 97 List helpResources= new ArrayList (); 98 99 String 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 (!ActionUtil.isOnBuildPath(element)) 105 continue; 106 107 if (BUG_85719_FIXED) { 109 if (javadocSummary == null) { 110 javadocSummary= retrieveText(element); 111 if (javadocSummary != null) { 112 String elementLabel= JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_DEFAULT); 113 114 javadocSummary= "<b>Javadoc for " + elementLabel + ":</b><br>" + javadocSummary; } 117 } else { 118 javadocSummary= ""; } 120 } 121 122 URL 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 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; else 162 fText= javadocSummary; 163 } 164 } 165 166 if (fText == null) 167 fText= ""; 169 } 170 171 private String getURLString(URL url) { 172 String 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); } 178 } 179 return location + "?noframes=true"; } 181 182 private boolean doesNotExist(URL url) { 183 if (url.getProtocol().equals("file")) { File file= new File (url.getFile()); 185 return !file.exists(); 186 } 187 return false; 188 } 189 190 private String retrieveText(IJavaElement elem) throws JavaModelException { 191 if (elem instanceof IMember) { 192 Reader reader= JavadocContentAccess.getHTMLContentReader((IMember)elem, true, true); 193 if (reader != null) 194 reader= new HTML2TextReader(reader, null); 195 if (reader != null) { 196 String str= getString(reader); 197 BreakIterator breakIterator= BreakIterator.getSentenceInstance(); 198 breakIterator.setText(str); 199 return str.substring(0, breakIterator.next()); 200 } 201 } 202 return ""; } 204 205 208 private static String getString(Reader reader) { 209 StringBuffer buf= new StringBuffer (); 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 e) { 216 return null; 217 } 218 return buf.toString(); 219 } 220 221 public IHelpResource[] getRelatedTopics() { 222 return fHelpResources; 223 } 224 225 public String getText() { 226 return fText; 227 } 228 229 public String getStyledText() { 230 return fText; 231 } 232 233 public String getCategory(IHelpResource topic) { 234 if (topic instanceof JavaUIHelpResource) 235 return JavaUIMessages.JavaUIHelpContext_javaHelpCategory_label; 236 237 return null; 238 } 239 240 244 public String getTitle() { 245 return fTitle; 246 } 247 248 } 249 250 | Popular Tags |