1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.MalformedURLException;
|
---|
6 | import java.net.URL;
|
---|
7 |
|
---|
8 | import javax.swing.JApplet;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Helper to open platform web browser on different platforms
|
---|
14 | * @author Imi
|
---|
15 | */
|
---|
16 | public class OpenBrowser {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @return <code>null</code> for success or a string in case of an error.
|
---|
20 | */
|
---|
21 | public static String displayUrl(String url) {
|
---|
22 | if (Main.applet) {
|
---|
23 | try {
|
---|
24 | JApplet applet = (JApplet) Main.parent;
|
---|
25 | applet.getAppletContext().showDocument(new URL(url));
|
---|
26 | return null;
|
---|
27 | } catch (MalformedURLException mue) {
|
---|
28 | return mue.getMessage();
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | String os = System.getProperty("os.name");
|
---|
33 | if (os == null)
|
---|
34 | return "unknown operating system";
|
---|
35 | try {
|
---|
36 | if (os != null && os.startsWith("Windows"))
|
---|
37 | windows(url);
|
---|
38 | else if (os.equals("Linux") || os.equals("Solaris") || os.equals("SunOS") || os.equals("AIX") || os.equals("FreeBSD"))
|
---|
39 | linux(url);
|
---|
40 | else if (os.equals("Mac OS") || os.equals("Mac OS X"))
|
---|
41 | mac(url);
|
---|
42 | else
|
---|
43 | return "unknown operating system";
|
---|
44 | } catch (IOException e) {
|
---|
45 | return e.getMessage();
|
---|
46 | }
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private static void windows(String url) throws IOException {
|
---|
51 | Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
|
---|
52 | }
|
---|
53 |
|
---|
54 | private static void linux(String url) {
|
---|
55 | String[] programs = {"gnome-open", "kfmclient openURL", "firefox"};
|
---|
56 | for (String program : programs) {
|
---|
57 | try {
|
---|
58 | Runtime.getRuntime().exec(program+" "+url);
|
---|
59 | return;
|
---|
60 | } catch (IOException e) {
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static void mac(String url) throws IOException {
|
---|
66 | Runtime.getRuntime().exec("open " + url);
|
---|
67 | }
|
---|
68 | }
|
---|