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.
master
Bilbo 2019-12-03 06:59:18 +01:00
parent f6ab949566
commit 39f14b40c8
4 changed files with 42 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import magic.model.MagicCardDefinition; import magic.model.MagicCardDefinition;
import magic.model.MagicSetDefinition; import magic.model.MagicSetDefinition;
import magic.ui.helpers.DesktopHelper;
import magic.utility.MagicFileSystem; import magic.utility.MagicFileSystem;
import magic.utility.MagicFileSystem.DataPath; import magic.utility.MagicFileSystem.DataPath;
import magic.utility.MagicResources; 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());
} }
} }

View File

@ -1,6 +1,7 @@
package magic.ui; package magic.ui;
import magic.translate.MText; import magic.translate.MText;
import magic.ui.helpers.DesktopHelper;
import magic.ui.helpers.ImageHelper; import magic.ui.helpers.ImageHelper;
import java.awt.Component; import java.awt.Component;
import java.awt.Desktop; 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)); 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); final int action = JOptionPane.showConfirmDialog(frame, prompt, MText.get(_S1), JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null);
if (action == JOptionPane.YES_OPTION) { if (action == JOptionPane.YES_OPTION) {
Desktop.getDesktop().open(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile()); DesktopHelper.tryOpen(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile());
} }
} else { } else {
JOptionPane.showMessageDialog(frame, prompt, MText.get(_S1), JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(frame, prompt, MText.get(_S1), JOptionPane.ERROR_MESSAGE);

View File

@ -1,8 +1,10 @@
package magic.ui.helpers; package magic.ui.helpers;
import java.awt.Desktop; import java.awt.Desktop;
import java.awt.HeadlessException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import javax.swing.JOptionPane;
import magic.exception.DesktopNotSupportedException; import magic.exception.DesktopNotSupportedException;
import magic.translate.MText; import magic.translate.MText;
import magic.utility.MagicFileSystem; import magic.utility.MagicFileSystem;
@ -30,10 +32,39 @@ public final class DesktopHelper {
// Windows incorrectly assumes you mean "Magarena.exe". // Windows incorrectly assumes you mean "Magarena.exe".
new ProcessBuilder("explorer.exe", folder.getPath()).start(); new ProcessBuilder("explorer.exe", folder.getPath()).start();
} else { } 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 { public static void openDirectory(final String path) throws IOException {
openDirectory(new File(path)); openDirectory(new File(path));
} }
@ -45,7 +76,7 @@ public final class DesktopHelper {
// fails silently. The recommended solution is to use getRuntime(). // fails silently. The recommended solution is to use getRuntime().
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file.toString()); Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file.toString());
} else { } else {
Desktop.getDesktop().open(file.getAbsoluteFile()); tryOpen(file.getAbsoluteFile());
} }
} else { } else {
throw new DesktopNotSupportedException(MText.get(_S1)); throw new DesktopNotSupportedException(MText.get(_S1));
@ -55,6 +86,7 @@ public final class DesktopHelper {
public static void openContainingDirectory(File aFile) throws IOException { public static void openContainingDirectory(File aFile) throws IOException {
openDirectory(aFile.getAbsoluteFile().getParentFile()); openDirectory(aFile.getAbsoluteFile().getParentFile());
} }
private DesktopHelper() {} private DesktopHelper() {
}
} }

View File

@ -18,6 +18,7 @@ import magic.game.state.GameLoader;
import magic.game.state.GameStateFileReader; import magic.game.state.GameStateFileReader;
import magic.ui.ScreenController; import magic.ui.ScreenController;
import magic.ui.dialog.GameStateRunner; import magic.ui.dialog.GameStateRunner;
import magic.ui.helpers.DesktopHelper;
import magic.ui.screen.menu.NewMenuScreenContentPanel; import magic.ui.screen.menu.NewMenuScreenContentPanel;
import magic.utility.MagicFileSystem; import magic.utility.MagicFileSystem;
import magic.utility.MagicSystem; import magic.utility.MagicSystem;
@ -103,7 +104,7 @@ class DevMenuContentPanel extends NewMenuScreenContentPanel {
try (final PrintWriter writer = new PrintWriter(savePath.toFile(), UTF_8.name())) { try (final PrintWriter writer = new PrintWriter(savePath.toFile(), UTF_8.name())) {
missingCards.forEach(writer::println); missingCards.forEach(writer::println);
} }
Desktop.getDesktop().open(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile()); DesktopHelper.tryOpen(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile());
} }
private void doSaveMissingCardsFile() { private void doSaveMissingCardsFile() {