root / tags / v1_0_2_Build_903 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / BrowserControl.java @ 10704
History | View | Annotate | Download (5.49 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.cit.gvsig.gui.panels; |
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 |
|
57 |
/**
|
58 |
* A simple, static class to display a URL in the system browser.
|
59 |
|
60 |
|
61 |
*
|
62 |
* Under Unix, the system browser is hard-coded to be 'netscape'.
|
63 |
* Netscape must be in your PATH for this to work. This has been
|
64 |
* tested with the following platforms: AIX, HP-UX and Solaris.
|
65 |
|
66 |
|
67 |
*
|
68 |
* Under Windows, this will bring up the default browser under windows,
|
69 |
* usually either Netscape or Microsoft IE. The default browser is
|
70 |
* determined by the OS. This has been tested under Windows 95/98/NT.
|
71 |
|
72 |
|
73 |
*
|
74 |
* Examples:
|
75 |
|
76 |
|
77 |
*
|
78 |
BrowserControl.displayURL("http://www.javaworld.com")
|
79 |
*
|
80 |
BrowserControl.displayURL("file://c:\\docs\\index.html")
|
81 |
*
|
82 |
BrowserContorl.displayURL("file:///user/joe/index.html");
|
83 |
*
|
84 |
|
85 |
* Note - you must include the url type -- either "http://" or
|
86 |
* "file://".
|
87 |
*/
|
88 |
public class BrowserControl |
89 |
{ |
90 |
/**
|
91 |
* Display a file in the system browser. If you want to display a
|
92 |
* file, you must include the absolute path name.
|
93 |
*
|
94 |
* @param url the file's url (the url must start with either "http://"
|
95 |
or
|
96 |
* "file://").
|
97 |
*/
|
98 |
public static void displayURL(String url) |
99 |
{ |
100 |
boolean windows = isWindowsPlatform();
|
101 |
String cmd = null; |
102 |
try
|
103 |
{ |
104 |
if (windows)
|
105 |
{ |
106 |
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
|
107 |
cmd = WIN_PATH + " " + WIN_FLAG + " " + url; |
108 |
Process p = Runtime.getRuntime().exec(cmd); |
109 |
} |
110 |
else
|
111 |
{ |
112 |
// Under Unix, Netscape has to be running for the "-remote"
|
113 |
// command to work. So, we try sending the command and
|
114 |
// check for an exit value. If the exit command is 0,
|
115 |
// it worked, otherwise we need to start the browser.
|
116 |
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
|
117 |
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")"; |
118 |
Process p = Runtime.getRuntime().exec(cmd); |
119 |
try
|
120 |
{ |
121 |
// wait for exit code -- if it's 0, command worked,
|
122 |
// otherwise we need to start the browser up.
|
123 |
int exitCode = p.waitFor();
|
124 |
if (exitCode != 0) |
125 |
{ |
126 |
// Command failed, start up the browser
|
127 |
// cmd = 'netscape http://www.javaworld.com'
|
128 |
cmd = UNIX_PATH + " " + url;
|
129 |
p = Runtime.getRuntime().exec(cmd);
|
130 |
} |
131 |
} |
132 |
catch(InterruptedException x) |
133 |
{ |
134 |
System.err.println("Error bringing up browser, cmd='" + |
135 |
cmd + "'");
|
136 |
System.err.println("Caught: " + x); |
137 |
} |
138 |
} |
139 |
} |
140 |
catch(IOException x) |
141 |
{ |
142 |
// couldn't exec browser
|
143 |
System.err.println("Could not invoke browser, command=" + cmd); |
144 |
System.err.println("Caught: " + x); |
145 |
} |
146 |
} |
147 |
/**
|
148 |
* Try to determine whether this application is running under Windows
|
149 |
* or some other platform by examing the "os.name" property.
|
150 |
*
|
151 |
* @return true if this application is running under a Windows OS
|
152 |
*/
|
153 |
public static boolean isWindowsPlatform() |
154 |
{ |
155 |
String os = System.getProperty("os.name"); |
156 |
if ( os != null && os.startsWith(WIN_ID)) |
157 |
return true; |
158 |
else
|
159 |
return false; |
160 |
|
161 |
} |
162 |
// Used to identify the windows platform.
|
163 |
private static final String WIN_ID = "Windows"; |
164 |
// The default system browser under windows.
|
165 |
private static final String WIN_PATH = "rundll32"; |
166 |
// The flag to display a url.
|
167 |
private static final String WIN_FLAG = "url.dll,FileProtocolHandler"; |
168 |
// The default browser under unix.
|
169 |
private static final String UNIX_PATH = "netscape"; |
170 |
// The flag to display a url.
|
171 |
private static final String UNIX_FLAG = "-remote openURL"; |
172 |
} |