KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > browser > BrowserManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.browser;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
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 /**
24  * Creates browser by delegating to appropriate browser adapter
25  */

26 public class BrowserManager {
27     public static final String JavaDoc DEFAULT_BROWSER_ID_KEY = "default_browser"; //$NON-NLS-1$
28

29     public static final String JavaDoc BROWSER_ID_CUSTOM = HelpBasePlugin.PLUGIN_ID
30             + ".custombrowser"; //$NON-NLS-1$
31

32     public static final String JavaDoc BROWSER_ID_FIREFOX = HelpBasePlugin.PLUGIN_ID
33     + ".firefox"; //$NON-NLS-1$
34

35     public static final String JavaDoc BROWSER_ID_MOZILLA = HelpBasePlugin.PLUGIN_ID
36     + ".mozilla"; //$NON-NLS-1$
37

38     public static final String JavaDoc BROWSER_ID_NETSCAPE = HelpBasePlugin.PLUGIN_ID
39             + ".netscape"; //$NON-NLS-1$
40

41     public static final String JavaDoc BROWSER_ID_MAC_SYSTEM = HelpBasePlugin.PLUGIN_ID
42             + ".defaultBrowserMacOSX"; //$NON-NLS-1$
43

44     public static final String JavaDoc BROWSER_ID_EMBEDDED = "org.eclipse.help.ui.embeddedbrowser"; //$NON-NLS-1$
45

46     public static final String JavaDoc BROWSER_ID_SYSTEM = "org.eclipse.help.ui.systembrowser"; //$NON-NLS-1$
47

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 JavaDoc browsers = new ArrayList JavaDoc();
61
62     private boolean alwaysUseExternal = false;
63
64     /**
65      * Private Constructor
66      */

67     private BrowserManager() {
68     }
69
70     /**
71      * Initialize
72      */

73     private void init() {
74         initialized = true;
75         // Find all available browsers
76
browsersDescriptors = createBrowserDescriptors();
77         // 1. set default browser from preferences
78
String JavaDoc defBrowserID = HelpBasePlugin.getDefault()
79                 .getPluginPreferences()
80                 .getDefaultString(DEFAULT_BROWSER_ID_KEY);
81         if (defBrowserID != null && (!"".equals(defBrowserID))) { //$NON-NLS-1$
82
setDefaultBrowserID(defBrowserID);
83         }
84         // 2. set default browser to embedded
85
// if (defaultBrowserDesc == null) {
86
// setDefaultBrowserID(BROWSER_ID_EMBEDDED);
87
// }
88
// 3. set default browser to help implementation of system specific
89
// browser
90
String JavaDoc 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         // 4. set browser to one of externally contributed
110
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         // 5. let user specify program
118
if (defaultBrowserDesc == null) {
119             setDefaultBrowserID(BROWSER_ID_CUSTOM);
120         }
121         // 6. use null browser
122
if (defaultBrowserDesc == null) {
123             // If no browsers at all, use the Null Browser Adapter
124
defaultBrowserDesc = new BrowserDescriptor("", "Null Browser", //$NON-NLS-1$ //$NON-NLS-2$
125
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 JavaDoc url) {
136                                     HelpBasePlugin
137                                             .logError(
138                                                     "There is no browser adapter configured to display " //$NON-NLS-1$
139
+ url
140                                                             + ". Ensure that you have a required browser and adapter installed, and that the browser program is available on the system path.", //$NON-NLS-1$
141
null);
142                                     String JavaDoc 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         // initialize current browser
169
String JavaDoc curBrowserID = HelpBasePlugin.getDefault()
170                 .getPluginPreferences().getString(DEFAULT_BROWSER_ID_KEY);
171         if (curBrowserID != null && (!"".equals(curBrowserID))) { //$NON-NLS-1$
172
setCurrentBrowserID(curBrowserID);
173             // may fail if such browser does not exist
174
}
175         if (currentBrowserDesc == null) {
176             setCurrentBrowserID(getDefaultBrowserID());
177         }
178         setAlwaysUseExternal(HelpBasePlugin.getDefault().getPluginPreferences()
179                 .getBoolean(IHelpBaseConstants.P_KEY_ALWAYS_EXTERNAL_BROWSER));
180     }
181
182     /**
183      * Obtains singleton instance.
184      */

185     public static BrowserManager getInstance() {
186         if (instance == null)
187             instance = new BrowserManager();
188         return instance;
189     }
190
191     /**
192      * Creates all adapters, and returns available ones.
193      */

194     private BrowserDescriptor[] createBrowserDescriptors() {
195         if (this.browsersDescriptors != null)
196             return this.browsersDescriptors;
197         Collection JavaDoc bDescriptors = new ArrayList JavaDoc();
198         IConfigurationElement configElements[] = Platform
199                 .getExtensionRegistry().getConfigurationElementsFor(
200                         HelpBasePlugin.PLUGIN_ID, "browser"); //$NON-NLS-1$
201
for (int i = 0; i < configElements.length; i++) {
202             if (!configElements[i].getName().equals("browser")) //$NON-NLS-1$
203
continue;
204             String JavaDoc id = configElements[i].getAttribute("id"); //$NON-NLS-1$
205
if (id == null)
206                 continue;
207             String JavaDoc label = configElements[i].getAttribute("name"); //$NON-NLS-1$
208
if (label == null)
209                 continue;
210             try {
211                 Object JavaDoc adapter = configElements[i]
212                         .createExecutableExtension("factoryclass"); //$NON-NLS-1$
213
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     /**
233      * Obtains browsers descriptors.
234      */

235     public BrowserDescriptor[] getBrowserDescriptors() {
236         if (!initialized) {
237             init();
238         }
239         return this.browsersDescriptors;
240     }
241
242     /**
243      * Gets the currentBrowserID.
244      *
245      * @return Returns a String or null if not set
246      */

247     public String JavaDoc getCurrentBrowserID() {
248         if (!initialized) {
249             init();
250         }
251         if (currentBrowserDesc == null)
252             return null;
253         return currentBrowserDesc.getID();
254     }
255
256     /**
257      * Gets the currentBrowserID.
258      *
259      * @return Returns a String or null if not set
260      */

261     public String JavaDoc getCurrentInternalBrowserID() {
262         if (!initialized) {
263             init();
264         }
265         if (isEmbeddedBrowserPresent() && !alwaysUseExternal) {
266             return internalBrowserDesc.getID();
267         }
268         return getCurrentBrowserID();
269     }
270
271     /**
272      * Gets the currentBrowserID.
273      *
274      * @return Returns a String or null if not set
275      */

276     public String JavaDoc getDefaultBrowserID() {
277         if (!initialized) {
278             init();
279         }
280         if (defaultBrowserDesc == null)
281             return null;
282         return defaultBrowserDesc.getID();
283     }
284
285     /**
286      * Sets the currentBrowserID. If browser of given ID does not exists, the
287      * method does nothing
288      *
289      * @param currentAdapterID
290      * The ID of the adapter to to set as current
291      */

292     public void setCurrentBrowserID(String JavaDoc 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     /**
305      * Sets the defaultBrowserID. If browser of given ID does not exists, the
306      * method does nothing
307      *
308      * @param defaultAdapterID
309      * The ID of the adapter to to set as default
310      */

311     private void setDefaultBrowserID(String JavaDoc 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     /**
324      * Creates web browser If preferences specify to always use external, the
325      * parameter will not be honored
326      */

327     public IBrowser createBrowser(boolean external) {
328         if (!initialized) {
329             init();
330         }
331         //external = external || alwaysUseExternal;
332
//return createBrowserAdapter(forceExternal);
333
if (external) {
334             return new CurrentBrowser(createBrowserAdapter(true),
335                     getCurrentBrowserID(), true);
336         }
337         return new CurrentBrowser(createBrowserAdapter(alwaysUseExternal),
338                 getCurrentInternalBrowserID(), false);
339     }
340
341     /**
342      * Creates web browser
343      */

344     public IBrowser createBrowser() {
345         return createBrowser(true);
346     }
347
348     /**
349      * Creates web browser for external == false, if no internal browsers are
350      * present it will create external one
351      */

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     /**
367      * Closes all browsers created
368      */

369     public void closeAll() {
370         if (!initialized) {
371             // nothing to do, do not initialize
372
return;
373         }
374         for (Iterator JavaDoc 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