1 11 package org.eclipse.help.internal.browser; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.help.browser.*; 19 import org.eclipse.help.internal.base.*; 20 import org.eclipse.osgi.service.environment.*; 21 import org.eclipse.osgi.util.NLS; 22 23 26 public class BrowserManager { 27 public static final String DEFAULT_BROWSER_ID_KEY = "default_browser"; 29 public static final String BROWSER_ID_CUSTOM = HelpBasePlugin.PLUGIN_ID 30 + ".custombrowser"; 32 public static final String BROWSER_ID_FIREFOX = HelpBasePlugin.PLUGIN_ID 33 + ".firefox"; 35 public static final String BROWSER_ID_MOZILLA = HelpBasePlugin.PLUGIN_ID 36 + ".mozilla"; 38 public static final String BROWSER_ID_NETSCAPE = HelpBasePlugin.PLUGIN_ID 39 + ".netscape"; 41 public static final String BROWSER_ID_MAC_SYSTEM = HelpBasePlugin.PLUGIN_ID 42 + ".defaultBrowserMacOSX"; 44 public static final String BROWSER_ID_EMBEDDED = "org.eclipse.help.ui.embeddedbrowser"; 46 public static final String BROWSER_ID_SYSTEM = "org.eclipse.help.ui.systembrowser"; 48 private static BrowserManager instance; 49 50 private boolean initialized = false; 51 52 private BrowserDescriptor currentBrowserDesc; 53 54 private BrowserDescriptor defaultBrowserDesc; 55 56 private BrowserDescriptor[] browsersDescriptors; 57 58 private BrowserDescriptor internalBrowserDesc; 59 60 private Collection browsers = new ArrayList (); 61 62 private boolean alwaysUseExternal = false; 63 64 67 private BrowserManager() { 68 } 69 70 73 private void init() { 74 initialized = true; 75 browsersDescriptors = createBrowserDescriptors(); 77 String defBrowserID = HelpBasePlugin.getDefault() 79 .getPluginPreferences() 80 .getDefaultString(DEFAULT_BROWSER_ID_KEY); 81 if (defBrowserID != null && (!"".equals(defBrowserID))) { setDefaultBrowserID(defBrowserID); 83 } 84 String os = Platform.getOS(); 91 if (defaultBrowserDesc == null) { 92 if (Constants.WS_WIN32.equalsIgnoreCase(os)) { 93 setDefaultBrowserID(BROWSER_ID_SYSTEM); 94 } else if (Constants.OS_AIX.equalsIgnoreCase(os) 95 || (Constants.OS_HPUX.equalsIgnoreCase(os)) 96 || (Constants.OS_LINUX.equalsIgnoreCase(os)) 97 || (Constants.OS_SOLARIS.equalsIgnoreCase(os))) { 98 setDefaultBrowserID(BROWSER_ID_MOZILLA); 99 if (defaultBrowserDesc == null) { 100 setDefaultBrowserID(BROWSER_ID_FIREFOX); 101 } 102 if (defaultBrowserDesc == null) { 103 setDefaultBrowserID(BROWSER_ID_NETSCAPE); 104 } 105 } else if (Constants.OS_MACOSX.equalsIgnoreCase(os)) { 106 setDefaultBrowserID(BROWSER_ID_MAC_SYSTEM); 107 } 108 } 109 if (defaultBrowserDesc == null) { 111 for (int i = 0; i < browsersDescriptors.length; i++) { 112 if (BROWSER_ID_CUSTOM.equals(browsersDescriptors[i].getID())) { 113 defaultBrowserDesc = browsersDescriptors[i]; 114 } 115 } 116 } 117 if (defaultBrowserDesc == null) { 119 setDefaultBrowserID(BROWSER_ID_CUSTOM); 120 } 121 if (defaultBrowserDesc == null) { 123 defaultBrowserDesc = new BrowserDescriptor("", "Null Browser", new IBrowserFactory() { 126 public boolean isAvailable() { 127 return true; 128 } 129 130 public IBrowser createBrowser() { 131 return new IBrowser() { 132 public void close() { 133 } 134 135 public void displayURL(String url) { 136 HelpBasePlugin 137 .logError( 138 "There is no browser adapter configured to display " + url 140 + ". Ensure that you have a required browser and adapter installed, and that the browser program is available on the system path.", null); 142 String msg = NLS.bind(HelpBaseResources.no_browsers, url); 143 BaseHelpSystem.getDefaultErrorUtil() 144 .displayError(msg); 145 } 146 147 public boolean isCloseSupported() { 148 return false; 149 } 150 151 public boolean isSetLocationSupported() { 152 return false; 153 } 154 155 public boolean isSetSizeSupported() { 156 return false; 157 } 158 159 public void setLocation(int width, int height) { 160 } 161 162 public void setSize(int x, int y) { 163 } 164 }; 165 } 166 }); 167 } 168 String curBrowserID = HelpBasePlugin.getDefault() 170 .getPluginPreferences().getString(DEFAULT_BROWSER_ID_KEY); 171 if (curBrowserID != null && (!"".equals(curBrowserID))) { setCurrentBrowserID(curBrowserID); 173 } 175 if (currentBrowserDesc == null) { 176 setCurrentBrowserID(getDefaultBrowserID()); 177 } 178 setAlwaysUseExternal(HelpBasePlugin.getDefault().getPluginPreferences() 179 .getBoolean(IHelpBaseConstants.P_KEY_ALWAYS_EXTERNAL_BROWSER)); 180 } 181 182 185 public static BrowserManager getInstance() { 186 if (instance == null) 187 instance = new BrowserManager(); 188 return instance; 189 } 190 191 194 private BrowserDescriptor[] createBrowserDescriptors() { 195 if (this.browsersDescriptors != null) 196 return this.browsersDescriptors; 197 Collection bDescriptors = new ArrayList (); 198 IConfigurationElement configElements[] = Platform 199 .getExtensionRegistry().getConfigurationElementsFor( 200 HelpBasePlugin.PLUGIN_ID, "browser"); for (int i = 0; i < configElements.length; i++) { 202 if (!configElements[i].getName().equals("browser")) continue; 204 String id = configElements[i].getAttribute("id"); if (id == null) 206 continue; 207 String label = configElements[i].getAttribute("name"); if (label == null) 209 continue; 210 try { 211 Object adapter = configElements[i] 212 .createExecutableExtension("factoryclass"); if (!(adapter instanceof IBrowserFactory)) 214 continue; 215 if (((IBrowserFactory) adapter).isAvailable()) { 216 BrowserDescriptor descriptor = new BrowserDescriptor(id, 217 label, (IBrowserFactory) adapter); 218 if (descriptor.isExternal()) { 219 bDescriptors.add(descriptor); 220 } else { 221 internalBrowserDesc = descriptor; 222 } 223 } 224 } catch (CoreException ce) { 225 } 226 } 227 this.browsersDescriptors = (BrowserDescriptor[]) bDescriptors 228 .toArray(new BrowserDescriptor[bDescriptors.size()]); 229 return this.browsersDescriptors; 230 } 231 232 235 public BrowserDescriptor[] getBrowserDescriptors() { 236 if (!initialized) { 237 init(); 238 } 239 return this.browsersDescriptors; 240 } 241 242 247 public String getCurrentBrowserID() { 248 if (!initialized) { 249 init(); 250 } 251 if (currentBrowserDesc == null) 252 return null; 253 return currentBrowserDesc.getID(); 254 } 255 256 261 public String getCurrentInternalBrowserID() { 262 if (!initialized) { 263 init(); 264 } 265 if (isEmbeddedBrowserPresent() && !alwaysUseExternal) { 266 return internalBrowserDesc.getID(); 267 } 268 return getCurrentBrowserID(); 269 } 270 271 276 public String getDefaultBrowserID() { 277 if (!initialized) { 278 init(); 279 } 280 if (defaultBrowserDesc == null) 281 return null; 282 return defaultBrowserDesc.getID(); 283 } 284 285 292 public void setCurrentBrowserID(String currentAdapterID) { 293 if (!initialized) { 294 init(); 295 } 296 for (int i = 0; i < browsersDescriptors.length; i++) { 297 if (browsersDescriptors[i].getID().equals(currentAdapterID)) { 298 currentBrowserDesc = browsersDescriptors[i]; 299 return; 300 } 301 } 302 } 303 304 311 private void setDefaultBrowserID(String defaultAdapterID) { 312 if (!initialized) { 313 init(); 314 } 315 for (int i = 0; i < browsersDescriptors.length; i++) { 316 if (browsersDescriptors[i].getID().equals(defaultAdapterID)) { 317 defaultBrowserDesc = browsersDescriptors[i]; 318 return; 319 } 320 } 321 } 322 323 327 public IBrowser createBrowser(boolean external) { 328 if (!initialized) { 329 init(); 330 } 331 if (external) { 334 return new CurrentBrowser(createBrowserAdapter(true), 335 getCurrentBrowserID(), true); 336 } 337 return new CurrentBrowser(createBrowserAdapter(alwaysUseExternal), 338 getCurrentInternalBrowserID(), false); 339 } 340 341 344 public IBrowser createBrowser() { 345 return createBrowser(true); 346 } 347 348 352 private IBrowser createBrowserAdapter(boolean external) { 353 if (!initialized) { 354 init(); 355 } 356 IBrowser browser = null; 357 if (!external && isEmbeddedBrowserPresent()) { 358 browser = internalBrowserDesc.getFactory().createBrowser(); 359 } else { 360 browser = currentBrowserDesc.getFactory().createBrowser(); 361 } 362 browsers.add(browser); 363 return browser; 364 } 365 366 369 public void closeAll() { 370 if (!initialized) { 371 return; 373 } 374 for (Iterator it = browsers.iterator(); it.hasNext();) { 375 IBrowser browser = (IBrowser) it.next(); 376 browser.close(); 377 } 378 } 379 380 public boolean isEmbeddedBrowserPresent() { 381 if (!initialized) { 382 init(); 383 } 384 return internalBrowserDesc != null; 385 } 386 387 public void setAlwaysUseExternal(boolean alwaysExternal) { 388 if (!initialized) { 389 init(); 390 } 391 alwaysUseExternal = alwaysExternal || !isEmbeddedBrowserPresent(); 392 } 393 394 public boolean isAlwaysUseExternal() { 395 if (!isEmbeddedBrowserPresent()) { 396 return true; 397 } 398 return alwaysUseExternal; 399 } 400 } 401 | Popular Tags |