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