Export wip

This commit is contained in:
lantzelot-swe 2020-12-12 16:32:32 +01:00
parent ccece9787b
commit 097043a3f2
12 changed files with 176 additions and 62 deletions

View File

@ -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;

View File

@ -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

View File

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

View File

@ -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<Void, String>
{
@ -22,13 +22,14 @@ public class ExportWorker extends SwingWorker<Void, String>
@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());

View File

@ -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
{

View File

@ -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

View File

@ -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<Void, String>
{

View File

@ -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<GameListData> gamesList;
private List<GameDetails> gameDetailsList;
private File targetDir;
private MainViewModel uiModel;
public ExportManager(MainViewModel uiModel)
{
this.uiModel = uiModel;
}
public void setGamesToExport(List<GameListData> 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);
}
}
}

View File

@ -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

View File

@ -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()
{
}
}

View File

@ -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<GameDetails> readGameDetailsForExport(StringBuilder infoBuilder, List<GameListData> gamesList)
{
List<GameDetails> 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()
{

View File

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