remove UI references from MagicFileSystem.
parent
ec3cc9d740
commit
f90654c04f
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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<String> list, final File targetFile) {
|
||||
try (final FileOutputStream fos = new FileOutputStream(targetFile);
|
||||
|
|
Loading…
Reference in New Issue