KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > HelpDisplay


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.help.internal.base;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.net.URLEncoder JavaDoc;
15
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.help.IContext;
18 import org.eclipse.help.IHelpResource;
19 import org.eclipse.help.IToc;
20 import org.eclipse.help.internal.HelpPlugin;
21 import org.eclipse.help.internal.context.Context;
22 import org.eclipse.help.internal.server.WebappManager;
23 import org.eclipse.osgi.util.NLS;
24
25 /**
26  * This class provides methods to display help. It is independent of platform
27  * UI.
28  */

29 public class HelpDisplay {
30
31     /**
32      * Constructor.
33      */

34     public HelpDisplay() {
35         super();
36     }
37
38     /**
39      * Displays help.
40      */

41     public void displayHelp(boolean forceExternal) {
42         displayHelpURL(null, forceExternal);
43     }
44
45     /**
46      * Displays a help resource specified as a url.
47      * <ul>
48      * <li>a URL in a format that can be returned by
49      * {@link org.eclipse.help.IHelpResource#getHref() IHelpResource.getHref()}
50      * <li>a URL query in the format format
51      * <em>key=value&amp;key=value ...</em> The valid keys are: "tab", "toc",
52      * "topic", "contextId". For example,
53      * <em>toc="/myplugin/mytoc.xml"&amp;topic="/myplugin/references/myclass.html"</em>
54      * is valid.
55      * </ul>
56      */

57     public void displayHelpResource(String JavaDoc href, boolean forceExternal) {
58         // check if this is a toc
59
IToc toc = HelpPlugin.getTocManager().getToc(href, Platform.getNL());
60         if (toc != null)
61             try {
62                 displayHelpURL(
63                         "toc=" + URLEncoder.encode(toc.getHref(), "UTF-8"), forceExternal); //$NON-NLS-1$ //$NON-NLS-2$
64
} catch (UnsupportedEncodingException JavaDoc uee) {
65             }
66         else if (href != null && (href.startsWith("tab=") //$NON-NLS-1$
67
|| href.startsWith("toc=") //$NON-NLS-1$
68
|| href.startsWith("topic=") //$NON-NLS-1$
69
|| href.startsWith("contextId="))) { //$NON-NLS-1$ // assume it is a query string
70
displayHelpURL(href, forceExternal);
71         } else { // assume this is a topic
72
if (getNoframesURL(href) == null) {
73                 try {
74                     displayHelpURL(
75                             "topic=" + URLEncoder.encode(href, "UTF-8"), forceExternal); //$NON-NLS-1$ //$NON-NLS-2$
76
} catch (UnsupportedEncodingException JavaDoc uee) {
77                 }
78             } else if (href.startsWith("jar:") || href.startsWith("platform:")) { //$NON-NLS-1$ //$NON-NLS-2$
79
// topic from a jar/workspace to display without frames
80
displayHelpURL(
81                         getBaseURL() + "nftopic/" + getNoframesURL(href), true); //$NON-NLS-1$
82
} else {
83                 displayHelpURL(getNoframesURL(href), true);
84             }
85         }
86     }
87
88     /**
89      * Display help for the a given topic and related topics.
90      *
91      * @param context
92      * context for which related topics will be displayed
93      * @param topic
94      * related topic to be selected
95      */

96     public void displayHelp(IContext context, IHelpResource topic,
97             boolean forceExternal) {
98         if (context == null || topic == null || topic.getHref() == null)
99             return;
100         String JavaDoc topicURL = getTopicURL(topic.getHref());
101         if (getNoframesURL(topicURL) == null) {
102             try {
103                 String JavaDoc url = "tab=links" //$NON-NLS-1$
104
+ "&contextId=" //$NON-NLS-1$
105
+ URLEncoder.encode(getContextID(context), "UTF-8") //$NON-NLS-1$
106
+ "&topic=" //$NON-NLS-1$
107
+ URLEncoder.encode(topicURL, "UTF-8"); //$NON-NLS-1$
108
displayHelpURL(url, forceExternal);
109             } catch (UnsupportedEncodingException JavaDoc uee) {
110             }
111
112         } else if (topicURL.startsWith("jar:file:")) { //$NON-NLS-1$
113
// topic from a jar to display without frames
114
displayHelpURL(
115                     getBaseURL() + "nftopic/" + getNoframesURL(topicURL), true); //$NON-NLS-1$
116
} else {
117             displayHelpURL(getNoframesURL(topicURL), true);
118         }
119     }
120
121     /**
122      * Display help to search view for given query and selected topic.
123      *
124      * @param searchQuery
125      * search query in URL format key=value&key=value
126      * @param topic
127      * selected from the search results
128      */

129     public void displaySearch(String JavaDoc searchQuery, String JavaDoc topic,
130             boolean forceExternal) {
131         if (searchQuery == null || topic == null)
132             return;
133         if (getNoframesURL(topic) == null) {
134             try {
135                 String JavaDoc url = "tab=search&" //$NON-NLS-1$
136
+ searchQuery + "&topic=" //$NON-NLS-1$
137
+ URLEncoder.encode(getTopicURL(topic), "UTF-8"); //$NON-NLS-1$
138
displayHelpURL(url, forceExternal);
139             } catch (UnsupportedEncodingException JavaDoc uee) {
140             }
141
142         } else {
143             displayHelpURL(getNoframesURL(topic), true);
144         }
145     }
146
147     /**
148      * Displays the specified url. The url can contain query parameters to
149      * identify how help displays the document
150      */

151     private void displayHelpURL(String JavaDoc helpURL, boolean forceExternal) {
152         if (!BaseHelpSystem.ensureWebappRunning()) {
153             return;
154         }
155         if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_STANDALONE) {
156             // wait for Display to be created
157
DisplayUtils.waitForDisplay();
158         }
159
160         try {
161             /*
162             if (helpURL == null || helpURL.length() == 0) {
163                 BaseHelpSystem.getHelpBrowser(forceExternal).displayURL(
164                         getFramesetURL());
165             } else if (helpURL.startsWith("tab=") //$NON-NLS-1$
166                     || helpURL.startsWith("toc=") //$NON-NLS-1$
167                     || helpURL.startsWith("topic=") //$NON-NLS-1$
168                     || helpURL.startsWith("contextId=")) { //$NON-NLS-1$
169                 BaseHelpSystem.getHelpBrowser(forceExternal).displayURL(
170                         getFramesetURL() + "?" + helpURL); //$NON-NLS-1$
171             } else {
172                 BaseHelpSystem.getHelpBrowser(forceExternal)
173                         .displayURL(helpURL);
174             }
175             */

176             if (helpURL == null || helpURL.length() == 0) {
177                 helpURL = getFramesetURL();
178             } else if (helpURL.startsWith("tab=") //$NON-NLS-1$
179
|| helpURL.startsWith("toc=") //$NON-NLS-1$
180
|| helpURL.startsWith("topic=") //$NON-NLS-1$
181
|| helpURL.startsWith("contextId=")) { //$NON-NLS-1$
182
helpURL = getFramesetURL() + "?" + helpURL; //$NON-NLS-1$
183
}
184             BaseHelpSystem.getHelpBrowser(forceExternal)
185                         .displayURL(helpURL);
186         } catch (Exception JavaDoc e) {
187             HelpBasePlugin
188                     .logError(
189                             "An exception occurred while launching help. Check the log at " + Platform.getLogFileLocation().toOSString(), e); //$NON-NLS-1$
190
BaseHelpSystem.getDefaultErrorUtil()
191                     .displayError(
192                             NLS.bind(HelpBaseResources.HelpDisplay_exceptionMessage, Platform.getLogFileLocation().toOSString()));
193         }
194     }
195
196     private String JavaDoc getContextID(IContext context) {
197         if (context instanceof Context) {
198             return ((Context)context).getId();
199         }
200         return HelpPlugin.getContextManager().addContext(context);
201     }
202
203     private String JavaDoc getBaseURL() {
204         return "http://" //$NON-NLS-1$
205
+ WebappManager.getHost() + ":" //$NON-NLS-1$
206
+ WebappManager.getPort() + "/help/"; //$NON-NLS-1$
207
}
208
209     private String JavaDoc getFramesetURL() {
210         return getBaseURL() + "index.jsp"; //$NON-NLS-1$
211
}
212
213     private String JavaDoc getTopicURL(String JavaDoc topic) {
214         if (topic == null)
215             return null;
216         if (topic.startsWith("../")) //$NON-NLS-1$
217
topic = topic.substring(2);
218         /*
219          * if (topic.startsWith("/")) { String base = "http://" +
220          * AppServer.getHost() + ":" + AppServer.getPort(); base +=
221          * "/help/content/help:"; topic = base + topic; }
222          */

223         return topic;
224     }
225
226     /**
227      * If href contains URL parameter noframes=true return href with that
228      * paramter removed, otherwise returns null
229      *
230      * @param href
231      * @return String or null
232      */

233     private String JavaDoc getNoframesURL(String JavaDoc href) {
234         if (href == null) {
235             return null;
236         }
237         int ix = href.indexOf("?noframes=true&"); //$NON-NLS-1$
238
if (ix >= 0) {
239             //remove noframes=true&
240
return href.substring(0, ix + 1)
241                     + href.substring(ix + "?noframes=true&".length()); //$NON-NLS-1$
242

243         }
244         ix = href.indexOf("noframes=true"); //$NON-NLS-1$
245
if (ix > 0) {
246             //remove &noframes=true
247
return href.substring(0, ix - 1)
248                     + href.substring(ix + "noframes=true".length()); //$NON-NLS-1$
249
}
250         // can be displayed in frames
251
return null;
252     }
253
254 }
255
Popular Tags