diff --git a/src/magic/ui/DesktopUtils.java b/src/magic/ui/DesktopUtils.java new file mode 100644 index 0000000000..96ca2d746d --- /dev/null +++ b/src/magic/ui/DesktopUtils.java @@ -0,0 +1,52 @@ +package magic.ui; + +import java.awt.Desktop; +import java.io.File; +import java.io.IOException; +import magic.exception.DesktopNotSupportedException; +import magic.utility.MagicFileSystem; +import static magic.utility.MagicFileSystem.getDataPath; +import magic.utility.MagicSystem; + + +public final class DesktopUtils { + private DesktopUtils() {} + + public static void openMagicDirectory(final MagicFileSystem.DataPath directory) throws IOException { + openDirectory(getDataPath(directory).toString()); + } + + /** + * Opens specified directory in OS file explorer. + */ + public static void openDirectory(final String path) throws IOException { + final File imagesPath = new File(path); + if (MagicSystem.IS_WINDOWS_OS) { + // Specific fix for Windows. + // If running Windows and path is the default "Magarena" directory + // then Desktop.getDesktop() will start a new instance of Magarena + // instead of opening the directory! This is because the "Magarena" + // directory and "Magarena.exe" are both at the same level and + // Windows incorrectly assumes you mean "Magarena.exe". + new ProcessBuilder("explorer.exe", imagesPath.getPath()).start(); + } else { + Desktop.getDesktop().open(imagesPath); + } + } + + public static void openFileInDefaultOsEditor(final File file) throws IOException, DesktopNotSupportedException { + if (Desktop.isDesktopSupported()) { + if (MagicSystem.IS_WINDOWS_OS) { + // There is an issue in Windows where the open() method of getDesktop() + // fails silently. The recommended solution is to use getRuntime(). + Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file.toString()); + } else { + Desktop.getDesktop().open(file); + } + } else { + throw new DesktopNotSupportedException("Sorry, opening this file with the default application is not supported on this operating system."); + } + } + + +} diff --git a/src/magic/ui/MagicFrame.java b/src/magic/ui/MagicFrame.java index 70d6c37a8a..975d15fa67 100644 --- a/src/magic/ui/MagicFrame.java +++ b/src/magic/ui/MagicFrame.java @@ -316,7 +316,7 @@ public class MagicFrame extends JFrame implements IImageDragDropListener { try { final Path filePath = MagicFileSystem.getDataPath(DataPath.LOGS).resolve("screenshot.png"); final File imageFile = GraphicsUtilities.doScreenshotToFile(this.getContentPane(), filePath); - MagicFileSystem.openFileInDefaultOsEditor(imageFile); + DesktopUtils.openFileInDefaultOsEditor(imageFile); } catch (IOException | DesktopNotSupportedException e) { e.printStackTrace(); ScreenController.showWarningMessage(e.toString()); diff --git a/src/magic/ui/screen/CardScriptScreen.java b/src/magic/ui/screen/CardScriptScreen.java index a05540b7e7..8dca2d72f5 100644 --- a/src/magic/ui/screen/CardScriptScreen.java +++ b/src/magic/ui/screen/CardScriptScreen.java @@ -22,6 +22,7 @@ import magic.ui.IconImages; import magic.ui.URLUtils; import magic.exception.DesktopNotSupportedException; import magic.model.MagicCardDefinition; +import magic.ui.DesktopUtils; import magic.ui.ScreenController; import magic.ui.screen.interfaces.IActionBar; import magic.ui.screen.interfaces.IStatusBar; @@ -233,7 +234,7 @@ public class CardScriptScreen @Override public void mouseClicked(MouseEvent e) { try { - MagicFileSystem.openFileInDefaultOsEditor(textFile); + DesktopUtils.openFileInDefaultOsEditor(textFile); } catch (IOException | DesktopNotSupportedException ex) { ScreenController.showWarningMessage("Unable to open file :\n" + textFile + "\n\n" + ex.getMessage()); } diff --git a/src/magic/ui/screen/GameLogScreen.java b/src/magic/ui/screen/GameLogScreen.java index 9d0729d363..8e6f4170fe 100644 --- a/src/magic/ui/screen/GameLogScreen.java +++ b/src/magic/ui/screen/GameLogScreen.java @@ -8,6 +8,7 @@ import java.util.List; import javax.swing.AbstractAction; import javax.swing.JPanel; import magic.data.MagicIcon; +import magic.ui.DesktopUtils; import magic.ui.IconImages; import magic.ui.ScreenController; import magic.ui.screen.interfaces.IStatusBar; @@ -60,7 +61,7 @@ public class GameLogScreen extends TextFileReaderScreen implements IStatusBar { @Override public void actionPerformed(ActionEvent e) { try { - MagicFileSystem.openMagicDirectory(DataPath.LOGS); + DesktopUtils.openMagicDirectory(DataPath.LOGS); } catch (IOException ex) { ScreenController.showWarningMessage("Could not open 'logs' directory : " + ex.getMessage()); } diff --git a/src/magic/ui/widget/DirectoryChooser.java b/src/magic/ui/widget/DirectoryChooser.java index 81a41e73e3..d867a92364 100644 --- a/src/magic/ui/widget/DirectoryChooser.java +++ b/src/magic/ui/widget/DirectoryChooser.java @@ -14,8 +14,8 @@ import javax.swing.JFileChooser; import javax.swing.JPanel; import javax.swing.JTextField; +import magic.ui.DesktopUtils; import magic.ui.ScreenController; -import magic.utility.MagicFileSystem; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") @@ -113,7 +113,7 @@ public class DirectoryChooser extends JPanel implements MouseListener { public void mouseReleased(MouseEvent e) { if (e.getSource() == textField && e.getButton() == MouseEvent.BUTTON3) { try { - MagicFileSystem.openDirectory(textField.getText()); + DesktopUtils.openDirectory(textField.getText()); } catch (IOException | IllegalArgumentException e1) { ScreenController.showWarningMessage(e1.getMessage()); } diff --git a/src/magic/utility/MagicFileSystem.java b/src/magic/utility/MagicFileSystem.java index 9e8ea89d34..2a8dec0b34 100644 --- a/src/magic/utility/MagicFileSystem.java +++ b/src/magic/utility/MagicFileSystem.java @@ -1,6 +1,5 @@ package magic.utility; -import java.awt.Desktop; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -17,7 +16,6 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.List; import magic.data.GeneralConfig; -import magic.exception.DesktopNotSupportedException; import magic.model.MagicCardDefinition; /** @@ -159,41 +157,6 @@ public final class MagicFileSystem { } } - public static void openMagicDirectory(final DataPath directory) throws IOException { - openDirectory(getDataPath(directory).toString()); - } - - /** - * Opens specified directory in OS file explorer. - */ - public static void openDirectory(final String path) throws IOException { - final File imagesPath = new File(path); - if (MagicSystem.IS_WINDOWS_OS) { - // Specific fix for Windows. - // If running Windows and path is the default "Magarena" directory - // then Desktop.getDesktop() will start a new instance of Magarena - // instead of opening the directory! This is because the "Magarena" - // directory and "Magarena.exe" are both at the same level and - // Windows incorrectly assumes you mean "Magarena.exe". - new ProcessBuilder("explorer.exe", imagesPath.getPath()).start(); - } else { - Desktop.getDesktop().open(imagesPath); - } - } - - public static void openFileInDefaultOsEditor(final File file) throws IOException, DesktopNotSupportedException { - if (Desktop.isDesktopSupported()) { - if (MagicSystem.IS_WINDOWS_OS) { - // There is an issue in Windows where the open() method of getDesktop() - // fails silently. The recommended solution is to use getRuntime(). - Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file.toString()); - } else { - Desktop.getDesktop().open(file); - } - } else { - throw new DesktopNotSupportedException("Sorry, opening this file with the default application is not supported on this operating system."); - } - } public static void serializeStringList(final List list, final File targetFile) { try (final FileOutputStream fos = new FileOutputStream(targetFile);