Adds delete option

This commit is contained in:
mikael.lantz 2020-12-15 16:46:57 +01:00
parent 976baac362
commit ed565cd9fe
6 changed files with 82 additions and 10 deletions

View File

@ -687,6 +687,20 @@ public class DbConnector
public void deleteGame(String rowId)
{
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("DELETE FROM gameinfo WHERE rowId = ");
sqlBuilder.append(rowId);
sqlBuilder.append(";");
String sql = sqlBuilder.toString();
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 game in db.");
}
}
}

View File

@ -158,6 +158,24 @@ public class ListPanel extends JPanel
}
return listViewComboBox;
}
int getSelectedIndexInList()
{
return getList().getSelectedIndex();
}
void setSelectedIndexInList(int index)
{
int indexToSelect = index;
if (index >= uiModel.getGameListModel().getSize())
{
indexToSelect = uiModel.getGameListModel().getSize()-1;
}
//Select -1 first to ensure a change of selection is done
list.setSelectedIndex(-1);
list.setSelectedIndex(indexToSelect);
list.ensureIndexIsVisible(indexToSelect);
}
private JPanel getViewInfoPanel()
{

View File

@ -6,6 +6,7 @@ import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import se.lantz.model.MainViewModel;
@ -108,11 +109,35 @@ public class MainPanel extends JPanel
public void deleteCurrentGame()
{
//TODO
uiModel.deleteGame();
if (getListPanel().getSelectedIndexInList() > -1)
{
int value = showDeleteDialog();
if (value == JOptionPane.YES_OPTION)
{
int currentSelectedIndex = getListPanel().getSelectedIndexInList();
uiModel.deleteCurrentGame();
repaintAfterModifications();
SwingUtilities.invokeLater(() -> getListPanel().setSelectedIndexInList(currentSelectedIndex));
}
}
}
public void repaintAfterImport()
int showDeleteDialog()
{
String message = "Do you want to delete " + uiModel.getInfoModel().getTitle() + " from the database?";
if (uiModel.isNewGameSelected())
{
message = "Do you want to delete the new game entry?";
}
return JOptionPane.showConfirmDialog(MainPanel.this,
message,
"Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
}
public void repaintAfterModifications()
{
this.invalidate();
this.repaint();

View File

@ -85,8 +85,8 @@ public class MainWindow extends JFrame
return menuBar;
}
public void repaintAfterImport()
public void repaintAfterModifications()
{
getMainPanel().repaintAfterImport();
getMainPanel().repaintAfterModifications();
}
}

View File

@ -38,7 +38,7 @@ public class MenuManager
private JMenuItem deleteGameItem;
private JMenuItem importItem;
private JMenuItem exportItem;
private JMenuItem exitItem;
private JMenuItem backupDbItem;
private JMenuItem restoreDbItem;
@ -47,6 +47,7 @@ public class MenuManager
private JMenuItem helpItem;
private JMenuItem aboutItem;
private JMenuItem exitItem;
private MainViewModel uiModel;
private ImportManager importManager;
private ExportManager exportManager;
@ -255,7 +256,7 @@ public class MenuManager
dialog.setVisible(true);
//Refresh current game view after import
uiModel.reloadCurrentGameView();
MainWindow.getInstance().repaintAfterImport();
MainWindow.getInstance().repaintAfterModifications();
}
}
else

View File

@ -302,9 +302,23 @@ public class MainViewModel extends AbstractModel
return false;
}
public void deleteGame()
public boolean isNewGameSelected()
{
dbConnector.deleteGame(currentGameId);
return currentGameId.isEmpty();
}
public void deleteCurrentGame()
{
if (isNewGameSelected())
{
removeNewGameListData();
}
else
{
dbConnector.deleteGame(currentGameId);
//Reload the current view
reloadCurrentGameView();
}
}
private List<String> validateRequiredFields()