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())
198 browserList.add(0, new InternalWebBrowser());
201 private void setupDefaultBrowsers() {
202 browsers = new ArrayList();
204 addInternalBrowser(browsers);
206 // handle all the EXTERNAL browsers by criteria and add those too at startup
207 WebBrowserUtil.addFoundBrowsers(browsers);
209 // by default, if internal is there, that is current, else set the first external one
210 if (!browsers.isEmpty())
211 currentBrowser = (IWebBrowser) browsers.get(0);
214 protected void addBrowser(IExternalWebBrowser browser) {
215 if (browsers == null)
217 if (!browsers.contains(browser))
218 browsers.add(browser);
219 fireWebBrowserEvent(browser, ADD);
223 protected void removeWebBrowser(IExternalWebBrowser browser) {
224 if (browsers == null)
226 browsers.remove(browser);
227 fireWebBrowserEvent(browser, REMOVE);
230 // Internal Web browser CAN be "edited", just not created or removed
231 protected void browserChanged(IWebBrowser browser) {
232 fireWebBrowserEvent(browser, CHANGE);
237 * Add Web browser listener.
241 public void addWebBrowserListener(IWebBrowserListener listener) {
242 browserListeners.add(listener);
246 * Remove Web browser listener.
250 public void removeWebBrowserListener(IWebBrowserListener listener) {
251 browserListeners.remove(listener);
255 * Fire a Web browser event.
260 protected void fireWebBrowserEvent(IWebBrowser browser, int type) {
261 Object[] obj = browserListeners.toArray();
263 int size = obj.length;
264 for (int i = 0; i < size; i++) {
265 IWebBrowserListener listener = (IWebBrowserListener) obj[i];
267 listener.browserAdded(browser);
268 else if (type == CHANGE)
269 listener.browserChanged(browser);
270 else if (type == REMOVE)
271 listener.browserRemoved(browser);
275 public IWebBrowser getCurrentWebBrowser() {
276 if (browsers == null)
279 return currentBrowser;
282 public void setCurrentWebBrowser(IWebBrowser wb) {
283 if (browsers.contains(wb))