From 7f317cd6a2cfd137237be07e2c9c26e683cf826c Mon Sep 17 00:00:00 2001 From: lantzelot-swe Date: Sun, 24 Jan 2021 23:20:19 +0100 Subject: [PATCH] feat: Changed export dialog to include export dir selection and option to delete all available games --- src/main/java/se/lantz/gui/MenuManager.java | 26 +-- .../java/se/lantz/gui/SelectDirPanel.java | 159 ++++++++++++++++++ .../lantz/gui/exports/ExportGamesDialog.java | 15 +- .../exports/ExportGamesSelectionPanel.java | 96 ++++++++++- .../java/se/lantz/manager/ExportManager.java | 32 +++- 5 files changed, 295 insertions(+), 33 deletions(-) create mode 100644 src/main/java/se/lantz/gui/SelectDirPanel.java diff --git a/src/main/java/se/lantz/gui/MenuManager.java b/src/main/java/se/lantz/gui/MenuManager.java index e9d7422..2b8e577 100644 --- a/src/main/java/se/lantz/gui/MenuManager.java +++ b/src/main/java/se/lantz/gui/MenuManager.java @@ -367,27 +367,11 @@ public class MenuManager { exportManager.setGamesToExport(gamesList); exportManager.setExportFormat(exportSelectionDialog.isFavFormat()); - final JFileChooser fileChooser = new JFileChooser(); - fileChooser.setDialogTitle("Select a directory to export to"); - fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - String exportDir = FileManager.getConfiguredProperties().getProperty(EXPORT_DIR_PROPERTY); - if (exportDir == null) - { - exportDir = "."; - } - fileChooser.setCurrentDirectory(new File(exportDir)); - fileChooser.setApproveButtonText("Export"); - int value = fileChooser.showDialog(this.mainWindow, "Export"); - if (value == JFileChooser.APPROVE_OPTION) - { - File selectedDir = fileChooser.getSelectedFile(); - FileManager.getConfiguredProperties().put(EXPORT_DIR_PROPERTY, selectedDir.toPath().toString()); - exportManager.setTargerDirectory(selectedDir); - ExportProgressDialog dialog = new ExportProgressDialog(this.mainWindow); - ExportWorker worker = new ExportWorker(exportManager, dialog); - worker.execute(); - dialog.setVisible(true); - } + exportManager.setTargetDirectory(exportSelectionDialog.getTargetDirectory(), exportSelectionDialog.deleteBeforeExport()); + 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/SelectDirPanel.java b/src/main/java/se/lantz/gui/SelectDirPanel.java new file mode 100644 index 0000000..3d69673 --- /dev/null +++ b/src/main/java/se/lantz/gui/SelectDirPanel.java @@ -0,0 +1,159 @@ +package se.lantz.gui; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; + +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import se.lantz.util.FileManager; + +public class SelectDirPanel extends JPanel +{ + private static final String IMPORT_DIR_PROPERTY = "importDir"; + private static final String EXPORT_DIR_PROPERTY = "exportDir"; + private JTextField dirTextField; + private JButton selectDirButton; + + private File targetDirectory; + private boolean importMode = true; + + private String configuredDir = ""; + + public SelectDirPanel(boolean importMode) + { + this.importMode = importMode; + GridBagLayout gridBagLayout = new GridBagLayout(); + setLayout(gridBagLayout); + GridBagConstraints gbc_dirTextField = new GridBagConstraints(); + gbc_dirTextField.anchor = GridBagConstraints.NORTHWEST; + gbc_dirTextField.weighty = 1.0; + gbc_dirTextField.weightx = 1.0; + gbc_dirTextField.insets = new Insets(5, 5, 0, 5); + gbc_dirTextField.fill = GridBagConstraints.HORIZONTAL; + gbc_dirTextField.gridx = 0; + gbc_dirTextField.gridy = 0; + add(getDirTextField(), gbc_dirTextField); + GridBagConstraints gbc_selectDirButton = new GridBagConstraints(); + gbc_selectDirButton.weighty = 1.0; + gbc_selectDirButton.anchor = GridBagConstraints.NORTHWEST; + gbc_selectDirButton.insets = new Insets(5, 0, 0, 5); + gbc_selectDirButton.gridx = 1; + gbc_selectDirButton.gridy = 0; + add(getSelectDirButton(), gbc_selectDirButton); + if (importMode) + { + configuredDir = FileManager.getConfiguredProperties().getProperty(IMPORT_DIR_PROPERTY); + } + else + { + configuredDir = FileManager.getConfiguredProperties().getProperty(EXPORT_DIR_PROPERTY); + } + if (configuredDir == null) + { + configuredDir = "."; + } + targetDirectory = new File(configuredDir); + getDirTextField().setText(configuredDir); + } + + private JTextField getDirTextField() { + if (dirTextField == null) { + dirTextField = new JTextField(); + dirTextField.setEditable(false); + dirTextField.setColumns(10); + } + return dirTextField; + } + private JButton getSelectDirButton() { + if (selectDirButton == null) { + selectDirButton = new JButton("..."); + selectDirButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + if (importMode) + { + selectImportDirectory(); + } + else + { + selectExportDirectory(); + } + } + }); + selectDirButton.setMargin(new Insets(1, 3, 1, 3)); + } + return selectDirButton; + } + + private void selectImportDirectory() + { + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Select directory containing a Carousel"); + fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + fileChooser.setCurrentDirectory(new File(configuredDir)); + int value = fileChooser.showDialog(this, "Import"); +// if (value == JFileChooser.APPROVE_OPTION) +// { +// targetDirectory = fileChooser.getSelectedFile(); +// configuredDir = targetDirectory.toPath().toString(); +// FileManager.getConfiguredProperties().put(IMPORT_DIR_PROPERTY, configuredDir); +// if (importManager.checkSelectedFolder(targetDirectory.toPath())) +// { +// //Show options dialog +// ImportOptionsDialog optionsDialog = new ImportOptionsDialog(this.mainWindow); +// optionsDialog.pack(); +// optionsDialog.setLocationRelativeTo(this.mainWindow); +// if (optionsDialog.showDialog()) +// { +// importManager.setSelectedOption(optionsDialog.getSelectedOption()); +// importManager.setAddAsFavorite(optionsDialog.getMarkAsFavorite()); +// ImportProgressDialog dialog = new ImportProgressDialog(this.mainWindow); +// ImportWorker worker = new ImportWorker(importManager, dialog); +// worker.execute(); +// dialog.setVisible(true); +// //Refresh current game view after import +// uiModel.reloadCurrentGameView(); +// MainWindow.getInstance().repaintAfterModifications(); +// } +// } +// else +// { +// JOptionPane.showMessageDialog(this.mainWindow, +// "The selected directory doesn't contain a valid carousel structure.", +// "Import games", +// JOptionPane.ERROR_MESSAGE); +// } + } + + private void selectExportDirectory() + { + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Select a directory to export to"); + fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + fileChooser.setCurrentDirectory(new File(configuredDir)); + fileChooser.setApproveButtonText("Export"); + int value = fileChooser.showDialog(this, "Export"); + if (value == JFileChooser.APPROVE_OPTION) + { + if (value == JFileChooser.APPROVE_OPTION) + { + targetDirectory = fileChooser.getSelectedFile(); + configuredDir = targetDirectory.toPath().toString(); + FileManager.getConfiguredProperties().put(EXPORT_DIR_PROPERTY, configuredDir); + getDirTextField().setText(configuredDir); + } + } + } + + public File getTargetDirectory() + { + return targetDirectory; + } +} diff --git a/src/main/java/se/lantz/gui/exports/ExportGamesDialog.java b/src/main/java/se/lantz/gui/exports/ExportGamesDialog.java index 68a824c..9f8c385 100644 --- a/src/main/java/se/lantz/gui/exports/ExportGamesDialog.java +++ b/src/main/java/se/lantz/gui/exports/ExportGamesDialog.java @@ -2,6 +2,7 @@ package se.lantz.gui.exports; import java.awt.Dimension; import java.awt.Frame; +import java.io.File; import java.util.List; import se.lantz.gui.BaseDialog; @@ -16,13 +17,13 @@ public class ExportGamesDialog extends BaseDialog super(owner); setTitle("Export games"); addContent(getExportGamesPanel()); - getOkButton().setText("OK"); + getOkButton().setText("Export"); this.setPreferredSize(new Dimension(800,700)); } private ExportGamesSelectionPanel getExportGamesPanel() { if (panel == null) { - panel = new ExportGamesSelectionPanel(); + panel = new ExportGamesSelectionPanel(getOkButton()); } return panel; } @@ -36,4 +37,14 @@ public class ExportGamesDialog extends BaseDialog { return getExportGamesPanel().isFavFormat(); } + + public File getTargetDirectory() + { + return getExportGamesPanel().getTargetDirectory(); + } + + public boolean deleteBeforeExport() + { + return getExportGamesPanel().deleteBeforeExport(); + } } diff --git a/src/main/java/se/lantz/gui/exports/ExportGamesSelectionPanel.java b/src/main/java/se/lantz/gui/exports/ExportGamesSelectionPanel.java index 6b0ed2e..0413ee9 100644 --- a/src/main/java/se/lantz/gui/exports/ExportGamesSelectionPanel.java +++ b/src/main/java/se/lantz/gui/exports/ExportGamesSelectionPanel.java @@ -6,6 +6,7 @@ import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -28,6 +29,8 @@ import se.lantz.model.data.GameView; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; +import se.lantz.gui.SelectDirPanel; +import javax.swing.JCheckBox; public class ExportGamesSelectionPanel extends JPanel { @@ -52,9 +55,16 @@ public class ExportGamesSelectionPanel extends JPanel private JRadioButton maxiFormatRadioButton; private JRadioButton favFormatRadioButton; private final ButtonGroup formatGroup = new ButtonGroup(); + private JPanel outputDirPanel; + private JLabel outputDirLabel; + private SelectDirPanel selectDirPanel; + private JCheckBox deleteCheckBox; + private JButton exportButton; - public ExportGamesSelectionPanel() + public ExportGamesSelectionPanel(JButton exportButton) { + this.exportButton = exportButton; + exportButton.setEnabled(false); uiModel = new MainViewModel(); GridBagLayout gridBagLayout = new GridBagLayout(); setLayout(gridBagLayout); @@ -174,9 +184,7 @@ public class ExportGamesSelectionPanel extends JPanel selectedListModel.addElement(gameListData); } } - sortSelectedList(); - getWarningLabel().setVisible(selectedListModel.getSize() > 226); - getCountLabel().setText(Integer.toString(selectedListModel.getSize())); + updateAfterEditingSelectedList(); } }); addButton.setEnabled(false); @@ -200,15 +208,21 @@ public class ExportGamesSelectionPanel extends JPanel { selectedListModel.removeElement(gameListData); } - sortSelectedList(); - getWarningLabel().setVisible(selectedListModel.getSize() > MAX_GAMES); - getCountLabel().setText(Integer.toString(selectedListModel.getSize())); + updateAfterEditingSelectedList(); } }); removeButton.setEnabled(false); } return removeButton; } + + private void updateAfterEditingSelectedList() + { + sortSelectedList(); + getWarningLabel().setVisible(selectedListModel.getSize() > MAX_GAMES); + exportButton.setEnabled(selectedListModel.getSize() > 0); + getCountLabel().setText(Integer.toString(selectedListModel.getSize())); + } private JPanel getSelectedListPanel() { @@ -374,11 +388,13 @@ public class ExportGamesSelectionPanel extends JPanel { formatPanel = new JPanel(); GridBagLayout gbl_formatPanel = new GridBagLayout(); + gbl_formatPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0}; + gbl_formatPanel.columnWeights = new double[]{1.0}; formatPanel.setLayout(gbl_formatPanel); GridBagConstraints gbc_formatInfoLabel = new GridBagConstraints(); gbc_formatInfoLabel.weightx = 1.0; gbc_formatInfoLabel.anchor = GridBagConstraints.WEST; - gbc_formatInfoLabel.insets = new Insets(10, 10, 5, 5); + gbc_formatInfoLabel.insets = new Insets(10, 10, 5, 0); gbc_formatInfoLabel.gridx = 0; gbc_formatInfoLabel.gridy = 0; formatPanel.add(getFormatInfoLabel(), gbc_formatInfoLabel); @@ -397,6 +413,12 @@ public class ExportGamesSelectionPanel extends JPanel gbc_favFormatRadioButton.gridx = 0; gbc_favFormatRadioButton.gridy = 2; formatPanel.add(getFavFormatRadioButton(), gbc_favFormatRadioButton); + GridBagConstraints gbc_outputDirPanel = new GridBagConstraints(); + gbc_outputDirPanel.insets = new Insets(10, 0, 0, 0); + gbc_outputDirPanel.fill = GridBagConstraints.BOTH; + gbc_outputDirPanel.gridx = 0; + gbc_outputDirPanel.gridy = 3; + formatPanel.add(getOutputDirPanel(), gbc_outputDirPanel); } return formatPanel; } @@ -431,4 +453,62 @@ public class ExportGamesSelectionPanel extends JPanel } return favFormatRadioButton; } + private JPanel getOutputDirPanel() { + if (outputDirPanel == null) { + outputDirPanel = new JPanel(); + GridBagLayout gbl_outputDirPanel = new GridBagLayout(); + gbl_outputDirPanel.columnWidths = new int[]{0, 0}; + gbl_outputDirPanel.rowHeights = new int[]{0, 0, 0, 0}; + gbl_outputDirPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE}; + gbl_outputDirPanel.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE}; + outputDirPanel.setLayout(gbl_outputDirPanel); + GridBagConstraints gbc_outputDirLabel = new GridBagConstraints(); + gbc_outputDirLabel.anchor = GridBagConstraints.WEST; + gbc_outputDirLabel.insets = new Insets(0, 10, 0, 0); + gbc_outputDirLabel.gridx = 0; + gbc_outputDirLabel.gridy = 0; + outputDirPanel.add(getOutputDirLabel(), gbc_outputDirLabel); + GridBagConstraints gbc_selectDirPanel = new GridBagConstraints(); + gbc_selectDirPanel.insets = new Insets(0, 5, 5, 0); + gbc_selectDirPanel.fill = GridBagConstraints.BOTH; + gbc_selectDirPanel.gridx = 0; + gbc_selectDirPanel.gridy = 1; + outputDirPanel.add(getSelectDirPanel(), gbc_selectDirPanel); + GridBagConstraints gbc_deleteCheckBox = new GridBagConstraints(); + gbc_deleteCheckBox.insets = new Insets(0, 10, 0, 0); + gbc_deleteCheckBox.anchor = GridBagConstraints.WEST; + gbc_deleteCheckBox.gridx = 0; + gbc_deleteCheckBox.gridy = 2; + outputDirPanel.add(getDeleteCheckBox(), gbc_deleteCheckBox); + } + return outputDirPanel; + } + private JLabel getOutputDirLabel() { + if (outputDirLabel == null) { + outputDirLabel = new JLabel("Select directory to export to:"); + } + return outputDirLabel; + } + private SelectDirPanel getSelectDirPanel() { + if (selectDirPanel == null) { + selectDirPanel = new SelectDirPanel(false); + } + return selectDirPanel; + } + private JCheckBox getDeleteCheckBox() { + if (deleteCheckBox == null) { + deleteCheckBox = new JCheckBox("Delete existing games in the selected directory before exporting"); + } + return deleteCheckBox; + } + + File getTargetDirectory() + { + return getSelectDirPanel().getTargetDirectory(); + } + + boolean deleteBeforeExport() + { + return getDeleteCheckBox().isSelected(); + } } diff --git a/src/main/java/se/lantz/manager/ExportManager.java b/src/main/java/se/lantz/manager/ExportManager.java index 704b06b..fdae802 100644 --- a/src/main/java/se/lantz/manager/ExportManager.java +++ b/src/main/java/se/lantz/manager/ExportManager.java @@ -6,6 +6,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.Comparator; import java.util.List; import org.slf4j.Logger; @@ -25,6 +26,7 @@ public class ExportManager private File targetDir; private MainViewModel uiModel; private boolean favFormat = false; + private boolean deleteBeforeExport = false; public ExportManager(MainViewModel uiModel) { @@ -36,9 +38,25 @@ public class ExportManager this.gamesList = gamesList; } - public void setTargerDirectory(File targetDir) + public void setTargetDirectory(File targetDir, boolean delete) { + this.deleteBeforeExport = delete; Path targetDirPath = targetDir.toPath().resolve("games"); + if (delete) + { + try + { + if (Files.exists(targetDirPath)) + { + Files.walk(targetDirPath).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + } + } + catch (IOException e) + { + ExceptionHandler.handleException(e, "Could not delete games folder"); + } + } + this.targetDir = targetDirPath.toFile(); try { @@ -49,7 +67,7 @@ public class ExportManager ExceptionHandler.handleException(e, "Could not create " + targetDirPath); } } - + public void readFromDb(StringBuilder infoBuilder) { gameDetailsList = uiModel.readGameDetailsForExport(infoBuilder, gamesList); @@ -57,11 +75,20 @@ public class ExportManager public void createGameInfoFiles(StringBuilder infoBuilder) { + if (deleteBeforeExport) + { + infoBuilder.append("Deleted existing games folder before creating new game files\n"); + } for (GameDetails gameDetails : gameDetailsList) { uiModel.exportGameInfoFile(gameDetails, targetDir, this.favFormat, infoBuilder); } } + + public void deleteSelectedGamesFolder() + { + + } public void setExportFormat(boolean favFormat) { @@ -152,5 +179,6 @@ public class ExportManager gameDetailsList.clear(); targetDir = null; favFormat = false; + deleteBeforeExport = false; } }