A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / WebBrowser.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 net.sourceforge.phpeclipse.webbrowser;
12
13 import java.net.URL;
14 import java.util.List;
15
16 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
17 import net.sourceforge.phpeclipse.webbrowser.internal.ExternalWebBrowserWorkingCopy;
18 import net.sourceforge.phpeclipse.webbrowser.internal.Trace;
19 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserEditor;
20 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUIPlugin;
21 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
22
23 import org.eclipse.swt.widgets.Display;
24
25 /**
26  * The main interface to the internal Web browser. If allows you to query the
27  * file types supported by the Web browser and open a URL.
28  */
29 public class WebBrowser {
30         /**
31          * WebBrowser constructor comment.
32          */
33         private WebBrowser() {
34                 super();
35         }
36
37         /**
38          * Returns true if the internal Web browser is supported on this platform
39          * and the user has chosen to use it.
40          * 
41          * @return boolean
42          */
43         public static boolean isUsingInternalBrowser() {
44                 return (getCurrentWebBrowser() instanceof IInternalWebBrowser);
45         }
46
47         /**
48          * Display the given URL in a Web browser. If the user has chosen not to use
49          * the internal browser, an external browser will be used. If not, a browser
50          * in the current page will be reused if forceNewPage is not true and the
51          * user preference is not set. Finally, showToolbar will decide when the
52          * toolbar should be shown in the internal browser.
53          * 
54          * @param input
55          */
56         public static void openURL(final IWebBrowserEditorInput input) {
57                 Trace.trace(Trace.FINEST, "openURL() " + input);
58                 if (input == null)
59                         return;
60
61                 Display.getDefault().asyncExec(new Runnable() {
62                         public void run() {
63                                 if (!isUsingInternalBrowser()) {
64                                         IWebBrowser browser = getCurrentWebBrowser();
65                                         browser.openURL(input.getURL());
66                                 } else
67                                         WebBrowserEditor.open(input);
68                         }
69                 });
70         }
71
72         /**
73          * Return a list of all the installed Web browsers.
74          * 
75          * @return
76          */
77         public static List getWebBrowsers() {
78                 return BrowserManager.getInstance().getWebBrowsers();
79         }
80
81         /**
82          * Return the current default web browser.
83          * 
84          * @return
85          */
86         public static IWebBrowser getCurrentWebBrowser() {
87                 return BrowserManager.getInstance().getCurrentWebBrowser();
88         }
89
90         /**
91          * Set the current default web browser.
92          * 
93          * @return
94          */
95         public static void getCurrentWebBrowser(IWebBrowser browser) {
96                 BrowserManager.getInstance().setCurrentWebBrowser(browser);
97         }
98
99         /**
100          * Create a new external Web browser.
101          * 
102          * @return
103          */
104         public static IExternalWebBrowserWorkingCopy createExternalWebBrowser() {
105                 return new ExternalWebBrowserWorkingCopy();
106         }
107
108         /**
109          * Display the given URL in a Web browser.
110          * 
111          * @param url
112          *            java.net.URL
113          */
114         public static void openURL(URL url) {
115                 IWebBrowser browser = getCurrentWebBrowser();
116                 if (browser != null)
117                         browser.openURL(url);
118                 else {
119                         Display.getDefault().asyncExec(new Runnable() {
120                                 public void run() {
121                                         WebBrowserUtil.openError(WebBrowserUIPlugin
122                                                         .getResource("%errorNoBrowser"));
123                                 }
124                         });
125                 }
126         }
127 }