From 96ce48d4c8c3fa536788627431ae730381da8d62 Mon Sep 17 00:00:00 2001 From: lantzelot-swe <75668734+lantzelot-swe@users.noreply.github.com> Date: Fri, 8 Sep 2023 23:16:07 +0200 Subject: [PATCH] fix: adding a new game works again The search text field is disabled when unsaved data is available for the currently selected game. --- src/main/java/se/lantz/gui/ListPanel.java | 55 ++++++++++++++--------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/main/java/se/lantz/gui/ListPanel.java b/src/main/java/se/lantz/gui/ListPanel.java index 19051b6..852096a 100644 --- a/src/main/java/se/lantz/gui/ListPanel.java +++ b/src/main/java/se/lantz/gui/ListPanel.java @@ -61,6 +61,8 @@ public class ListPanel extends JPanel private boolean delayDetailsUpdate = false; private boolean pageButtonPressed = false; + private boolean filterEnabled = true; + private boolean isFiltering = false; private ListDataListener listDataListener = new ListDataListener() { @@ -106,31 +108,34 @@ public class ListPanel extends JPanel private void filter() { - isFiltering = true; - GameListModel listModel = uiModel.getGameListModel(); - GameListData selectedGame = list.getSelectedValue(); - listModel.clear(); - String filterText = filterTextField.getText(); - List filteredList = new ArrayList<>(); - for (GameListData item : listModel.getCurrentGameList()) + if (filterEnabled) { - if (listModel.filterMatch(item, filterText)) + isFiltering = true; + GameListModel listModel = uiModel.getGameListModel(); + GameListData selectedGame = list.getSelectedValue(); + listModel.clear(); + String filterText = filterTextField.getText(); + List filteredList = new ArrayList<>(); + for (GameListData item : listModel.getCurrentGameList()) { - filteredList.add(item); + if (listModel.filterMatch(item, filterText)) + { + filteredList.add(item); + } } + //Add all at once (for performance!) + listModel.addAll(filteredList); + if (filteredList.contains(selectedGame)) + { + list.setSelectedValue(selectedGame, true); + } + else if (!filteredList.isEmpty()) + { + list.setSelectedIndex(0); + } + updateViewInfoLabel(); + isFiltering = false; } - //Add all at once (for performance!) - listModel.addAll(filteredList); - if (filteredList.contains(selectedGame)) - { - list.setSelectedValue(selectedGame, true); - } - else if (!filteredList.isEmpty()) - { - list.setSelectedIndex(0); - } - updateViewInfoLabel(); - isFiltering = false; } }; @@ -621,10 +626,17 @@ public class ListPanel extends JPanel { getListViewComboBox().setEnabled(!uiModel.isDataChanged()); getListViewEditButton().setEnabled(!uiModel.isDataChanged()); + if (!getFilterTextField().hasFocus()) + { + getFilterTextField().setEnabled(!uiModel.isDataChanged()); + } + filterEnabled = !uiModel.isDataChanged(); } public void addNewGame() { + getFilterTextField().setText(""); + filterEnabled = false; //Add new entry and select in the list uiModel.addNewGameListData(); int rowToSelect = getList().getModel().getSize() - 1; @@ -632,6 +644,7 @@ public class ListPanel extends JPanel getList().ensureIndexIsVisible(rowToSelect); mainPanel.getGameDetailsBackgroundPanel().updateSelectedGame(list.getSelectedValue()); mainPanel.getGameDetailsBackgroundPanel().focusTitleField(); + getFilterTextField().setEnabled(false); } public void addNewInfoSlot()