| 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.MalformedURLException;
|
|---|
| 9 | import java.net.URI;
|
|---|
| 10 | import java.net.URISyntaxException;
|
|---|
| 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 | /**
|
|---|
| 26 | * Displays an external URI using platform associated software.
|
|---|
| 27 | * A web resource will launch platform's browser, an audio file URI will launch audio player, etc.
|
|---|
| 28 | * @param uri The URI to display
|
|---|
| 29 | * @return <code>null</code> for success or a string in case of an error.
|
|---|
| 30 | * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
|
|---|
| 31 | * {@link PlatformManager#getPlatform}
|
|---|
| 32 | */
|
|---|
| 33 | public static String displayUrl(URI uri) {
|
|---|
| 34 | CheckParameterUtil.ensureParameterNotNull(uri, "uri");
|
|---|
| 35 |
|
|---|
| 36 | Logging.info(tr("Opening URL: {0}", uri));
|
|---|
| 37 |
|
|---|
| 38 | try {
|
|---|
| 39 | if (PlatformManager.getPlatform() != null) {
|
|---|
| 40 | // see #5629 #5108 #9568
|
|---|
| 41 | PlatformManager.getPlatform().openUrl(uri.toString());
|
|---|
| 42 | } else if (Desktop.isDesktopSupported()) {
|
|---|
| 43 | // This is not the case with some Linux environments (see below),
|
|---|
| 44 | // and not sure about Mac OS X, so we need to handle API failure
|
|---|
| 45 | Desktop.getDesktop().browse(uri);
|
|---|
| 46 | } else {
|
|---|
| 47 | Logging.warn("Neither Platform nor Desktop class is not supported. Cannot open " + uri);
|
|---|
| 48 | }
|
|---|
| 49 | } catch (IOException e) {
|
|---|
| 50 | Logging.warn(e);
|
|---|
| 51 | return e.getMessage();
|
|---|
| 52 | }
|
|---|
| 53 | return null;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Displays an external URL using platform associated software.
|
|---|
| 58 | * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
|
|---|
| 59 | * @param url The URL to display
|
|---|
| 60 | * @return <code>null</code> for success or a string in case of an error.
|
|---|
| 61 | * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
|
|---|
| 62 | * {@link PlatformManager#getPlatform}
|
|---|
| 63 | */
|
|---|
| 64 | public static String displayUrl(String url) {
|
|---|
| 65 | try {
|
|---|
| 66 | return displayUrl(Utils.urlToURI(url));
|
|---|
| 67 | } catch (URISyntaxException | MalformedURLException e) {
|
|---|
| 68 | Logging.debug(e);
|
|---|
| 69 | return e.getMessage();
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|