From 39f14b40c8ac7f45f3df3ca54467148e1c1384a1 Mon Sep 17 00:00:00 2001 From: Bilbo Date: Tue, 3 Dec 2019 06:59:18 +0100 Subject: [PATCH] Fix exception when attempting to open file or directory (mostly log directory, crash logs, etc ...) with default application on a system where this is not supported. If opening fails, show a messagebox that also shows the file/directory name. --- src/magic/data/MagicSetDefinitions.java | 3 +- src/magic/ui/UiExceptionHandler.java | 3 +- src/magic/ui/helpers/DesktopHelper.java | 40 +++++++++++++++++-- .../screen/menu/dev/DevMenuContentPanel.java | 3 +- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/magic/data/MagicSetDefinitions.java b/src/magic/data/MagicSetDefinitions.java index dc07669805..ba51389be6 100644 --- a/src/magic/data/MagicSetDefinitions.java +++ b/src/magic/data/MagicSetDefinitions.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Scanner; import magic.model.MagicCardDefinition; import magic.model.MagicSetDefinition; +import magic.ui.helpers.DesktopHelper; import magic.utility.MagicFileSystem; import magic.utility.MagicFileSystem.DataPath; import magic.utility.MagicResources; @@ -89,7 +90,7 @@ public class MagicSetDefinitions { ); } } - Desktop.getDesktop().open(MagicFileSystem.getDataPath(DataPath.LOGS).toFile()); + DesktopHelper.tryOpen(MagicFileSystem.getDataPath(DataPath.LOGS).toFile()); } } diff --git a/src/magic/ui/UiExceptionHandler.java b/src/magic/ui/UiExceptionHandler.java index 62b462001f..5b64a1ce03 100644 --- a/src/magic/ui/UiExceptionHandler.java +++ b/src/magic/ui/UiExceptionHandler.java @@ -1,6 +1,7 @@ package magic.ui; import magic.translate.MText; +import magic.ui.helpers.DesktopHelper; import magic.ui.helpers.ImageHelper; import java.awt.Component; import java.awt.Desktop; @@ -48,7 +49,7 @@ public class UiExceptionHandler extends FileExceptionHandler { prompt += String.format("\n\n%s\n%s", MText.get(_S3), MText.get(_S4)); final int action = JOptionPane.showConfirmDialog(frame, prompt, MText.get(_S1), JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null); if (action == JOptionPane.YES_OPTION) { - Desktop.getDesktop().open(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile()); + DesktopHelper.tryOpen(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile()); } } else { JOptionPane.showMessageDialog(frame, prompt, MText.get(_S1), JOptionPane.ERROR_MESSAGE); diff --git a/src/magic/ui/helpers/DesktopHelper.java b/src/magic/ui/helpers/DesktopHelper.java index 3faf4ef69a..e2041daab8 100644 --- a/src/magic/ui/helpers/DesktopHelper.java +++ b/src/magic/ui/helpers/DesktopHelper.java @@ -1,8 +1,10 @@ package magic.ui.helpers; import java.awt.Desktop; +import java.awt.HeadlessException; import java.io.File; import java.io.IOException; +import javax.swing.JOptionPane; import magic.exception.DesktopNotSupportedException; import magic.translate.MText; import magic.utility.MagicFileSystem; @@ -30,10 +32,39 @@ public final class DesktopHelper { // Windows incorrectly assumes you mean "Magarena.exe". new ProcessBuilder("explorer.exe", folder.getPath()).start(); } else { - Desktop.getDesktop().open(folder); + tryOpen(folder); } } + public static boolean tryDesktopOpen(final File target) { + try { + Desktop desktop = null; + if (Desktop.isDesktopSupported()) { + desktop = Desktop.getDesktop(); + } + if (desktop != null) { + Desktop.getDesktop().open(target); + return true; + } + } catch (IOException e) { + return false; + } + return false; + } + + public static void tryOpen(final File target) { + if (!tryDesktopOpen(target)) { + try { + JOptionPane.showMessageDialog(null, "Unable to open file or directory " + target.getAbsolutePath() + + " with default application", + "Error opening file or directory", JOptionPane.ERROR_MESSAGE); + } catch (HeadlessException e) { + System.err.println("Unable to open file or directory " + target.getAbsolutePath()); + } + } + } + + public static void openDirectory(final String path) throws IOException { openDirectory(new File(path)); } @@ -45,7 +76,7 @@ public final class DesktopHelper { // fails silently. The recommended solution is to use getRuntime(). Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file.toString()); } else { - Desktop.getDesktop().open(file.getAbsoluteFile()); + tryOpen(file.getAbsoluteFile()); } } else { throw new DesktopNotSupportedException(MText.get(_S1)); @@ -55,6 +86,7 @@ public final class DesktopHelper { public static void openContainingDirectory(File aFile) throws IOException { openDirectory(aFile.getAbsoluteFile().getParentFile()); } - - private DesktopHelper() {} + + private DesktopHelper() { + } } diff --git a/src/magic/ui/screen/menu/dev/DevMenuContentPanel.java b/src/magic/ui/screen/menu/dev/DevMenuContentPanel.java index 22e4aca324..514204fa52 100644 --- a/src/magic/ui/screen/menu/dev/DevMenuContentPanel.java +++ b/src/magic/ui/screen/menu/dev/DevMenuContentPanel.java @@ -18,6 +18,7 @@ import magic.game.state.GameLoader; import magic.game.state.GameStateFileReader; import magic.ui.ScreenController; import magic.ui.dialog.GameStateRunner; +import magic.ui.helpers.DesktopHelper; import magic.ui.screen.menu.NewMenuScreenContentPanel; import magic.utility.MagicFileSystem; import magic.utility.MagicSystem; @@ -103,7 +104,7 @@ class DevMenuContentPanel extends NewMenuScreenContentPanel { try (final PrintWriter writer = new PrintWriter(savePath.toFile(), UTF_8.name())) { missingCards.forEach(writer::println); } - Desktop.getDesktop().open(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile()); + DesktopHelper.tryOpen(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile()); } private void doSaveMissingCardsFile() {