55b33d965b0c62fa85ab1989036c7e84da7ada1c
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / org / eclipse / webbrowser / internal / BrowserManager.java
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package org.eclipse.webbrowser.internal;
12
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;
20
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;
30 /**
31  * 
32  */
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;
37
38         protected List browsers;
39         protected IWebBrowser currentBrowser;
40         protected List browserListeners = new ArrayList();
41         
42         private Preferences.IPropertyChangeListener pcl;
43         protected boolean ignorePreferenceChanges = false;
44
45         protected static BrowserManager instance;
46         
47         public static BrowserManager getInstance() {
48                 if (instance == null)
49                         instance = new BrowserManager();
50                 return instance;
51         }
52         
53         private BrowserManager() {
54                 pcl = new Preferences.IPropertyChangeListener() {
55                         public void propertyChange(Preferences.PropertyChangeEvent event) {
56                                 if (ignorePreferenceChanges)
57                                         return;
58                                 String property = event.getProperty();
59                                 if (property.equals("browsers")) {
60                                         loadBrowsers();
61                                 }
62                         }
63                 };
64                 
65                 WebBrowserUIPlugin.getInstance().getPluginPreferences().addPropertyChangeListener(pcl);
66         }
67         
68         protected void dispose() {
69                 WebBrowserUIPlugin.getInstance().getPluginPreferences().removePropertyChangeListener(pcl);
70                 
71                 // clear the cache
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);
79                         }
80                 }
81         }
82
83         public IExternalWebBrowserWorkingCopy createExternalWebBrowser() {
84                 return new ExternalWebBrowserWorkingCopy();
85         }       
86
87         public List getWebBrowsers() {
88                 if (browsers == null)
89                         loadBrowsers();
90                 return new ArrayList(browsers);
91         }
92         
93         protected void loadBrowsers() {
94                 Trace.trace(Trace.FINEST, "Loading web browsers");
95                 
96                 Preferences prefs = WebBrowserUIPlugin.getInstance().getPluginPreferences();
97                 String xmlString = prefs.getString("browsers");
98                 if (xmlString != null && xmlString.length() > 0) {
99                         browsers = new ArrayList();
100                         
101                         try {
102                                 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
103                                 Reader reader = new InputStreamReader(in);
104                                 IMemento memento = XMLMemento.createReadRoot(reader);
105                 
106                                 IMemento child = memento.getChild("internal");
107                                 if (child != null) {
108                                         InternalWebBrowser browser = new InternalWebBrowser();
109                                         browser.load(child);
110                                         browsers.add(browser);
111                                 }
112                                 
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);
119                                 }
120                                 
121                                 Integer current = memento.getInteger("current");
122                                 if (current != null) {
123                                         currentBrowser = (IWebBrowser) browsers.get(current.intValue()); 
124                                 }       
125                         } catch (Exception e) {
126                                 Trace.trace(Trace.WARNING, "Could not load browsers: " + e.getMessage());
127                         }
128                         addInternalBrowser(browsers);
129                         if (currentBrowser == null && browsers.size() > 0)
130                                 currentBrowser = (IWebBrowser) browsers.get(0);
131                 } else {
132                         setupDefaultBrowsers();
133                         saveBrowsers();
134                         return;
135                 }
136         }
137
138         protected void saveBrowsers() {
139                 try {
140                         ignorePreferenceChanges = true;
141                         XMLMemento memento = XMLMemento.createWriteRoot("web-browsers");
142
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");
149                                         browser.save(child);
150                                 } else if (obj instanceof ExternalWebBrowser) {
151                                         ExternalWebBrowser browser = (ExternalWebBrowser) obj;
152                                         IMemento child = memento.createChild("external");
153                                         browser.save(child);
154                                 }
155                         }
156                         
157                         memento.putInteger("current", browsers.indexOf(currentBrowser));
158
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);
167                 }
168                 ignorePreferenceChanges = false;
169         }
170         
171         protected void addInternalBrowser(List browserList) {
172                 if (browserList == null)
173                         return;
174
175                 Iterator iterator = browserList.iterator();
176                 while (iterator.hasNext()) {
177                         IWebBrowser browser = (IWebBrowser) iterator.next();
178                         if (browser instanceof IInternalWebBrowser)
179                                 return;
180                 }
181
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())
186                         return;
187                 
188                 browserList.add(0, new InternalWebBrowser());
189         }
190         
191         private void setupDefaultBrowsers() {
192                 browsers = new ArrayList();
193                 
194                 addInternalBrowser(browsers);
195                 
196                 // handle all the EXTERNAL browsers by criteria and add those too at startup
197                 WebBrowserUtil.addFoundBrowsers(browsers);
198                 
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);
202         }
203
204         protected void addBrowser(IExternalWebBrowser browser) {
205                 if (browsers == null)
206                         loadBrowsers();
207                 if (!browsers.contains(browser))
208                         browsers.add(browser);
209                 fireWebBrowserEvent(browser, ADD);
210                 saveBrowsers();
211         }
212         
213         protected void removeWebBrowser(IExternalWebBrowser browser) {
214                 if (browsers == null)
215                         loadBrowsers();
216                 browsers.remove(browser);
217                 fireWebBrowserEvent(browser, REMOVE);
218         }
219         
220         // Internal Web browser CAN be "edited", just not created or removed
221         protected void browserChanged(IWebBrowser browser) {
222                 fireWebBrowserEvent(browser, CHANGE);
223                 saveBrowsers();
224         }
225         
226         /**
227          * Add Web browser listener.
228          * @param listener
229          */
230         public void addWebBrowserListener(IWebBrowserListener listener) {
231                 browserListeners.add(listener);
232         }
233
234         /**
235          * Remove Web browser listener.
236          * @param listener
237          */
238         public void removeWebBrowserListener(IWebBrowserListener listener) {
239                 browserListeners.remove(listener);
240         }
241         
242         /**
243          * Fire a Web browser event.
244          * @param browser
245          * @param type
246          */
247         protected void fireWebBrowserEvent(IWebBrowser browser, int type) {
248                 Object[] obj = browserListeners.toArray();
249                 
250                 int size = obj.length;
251                 for (int i = 0; i < size; i++) {
252                         IWebBrowserListener listener = (IWebBrowserListener) obj[i];
253                         if (type == ADD)
254                                 listener.browserAdded(browser);
255                         else if (type == CHANGE)
256                                 listener.browserChanged(browser);
257                         else if (type == REMOVE)
258                                 listener.browserRemoved(browser);
259                 }
260         }
261         
262         public IWebBrowser getCurrentWebBrowser() {
263                 if (browsers == null)
264                         loadBrowsers();
265
266                 return currentBrowser; 
267         }
268
269         public void setCurrentWebBrowser(IWebBrowser wb) {
270                 if (browsers.contains(wb))
271                         currentBrowser = wb;
272                 saveBrowsers();
273         }
274 }