1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package org.eclipse.webbrowser.internal;
13 import java.io.ByteArrayInputStream;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
16 import java.io.StringWriter;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Preferences;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.ui.IMemento;
25 import org.eclipse.ui.XMLMemento;
26 import org.eclipse.webbrowser.IExternalWebBrowser;
27 import org.eclipse.webbrowser.IExternalWebBrowserWorkingCopy;
28 import org.eclipse.webbrowser.IInternalWebBrowser;
29 import org.eclipse.webbrowser.IWebBrowser;
33 public class BrowserManager {
34 private static final int ADD = 0;
35 private static final int CHANGE = 1;
36 private static final int REMOVE = 2;
38 protected List browsers;
39 protected IWebBrowser currentBrowser;
40 protected List browserListeners = new ArrayList();
42 private Preferences.IPropertyChangeListener pcl;
43 protected boolean ignorePreferenceChanges = false;
45 protected static BrowserManager instance;
47 public static BrowserManager getInstance() {
49 instance = new BrowserManager();
53 private BrowserManager() {
54 pcl = new Preferences.IPropertyChangeListener() {
55 public void propertyChange(Preferences.PropertyChangeEvent event) {
56 if (ignorePreferenceChanges)
58 String property = event.getProperty();
59 if (property.equals("browsers")) {
65 WebBrowserUIPlugin.getInstance().getPluginPreferences().addPropertyChangeListener(pcl);
68 protected void dispose() {
69 WebBrowserUIPlugin.getInstance().getPluginPreferences().removePropertyChangeListener(pcl);
72 Iterator iterator = browsers.iterator();
73 while (iterator.hasNext()) {
74 Object obj = iterator.next();
75 if (obj instanceof IInternalWebBrowser) {
76 IInternalWebBrowser wb = (IInternalWebBrowser) obj;
77 if (wb.getClearHistoryOnExit())
78 WebBrowserPreference.setInternalWebBrowserHistory(null);
83 public IExternalWebBrowserWorkingCopy createExternalWebBrowser() {
84 return new ExternalWebBrowserWorkingCopy();
87 public List getWebBrowsers() {
90 return new ArrayList(browsers);
93 protected void loadBrowsers() {
94 Trace.trace(Trace.FINEST, "Loading web browsers");
96 Preferences prefs = WebBrowserUIPlugin.getInstance().getPluginPreferences();
97 String xmlString = prefs.getString("browsers");
98 if (xmlString != null && xmlString.length() > 0) {
99 browsers = new ArrayList();
102 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
103 Reader reader = new InputStreamReader(in);
104 IMemento memento = XMLMemento.createReadRoot(reader);
106 IMemento child = memento.getChild("internal");
108 InternalWebBrowser browser = new InternalWebBrowser();
110 browsers.add(browser);
113 IMemento[] children = memento.getChildren("external");
114 int size = children.length;
115 for (int i = 0; i < size; i++) {
116 ExternalWebBrowser browser = new ExternalWebBrowser();
117 browser.load(children[i]);
118 browsers.add(browser);
121 Integer current = memento.getInteger("current");
122 if (current != null) {
123 currentBrowser = (IWebBrowser) browsers.get(current.intValue());
125 } catch (Exception e) {
126 Trace.trace(Trace.WARNING, "Could not load browsers: " + e.getMessage());
128 addInternalBrowser(browsers);
129 if (currentBrowser == null && browsers.size() > 0)
130 currentBrowser = (IWebBrowser) browsers.get(0);
132 setupDefaultBrowsers();
138 protected void saveBrowsers() {
140 ignorePreferenceChanges = true;
141 XMLMemento memento = XMLMemento.createWriteRoot("web-browsers");
143 Iterator iterator = browsers.iterator();
144 while (iterator.hasNext()) {
145 Object obj = iterator.next();
146 if (obj instanceof InternalWebBrowser) {
147 InternalWebBrowser browser = (InternalWebBrowser) obj;
148 IMemento child = memento.createChild("internal");
150 } else if (obj instanceof ExternalWebBrowser) {
151 ExternalWebBrowser browser = (ExternalWebBrowser) obj;
152 IMemento child = memento.createChild("external");
157 memento.putInteger("current", browsers.indexOf(currentBrowser));
159 StringWriter writer = new StringWriter();
160 memento.save(writer);
161 String xmlString = writer.getBuffer().toString();
162 Preferences prefs = WebBrowserUIPlugin.getInstance().getPluginPreferences();
163 prefs.setValue("browsers", xmlString);
164 WebBrowserUIPlugin.getInstance().savePluginPreferences();
165 } catch (Exception e) {
166 Trace.trace(Trace.SEVERE, "Could not save browsers", e);
168 ignorePreferenceChanges = false;
171 protected void addInternalBrowser(List browserList) {
172 if (browserList == null)
175 Iterator iterator = browserList.iterator();
176 while (iterator.hasNext()) {
177 IWebBrowser browser = (IWebBrowser) iterator.next();
178 if (browser instanceof IInternalWebBrowser)
182 // add the internal browser if we can
183 WebBrowserUIPlugin.getInstance().getLog().log(new Status(IStatus.INFO,
184 WebBrowserUIPlugin.PLUGIN_ID, 0, WebBrowserUtil.canUseInternalWebBrowser() + "/" + WebBrowserUtil.isInternalBrowserOperational(), null));
185 if (!WebBrowserUtil.canUseInternalWebBrowser() || !WebBrowserUtil.isInternalBrowserOperational())
188 browserList.add(0, new InternalWebBrowser());
191 private void setupDefaultBrowsers() {
192 browsers = new ArrayList();
194 addInternalBrowser(browsers);
196 // handle all the EXTERNAL browsers by criteria and add those too at startup
197 WebBrowserUtil.addFoundBrowsers(browsers);
199 // by default, if internal is there, that is current, else set the first external one
200 if (!browsers.isEmpty())
201 currentBrowser = (IWebBrowser) browsers.get(0);
204 protected void addBrowser(IExternalWebBrowser browser) {
205 if (browsers == null)
207 if (!browsers.contains(browser))
208 browsers.add(browser);
209 fireWebBrowserEvent(browser, ADD);
213 protected void removeWebBrowser(IExternalWebBrowser browser) {
214 if (browsers == null)
216 browsers.remove(browser);
217 fireWebBrowserEvent(browser, REMOVE);
220 // Internal Web browser CAN be "edited", just not created or removed
221 protected void browserChanged(IWebBrowser browser) {
222 fireWebBrowserEvent(browser, CHANGE);
227 * Add Web browser listener.
230 public void addWebBrowserListener(IWebBrowserListener listener) {
231 browserListeners.add(listener);
235 * Remove Web browser listener.
238 public void removeWebBrowserListener(IWebBrowserListener listener) {
239 browserListeners.remove(listener);
243 * Fire a Web browser event.
247 protected void fireWebBrowserEvent(IWebBrowser browser, int type) {
248 Object[] obj = browserListeners.toArray();
250 int size = obj.length;
251 for (int i = 0; i < size; i++) {
252 IWebBrowserListener listener = (IWebBrowserListener) obj[i];
254 listener.browserAdded(browser);
255 else if (type == CHANGE)
256 listener.browserChanged(browser);
257 else if (type == REMOVE)
258 listener.browserRemoved(browser);
262 public IWebBrowser getCurrentWebBrowser() {
263 if (browsers == null)
266 return currentBrowser;
269 public void setCurrentWebBrowser(IWebBrowser wb) {
270 if (browsers.contains(wb))