001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.util;
011:
012: import com.ibm.icu.text.BreakIterator;
013:
014: import java.io.File;
015: import java.io.IOException;
016: import java.io.Reader;
017: import java.net.URL;
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.eclipse.core.runtime.Assert;
022: import org.eclipse.core.runtime.CoreException;
023:
024: import org.eclipse.help.HelpSystem;
025: import org.eclipse.help.IContext;
026: import org.eclipse.help.IContext2;
027: import org.eclipse.help.IHelpResource;
028:
029: import org.eclipse.jface.internal.text.html.HTML2TextReader;
030:
031: import org.eclipse.ui.PlatformUI;
032:
033: import org.eclipse.jdt.core.IJavaElement;
034: import org.eclipse.jdt.core.IMember;
035: import org.eclipse.jdt.core.IPackageFragmentRoot;
036: import org.eclipse.jdt.core.JavaModelException;
037:
038: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
039: import org.eclipse.jdt.internal.corext.util.Messages;
040:
041: import org.eclipse.jdt.ui.JavaElementLabels;
042: import org.eclipse.jdt.ui.JavaUI;
043: import org.eclipse.jdt.ui.JavadocContentAccess;
044:
045: import org.eclipse.jdt.internal.ui.JavaUIMessages;
046: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
047:
048: public class JavadocHelpContext implements IContext2 {
049:
050: public static void displayHelp(String contextId, Object[] selected)
051: throws CoreException {
052: IContext context = HelpSystem.getContext(contextId);
053: if (context != null) {
054: if (selected != null && selected.length > 0) {
055: context = new JavadocHelpContext(context, selected);
056: }
057: PlatformUI.getWorkbench().getHelpSystem().displayHelp(
058: context);
059: }
060: }
061:
062: private static class JavaUIHelpResource implements IHelpResource {
063:
064: private IJavaElement fElement;
065: private String fUrl;
066:
067: public JavaUIHelpResource(IJavaElement element, String url) {
068: fElement = element;
069: fUrl = url;
070: }
071:
072: public String getHref() {
073: return fUrl;
074: }
075:
076: public String getLabel() {
077: String label = JavaElementLabels.getTextLabel(fElement,
078: JavaElementLabels.ALL_DEFAULT
079: | JavaElementLabels.ALL_FULLY_QUALIFIED);
080: return Messages.format(
081: JavaUIMessages.JavaUIHelp_link_label, label);
082: }
083: }
084:
085: private IHelpResource[] fHelpResources;
086: private String fText;
087: private String fTitle;
088:
089: // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=85719
090: private static final boolean BUG_85719_FIXED = false;
091:
092: public JavadocHelpContext(IContext context, Object[] elements)
093: throws JavaModelException {
094: Assert.isNotNull(elements);
095: if (context instanceof IContext2)
096: fTitle = ((IContext2) context).getTitle();
097:
098: List helpResources = new ArrayList();
099:
100: String javadocSummary = null;
101: for (int i = 0; i < elements.length; i++) {
102: if (elements[i] instanceof IJavaElement) {
103: IJavaElement element = (IJavaElement) elements[i];
104: // if element isn't on the build path skip it
105: if (!ActionUtil.isOnBuildPath(element))
106: continue;
107:
108: // Create Javadoc summary
109: if (BUG_85719_FIXED) {
110: if (javadocSummary == null) {
111: javadocSummary = retrieveText(element);
112: if (javadocSummary != null) {
113: String elementLabel = JavaElementLabels
114: .getTextLabel(
115: element,
116: JavaElementLabels.ALL_DEFAULT);
117:
118: // FIXME: needs to be NLSed once the code becomes active
119: javadocSummary = "<b>Javadoc for " + elementLabel + ":</b><br>" + javadocSummary; //$NON-NLS-1$//$NON-NLS-2$
120: }
121: } else {
122: javadocSummary = ""; // no Javadoc summary for multiple selection //$NON-NLS-1$
123: }
124: }
125:
126: URL url = JavaUI.getJavadocLocation(element, true);
127: if (url == null || doesNotExist(url)) {
128: IPackageFragmentRoot root = JavaModelUtil
129: .getPackageFragmentRoot(element);
130: if (root != null) {
131: url = JavaUI.getJavadocBaseLocation(element);
132: if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
133: element = element.getJavaProject();
134: } else {
135: element = root;
136: }
137: url = JavaUI.getJavadocLocation(element, false);
138: }
139: }
140: if (url != null) {
141: IHelpResource javaResource = new JavaUIHelpResource(
142: element, getURLString(url));
143: helpResources.add(javaResource);
144: }
145: }
146: }
147:
148: // Add static help topics
149: if (context != null) {
150: IHelpResource[] resources = context.getRelatedTopics();
151: if (resources != null) {
152: for (int j = 0; j < resources.length; j++) {
153: helpResources.add(resources[j]);
154: }
155: }
156: }
157:
158: fHelpResources = (IHelpResource[]) helpResources
159: .toArray(new IHelpResource[helpResources.size()]);
160:
161: if (context != null)
162: fText = context.getText();
163:
164: if (BUG_85719_FIXED) {
165: if (javadocSummary != null && javadocSummary.length() > 0) {
166: if (fText != null)
167: fText = context.getText()
168: + "<br><br>" + javadocSummary; //$NON-NLS-1$
169: else
170: fText = javadocSummary;
171: }
172: }
173:
174: if (fText == null)
175: fText = ""; //$NON-NLS-1$
176:
177: }
178:
179: private String getURLString(URL url) {
180: String location = url.toExternalForm();
181: if (url.getRef() != null) {
182: int anchorIdx = location.lastIndexOf('#');
183: if (anchorIdx != -1) {
184: return location.substring(0, anchorIdx)
185: + "?noframes=true" + location.substring(anchorIdx); //$NON-NLS-1$
186: }
187: }
188: return location + "?noframes=true"; //$NON-NLS-1$
189: }
190:
191: private boolean doesNotExist(URL url) {
192: if (url.getProtocol().equals("file")) { //$NON-NLS-1$
193: File file = new File(url.getFile());
194: return !file.exists();
195: }
196: return false;
197: }
198:
199: private String retrieveText(IJavaElement elem)
200: throws JavaModelException {
201: if (elem instanceof IMember) {
202: Reader reader = JavadocContentAccess.getHTMLContentReader(
203: (IMember) elem, true, true);
204: if (reader != null)
205: reader = new HTML2TextReader(reader, null);
206: if (reader != null) {
207: String str = getString(reader);
208: BreakIterator breakIterator = BreakIterator
209: .getSentenceInstance();
210: breakIterator.setText(str);
211: return str.substring(0, breakIterator.next());
212: }
213: }
214: return ""; //$NON-NLS-1$
215: }
216:
217: /**
218: * Gets the reader content as a String
219: */
220: private static String getString(Reader reader) {
221: StringBuffer buf = new StringBuffer();
222: char[] buffer = new char[1024];
223: int count;
224: try {
225: while ((count = reader.read(buffer)) != -1)
226: buf.append(buffer, 0, count);
227: } catch (IOException e) {
228: return null;
229: }
230: return buf.toString();
231: }
232:
233: public IHelpResource[] getRelatedTopics() {
234: return fHelpResources;
235: }
236:
237: public String getText() {
238: return fText;
239: }
240:
241: public String getStyledText() {
242: return fText;
243: }
244:
245: public String getCategory(IHelpResource topic) {
246: if (topic instanceof JavaUIHelpResource)
247: return JavaUIMessages.JavaUIHelpContext_javaHelpCategory_label;
248:
249: return null;
250: }
251:
252: /*
253: * @see org.eclipse.help.IContext2#getTitle()
254: * @since 3.1
255: */
256: public String getTitle() {
257: return fTitle;
258: }
259:
260: }
|