Adds delete game view + bug fixes

This commit is contained in:
lantzelot-swe 2020-12-18 23:19:24 +01:00
parent 7a9fee0d49
commit f9aec586dc
8 changed files with 70 additions and 9 deletions

View File

@ -718,4 +718,21 @@ public class DbConnector
ExceptionHandler.handleException(e, "Could not delete games in db."); ExceptionHandler.handleException(e, "Could not delete games in db.");
} }
} }
public void deleteView(GameView view)
{
String viewFilterSql = "DELETE FROM viewfilter WHERE gameview = " + view.getGameViewId();
String gameViewSql = "DELETE FROM gameview WHERE viewId = " + view.getGameViewId();
try (Connection conn = this.connect(); PreparedStatement viewFilterstmt = conn.prepareStatement(viewFilterSql); PreparedStatement gameViewStmt = conn.prepareStatement(gameViewSql))
{
int value = viewFilterstmt.executeUpdate();
logger.debug("Executed successfully, value = {}", value);
value = gameViewStmt.executeUpdate();
logger.debug("Executed successfully, value = {}", value);
}
catch (SQLException e)
{
ExceptionHandler.handleException(e, "Could not delete gameview or viewfilter in db.");
}
}
} }

View File

@ -126,6 +126,10 @@ public class ListPanel extends JPanel
editItem.addActionListener(e -> gameViewManager editItem.addActionListener(e -> gameViewManager
.openViewEditDialog((GameView) getListViewComboBox().getSelectedItem())); .openViewEditDialog((GameView) getListViewComboBox().getSelectedItem()));
menu.add(editItem); menu.add(editItem);
JMenuItem deleteItem = new JMenuItem("Delete view...");
deleteItem.addActionListener(e -> gameViewManager
.deleteView((GameView) getListViewComboBox().getSelectedItem()));
menu.add(deleteItem);
} }
menu.show(listViewEditButton, 15, 15); menu.show(listViewEditButton, 15, 15);
@ -174,11 +178,15 @@ public class ListPanel extends JPanel
{ {
indexToSelect = uiModel.getGameListModel().getSize()-1; indexToSelect = uiModel.getGameListModel().getSize()-1;
} }
//Select -1 first to ensure a change of selection is done list.clearSelection();
list.setSelectedIndex(-1);
list.setSelectedIndex(indexToSelect); list.setSelectedIndex(indexToSelect);
list.ensureIndexIsVisible(indexToSelect); list.ensureIndexIsVisible(indexToSelect);
} }
public void clearGameListSelection()
{
list.clearSelection();
}
private JPanel getViewInfoPanel() private JPanel getViewInfoPanel()
{ {

View File

@ -156,4 +156,9 @@ public class MainPanel extends JPanel
this.repaint(); this.repaint();
getListPanel().updateViewInfoLabel(); getListPanel().updateViewInfoLabel();
} }
public void clearGameListSelection()
{
getListPanel().clearGameListSelection();
}
} }

View File

@ -74,7 +74,7 @@ public final class MainWindow extends JFrame
menuManager.intialize(); menuManager.intialize();
} }
MainPanel getMainPanel() public MainPanel getMainPanel()
{ {
if (mainPanel == null) if (mainPanel == null)
{ {

View File

@ -337,6 +337,7 @@ public class MenuManager
if (option == JOptionPane.YES_OPTION) if (option == JOptionPane.YES_OPTION)
{ {
backupDb(); backupDb();
MainWindow.getInstance().getMainPanel().clearGameListSelection();
uiModel.deleteAllGames(); uiModel.deleteAllGames();
FileManager.deleteAllFolderContent(); FileManager.deleteAllFolderContent();
//Trigger a reload of game views //Trigger a reload of game views

View File

@ -1,6 +1,7 @@
package se.lantz.gui.gameview; package se.lantz.gui.gameview;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import se.lantz.gui.ListPanel; import se.lantz.gui.ListPanel;
@ -12,7 +13,6 @@ import se.lantz.model.data.GameView;
public class GameViewManager public class GameViewManager
{ {
private JComboBox<GameView> viewCombobox; private JComboBox<GameView> viewCombobox;
private MainWindow mainWindow;
private final MainViewModel uiModel; private final MainViewModel uiModel;
private ListPanel mainPanel; private ListPanel mainPanel;
@ -25,8 +25,7 @@ public class GameViewManager
public void openViewEditDialog(GameView gameView) public void openViewEditDialog(GameView gameView)
{ {
mainWindow = (MainWindow)SwingUtilities.getAncestorOfClass(MainWindow.class, viewCombobox); GameViewEditDialog dialog = new GameViewEditDialog(MainWindow.getInstance(), gameView);
GameViewEditDialog dialog = new GameViewEditDialog(mainWindow, gameView);
if (gameView.getGameViewId() == 0) if (gameView.getGameViewId() == 0)
{ {
dialog.setTitle("Add game view"); dialog.setTitle("Add game view");
@ -36,7 +35,7 @@ public class GameViewManager
dialog.setTitle("Edit game view"); dialog.setTitle("Edit game view");
} }
dialog.pack(); dialog.pack();
dialog.setLocationRelativeTo(mainWindow); dialog.setLocationRelativeTo(MainWindow.getInstance());
if (dialog.showDialog()) if (dialog.showDialog())
{ {
//Update gameView instance with edited values in the dialog //Update gameView instance with edited values in the dialog
@ -60,4 +59,17 @@ public class GameViewManager
} }
} }
} }
public void deleteView(GameView view)
{
String message = "Do you want to delete the game view \"" + view.getName() + "\"?";
int value = JOptionPane.showConfirmDialog(MainWindow.getInstance().getMainPanel(), message, "Delete game view", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (value == JOptionPane.YES_OPTION)
{
uiModel.deleteGameView(view);
//Trigger a reload of game views
uiModel.reloadGameViews();
MainWindow.getInstance().selectViewAfterRestore();
}
}
} }

View File

@ -4,6 +4,7 @@ import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultComboBoxModel;
@ -68,7 +69,9 @@ public class MainViewModel extends AbstractModel
selectedGameView.setName("All Games"); selectedGameView.setName("All Games");
selectedGameView.setSqlQuery(""); selectedGameView.setSqlQuery("");
gameViewModel.addElement(selectedGameView); gameViewModel.addElement(selectedGameView);
for (GameView gameView : dbConnector.loadGameViews()) List<GameView> gameViewList = dbConnector.loadGameViews();
Collections.sort(gameViewList);
for (GameView gameView : gameViewList)
{ {
gameViewModel.addElement(gameView); gameViewModel.addElement(gameView);
} }
@ -363,6 +366,15 @@ public class MainViewModel extends AbstractModel
//Reload the current view //Reload the current view
reloadCurrentGameView(); reloadCurrentGameView();
} }
public void deleteGameView(GameView view)
{
if (view.getGameViewId() != GameView.ALL_GAMES_ID)
{
dbConnector.deleteView(view);
reloadGameViews();
}
}
private List<String> validateRequiredFields() private List<String> validateRequiredFields()
{ {

View File

@ -6,7 +6,7 @@ import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class GameView public class GameView implements Comparable
{ {
public static final int ALL_GAMES_ID = -1; public static final int ALL_GAMES_ID = -1;
private static final Logger logger = LoggerFactory.getLogger(GameView.class); private static final Logger logger = LoggerFactory.getLogger(GameView.class);
@ -139,4 +139,10 @@ public class GameView
{ {
this.gameCount = count; this.gameCount = count;
} }
@Override
public int compareTo(Object o)
{
return this.name.compareTo(o.toString());
}
} }