From 097043a3f28cf8e9feb1716c0af6f9253cc8a4ef Mon Sep 17 00:00:00 2001 From: lantzelot-swe Date: Sat, 12 Dec 2020 16:32:32 +0100 Subject: [PATCH] Export wip --- src/main/java/se/lantz/db/DbConnector.java | 2 +- src/main/java/se/lantz/gui/ListPanel.java | 2 +- src/main/java/se/lantz/gui/MenuManager.java | 34 ++++--- .../se/lantz/gui/exports/ExportWorker.java | 15 +-- .../gui/imports/ImportOptionsDialog.java | 2 +- .../lantz/gui/imports/ImportOptionsPanel.java | 3 +- .../se/lantz/gui/imports/ImportWorker.java | 2 +- .../java/se/lantz/manager/ExportManager.java | 49 ++++++++++ .../{model => manager}/ImportManager.java | 3 +- .../java/se/lantz/model/ExportManager.java | 16 ---- .../java/se/lantz/model/MainViewModel.java | 18 ++++ src/main/java/se/lantz/util/FileManager.java | 92 +++++++++++++++---- 12 files changed, 176 insertions(+), 62 deletions(-) create mode 100644 src/main/java/se/lantz/manager/ExportManager.java rename src/main/java/se/lantz/{model => manager}/ImportManager.java (99%) delete mode 100644 src/main/java/se/lantz/model/ExportManager.java diff --git a/src/main/java/se/lantz/db/DbConnector.java b/src/main/java/se/lantz/db/DbConnector.java index 97bfff3..5398e62 100644 --- a/src/main/java/se/lantz/db/DbConnector.java +++ b/src/main/java/se/lantz/db/DbConnector.java @@ -13,7 +13,7 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import se.lantz.model.ImportManager; +import se.lantz.manager.ImportManager; import se.lantz.model.data.GameDetails; import se.lantz.model.data.GameListData; import se.lantz.model.data.GameView; diff --git a/src/main/java/se/lantz/gui/ListPanel.java b/src/main/java/se/lantz/gui/ListPanel.java index 9052fb4..14904f9 100644 --- a/src/main/java/se/lantz/gui/ListPanel.java +++ b/src/main/java/se/lantz/gui/ListPanel.java @@ -233,7 +233,7 @@ public class ListPanel extends JPanel } } }; - list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //If the user holds down "down" or "up" (scrolling in the list) the details is not //updated until the key is released diff --git a/src/main/java/se/lantz/gui/MenuManager.java b/src/main/java/se/lantz/gui/MenuManager.java index 45e00a0..a14de88 100644 --- a/src/main/java/se/lantz/gui/MenuManager.java +++ b/src/main/java/se/lantz/gui/MenuManager.java @@ -21,9 +21,10 @@ import se.lantz.gui.exports.ExportWorker; import se.lantz.gui.imports.ImportOptionsDialog; import se.lantz.gui.imports.ImportProgressDialog; import se.lantz.gui.imports.ImportWorker; -import se.lantz.model.ExportManager; -import se.lantz.model.ImportManager; +import se.lantz.manager.ExportManager; +import se.lantz.manager.ImportManager; import se.lantz.model.MainViewModel; +import se.lantz.model.data.GameListData; public class MenuManager { @@ -54,6 +55,7 @@ public class MenuManager this.uiModel = uiModel; this.mainWindow = mainWindow; this.importManager = new ImportManager(uiModel); + this.exportManager = new ExportManager(uiModel); setupMenues(); } @@ -259,18 +261,24 @@ public class MenuManager exportSelectionDialog.setLocationRelativeTo(this.mainWindow); if (exportSelectionDialog.showDialog()) { - final JFileChooser fileChooser = new JFileChooser(); - fileChooser.setDialogTitle("Select a directory to export to"); - fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - fileChooser.setSelectedFile(new File("F:\\C64\\GameManagerTest")); - fileChooser.setApproveButtonText("Export"); - int value = fileChooser.showDialog(this.mainWindow, "Export"); - if (value == JFileChooser.APPROVE_OPTION) + List gamesList = exportSelectionDialog.getSelectedGames(); + if (!gamesList.isEmpty()) { - ExportProgressDialog dialog = new ExportProgressDialog(this.mainWindow); - ExportWorker worker = new ExportWorker(exportManager, dialog); - worker.execute(); - dialog.setVisible(true); + exportManager.setGamesToExport(gamesList); + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Select a directory to export to"); + fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + fileChooser.setSelectedFile(new File("F:\\C64\\GameManagerTest")); + fileChooser.setApproveButtonText("Export"); + int value = fileChooser.showDialog(this.mainWindow, "Export"); + if (value == JFileChooser.APPROVE_OPTION) + { + exportManager.setTargerDirectory(fileChooser.getSelectedFile()); + ExportProgressDialog dialog = new ExportProgressDialog(this.mainWindow); + ExportWorker worker = new ExportWorker(exportManager, dialog); + worker.execute(); + dialog.setVisible(true); + } } } } diff --git a/src/main/java/se/lantz/gui/exports/ExportWorker.java b/src/main/java/se/lantz/gui/exports/ExportWorker.java index 20ff22b..64dbab5 100644 --- a/src/main/java/se/lantz/gui/exports/ExportWorker.java +++ b/src/main/java/se/lantz/gui/exports/ExportWorker.java @@ -4,8 +4,8 @@ import java.util.List; import javax.swing.SwingWorker; -import se.lantz.model.ExportManager; -import se.lantz.model.ImportManager; +import se.lantz.manager.ExportManager; +import se.lantz.manager.ImportManager; public class ExportWorker extends SwingWorker { @@ -22,13 +22,14 @@ public class ExportWorker extends SwingWorker @Override protected Void doInBackground() throws Exception { - - StringBuilder infoBuilder = new StringBuilder(); -// exportManager.readGameInfoFiles(infoBuilder); - publish(infoBuilder.toString()); -// exportManager.convertIntoDbRows(); publish("Exporting from db..."); + StringBuilder infoBuilder = new StringBuilder(); + exportManager.readFromDb(infoBuilder); + publish(infoBuilder.toString()); publish("Creating game info files..."); + infoBuilder = new StringBuilder(); + exportManager.createGameInfoFiles(infoBuilder); + publish(infoBuilder.toString()); // publish(exportManager.insertRowsIntoDb().toString()); publish("Copy screenshots, covers and game files..."); // publish(exportManager.copyFiles().toString()); diff --git a/src/main/java/se/lantz/gui/imports/ImportOptionsDialog.java b/src/main/java/se/lantz/gui/imports/ImportOptionsDialog.java index df44410..14899a7 100644 --- a/src/main/java/se/lantz/gui/imports/ImportOptionsDialog.java +++ b/src/main/java/se/lantz/gui/imports/ImportOptionsDialog.java @@ -3,7 +3,7 @@ package se.lantz.gui.imports; import java.awt.Frame; import se.lantz.gui.BaseDialog; -import se.lantz.model.ImportManager; +import se.lantz.manager.ImportManager; public class ImportOptionsDialog extends BaseDialog { diff --git a/src/main/java/se/lantz/gui/imports/ImportOptionsPanel.java b/src/main/java/se/lantz/gui/imports/ImportOptionsPanel.java index 3d674c4..27e7399 100644 --- a/src/main/java/se/lantz/gui/imports/ImportOptionsPanel.java +++ b/src/main/java/se/lantz/gui/imports/ImportOptionsPanel.java @@ -8,7 +8,8 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; -import se.lantz.model.ImportManager; +import se.lantz.manager.ImportManager; + import javax.swing.ButtonGroup; public class ImportOptionsPanel extends JPanel diff --git a/src/main/java/se/lantz/gui/imports/ImportWorker.java b/src/main/java/se/lantz/gui/imports/ImportWorker.java index 1100ef7..8853cc3 100644 --- a/src/main/java/se/lantz/gui/imports/ImportWorker.java +++ b/src/main/java/se/lantz/gui/imports/ImportWorker.java @@ -4,7 +4,7 @@ import java.util.List; import javax.swing.SwingWorker; -import se.lantz.model.ImportManager; +import se.lantz.manager.ImportManager; public class ImportWorker extends SwingWorker { diff --git a/src/main/java/se/lantz/manager/ExportManager.java b/src/main/java/se/lantz/manager/ExportManager.java new file mode 100644 index 0000000..02ff6cf --- /dev/null +++ b/src/main/java/se/lantz/manager/ExportManager.java @@ -0,0 +1,49 @@ +package se.lantz.manager; + +import java.io.File; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import se.lantz.model.MainViewModel; +import se.lantz.model.data.GameDetails; +import se.lantz.model.data.GameListData; + +public class ExportManager +{ + private static final Logger logger = LoggerFactory.getLogger(ExportManager.class); + private List gamesList; + private List gameDetailsList; + private File targetDir; + private MainViewModel uiModel; + + public ExportManager(MainViewModel uiModel) + { + this.uiModel = uiModel; + } + + public void setGamesToExport(List gamesList) + { + this.gamesList = gamesList; + } + + public void setTargerDirectory(File targetDir) + { + this.targetDir = targetDir; + } + + public void readFromDb(StringBuilder infoBuilder) + { + gameDetailsList = uiModel.readGameDetailsForExport(infoBuilder, gamesList); + } + + public void createGameInfoFiles(StringBuilder infoBuilder) + { + for (GameDetails gameDetails : gameDetailsList) + { + uiModel.exportGame(gameDetails, targetDir, infoBuilder); + } + } + +} diff --git a/src/main/java/se/lantz/model/ImportManager.java b/src/main/java/se/lantz/manager/ImportManager.java similarity index 99% rename from src/main/java/se/lantz/model/ImportManager.java rename to src/main/java/se/lantz/manager/ImportManager.java index f6f9d2d..0249dfd 100644 --- a/src/main/java/se/lantz/model/ImportManager.java +++ b/src/main/java/se/lantz/manager/ImportManager.java @@ -1,4 +1,4 @@ -package se.lantz.model; +package se.lantz.manager; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -18,6 +18,7 @@ import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import se.lantz.model.MainViewModel; import se.lantz.util.ExceptionHandler; public class ImportManager diff --git a/src/main/java/se/lantz/model/ExportManager.java b/src/main/java/se/lantz/model/ExportManager.java deleted file mode 100644 index 639f414..0000000 --- a/src/main/java/se/lantz/model/ExportManager.java +++ /dev/null @@ -1,16 +0,0 @@ -package se.lantz.model; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ExportManager -{ - - private static final Logger logger = LoggerFactory.getLogger(ExportManager.class); - - public ExportManager() - { - - } - -} diff --git a/src/main/java/se/lantz/model/MainViewModel.java b/src/main/java/se/lantz/model/MainViewModel.java index 8b371b3..5fa717c 100644 --- a/src/main/java/se/lantz/model/MainViewModel.java +++ b/src/main/java/se/lantz/model/MainViewModel.java @@ -2,6 +2,7 @@ package se.lantz.model; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.io.File; import java.util.ArrayList; import java.util.List; @@ -12,6 +13,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import se.lantz.db.DbConnector; +import se.lantz.manager.ImportManager; import se.lantz.model.data.GameDetails; import se.lantz.model.data.GameListData; import se.lantz.model.data.GameView; @@ -121,6 +123,22 @@ public class MainViewModel extends AbstractModel { return dbConnector.importRowsInGameInfoTable(rowValues, option); } + + public List readGameDetailsForExport(StringBuilder infoBuilder, List gamesList) + { + List returnList = new ArrayList<>(); + for (GameListData game : gamesList) + { + infoBuilder.append("Fetching information for " + game.getTitle() + "\n"); + returnList.add(dbConnector.getGameDetails(game.getGameId())); + } + return returnList; + } + + public void exportGame(GameDetails gameDetails, File targetDir, StringBuilder infoBuilder) + { + fileManager.exportGame(gameDetails, targetDir, infoBuilder); + } public InfoModel getInfoModel() { diff --git a/src/main/java/se/lantz/util/FileManager.java b/src/main/java/se/lantz/util/FileManager.java index e166bdb..5669911 100644 --- a/src/main/java/se/lantz/util/FileManager.java +++ b/src/main/java/se/lantz/util/FileManager.java @@ -6,6 +6,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -20,6 +21,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import se.lantz.model.InfoModel; +import se.lantz.model.data.GameDetails; public class FileManager { @@ -52,7 +54,7 @@ public class FileManager String coverFileName = model.getCoverFile(); String screen1FileName = model.getScreens1File(); String screen2FileName = model.getScreens2File(); - + String gameName = model.getGamesFile(); Path gamePath = model.getGamesPath(); @@ -110,34 +112,40 @@ public class FileManager Path source = gamePath; Path target = new File(GAMES + gameName).toPath(); - if (Files.notExists(source)) { - System.err.printf("The path %s doesn't exist!", source); - return; + if (Files.notExists(source)) + { + System.err.printf("The path %s doesn't exist!", source); + return; } - try { + try + { - compressGzip(source, target); + compressGzip(source, target); - } catch (IOException e) { - e.printStackTrace(); + } + catch (IOException e) + { + e.printStackTrace(); } } } - - private void compressGzip(Path source, Path target) throws IOException { - try (GZIPOutputStream gos = new GZIPOutputStream( - new FileOutputStream(target.toFile())); - FileInputStream fis = new FileInputStream(source.toFile())) { - // copy file - byte[] buffer = new byte[1024]; - int len; - while ((len = fis.read(buffer)) > 0) { - gos.write(buffer, 0, len); - } + private void compressGzip(Path source, Path target) throws IOException + { + + try (GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream(target.toFile())); + FileInputStream fis = new FileInputStream(source.toFile())) + { + // copy file + byte[] buffer = new byte[1024]; + int len; + while ((len = fis.read(buffer)) > 0) + { + gos.write(buffer, 0, len); + } } -} + } private void renameFiles() { @@ -218,4 +226,48 @@ public class FileManager logger.debug("Game title: \"{}\" ---- New fileName: \"{}\"", title, newNameString); return newNameString; } + + public void exportGame(GameDetails gameDetails, File targetDir, StringBuilder infoBuilder) + { + try + { + String fileName = generateFileNameFromTitle(gameDetails.getTitle()); + + infoBuilder.append("Creating game info file for " + gameDetails.getTitle() + "\n"); + //Add -ms to comply with the maxi game tool. + writeGameInfoFile(fileName + "-ms.tsg", targetDir, gameDetails); + } + catch (Exception e) + { + String message = "Could not create file for: " + gameDetails.getTitle(); + logger.error(message, e); + infoBuilder.append(message); + } + + //TODO copy the other files into the right location + + } + + public static void writeGameInfoFile(String fileName, File targetDir, GameDetails gameDetails) throws IOException + { + Path outDirPath = targetDir.toPath(); + Path filePath = outDirPath.resolve(fileName); + filePath.toFile().createNewFile(); + FileWriter fw = new FileWriter(filePath.toFile()); + + fw.write("T:" + gameDetails.getTitle() + "\n"); + fw.write("X:" + gameDetails.getSystem() + "\n"); + fw.write("D:en:" + gameDetails.getDescription() + "\n"); + fw.write("A:" + gameDetails.getAuthor() + "\n"); + fw.write("M:" + gameDetails.getComposer() + "\n"); + fw.write("E:" + gameDetails.getGenre() + "\n"); + fw.write("F:" + gameDetails.getGame() + "\n"); + fw.write("C:" + gameDetails.getCover() + "\n"); + fw.write("G:" + gameDetails.getScreen1() + "\n"); + fw.write("G:" + gameDetails.getScreen2() + "\n"); + fw.write(gameDetails.getJoy1() + "\n"); + fw.write(gameDetails.getJoy2() + "\n"); + fw.write("V:" + gameDetails.getVerticalShift() + "\n"); + fw.close(); + } }