1 | // License: GPL. For details, see LICENSE file.
|
---|
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.URI;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Helper to open platform web browser on different platforms
|
---|
14 | *
|
---|
15 | * This now delegates the real work to a platform specific class.
|
---|
16 | *
|
---|
17 | * @author Imi
|
---|
18 | */
|
---|
19 | public final class OpenBrowser {
|
---|
20 |
|
---|
21 | private OpenBrowser() {
|
---|
22 | // Hide default constructor for utils classes
|
---|
23 | }
|
---|
24 |
|
---|
25 | private static void displayUrlFallback(URI uri) throws IOException {
|
---|
26 | if (Main.platform == null)
|
---|
27 | throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
|
---|
28 | Main.platform.openUrl(uri.toString());
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Displays an external URI using platform associated software.
|
---|
33 | * A web resource will launch platform's browser, an audio file URI will launch audio player, etc.
|
---|
34 | * @param uri The URI to display
|
---|
35 | * @return <code>null</code> for success or a string in case of an error.
|
---|
36 | * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
|
---|
37 | * {@link Main#platform}
|
---|
38 | */
|
---|
39 | public static String displayUrl(URI uri) {
|
---|
40 | CheckParameterUtil.ensureParameterNotNull(uri, "uri");
|
---|
41 |
|
---|
42 | Main.info(tr("Opening URL: {0}", uri));
|
---|
43 |
|
---|
44 | if (Desktop.isDesktopSupported()) {
|
---|
45 | try {
|
---|
46 | if (Main.isPlatformWindows()) {
|
---|
47 | // Desktop API works fine under Windows, so we don't try any fallback in case of I/O exceptions because it's not API's fault
|
---|
48 | Desktop.getDesktop().browse(uri);
|
---|
49 | } else if (Main.platform instanceof PlatformHookUnixoid) {
|
---|
50 | // see #5629 #5108 #9568
|
---|
51 | Main.platform.openUrl(uri.toString());
|
---|
52 | } else {
|
---|
53 | // This is not the case with some Linux environments (see below), and not sure about Mac OS X, so we need to handle API failure
|
---|
54 | try {
|
---|
55 | Desktop.getDesktop().browse(uri);
|
---|
56 | } catch (IOException e) {
|
---|
57 | // Workaround for KDE (Desktop API is severely flawed)
|
---|
58 | // see https://bugs.openjdk.java.net/browse/JDK-6486393
|
---|
59 | Main.warn("Desktop class failed. Platform dependent fall back for open url in browser.");
|
---|
60 | displayUrlFallback(uri);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | } catch (Exception e) {
|
---|
64 | Main.warn(e);
|
---|
65 | return e.getMessage();
|
---|
66 | }
|
---|
67 | } else {
|
---|
68 | try {
|
---|
69 | Main.warn("Desktop class is not supported. Platform dependent fall back for open url in browser.");
|
---|
70 | displayUrlFallback(uri);
|
---|
71 | } catch (IOException e) {
|
---|
72 | return e.getMessage();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return null;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Displays an external URL using platform associated software.
|
---|
80 | * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
|
---|
81 | * @param url The URL to display
|
---|
82 | * @return <code>null</code> for success or a string in case of an error.
|
---|
83 | * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
|
---|
84 | * {@link Main#platform}
|
---|
85 | */
|
---|
86 | public static String displayUrl(String url) {
|
---|
87 | try {
|
---|
88 | return displayUrl(new URI(url));
|
---|
89 | } catch (Exception e) {
|
---|
90 | return e.getMessage();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|