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 | import java.net.URL;
|
---|
11 |
|
---|
12 | import javax.swing.JApplet;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Helper to open platform web browser on different platforms
|
---|
18 | *
|
---|
19 | * This now delegates the real work to a platform specific class.
|
---|
20 | *
|
---|
21 | * @author Imi
|
---|
22 | */
|
---|
23 | public class OpenBrowser {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @return <code>null</code> for success or a string in case of an error.
|
---|
27 | * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
|
---|
28 | * {@see Main#platform}
|
---|
29 | */
|
---|
30 | public static String displayUrl(URI uri) {
|
---|
31 | if (Main.applet) {
|
---|
32 | try {
|
---|
33 | JApplet applet = (JApplet) Main.parent;
|
---|
34 | applet.getAppletContext().showDocument(uri.toURL());
|
---|
35 | return null;
|
---|
36 | } catch (MalformedURLException mue) {
|
---|
37 | return mue.getMessage();
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (Desktop.isDesktopSupported()) {
|
---|
42 | try {
|
---|
43 | Desktop.getDesktop().browse(uri);
|
---|
44 | } catch (Exception e) {
|
---|
45 | e.printStackTrace();
|
---|
46 | return e.getMessage();
|
---|
47 | }
|
---|
48 | } else {
|
---|
49 | System.err.println("Warning: Desktop class is not supported. Platform dependent fall back for open url in browser.");
|
---|
50 |
|
---|
51 | if (Main.platform == null)
|
---|
52 | throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
|
---|
53 | try {
|
---|
54 | Main.platform.openUrl(uri.toString());
|
---|
55 | } catch (IOException e) {
|
---|
56 | return e.getMessage();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static String displayUrl(String url) {
|
---|
63 | try {
|
---|
64 | return displayUrl(new URI(url));
|
---|
65 | } catch (Exception e) {
|
---|
66 | return e.getMessage();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|