1 11 package org.eclipse.help.internal.base; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.Locale ; 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 37 public final class BaseHelpSystem { 38 39 private static final BaseHelpSystem instance = new BaseHelpSystem(); 40 41 public static final String BOOKMARKS = "bookmarks"; public static final String WORKING_SETS = "workingSets"; public static final String WORKING_SET = "workingSet"; 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 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 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 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 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 WebappManager.stop("help"); } 152 } 153 154 157 public static void startup() { 158 try { 159 setDefaultErrorUtil(new IErrorUtil() { 160 public void displayError(String msg) { 161 System.out.println(msg); 162 } 163 public void displayError(String msg, Thread uiThread) { 164 System.out.println(msg); 165 } 166 }); 167 HelpBasePlugin.getDefault().getPluginPreferences(); 168 } 169 catch (Exception e) { 170 HelpBasePlugin.getDefault().getLog().log( 171 new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, 0, 172 "Error launching help.", e)); } 174 175 179 HelpPlugin.getDefault().setHelpProvider(new HelpProvider()); 180 } 181 182 public static boolean ensureWebappRunning() { 183 if (!getInstance().webappStarted) { 184 getInstance().webappStarted = true; 185 try { 186 WebappManager.start("help"); } catch (CoreException e) { 189 HelpBasePlugin.logError("The embedded application server could not run help web application.", e); BaseHelpSystem.getDefaultErrorUtil().displayError(HelpBaseResources.HelpWebappNotStarted); 191 return false; 192 } 193 getInstance().webappRunning = true; 194 } 195 return getInstance().webappRunning; 196 } 197 198 public static URL resolve(String href, boolean documentOnly) { 199 String url = null; 200 if (href == null || href.indexOf("://") != -1) url = href; 202 else { 203 BaseHelpSystem.ensureWebappRunning(); 204 String base = getBase(documentOnly); 205 if (href.startsWith("/")) url = base + href; 207 else 208 url = base + "/" + href; } 210 try { 211 return new URL (url); 212 } catch (MalformedURLException e) { 213 return null; 214 } 215 } 216 217 public static URL resolve(String href, String servlet) { 218 String url = null; 219 if (href == null || href.indexOf("://") != -1) { url = href; 221 } 222 else { 223 BaseHelpSystem.ensureWebappRunning(); 224 String base = getBase(servlet); 225 if (href.startsWith("/")) { url = base + href; 227 } 228 else { 229 url = base + "/" + href; } 231 } 232 try { 233 return new URL (url); 234 } 235 catch (MalformedURLException e) { 236 return null; 237 } 238 } 239 240 public static String unresolve(URL url) { 241 return unresolve(url.toString()); 242 } 243 244 public static String unresolve(String href) { 245 String [] baseVariants = { getBase("/help/topic"), getBase("/help/nftopic"), getBase("/help/ntopic"), getBase("/help/rtopic") }; 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 getBase(boolean documentOnly) { 258 String servlet = documentOnly ? "/help/nftopic" : "/help/topic"; return getBase(servlet); 260 } 261 262 private static String getBase(String servlet) { 263 return "http://" + WebappManager.getHost() + ":" + WebappManager.getPort() + servlet; 266 } 267 268 271 public static int getMode() { 272 return getInstance().mode; 273 } 274 275 278 public static void setMode(int mode) { 279 getInstance().mode = mode; 280 HelpSystem.setShared(mode == MODE_INFOCENTER); 281 } 282 283 286 public static void setDefaultErrorUtil(IErrorUtil em) { 287 getInstance().defaultErrorMessenger = em; 288 } 289 290 294 public static IErrorUtil getDefaultErrorUtil() { 295 return getInstance().defaultErrorMessenger; 296 } 297 298 303 public static String getProductName() { 304 IProduct product = Platform.getProduct(); 305 if (product == null) { 306 return ""; } 308 String name = product.getName(); 309 return name == null ? "" : name; } 311 312 private static boolean initializeRTL() { 313 String orientation = System.getProperty("eclipse.orientation"); if ("rtl".equals(orientation)) { return true; 317 } else if ("ltr".equals(orientation)) { return false; 319 } 320 String [] args = Platform.getCommandLineArgs(); 322 for (int i = 0; i < args.length; i++) { 323 if ("-dir".equalsIgnoreCase(args[i])) { if ((i + 1) < args.length 325 && "rtl".equalsIgnoreCase(args[i + 1])) { return true; 327 } 328 return false; 329 } 330 } 331 332 if (System.getProperty("osgi.nl.user") == null) return false; 336 337 String locale = Platform.getNL(); 339 if (locale == null) { 340 locale = Locale.getDefault().toString(); 341 } 342 if (locale.startsWith("ar") || locale.startsWith("fa") || locale.startsWith("he") || locale.startsWith("iw") || locale.startsWith("ur")) { 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 pluginID, String className, String arg) { 355 Bundle bundle = Platform.getBundle(pluginID); 356 if (bundle == null) { 357 return; 358 } 359 360 try { 361 Class c = bundle.loadClass(className); 362 Object o = c.newInstance(); 363 if (o != null && o instanceof ILiveHelpAction) { 365 ILiveHelpAction helpExt = (ILiveHelpAction) o; 366 if (arg != null) 367 helpExt.setInitializationString(arg); 368 Thread runnableLiveHelp = new Thread (helpExt); 369 runnableLiveHelp.setDaemon(true); 370 runnableLiveHelp.start(); 371 } 372 } catch (ThreadDeath td) { 373 throw td; 374 } catch (Exception e) { 375 } 376 } 377 } 378 | Popular Tags |