root / trunk / libraries / libIverUtiles / src / com / iver / utiles / BrowserControl.java @ 9656
History | View | Annotate | Download (7.69 KB)
1 |
/*
|
---|---|
2 |
* Created on 18-oct-2004
|
3 |
*
|
4 |
* TODO To change the template for this generated file go to
|
5 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package com.iver.utiles; |
48 |
|
49 |
/**
|
50 |
* @author Steven Spencer
|
51 |
*
|
52 |
* TODO To change the template for this generated type comment go to
|
53 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
54 |
*/
|
55 |
import java.io.IOException; |
56 |
import java.util.ArrayList; |
57 |
import java.util.logging.Level; |
58 |
import java.util.logging.Logger; |
59 |
|
60 |
|
61 |
/**
|
62 |
* <p>A simple, static class to display a URL in the system browser.<br></p>
|
63 |
*
|
64 |
* <p>Under <b>Unix</b> systems, it will open one of the browser set through the
|
65 |
* <b>setBrowserCommand(String)</b>. The argument of this method
|
66 |
* must be a fully qualified command or one of the commands returned
|
67 |
* by <b>getSupportedBrowsers()</b>.<br>By default, the browser is Firefox,
|
68 |
* which is included in the list of supported browsers.<br></p>
|
69 |
*
|
70 |
* <p>Under <b>Windows</b>, this will bring up the default browser under windows,
|
71 |
* usually either Netscape or Microsoft IE. The default browser is
|
72 |
* determined by the OS so no config is necessary.<br>This has been tested under Windows 95/98/NT/XP.</p>
|
73 |
* <p>Notice that <b>you must include</b> the url type -- either "http://" or "file://".</p>
|
74 |
*
|
75 |
* <p>Under <b>Mac</b>, the usability of this class is pending of test. So probably it does not work</p>
|
76 |
* Examples:
|
77 |
* BrowserControl.displayURL("http://www.javaworld.com")
|
78 |
* BrowserControl.displayURL("file://c:\\docs\\index.html")
|
79 |
* BrowserContorl.displayURL("file:///user/joe/index.html");
|
80 |
*/
|
81 |
public class BrowserControl |
82 |
{ |
83 |
private static Logger logger = Logger.getLogger(BrowserControl.class.getName()); |
84 |
public static final String OPERA = "Opera"; |
85 |
public static final String NETSCAPE = "Netscape"; |
86 |
public static final String MOZILLA = "Mozilla"; |
87 |
public static final String GALEON = "Galeon"; |
88 |
public static final String EPIPHANY = "Epiphany"; |
89 |
public static final String FIREFOX = "Firefox"; |
90 |
public static final String KONQUEROR = "Konqueror"; |
91 |
private static String browserCommand = FIREFOX; |
92 |
private static ArrayList supportedBrowsers; |
93 |
|
94 |
/**
|
95 |
* Display a file in the system browser. If you want to display a
|
96 |
* file, you must include the absolute path name.
|
97 |
*
|
98 |
* @param url the file's url (the url must start with either "http://" or "file://").
|
99 |
*/
|
100 |
public static void displayURL(String url) |
101 |
{ |
102 |
boolean windows = isWindowsPlatform();
|
103 |
String cmd = null; |
104 |
try
|
105 |
{ |
106 |
if (windows)
|
107 |
{ |
108 |
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
|
109 |
cmd = WIN_PATH + " " + WIN_FLAG + " " + url; |
110 |
Process p = Runtime.getRuntime().exec(cmd); |
111 |
} |
112 |
else
|
113 |
{ |
114 |
//
|
115 |
if (browserCommand.equals(NETSCAPE) ||
|
116 |
browserCommand.equals(FIREFOX) || |
117 |
browserCommand.equals(OPERA) || |
118 |
browserCommand.equals(MOZILLA)) { |
119 |
// Under Unix, Netscape has to be running for the "-remote"
|
120 |
// command to work. So, we try sending the command and
|
121 |
// check for an exit value. If the exit command is 0,
|
122 |
// it worked, otherwise we need to start the browser.
|
123 |
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
|
124 |
cmd = browserCommand.toLowerCase() + " -remote openURL(" + url + ")"; |
125 |
Process p = Runtime.getRuntime().exec(cmd); |
126 |
try
|
127 |
{ |
128 |
// wait for exit code -- if it's 0, command worked,
|
129 |
// otherwise we need to start the browser up.
|
130 |
int exitCode = p.waitFor();
|
131 |
if (exitCode != 0) |
132 |
{ |
133 |
// Command failed, start up the browser
|
134 |
// cmd = 'netscape http://www.javaworld.com'
|
135 |
cmd = browserCommand.toLowerCase() + " " + url;
|
136 |
p = Runtime.getRuntime().exec(cmd);
|
137 |
} |
138 |
} |
139 |
catch(InterruptedException x) |
140 |
{ |
141 |
System.err.println("Error bringing up browser, cmd='" + |
142 |
cmd + "'");
|
143 |
System.err.println("Caught: " + x); |
144 |
} |
145 |
} else if (browserCommand.equals(KONQUEROR)) { |
146 |
cmd = "konqueror "+ url;
|
147 |
Runtime.getRuntime().exec(cmd);
|
148 |
} else if (browserCommand.equals(EPIPHANY)) { |
149 |
cmd = "epiphany "+ url;
|
150 |
Runtime.getRuntime().exec(cmd);
|
151 |
// } else if (browserCommand.equals(GALEON)) {
|
152 |
// TODO available if command is rigth, check it
|
153 |
// cmd = "galeon "+ url;
|
154 |
// Runtime.getRuntime().exec(cmd);
|
155 |
} else {
|
156 |
cmd = cmd.replaceAll("%url|%URL", url);
|
157 |
logger.log(Level.FINEST, "Executing system runtime command: " + cmd); |
158 |
|
159 |
Runtime.getRuntime().exec(cmd);
|
160 |
} |
161 |
|
162 |
|
163 |
} |
164 |
} |
165 |
catch(IOException x) |
166 |
{ |
167 |
// couldn't exec browser
|
168 |
System.err.println("Could not invoke browser, command=" + cmd); |
169 |
System.err.println("Caught: " + x); |
170 |
} |
171 |
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* Try to determine whether this application is running under Windows
|
176 |
* or some other platform by examing the "os.name" property.
|
177 |
*
|
178 |
* @return true if this application is running under a Windows OS
|
179 |
*/
|
180 |
public static boolean isWindowsPlatform() |
181 |
{ |
182 |
String os = System.getProperty("os.name"); |
183 |
if ( os != null && os.startsWith(WIN_ID)) |
184 |
return true; |
185 |
else
|
186 |
return false; |
187 |
} |
188 |
|
189 |
|
190 |
public static void setBrowserCommand(String browserCommand) { |
191 |
BrowserControl.browserCommand = browserCommand; |
192 |
} |
193 |
/**
|
194 |
* Returns a list of supported browsers.
|
195 |
* @return
|
196 |
*/
|
197 |
public static ArrayList getSupportedBrowsers() { |
198 |
if (supportedBrowsers==null) |
199 |
{ |
200 |
supportedBrowsers = new ArrayList(); |
201 |
supportedBrowsers.add(KONQUEROR); |
202 |
supportedBrowsers.add(FIREFOX); |
203 |
supportedBrowsers.add(EPIPHANY); |
204 |
// supportedBrowsers.add(GALEON);
|
205 |
supportedBrowsers.add(MOZILLA); |
206 |
supportedBrowsers.add(NETSCAPE); |
207 |
// supportedBrowsers.add(OPERA);
|
208 |
} |
209 |
return supportedBrowsers;
|
210 |
} |
211 |
// Used to identify the windows platform.
|
212 |
private static final String WIN_ID = "Windows"; |
213 |
// The default system browser under windows.
|
214 |
private static final String WIN_PATH = "rundll32"; |
215 |
// The flag to display a url.
|
216 |
private static final String WIN_FLAG = "url.dll,FileProtocolHandler"; |
217 |
// The default browser under unix.
|
218 |
// private static final String UNIX_PATH = "netscape";
|
219 |
// The flag to display a url.
|
220 |
// private static final String UNIX_FLAG = "-remote openURL";
|
221 |
} |