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 net.sourceforge.phpeclipse.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 net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser;
22 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
23 import net.sourceforge.phpeclipse.webbrowser.IInternalWebBrowser;
24 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Preferences;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.ui.IMemento;
30 import org.eclipse.ui.XMLMemento;
35 public class BrowserManager {
36 private static final int ADD = 0;
38 private static final int CHANGE = 1;
40 private static final int REMOVE = 2;
42 protected List browsers;
44 protected IWebBrowser currentBrowser;
46 protected List browserListeners = new ArrayList();
48 private Preferences.IPropertyChangeListener pcl;
50 protected boolean ignorePreferenceChanges = false;
52 protected static BrowserManager instance;
54 public static BrowserManager getInstance() {
56 instance = new BrowserManager();
60 private BrowserManager() {
61 pcl = new Preferences.IPropertyChangeListener() {
62 public void propertyChange(Preferences.PropertyChangeEvent event) {
63 if (ignorePreferenceChanges)
65 String property = event.getProperty();
66 if (property.equals("browsers")) {
72 WebBrowserUIPlugin.getInstance().getPluginPreferences()
73 .addPropertyChangeListener(pcl);
76 protected void dispose() {
77 WebBrowserUIPlugin.getInstance().getPluginPreferences()
78 .removePropertyChangeListener(pcl);
81 if (browsers != null) {
82 Iterator iterator = browsers.iterator();
83 while (iterator.hasNext()) {
84 Object obj = iterator.next();
85 if (obj instanceof IInternalWebBrowser) {
86 IInternalWebBrowser wb = (IInternalWebBrowser) obj;
87 if (wb.getClearHistoryOnExit())
88 WebBrowserPreference.setInternalWebBrowserHistory(null);
94 public IExternalWebBrowserWorkingCopy createExternalWebBrowser() {
95 return new ExternalWebBrowserWorkingCopy();
98 public List getWebBrowsers() {
101 return new ArrayList(browsers);
104 protected void loadBrowsers() {
105 Trace.trace(Trace.FINEST, "Loading web browsers");
107 Preferences prefs = WebBrowserUIPlugin.getInstance()
108 .getPluginPreferences();
109 String xmlString = prefs.getString("browsers");
110 if (xmlString != null && xmlString.length() > 0) {
111 browsers = new ArrayList();
114 ByteArrayInputStream in = new ByteArrayInputStream(xmlString
116 Reader reader = new InputStreamReader(in);
117 IMemento memento = XMLMemento.createReadRoot(reader);
119 IMemento child = memento.getChild("internal");
121 InternalWebBrowser browser = new InternalWebBrowser();
123 browsers.add(browser);
126 IMemento[] children = memento.getChildren("external");
127 int size = children.length;
128 for (int i = 0; i < size; i++) {
129 ExternalWebBrowser browser = new ExternalWebBrowser();
130 browser.load(children[i]);
131 browsers.add(browser);
134 Integer current = memento.getInteger("current");
135 if (current != null) {
136 currentBrowser = (IWebBrowser) browsers.get(current
139 } catch (Exception e) {
140 Trace.trace(Trace.WARNING, "Could not load browsers: "
143 addInternalBrowser(browsers);
144 if (currentBrowser == null && browsers.size() > 0)
145 currentBrowser = (IWebBrowser) browsers.get(0);
147 setupDefaultBrowsers();
153 protected void saveBrowsers() {
155 ignorePreferenceChanges = true;
156 XMLMemento memento = XMLMemento.createWriteRoot("web-browsers");
158 Iterator iterator = browsers.iterator();
159 while (iterator.hasNext()) {
160 Object obj = iterator.next();
161 if (obj instanceof InternalWebBrowser) {
162 InternalWebBrowser browser = (InternalWebBrowser) obj;
163 IMemento child = memento.createChild("internal");
165 } else if (obj instanceof ExternalWebBrowser) {
166 ExternalWebBrowser browser = (ExternalWebBrowser) obj;
167 IMemento child = memento.createChild("external");
172 memento.putInteger("current", browsers.indexOf(currentBrowser));
174 StringWriter writer = new StringWriter();
175 memento.save(writer);
176 String xmlString = writer.getBuffer().toString();
177 Preferences prefs = WebBrowserUIPlugin.getInstance()
178 .getPluginPreferences();
179 prefs.setValue("browsers", xmlString);
180 WebBrowserUIPlugin.getInstance().savePluginPreferences();
181 } catch (Exception e) {
182 Trace.trace(Trace.SEVERE, "Could not save browsers", e);
184 ignorePreferenceChanges = false;
187 protected void addInternalBrowser(List browserList) {
188 if (browserList == null)
191 Iterator iterator = browserList.iterator();
192 while (iterator.hasNext()) {
193 IWebBrowser browser = (IWebBrowser) iterator.next();
194 if (browser instanceof IInternalWebBrowser)
198 // add the internal browser if we can
199 // WebBrowserUIPlugin.getInstance().getLog().log(
200 // new Status(IStatus.INFO, WebBrowserUIPlugin.PLUGIN_ID, 0,
201 // WebBrowserUtil.canUseInternalWebBrowser() + "/"
202 // + WebBrowserUtil.isInternalBrowserOperational(), null));
203 // if (!WebBrowserUtil.canUseInternalWebBrowser() ||
204 // !WebBrowserUtil.isInternalBrowserOperational())
206 WebBrowserUIPlugin.getInstance().getLog().log(
207 new Status(IStatus.INFO, WebBrowserUIPlugin.PLUGIN_ID, 0, "-"
208 + WebBrowserUtil.isInternalBrowserOperational(), null));
209 if (!WebBrowserUtil.isInternalBrowserOperational())
211 browserList.add(0, new InternalWebBrowser());
214 private void setupDefaultBrowsers() {
215 browsers = new ArrayList();
217 addInternalBrowser(browsers);
219 // handle all the EXTERNAL browsers by criteria and add those too at
221 WebBrowserUtil.addFoundBrowsers(browsers);
223 // by default, if internal is there, that is current, else set the first
225 if (!browsers.isEmpty())
226 currentBrowser = (IWebBrowser) browsers.get(0);
229 protected void addBrowser(IExternalWebBrowser browser) {
230 if (browsers == null)
232 if (!browsers.contains(browser))
233 browsers.add(browser);
234 fireWebBrowserEvent(browser, ADD);
238 protected void removeWebBrowser(IExternalWebBrowser browser) {
239 if (browsers == null)
241 browsers.remove(browser);
242 fireWebBrowserEvent(browser, REMOVE);
245 // Internal Web browser CAN be "edited", just not created or removed
246 protected void browserChanged(IWebBrowser browser) {
247 fireWebBrowserEvent(browser, CHANGE);
252 * Add Web browser listener.
256 public void addWebBrowserListener(IWebBrowserListener listener) {
257 browserListeners.add(listener);
261 * Remove Web browser listener.
265 public void removeWebBrowserListener(IWebBrowserListener listener) {
266 browserListeners.remove(listener);
270 * Fire a Web browser event.
275 protected void fireWebBrowserEvent(IWebBrowser browser, int type) {
276 Object[] obj = browserListeners.toArray();
278 int size = obj.length;
279 for (int i = 0; i < size; i++) {
280 IWebBrowserListener listener = (IWebBrowserListener) obj[i];
282 listener.browserAdded(browser);
283 else if (type == CHANGE)
284 listener.browserChanged(browser);
285 else if (type == REMOVE)
286 listener.browserRemoved(browser);
290 public IWebBrowser getCurrentWebBrowser() {
291 if (browsers == null)
294 return currentBrowser;
297 public void setCurrentWebBrowser(IWebBrowser wb) {
298 if (browsers.contains(wb))