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.
master
Bilbo 2019-12-03 05:45:06 +01:00
parent 217bee1bc6
commit f6ab949566
1 changed files with 27 additions and 5 deletions

View File

@ -1,12 +1,14 @@
package magic.ui.helpers; package magic.ui.helpers;
import java.awt.Desktop; import java.awt.Desktop;
import java.awt.HeadlessException;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import javax.swing.JOptionPane;
import magic.data.DownloadableFile; import magic.data.DownloadableFile;
import magic.ui.CardTextLanguage; 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_FIREMIND_SCRIPTS = "http://www.firemind.ch/card_script_submissions/new";
public static final String URL_HOMEPAGE = "http://magarena.github.io/"; 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 { try {
Desktop desktop = null; Desktop desktop = null;
if (Desktop.isDesktopSupported()) { if (Desktop.isDesktopSupported()) {
@ -29,9 +37,10 @@ public final class UrlHelper {
if (desktop != null) { if (desktop != null) {
final URI uri = new URI(url); final URI uri = new URI(url);
desktop.browse(uri); desktop.browse(uri);
return true;
} }
} catch (IOException ioe) { } catch (IOException | UnsupportedOperationException ioe) {
System.err.println("ERROR! Unable to launch browser"); System.err.println("ERROR! Unable to launch browser url " + url);
System.err.println(ioe.getMessage()); System.err.println(ioe.getMessage());
ioe.printStackTrace(); ioe.printStackTrace();
} catch (URISyntaxException use) { } catch (URISyntaxException use) {
@ -39,6 +48,18 @@ public final class UrlHelper {
System.err.println(use.getMessage()); System.err.println(use.getMessage());
use.printStackTrace(); 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. * Quickly checks to see whether URL is reachable.
*/ */
public static boolean isUrlValid(final URL url) { 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) if (url == null)
return false; return false;
@ -74,5 +95,6 @@ public final class UrlHelper {
} }
} }
private UrlHelper() { } private UrlHelper() {
}
} }