remove UI references from MagicFileSystem.

master
lodici 2015-02-22 20:11:47 +00:00
parent ec3cc9d740
commit f90654c04f
6 changed files with 59 additions and 42 deletions

View File

@ -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.");
}
}
}

View File

@ -316,7 +316,7 @@ public class MagicFrame extends JFrame implements IImageDragDropListener {
try { try {
final Path filePath = MagicFileSystem.getDataPath(DataPath.LOGS).resolve("screenshot.png"); final Path filePath = MagicFileSystem.getDataPath(DataPath.LOGS).resolve("screenshot.png");
final File imageFile = GraphicsUtilities.doScreenshotToFile(this.getContentPane(), filePath); final File imageFile = GraphicsUtilities.doScreenshotToFile(this.getContentPane(), filePath);
MagicFileSystem.openFileInDefaultOsEditor(imageFile); DesktopUtils.openFileInDefaultOsEditor(imageFile);
} catch (IOException | DesktopNotSupportedException e) { } catch (IOException | DesktopNotSupportedException e) {
e.printStackTrace(); e.printStackTrace();
ScreenController.showWarningMessage(e.toString()); ScreenController.showWarningMessage(e.toString());

View File

@ -22,6 +22,7 @@ import magic.ui.IconImages;
import magic.ui.URLUtils; import magic.ui.URLUtils;
import magic.exception.DesktopNotSupportedException; import magic.exception.DesktopNotSupportedException;
import magic.model.MagicCardDefinition; import magic.model.MagicCardDefinition;
import magic.ui.DesktopUtils;
import magic.ui.ScreenController; import magic.ui.ScreenController;
import magic.ui.screen.interfaces.IActionBar; import magic.ui.screen.interfaces.IActionBar;
import magic.ui.screen.interfaces.IStatusBar; import magic.ui.screen.interfaces.IStatusBar;
@ -233,7 +234,7 @@ public class CardScriptScreen
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
try { try {
MagicFileSystem.openFileInDefaultOsEditor(textFile); DesktopUtils.openFileInDefaultOsEditor(textFile);
} catch (IOException | DesktopNotSupportedException ex) { } catch (IOException | DesktopNotSupportedException ex) {
ScreenController.showWarningMessage("Unable to open file :\n" + textFile + "\n\n" + ex.getMessage()); ScreenController.showWarningMessage("Unable to open file :\n" + textFile + "\n\n" + ex.getMessage());
} }

View File

@ -8,6 +8,7 @@ import java.util.List;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.JPanel; import javax.swing.JPanel;
import magic.data.MagicIcon; import magic.data.MagicIcon;
import magic.ui.DesktopUtils;
import magic.ui.IconImages; import magic.ui.IconImages;
import magic.ui.ScreenController; import magic.ui.ScreenController;
import magic.ui.screen.interfaces.IStatusBar; import magic.ui.screen.interfaces.IStatusBar;
@ -60,7 +61,7 @@ public class GameLogScreen extends TextFileReaderScreen implements IStatusBar {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
MagicFileSystem.openMagicDirectory(DataPath.LOGS); DesktopUtils.openMagicDirectory(DataPath.LOGS);
} catch (IOException ex) { } catch (IOException ex) {
ScreenController.showWarningMessage("Could not open 'logs' directory : " + ex.getMessage()); ScreenController.showWarningMessage("Could not open 'logs' directory : " + ex.getMessage());
} }

View File

@ -14,8 +14,8 @@ import javax.swing.JFileChooser;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JTextField; import javax.swing.JTextField;
import magic.ui.DesktopUtils;
import magic.ui.ScreenController; import magic.ui.ScreenController;
import magic.utility.MagicFileSystem;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -113,7 +113,7 @@ public class DirectoryChooser extends JPanel implements MouseListener {
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
if (e.getSource() == textField && e.getButton() == MouseEvent.BUTTON3) { if (e.getSource() == textField && e.getButton() == MouseEvent.BUTTON3) {
try { try {
MagicFileSystem.openDirectory(textField.getText()); DesktopUtils.openDirectory(textField.getText());
} catch (IOException | IllegalArgumentException e1) { } catch (IOException | IllegalArgumentException e1) {
ScreenController.showWarningMessage(e1.getMessage()); ScreenController.showWarningMessage(e1.getMessage());
} }

View File

@ -1,6 +1,5 @@
package magic.utility; package magic.utility;
import java.awt.Desktop;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -17,7 +16,6 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import magic.data.GeneralConfig; import magic.data.GeneralConfig;
import magic.exception.DesktopNotSupportedException;
import magic.model.MagicCardDefinition; 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) { public static void serializeStringList(final List<String> list, final File targetFile) {
try (final FileOutputStream fos = new FileOutputStream(targetFile); try (final FileOutputStream fos = new FileOutputStream(targetFile);