feat: adds menu option for deleting all games in the selected view.

Also keeps all games that has "THEC64" in the title when deleting multiple games
This commit is contained in:
lantzelot-swe 2021-02-06 23:28:40 +01:00
parent 1ebd4a14e7
commit 4ba96a2a6a
5 changed files with 142 additions and 52 deletions

View File

@ -831,7 +831,22 @@ public class DbConnector
public void deleteAllGames()
{
String sql = "DELETE FROM gameinfo;";
String sql = "DELETE FROM gameinfo WHERE title NOT LIKE '%THEC64%'";
logger.debug("Generated DELETE String:\n{}", sql);
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql))
{
int value = pstmt.executeUpdate();
logger.debug("Executed successfully, value = {}", value);
}
catch (SQLException e)
{
ExceptionHandler.handleException(e, "Could not delete games in db.");
}
}
public void deleteAllGamesInView(GameView view)
{
String sql = "DELETE FROM gameinfo " + view.getSqlQuery() + " AND title NOT LIKE '%THEC64%'";
logger.debug("Generated DELETE String:\n{}", sql);
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql))
{

View File

@ -122,7 +122,7 @@ public class MainPanel extends JPanel
}
}
}
int showDeleteDialog()
{
String message = "Do you want to delete " + uiModel.getInfoModel().getTitle() + " from the database?";

View File

@ -33,21 +33,20 @@ import se.lantz.manager.ImportManager;
import se.lantz.manager.RestoreManager;
import se.lantz.model.MainViewModel;
import se.lantz.model.data.GameListData;
import se.lantz.model.data.GameView;
import se.lantz.util.FileManager;
import se.lantz.util.VersionChecker;
public class MenuManager
{
private static final String IMPORT_DIR_PROPERTY = "importDir";
private static final String EXPORT_DIR_PROPERTY = "exportDir";
private JMenu fileMenu;
private JMenu editMenu;
private JMenu dbMenu;
private JMenu toolsMenu;
private JMenu helpMenu;
private JMenuItem addGameItem;
private JMenuItem deleteGameItem;
private JMenuItem runGameItem;
private JMenuItem importItem;
private JMenuItem exportItem;
@ -57,7 +56,8 @@ public class MenuManager
private JMenuItem backupDbItem;
private JMenuItem restoreDbItem;
private JMenuItem createEmptyDbItem;
private JMenuItem deleteAllGamesItem;
private JMenuItem deleteGamesForViewItem;
private JMenuItem convertScreensItem;
@ -104,12 +104,14 @@ public class MenuManager
editMenu = new JMenu("Edit");
editMenu.add(getToggleFavoriteItem());
editMenu.add(getClearFavoritesItem());
dbMenu = new JMenu("Tools");
dbMenu.add(getBackupDbItem());
dbMenu.add(getRestoreDbItem());
dbMenu.add(getCreateEmptyDbItem());
dbMenu.addSeparator();
dbMenu.add(getConvertScreensItem());
toolsMenu = new JMenu("Tools");
toolsMenu.add(getBackupDbItem());
toolsMenu.add(getRestoreDbItem());
toolsMenu.addSeparator();
toolsMenu.add(getDeleteAllGamesItem());
toolsMenu.add(getDeleteGamesForViewMenuItem());
toolsMenu.addSeparator();
toolsMenu.add(getConvertScreensItem());
helpMenu = new JMenu("Help");
helpMenu.add(getHelpItem());
helpMenu.add(getCheckVersionItem());
@ -133,7 +135,7 @@ public class MenuManager
List<JMenu> menuList = new ArrayList<JMenu>();
menuList.add(fileMenu);
menuList.add(editMenu);
menuList.add(dbMenu);
menuList.add(toolsMenu);
menuList.add(helpMenu);
return menuList;
}
@ -160,6 +162,14 @@ public class MenuManager
return deleteGameItem;
}
JMenuItem getDeleteGamesForViewMenuItem()
{
deleteGamesForViewItem = new JMenuItem("Delete all games in current view");
deleteGamesForViewItem.addActionListener(e -> deleteAllGamesInView());
return deleteGamesForViewItem;
}
JMenuItem getRunGameMenuItem()
{
runGameItem = new JMenuItem("Run Current Game");
@ -256,11 +266,11 @@ public class MenuManager
return restoreDbItem;
}
private JMenuItem getCreateEmptyDbItem()
private JMenuItem getDeleteAllGamesItem()
{
createEmptyDbItem = new JMenuItem("Delete all games");
createEmptyDbItem.addActionListener(e -> deleteAllGames());
return createEmptyDbItem;
deleteAllGamesItem = new JMenuItem("Delete all games");
deleteAllGamesItem.addActionListener(e -> deleteAllGames());
return deleteAllGamesItem;
}
private JMenuItem getConvertScreensItem()
@ -383,7 +393,7 @@ public class MenuManager
private void deleteAllGames()
{
String message =
"Do you want to delete all games from the database? A backup will added to the backups folder before deleting.\nCover, screenshot and game files will also be deleted.";
"Do you want to delete all games from the database? A backup will added to the backups folder before deleting.\nCovers, screenshots and game files will also be deleted.";
int option = JOptionPane.showConfirmDialog(MainWindow.getInstance()
.getMainPanel(), message, "Delete all games", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (option == JOptionPane.YES_OPTION)
@ -398,6 +408,27 @@ public class MenuManager
}
}
private void deleteAllGamesInView()
{
if (uiModel.getSelectedGameView().getGameViewId() == GameView.ALL_GAMES_ID)
{
deleteAllGames();
}
else
{
String message =
"Do you want to delete all games in the current game view?\nCovers, screenshots and game files will also be deleted.";
int option = JOptionPane.showConfirmDialog(MainWindow.getInstance()
.getMainPanel(), message, "Delete all games in view", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (option == JOptionPane.YES_OPTION)
{
MainWindow.getInstance().getMainPanel().clearGameListSelection();
uiModel.deleteAllGamesInCurrentView();
MainWindow.getInstance().getMainPanel().repaintAfterModifications();
}
}
}
private void convertScreens()
{
String message =

View File

@ -46,6 +46,7 @@ public class MainViewModel extends AbstractModel
private FileManager fileManager = new FileManager(this);
private String currentGameId = "";
private GameDetails currentGameDetails = null;
private PropertyChangeListener duplicateListener;
@ -131,45 +132,41 @@ public class MainViewModel extends AbstractModel
{
this.selectedData = selectedData;
currentGameId = selectedData.getGameId();
GameDetails details = null;
if (selectedData.getGameId().isEmpty())
{
//Create a default GameDetails and return
details = new GameDetails();
currentGameDetails = new GameDetails();
}
else
{
// Read from db, update all models after that.
details = dbConnector.getGameDetails(selectedData.getGameId());
currentGameDetails = dbConnector.getGameDetails(selectedData.getGameId());
}
disableChangeNotification(true);
// Map to models
infoModel.setTitleFromDb(details.getTitle());
infoModel.setTitleFromDb(currentGameDetails.getTitle());
infoModel.setTitle(details.getTitle());
infoModel.setDescription(details.getDescription());
infoModel.setDescriptionDe(details.getDescriptionDe());
infoModel.setDescriptionFr(details.getDescriptionFr());
infoModel.setDescriptionEs(details.getDescriptionEs());
infoModel.setDescriptionIt(details.getDescriptionIt());
infoModel.setYear(details.getYear());
infoModel.setAuthor(details.getAuthor());
infoModel.setGenre(details.getGenre());
infoModel.setComposer(details.getComposer());
infoModel.setGamesFile(details.getGame());
infoModel.setCoverFile(details.getCover());
infoModel.setScreens1File(details.getScreen1());
infoModel.setScreens2File(details.getScreen2());
infoModel.setTitle(currentGameDetails.getTitle());
infoModel.setDescription(currentGameDetails.getDescription());
infoModel.setDescriptionDe(currentGameDetails.getDescriptionDe());
infoModel.setDescriptionFr(currentGameDetails.getDescriptionFr());
infoModel.setDescriptionEs(currentGameDetails.getDescriptionEs());
infoModel.setDescriptionIt(currentGameDetails.getDescriptionIt());
infoModel.setYear(currentGameDetails.getYear());
infoModel.setAuthor(currentGameDetails.getAuthor());
infoModel.setGenre(currentGameDetails.getGenre());
infoModel.setComposer(currentGameDetails.getComposer());
infoModel.setGamesFile(currentGameDetails.getGame());
infoModel.setCoverFile(currentGameDetails.getCover());
infoModel.setScreens1File(currentGameDetails.getScreen1());
infoModel.setScreens2File(currentGameDetails.getScreen2());
//Reset and images that where added previously
infoModel.resetImagesAndOldFileNames();
joy1Model.setConfigStringFromDb(details.getJoy1());
joy2Model.setConfigStringFromDb(details.getJoy2());
systemModel.setConfigStringFromDb(details.getSystem());
systemModel.setVerticalShift(details.getVerticalShift());
joy1Model.setConfigStringFromDb(currentGameDetails.getJoy1());
joy2Model.setConfigStringFromDb(currentGameDetails.getJoy2());
systemModel.setConfigStringFromDb(currentGameDetails.getSystem());
systemModel.setVerticalShift(currentGameDetails.getVerticalShift());
//Set empty title to trigger a change
if (selectedData.getGameId().isEmpty())
@ -416,6 +413,7 @@ public class MainViewModel extends AbstractModel
}
else
{
FileManager.deleteFilesForGame(currentGameDetails);
dbConnector.deleteGame(currentGameId);
//Update all games count, will be reset if its All that is loaded
allGamesCount--;
@ -431,6 +429,25 @@ public class MainViewModel extends AbstractModel
//Reload the current view
reloadCurrentGameView();
}
public void deleteAllGamesInCurrentView()
{
//First delete all covers, screens and games
for (int i = 0; i < getGameListModel().getSize(); i++)
{
GameListData currentData = getGameListModel().getElementAt(i);
if (!currentData.getTitle().contains("THEC64"))
{
GameDetails details = dbConnector.getGameDetails(currentData.getGameId());
FileManager.deleteFilesForGame(details);
allGamesCount--;
allGameView.setGameCount(allGamesCount);
}
}
dbConnector.deleteAllGamesInView(getSelectedGameView());
//Reload the current view
reloadCurrentGameView();
}
public void deleteGameView(GameView view)
{

View File

@ -28,6 +28,7 @@ import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import javax.imageio.ImageIO;
import javax.swing.ListModel;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.slf4j.Logger;
@ -38,6 +39,7 @@ import se.lantz.model.InfoModel;
import se.lantz.model.MainViewModel;
import se.lantz.model.SystemModel;
import se.lantz.model.data.GameDetails;
import se.lantz.model.data.GameListData;
public class FileManager
{
@ -664,21 +666,46 @@ public class FileManager
public static void deleteAllFolderContent()
{
deleteDirContent(new File(COVERS));
deleteDirContent(new File(SCREENS));
deleteDirContent(new File(GAMES));
deleteDirContent(new File(COVERS), false);
deleteDirContent(new File(SCREENS), false);
deleteDirContent(new File(GAMES), false);
}
private static void deleteDirContent(File dir)
private static void deleteDirContent(File dir, boolean deleteAll)
{
for (File file : dir.listFiles())
{
if (!file.isDirectory())
if (!file.isDirectory() && (deleteAll || !file.getName().contains("THEC64")))
{
file.delete();
}
}
}
public static void deleteFilesForGame(GameDetails details)
{
if (!details.getCover().isEmpty())
{
File coverFile = new File(COVERS + details.getCover());
coverFile.delete();
}
if (!details.getScreen1().isEmpty())
{
File screens1File = new File(SCREENS + details.getScreen1());
screens1File.delete();
}
if (!details.getScreen2().isEmpty())
{
File screens2File = new File(SCREENS + details.getScreen2());
screens2File.delete();
}
if (!details.getGame().isEmpty())
{
File gameFile = new File(GAMES + details.getGame());
gameFile.delete();
}
}
public static void restoreDb(String backupFolderName)
{
@ -701,7 +728,7 @@ public class FileManager
try
{
File coversDir = new File(COVERS);
deleteDirContent(coversDir);
deleteDirContent(coversDir, true);
copyDirectory(backupFolder.toPath().resolve("covers").toString(), coversDir.toPath().toString());
}
catch (IOException e)
@ -716,7 +743,7 @@ public class FileManager
try
{
File coversDir = new File(SCREENS);
deleteDirContent(coversDir);
deleteDirContent(coversDir, true);
copyDirectory(backupFolder.toPath().resolve("screens").toString(), coversDir.toPath().toString());
}
catch (IOException e)
@ -731,7 +758,7 @@ public class FileManager
try
{
File coversDir = new File(GAMES);
deleteDirContent(coversDir);
deleteDirContent(coversDir, true);
copyDirectory(backupFolder.toPath().resolve("games").toString(), coversDir.toPath().toString());
}
catch (IOException e)