Organized imports
[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  * The main interface to the internal Web browser. If allows
26  * you to query the file types supported by the Web browser
27  * 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
39          * platform 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
49          * to use the internal browser, an external browser will be used. If
50          * not, a browser in the current page will be reused if forceNewPage
51          * is not true and the user preference is not set. Finally, showToolbar
52          * will decide when the 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 java.net.URL
112          */
113         public static void openURL(URL url) {
114                 IWebBrowser browser = getCurrentWebBrowser();
115                 if (browser != null)
116                         browser.openURL(url);
117                 else {
118                         Display.getDefault().asyncExec(new Runnable() {
119                                 public void run() {
120                                         WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%errorNoBrowser"));
121                                 }
122                         });
123                 }
124         }
125 }