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().addPropertyChangeListener(pcl);
75 protected void dispose() {
76 WebBrowserUIPlugin.getInstance().getPluginPreferences().removePropertyChangeListener(pcl);
79 if (browsers != null) {
80 Iterator iterator = browsers.iterator();
81 while (iterator.hasNext()) {
82 Object obj = iterator.next();
83 if (obj instanceof IInternalWebBrowser) {
84 IInternalWebBrowser wb = (IInternalWebBrowser) obj;
85 if (wb.getClearHistoryOnExit())
86 WebBrowserPreference.setInternalWebBrowserHistory(null);
92 public IExternalWebBrowserWorkingCopy createExternalWebBrowser() {
93 return new ExternalWebBrowserWorkingCopy();
96 public List getWebBrowsers() {
99 return new ArrayList(browsers);
102 protected void loadBrowsers() {
103 Trace.trace(Trace.FINEST, "Loading web browsers");
105 Preferences prefs = WebBrowserUIPlugin.getInstance().getPluginPreferences();
106 String xmlString = prefs.getString("browsers");
107 if (xmlString != null && xmlString.length() > 0) {
108 browsers = new ArrayList();
111 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
112 Reader reader = new InputStreamReader(in);
113 IMemento memento = XMLMemento.createReadRoot(reader);
115 IMemento child = memento.getChild("internal");
117 InternalWebBrowser browser = new InternalWebBrowser();
119 browsers.add(browser);
122 IMemento[] children = memento.getChildren("external");
123 int size = children.length;
124 for (int i = 0; i < size; i++) {
125 ExternalWebBrowser browser = new ExternalWebBrowser();
126 browser.load(children[i]);
127 browsers.add(browser);
130 Integer current = memento.getInteger("current");
131 if (current != null) {
132 currentBrowser = (IWebBrowser) browsers.get(current.intValue());
134 } catch (Exception e) {
135 Trace.trace(Trace.WARNING, "Could not load browsers: " + e.getMessage());
137 addInternalBrowser(browsers);
138 if (currentBrowser == null && browsers.size() > 0)
139 currentBrowser = (IWebBrowser) browsers.get(0);
141 setupDefaultBrowsers();
147 protected void saveBrowsers() {
149 ignorePreferenceChanges = true;
150 XMLMemento memento = XMLMemento.createWriteRoot("web-browsers");
152 Iterator iterator = browsers.iterator();
153 while (iterator.hasNext()) {
154 Object obj = iterator.next();
155 if (obj instanceof InternalWebBrowser) {
156 InternalWebBrowser browser = (InternalWebBrowser) obj;
157 IMemento child = memento.createChild("internal");
159 } else if (obj instanceof ExternalWebBrowser) {
160 ExternalWebBrowser browser = (ExternalWebBrowser) obj;
161 IMemento child = memento.createChild("external");
166 memento.putInteger("current", browsers.indexOf(currentBrowser));
168 StringWriter writer = new StringWriter();
169 memento.save(writer);
170 String xmlString = writer.getBuffer().toString();
171 Preferences prefs = WebBrowserUIPlugin.getInstance().getPluginPreferences();
172 prefs.setValue("browsers", xmlString);
173 WebBrowserUIPlugin.getInstance().savePluginPreferences();
174 } catch (Exception e) {
175 Trace.trace(Trace.SEVERE, "Could not save browsers", e);
177 ignorePreferenceChanges = false;
180 protected void addInternalBrowser(List browserList) {
181 if (browserList == null)
184 Iterator iterator = browserList.iterator();
185 while (iterator.hasNext()) {
186 IWebBrowser browser = (IWebBrowser) iterator.next();
187 if (browser instanceof IInternalWebBrowser)
191 // add the internal browser if we can
192 // WebBrowserUIPlugin.getInstance().getLog().log(
193 // new Status(IStatus.INFO, WebBrowserUIPlugin.PLUGIN_ID, 0, WebBrowserUtil.canUseInternalWebBrowser() + "/"
194 // + WebBrowserUtil.isInternalBrowserOperational(), null));
195 // if (!WebBrowserUtil.canUseInternalWebBrowser() || !WebBrowserUtil.isInternalBrowserOperational())
197 WebBrowserUIPlugin.getInstance().getLog().log(
198 new Status(IStatus.INFO, WebBrowserUIPlugin.PLUGIN_ID, 0,
199 "-"+WebBrowserUtil.isInternalBrowserOperational(), null));
200 if (!WebBrowserUtil.isInternalBrowserOperational())
202 browserList.add(0, new InternalWebBrowser());
205 private void setupDefaultBrowsers() {
206 browsers = new ArrayList();
208 addInternalBrowser(browsers);
210 // handle all the EXTERNAL browsers by criteria and add those too at startup
211 WebBrowserUtil.addFoundBrowsers(browsers);
213 // by default, if internal is there, that is current, else set the first external one
214 if (!browsers.isEmpty())
215 currentBrowser = (IWebBrowser) browsers.get(0);
218 protected void addBrowser(IExternalWebBrowser browser) {
219 if (browsers == null)
221 if (!browsers.contains(browser))
222 browsers.add(browser);
223 fireWebBrowserEvent(browser, ADD);
227 protected void removeWebBrowser(IExternalWebBrowser browser) {
228 if (browsers == null)
230 browsers.remove(browser);
231 fireWebBrowserEvent(browser, REMOVE);
234 // Internal Web browser CAN be "edited", just not created or removed
235 protected void browserChanged(IWebBrowser browser) {
236 fireWebBrowserEvent(browser, CHANGE);
241 * Add Web browser listener.
245 public void addWebBrowserListener(IWebBrowserListener listener) {
246 browserListeners.add(listener);
250 * Remove Web browser listener.
254 public void removeWebBrowserListener(IWebBrowserListener listener) {
255 browserListeners.remove(listener);
259 * Fire a Web browser event.
264 protected void fireWebBrowserEvent(IWebBrowser browser, int type) {
265 Object[] obj = browserListeners.toArray();
267 int size = obj.length;
268 for (int i = 0; i < size; i++) {
269 IWebBrowserListener listener = (IWebBrowserListener) obj[i];
271 listener.browserAdded(browser);
272 else if (type == CHANGE)
273 listener.browserChanged(browser);
274 else if (type == REMOVE)
275 listener.browserRemoved(browser);
279 public IWebBrowser getCurrentWebBrowser() {
280 if (browsers == null)
283 return currentBrowser;
286 public void setCurrentWebBrowser(IWebBrowser wb) {
287 if (browsers.contains(wb))