From f6ab9495669332b6cceea4b9ef45ff81ec1c0872 Mon Sep 17 00:00:00 2001 From: Bilbo Date: Tue, 3 Dec 2019 05:45:06 +0100 Subject: [PATCH] Fix exception when attempting to open URL on a system where this is not supported. If opening URL fails, show a messagebox that also shows the URL. --- src/magic/ui/helpers/UrlHelper.java | 32 ++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/magic/ui/helpers/UrlHelper.java b/src/magic/ui/helpers/UrlHelper.java index a5d15d33af..a45564f5cd 100644 --- a/src/magic/ui/helpers/UrlHelper.java +++ b/src/magic/ui/helpers/UrlHelper.java @@ -1,12 +1,14 @@ package magic.ui.helpers; import java.awt.Desktop; +import java.awt.HeadlessException; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import javax.swing.JOptionPane; import magic.data.DownloadableFile; import magic.ui.CardTextLanguage; @@ -20,7 +22,13 @@ public final class UrlHelper { public static final String URL_FIREMIND_SCRIPTS = "http://www.firemind.ch/card_script_submissions/new"; public static final String URL_HOMEPAGE = "http://magarena.github.io/"; - public static void openURL(final String url) { + /** + * Try to open URL in browser. + * + * @param url URl to open + * @return true if successful, false if operation failed or is not supported + */ + public static boolean tryOpenURL(final String url) { try { Desktop desktop = null; if (Desktop.isDesktopSupported()) { @@ -29,9 +37,10 @@ public final class UrlHelper { if (desktop != null) { final URI uri = new URI(url); desktop.browse(uri); + return true; } - } catch (IOException ioe) { - System.err.println("ERROR! Unable to launch browser"); + } catch (IOException | UnsupportedOperationException ioe) { + System.err.println("ERROR! Unable to launch browser url " + url); System.err.println(ioe.getMessage()); ioe.printStackTrace(); } catch (URISyntaxException use) { @@ -39,6 +48,18 @@ public final class UrlHelper { System.err.println(use.getMessage()); use.printStackTrace(); } + return false; + } + + public static void openURL(final String url) { + if (!tryOpenURL(url)) { + try { + JOptionPane.showMessageDialog(null, "Unable to launch browser for url " + url, + "Error launching browser", JOptionPane.ERROR_MESSAGE); + } catch (HeadlessException e) { + System.err.println("Unable to launch browser for url " + url); + } + } } /** @@ -57,7 +78,7 @@ public final class UrlHelper { * Quickly checks to see whether URL is reachable. */ public static boolean isUrlValid(final URL url) { - final int timeout= 2000; // time, in milliseconds, used for open a link referenced by this URLConnection. + final int timeout = 2000; // time, in milliseconds, used for open a link referenced by this URLConnection. if (url == null) return false; @@ -74,5 +95,6 @@ public final class UrlHelper { } } - private UrlHelper() { } + private UrlHelper() { + } }