[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[2748] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[3524] | 6 | import java.awt.Desktop;
|
---|
[626] | 7 | import java.io.IOException;
|
---|
[3524] | 8 | import java.net.URI;
|
---|
[10212] | 9 | import java.net.URISyntaxException;
|
---|
[626] | 10 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Helper to open platform web browser on different platforms
|
---|
[1023] | 15 | *
|
---|
| 16 | * This now delegates the real work to a platform specific class.
|
---|
| 17 | *
|
---|
[626] | 18 | * @author Imi
|
---|
| 19 | */
|
---|
[6362] | 20 | public final class OpenBrowser {
|
---|
[626] | 21 |
|
---|
[6360] | 22 | private OpenBrowser() {
|
---|
| 23 | // Hide default constructor for utils classes
|
---|
| 24 | }
|
---|
[7029] | 25 |
|
---|
[4578] | 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 | }
|
---|
[6070] | 31 |
|
---|
[1169] | 32 | /**
|
---|
[7029] | 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.
|
---|
[6299] | 35 | * @param uri The URI to display
|
---|
[1169] | 36 | * @return <code>null</code> for success or a string in case of an error.
|
---|
[8291] | 37 | * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
|
---|
[5266] | 38 | * {@link Main#platform}
|
---|
[1169] | 39 | */
|
---|
[3524] | 40 | public static String displayUrl(URI uri) {
|
---|
[6299] | 41 | CheckParameterUtil.ensureParameterNotNull(uri, "uri");
|
---|
[7026] | 42 |
|
---|
[12620] | 43 | Logging.info(tr("Opening URL: {0}", uri));
|
---|
[626] | 44 |
|
---|
[3524] | 45 | if (Desktop.isDesktopSupported()) {
|
---|
| 46 | try {
|
---|
[7335] | 47 | if (Main.isPlatformWindows()) {
|
---|
[6299] | 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
|
---|
[4578] | 49 | Desktop.getDesktop().browse(uri);
|
---|
[11156] | 50 | } else if (Main.platform instanceof PlatformHookUnixoid || Main.platform instanceof PlatformHookOsx) {
|
---|
[6682] | 51 | // see #5629 #5108 #9568
|
---|
| 52 | Main.platform.openUrl(uri.toString());
|
---|
[6299] | 53 | } else {
|
---|
[8540] | 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
|
---|
[6299] | 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
|
---|
[12620] | 61 | Logging.log(Logging.LEVEL_WARN, "Desktop class failed. Platform dependent fall back for open url in browser.", e);
|
---|
[6299] | 62 | displayUrlFallback(uri);
|
---|
| 63 | }
|
---|
[4578] | 64 | }
|
---|
[10212] | 65 | } catch (IOException e) {
|
---|
[12620] | 66 | Logging.warn(e);
|
---|
[3524] | 67 | return e.getMessage();
|
---|
| 68 | }
|
---|
| 69 | } else {
|
---|
| 70 | try {
|
---|
[12620] | 71 | Logging.warn("Desktop class is not supported. Platform dependent fall back for open url in browser.");
|
---|
[4578] | 72 | displayUrlFallback(uri);
|
---|
[3524] | 73 | } catch (IOException e) {
|
---|
[12620] | 74 | Logging.debug(e);
|
---|
[3524] | 75 | return e.getMessage();
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | return null;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[6299] | 81 | /**
|
---|
[7029] | 82 | * Displays an external URL using platform associated software.
|
---|
| 83 | * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
|
---|
[6299] | 84 | * @param url The URL to display
|
---|
| 85 | * @return <code>null</code> for success or a string in case of an error.
|
---|
[8291] | 86 | * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
|
---|
[6299] | 87 | * {@link Main#platform}
|
---|
| 88 | */
|
---|
[3524] | 89 | public static String displayUrl(String url) {
|
---|
[1169] | 90 | try {
|
---|
[3524] | 91 | return displayUrl(new URI(url));
|
---|
[10212] | 92 | } catch (URISyntaxException e) {
|
---|
[12620] | 93 | Logging.debug(e);
|
---|
[1169] | 94 | return e.getMessage();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[626] | 97 | }
|
---|