KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProduct;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.help.HelpSystem;
23 import org.eclipse.help.ILiveHelpAction;
24 import org.eclipse.help.browser.IBrowser;
25 import org.eclipse.help.internal.HelpPlugin;
26 import org.eclipse.help.internal.base.util.IErrorUtil;
27 import org.eclipse.help.internal.browser.BrowserManager;
28 import org.eclipse.help.internal.search.LocalSearchManager;
29 import org.eclipse.help.internal.search.SearchManager;
30 import org.eclipse.help.internal.server.WebappManager;
31 import org.eclipse.help.internal.workingset.WorkingSetManager;
32 import org.osgi.framework.Bundle;
33
34 /**
35  * Base Help System.
36  */

37 public final class BaseHelpSystem {
38     
39     private static final BaseHelpSystem instance = new BaseHelpSystem();
40     
41     public static final String JavaDoc BOOKMARKS = "bookmarks"; //$NON-NLS-1$
42
public static final String JavaDoc WORKING_SETS = "workingSets"; //$NON-NLS-1$
43
public static final String JavaDoc WORKING_SET = "workingSet"; //$NON-NLS-1$
44

45     public static final int MODE_WORKBENCH = 0;
46     public static final int MODE_INFOCENTER = 1;
47     public static final int MODE_STANDALONE = 2;
48
49     private int mode = MODE_WORKBENCH;
50     
51     private SearchManager searchManager;
52     private WorkingSetManager workingSetManager;
53     private BookmarkManager bookmarkManager;
54
55     private boolean webappStarted = false;
56     private boolean webappRunning = false;
57     private IErrorUtil defaultErrorMessenger;
58     private IBrowser browser;
59     private IBrowser internalBrowser;
60     private HelpDisplay helpDisplay = null;
61     private boolean rtl = false;
62
63     private BaseHelpSystem() {
64         super();
65         rtl = initializeRTL();
66     }
67
68     public static BaseHelpSystem getInstance() {
69         return instance;
70     }
71
72     /*
73      * Returns the singleton search manager, which is the main interface to the
74      * help system's search capability.
75      */

76     public static SearchManager getSearchManager() {
77         if (getInstance().searchManager == null) {
78             synchronized (BaseHelpSystem.class) {
79                 if (getInstance().searchManager == null) {
80                     getInstance().searchManager = new SearchManager();
81                 }
82             }
83         }
84         return getInstance().searchManager;
85     }
86     
87     /*
88      * Returns the local search manager which deals only with the local content
89      * and is called by the global search manager.
90      */

91     public static LocalSearchManager getLocalSearchManager() {
92         return getSearchManager().getLocalSearchManager();
93     }
94
95     public static synchronized WorkingSetManager getWorkingSetManager() {
96         if (getInstance().workingSetManager == null) {
97             getInstance().workingSetManager = new WorkingSetManager();
98         }
99         return getInstance().workingSetManager;
100     }
101
102     public static synchronized BookmarkManager getBookmarkManager() {
103         if (getInstance().bookmarkManager == null) {
104             getInstance().bookmarkManager = new BookmarkManager();
105         }
106         return getInstance().bookmarkManager;
107     }
108
109     /*
110      * Allows Help UI to plug-in a soft adapter that delegates all the work to
111      * the workbench browser support.
112      */

113     public synchronized void setBrowserInstance(IBrowser browser) {
114         this.browser = browser;
115     }
116
117     public static synchronized IBrowser getHelpBrowser(boolean forceExternal) {
118         if (!forceExternal && !BrowserManager.getInstance().isAlwaysUseExternal()) {
119             if (getInstance().internalBrowser == null) {
120                 getInstance().internalBrowser = BrowserManager.getInstance().createBrowser(false);
121             }
122             return getInstance().internalBrowser;
123         }
124         if (getInstance().browser == null) {
125             getInstance().browser = BrowserManager.getInstance().createBrowser(true);
126         }
127         return getInstance().browser;
128     }
129
130     public static synchronized HelpDisplay getHelpDisplay() {
131         if (getInstance().helpDisplay == null)
132             getInstance().helpDisplay = new HelpDisplay();
133         return getInstance().helpDisplay;
134     }
135
136     /*
137      * Shuts down the BaseHelpSystem.
138      */

139     public static void shutdown() throws CoreException {
140         if (getInstance().bookmarkManager != null) {
141             getInstance().bookmarkManager.close();
142             getInstance().bookmarkManager = null;
143         }
144         if (getInstance().searchManager != null) {
145             getInstance().searchManager.close();
146             getInstance().searchManager = null;
147         }
148         if (getInstance().webappStarted) {
149             // stop the web app
150
WebappManager.stop("help"); //$NON-NLS-1$
151
}
152     }
153
154     /**
155      * Called by Platform after loading the plugin
156      */

157     public static void startup() {
158         try {
159             setDefaultErrorUtil(new IErrorUtil() {
160                 public void displayError(String JavaDoc msg) {
161                     System.out.println(msg);
162                 }
163                 public void displayError(String JavaDoc msg, Thread JavaDoc uiThread) {
164                     System.out.println(msg);
165                 }
166             });
167             HelpBasePlugin.getDefault().getPluginPreferences();
168         }
169         catch (Exception JavaDoc e) {
170             HelpBasePlugin.getDefault().getLog().log(
171                     new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, 0,
172                             "Error launching help.", e)); //$NON-NLS-1$
173
}
174         
175         /*
176          * Assigns the provider responsible for providing help
177          * document content.
178          */

179         HelpPlugin.getDefault().setHelpProvider(new HelpProvider());
180     }
181
182     public static boolean ensureWebappRunning() {
183         if (!getInstance().webappStarted) {
184             getInstance().webappStarted = true;
185             try {
186                 // start the help web app
187
WebappManager.start("help"); //$NON-NLS-1$
188
} catch (CoreException e) {
189                 HelpBasePlugin.logError("The embedded application server could not run help web application.", e); //$NON-NLS-1$
190
BaseHelpSystem.getDefaultErrorUtil().displayError(HelpBaseResources.HelpWebappNotStarted);
191                 return false;
192             }
193             getInstance().webappRunning = true;
194         }
195         return getInstance().webappRunning;
196     }
197
198     public static URL JavaDoc resolve(String JavaDoc href, boolean documentOnly) {
199         String JavaDoc url = null;
200         if (href == null || href.indexOf("://") != -1) //$NON-NLS-1$
201
url = href;
202         else {
203             BaseHelpSystem.ensureWebappRunning();
204             String JavaDoc base = getBase(documentOnly);
205             if (href.startsWith("/")) //$NON-NLS-1$
206
url = base + href;
207             else
208                 url = base + "/" + href; //$NON-NLS-1$
209
}
210         try {
211             return new URL JavaDoc(url);
212         } catch (MalformedURLException JavaDoc e) {
213             return null;
214         }
215     }
216
217     public static URL JavaDoc resolve(String JavaDoc href, String JavaDoc servlet) {
218         String JavaDoc url = null;
219         if (href == null || href.indexOf("://") != -1) { //$NON-NLS-1$
220
url = href;
221         }
222         else {
223             BaseHelpSystem.ensureWebappRunning();
224             String JavaDoc base = getBase(servlet);
225             if (href.startsWith("/")) { //$NON-NLS-1$
226
url = base + href;
227             }
228             else {
229                 url = base + "/" + href; //$NON-NLS-1$
230
}
231         }
232         try {
233             return new URL JavaDoc(url);
234         }
235         catch (MalformedURLException JavaDoc e) {
236             return null;
237         }
238     }
239
240     public static String JavaDoc unresolve(URL JavaDoc url) {
241         return unresolve(url.toString());
242     }
243
244     public static String JavaDoc unresolve(String JavaDoc href) {
245         String JavaDoc[] baseVariants = { getBase("/help/topic"), //$NON-NLS-1$
246
getBase("/help/nftopic"), //$NON-NLS-1$
247
getBase("/help/ntopic"), //$NON-NLS-1$
248
getBase("/help/rtopic") }; //$NON-NLS-1$
249
for (int i = 0; i < baseVariants.length; i++) {
250             if (href.startsWith(baseVariants[i])) {
251                 return href.substring(baseVariants[i].length());
252             }
253         }
254         return href;
255     }
256
257     private static String JavaDoc getBase(boolean documentOnly) {
258         String JavaDoc servlet = documentOnly ? "/help/nftopic" : "/help/topic";//$NON-NLS-1$ //$NON-NLS-2$
259
return getBase(servlet);
260     }
261
262     private static String JavaDoc getBase(String JavaDoc servlet) {
263         return "http://" //$NON-NLS-1$
264
+ WebappManager.getHost() + ":" //$NON-NLS-1$
265
+ WebappManager.getPort() + servlet;
266     }
267
268     /*
269      * Returns the mode of operation.
270      */

271     public static int getMode() {
272         return getInstance().mode;
273     }
274
275     /*
276      * Sets the mode of operation.
277      */

278     public static void setMode(int mode) {
279         getInstance().mode = mode;
280         HelpSystem.setShared(mode == MODE_INFOCENTER);
281     }
282
283     /*
284      * Sets the error messenger
285      */

286     public static void setDefaultErrorUtil(IErrorUtil em) {
287         getInstance().defaultErrorMessenger = em;
288     }
289
290     /*
291      * Returns the default error messenger. When no UI is present, all errors
292      * are sent to System.out.
293      */

294     public static IErrorUtil getDefaultErrorUtil() {
295         return getInstance().defaultErrorMessenger;
296     }
297
298     /**
299      * Obtains name of the Eclipse product
300      *
301      * @return String
302      */

303     public static String JavaDoc getProductName() {
304         IProduct product = Platform.getProduct();
305         if (product == null) {
306             return ""; //$NON-NLS-1$
307
}
308         String JavaDoc name = product.getName();
309         return name == null ? "" : name; //$NON-NLS-1$
310
}
311
312     private static boolean initializeRTL() {
313         // from property
314
String JavaDoc orientation = System.getProperty("eclipse.orientation"); //$NON-NLS-1$
315
if ("rtl".equals(orientation)) { //$NON-NLS-1$
316
return true;
317         } else if ("ltr".equals(orientation)) { //$NON-NLS-1$
318
return false;
319         }
320         // from command line
321
String JavaDoc[] args = Platform.getCommandLineArgs();
322         for (int i = 0; i < args.length; i++) {
323             if ("-dir".equalsIgnoreCase(args[i])) { //$NON-NLS-1$
324
if ((i + 1) < args.length
325                         && "rtl".equalsIgnoreCase(args[i + 1])) { //$NON-NLS-1$
326
return true;
327                 }
328                 return false;
329             }
330         }
331
332         // Check if the user property is set. If not do not
333
// rely on the vm.
334
if (System.getProperty("osgi.nl.user") == null) //$NON-NLS-1$
335
return false;
336
337         // guess from default locale
338
String JavaDoc locale = Platform.getNL();
339         if (locale == null) {
340             locale = Locale.getDefault().toString();
341         }
342         if (locale.startsWith("ar") || locale.startsWith("fa") //$NON-NLS-1$//$NON-NLS-2$
343
|| locale.startsWith("he") || locale.startsWith("iw") //$NON-NLS-1$//$NON-NLS-2$
344
|| locale.startsWith("ur")) { //$NON-NLS-1$
345
return true;
346         }
347         return false;
348     }
349     
350     public static boolean isRTL() {
351         return getInstance().rtl;
352     }
353
354     public static void runLiveHelp(String JavaDoc pluginID, String JavaDoc className, String JavaDoc arg) {
355         Bundle bundle = Platform.getBundle(pluginID);
356         if (bundle == null) {
357             return;
358         }
359     
360         try {
361             Class JavaDoc c = bundle.loadClass(className);
362             Object JavaDoc o = c.newInstance();
363             //Runnable runnable = null;
364
if (o != null && o instanceof ILiveHelpAction) {
365                 ILiveHelpAction helpExt = (ILiveHelpAction) o;
366                 if (arg != null)
367                     helpExt.setInitializationString(arg);
368                 Thread JavaDoc runnableLiveHelp = new Thread JavaDoc(helpExt);
369                 runnableLiveHelp.setDaemon(true);
370                 runnableLiveHelp.start();
371             }
372         } catch (ThreadDeath JavaDoc td) {
373             throw td;
374         } catch (Exception JavaDoc e) {
375         }
376     }
377 }
378
Popular Tags