1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Desktop;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.URI;
|
---|
10 |
|
---|
11 | import javax.swing.JApplet;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Helper to open platform web browser on different platforms
|
---|
17 | *
|
---|
18 | * This now delegates the real work to a platform specific class.
|
---|
19 | *
|
---|
20 | * @author Imi
|
---|
21 | */
|
---|
22 | public class OpenBrowser {
|
---|
23 |
|
---|
24 | private static void displayUrlFallback(URI uri) throws IOException {
|
---|
25 | if (Main.platform == null)
|
---|
26 | throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
|
---|
27 | Main.platform.openUrl(uri.toString());
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @return <code>null</code> for success or a string in case of an error.
|
---|
32 | * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
|
---|
33 | * {@link Main#platform}
|
---|
34 | */
|
---|
35 | public static String displayUrl(URI uri) {
|
---|
36 | if (Main.applet) {
|
---|
37 | try {
|
---|
38 | JApplet applet = (JApplet) Main.parent;
|
---|
39 | applet.getAppletContext().showDocument(uri.toURL());
|
---|
40 | return null;
|
---|
41 | } catch (MalformedURLException mue) {
|
---|
42 | return mue.getMessage();
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (Desktop.isDesktopSupported()) {
|
---|
47 | try {
|
---|
48 | try {
|
---|
49 | Desktop.getDesktop().browse(uri);
|
---|
50 | } catch (IOException e) {
|
---|
51 | // Workaround for KDE (Desktop API is severely flawed)
|
---|
52 | // see http://bugs.sun.com/view_bug.do?bug_id=6486393
|
---|
53 | System.err.println("Warning: Desktop class failed. Platform dependent fall back for open url in browser.");
|
---|
54 | displayUrlFallback(uri);
|
---|
55 | }
|
---|
56 | } catch (Exception e) {
|
---|
57 | e.printStackTrace();
|
---|
58 | return e.getMessage();
|
---|
59 | }
|
---|
60 | } else {
|
---|
61 | try {
|
---|
62 | System.err.println("Warning: Desktop class is not supported. Platform dependent fall back for open url in browser.");
|
---|
63 | displayUrlFallback(uri);
|
---|
64 | } catch (IOException e) {
|
---|
65 | return e.getMessage();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static String displayUrl(String url) {
|
---|
72 | try {
|
---|
73 | return displayUrl(new URI(url));
|
---|
74 | } catch (Exception e) {
|
---|
75 | return e.getMessage();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|