svn-gvsig-desktop / tags / v1_1_Build_1007 / libraries / libIverUtiles / src / com / iver / utiles / BrowserControl.java @ 12478
History | View | Annotate | Download (7.57 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 |
import java.io.IOException; |
50 |
import java.util.ArrayList; |
51 |
import java.util.logging.Level; |
52 |
import java.util.logging.Logger; |
53 |
|
54 |
/**
|
55 |
* <p>A simple, static class to display a URL in the system browser.<br></p>
|
56 |
*
|
57 |
* <p>Under <b>Unix</b> systems, it will open one of the browser set through the
|
58 |
* <b>setBrowserCommand(String)</b>. The argument of this method
|
59 |
* must be a fully qualified command or one of the commands returned
|
60 |
* by <b>getSupportedBrowsers()</b>.<br>By default, the browser is Firefox,
|
61 |
* which is included in the list of supported browsers.<br></p>
|
62 |
*
|
63 |
* <p>Under <b>Windows</b>, this will bring up the default browser under windows,
|
64 |
* usually either Netscape or Microsoft IE. The default browser is
|
65 |
* determined by the OS so no config is necessary.<br>This has been tested under Windows 95/98/NT/XP.</p>
|
66 |
* <p>Notice that <b>you must include</b> the url type -- either "http://" or "file://".</p>
|
67 |
*
|
68 |
* <p>Under <b>Mac</b>, the usability of this class is pending of test. So probably it does not work</p>
|
69 |
* Examples:
|
70 |
* BrowserControl.displayURL("http://www.javaworld.com")
|
71 |
* BrowserControl.displayURL("file://c:\\docs\\index.html")
|
72 |
* BrowserContorl.displayURL("file:///user/joe/index.html");
|
73 |
*
|
74 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
75 |
*/
|
76 |
public class BrowserControl |
77 |
{ |
78 |
private static Logger logger = Logger.getLogger(BrowserControl.class.getName()); |
79 |
public static final String OPERA = "Opera"; |
80 |
public static final String NETSCAPE = "Netscape"; |
81 |
public static final String MOZILLA = "Mozilla"; |
82 |
public static final String GALEON = "Galeon"; |
83 |
public static final String EPIPHANY = "Epiphany"; |
84 |
public static final String FIREFOX = "Firefox"; |
85 |
public static final String KONQUEROR = "Konqueror"; |
86 |
private static String browserCommand = FIREFOX; |
87 |
private static ArrayList supportedBrowsers; |
88 |
|
89 |
/**
|
90 |
* Display a file in the system browser. If you want to display a
|
91 |
* file, you must include the absolute path name.
|
92 |
*
|
93 |
* @param url the file's url (the url must start with either "http://" or "file://").
|
94 |
*/
|
95 |
public static void displayURL(String url) |
96 |
{ |
97 |
boolean windows = isWindowsPlatform();
|
98 |
String cmd = null; |
99 |
try
|
100 |
{ |
101 |
if (windows)
|
102 |
{ |
103 |
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
|
104 |
cmd = WIN_PATH + " " + WIN_FLAG + " " + url; |
105 |
Process p = Runtime.getRuntime().exec(cmd); |
106 |
} |
107 |
else
|
108 |
{ |
109 |
//
|
110 |
if (browserCommand.equals(NETSCAPE) ||
|
111 |
browserCommand.equals(FIREFOX) || |
112 |
browserCommand.equals(OPERA) || |
113 |
browserCommand.equals(MOZILLA)) { |
114 |
// Under Unix, Netscape has to be running for the "-remote"
|
115 |
// command to work. So, we try sending the command and
|
116 |
// check for an exit value. If the exit command is 0,
|
117 |
// it worked, otherwise we need to start the browser.
|
118 |
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
|
119 |
cmd = browserCommand.toLowerCase() + " -remote openURL(" + url + ")"; |
120 |
Process p = Runtime.getRuntime().exec(cmd); |
121 |
try
|
122 |
{ |
123 |
// wait for exit code -- if it's 0, command worked,
|
124 |
// otherwise we need to start the browser up.
|
125 |
int exitCode = p.waitFor();
|
126 |
if (exitCode != 0) |
127 |
{ |
128 |
// Command failed, start up the browser
|
129 |
// cmd = 'netscape http://www.javaworld.com'
|
130 |
cmd = browserCommand.toLowerCase() + " " + url;
|
131 |
p = Runtime.getRuntime().exec(cmd);
|
132 |
} |
133 |
} |
134 |
catch(InterruptedException x) |
135 |
{ |
136 |
System.err.println("Error bringing up browser, cmd='" + |
137 |
cmd + "'");
|
138 |
System.err.println("Caught: " + x); |
139 |
} |
140 |
} else if (browserCommand.equals(KONQUEROR)) { |
141 |
cmd = "konqueror "+ url;
|
142 |
Runtime.getRuntime().exec(cmd);
|
143 |
} else if (browserCommand.equals(EPIPHANY)) { |
144 |
cmd = "epiphany "+ url;
|
145 |
Runtime.getRuntime().exec(cmd);
|
146 |
// } else if (browserCommand.equals(GALEON)) {
|
147 |
// TODO available if command is rigth, check it
|
148 |
// cmd = "galeon "+ url;
|
149 |
// Runtime.getRuntime().exec(cmd);
|
150 |
} else {
|
151 |
cmd = cmd.replaceAll("%url|%URL", url);
|
152 |
logger.log(Level.FINEST, "Executing system runtime command: " + cmd); |
153 |
|
154 |
Runtime.getRuntime().exec(cmd);
|
155 |
} |
156 |
|
157 |
|
158 |
} |
159 |
} |
160 |
catch(IOException x) |
161 |
{ |
162 |
// couldn't exec browser
|
163 |
System.err.println("Could not invoke browser, command=" + cmd); |
164 |
System.err.println("Caught: " + x); |
165 |
} |
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Try to determine whether this application is running under Windows
|
171 |
* or some other platform by examing the "os.name" property.
|
172 |
*
|
173 |
* @return true if this application is running under a Windows OS
|
174 |
*/
|
175 |
public static boolean isWindowsPlatform() |
176 |
{ |
177 |
String os = System.getProperty("os.name"); |
178 |
if ( os != null && os.startsWith(WIN_ID)) |
179 |
return true; |
180 |
else
|
181 |
return false; |
182 |
} |
183 |
|
184 |
|
185 |
public static void setBrowserCommand(String browserCommand) { |
186 |
BrowserControl.browserCommand = browserCommand; |
187 |
} |
188 |
/**
|
189 |
* Returns a list of supported browsers.
|
190 |
* @return
|
191 |
*/
|
192 |
public static ArrayList getSupportedBrowsers() { |
193 |
if (supportedBrowsers==null) |
194 |
{ |
195 |
supportedBrowsers = new ArrayList(); |
196 |
supportedBrowsers.add(KONQUEROR); |
197 |
supportedBrowsers.add(FIREFOX); |
198 |
supportedBrowsers.add(EPIPHANY); |
199 |
// supportedBrowsers.add(GALEON);
|
200 |
supportedBrowsers.add(MOZILLA); |
201 |
supportedBrowsers.add(NETSCAPE); |
202 |
supportedBrowsers.add(OPERA); |
203 |
} |
204 |
return supportedBrowsers;
|
205 |
} |
206 |
// Used to identify the windows platform.
|
207 |
private static final String WIN_ID = "Windows"; |
208 |
// The default system browser under windows.
|
209 |
private static final String WIN_PATH = "rundll32"; |
210 |
// The flag to display a url.
|
211 |
private static final String WIN_FLAG = "url.dll,FileProtocolHandler"; |
212 |
// The default browser under unix.
|
213 |
// private static final String UNIX_PATH = "netscape";
|
214 |
// The flag to display a url.
|
215 |
// private static final String UNIX_FLAG = "-remote openURL";
|
216 |
} |