From 48e9536769d8163268ea805b13d6a95d5c6ada57 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Wed, 7 Aug 2024 23:12:42 +0200 Subject: [PATCH] Goddamn, this index issue caused me so much trouble Fixes updating combobox after adding custom material --- .../swing/gui/adaptors/MaterialModel.java | 6 +- .../SearchableAndCategorizableComboBox.java | 56 ++++++++++++------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/swing/src/main/java/info/openrocket/swing/gui/adaptors/MaterialModel.java b/swing/src/main/java/info/openrocket/swing/gui/adaptors/MaterialModel.java index c06670866..8b46adf93 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/adaptors/MaterialModel.java +++ b/swing/src/main/java/info/openrocket/swing/gui/adaptors/MaterialModel.java @@ -103,21 +103,21 @@ public class MaterialModel extends AbstractListModel implements return; Material material = dialog.getMaterial(); - this.setMethod.invoke(this.rocketComponent, material); - if (dialog.isAddSelected()) { this.applicationDatabase.add(material); } else { material.setDocumentMaterial(true); this.documentDatabase.add(material); } + + this.setMethod.invoke(this.rocketComponent, material); } @Override public Material getElementAt(int index) { if (index < applicationDatabase.size()) { return applicationDatabase.get(index); - } else if (index < applicationDatabase.size() + documentDatabase.size() - 1) { + } else if (index < applicationDatabase.size() + documentDatabase.size()) { return documentDatabase.get(index - applicationDatabase.size()); } return null; diff --git a/swing/src/main/java/info/openrocket/swing/gui/widgets/SearchableAndCategorizableComboBox.java b/swing/src/main/java/info/openrocket/swing/gui/widgets/SearchableAndCategorizableComboBox.java index f2b612909..601ae7e16 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/widgets/SearchableAndCategorizableComboBox.java +++ b/swing/src/main/java/info/openrocket/swing/gui/widgets/SearchableAndCategorizableComboBox.java @@ -16,10 +16,8 @@ import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; -import javax.swing.MutableComboBoxModel; import javax.swing.SwingUtilities; import javax.swing.event.ListDataEvent; -import javax.swing.event.ListDataListener; import javax.swing.event.ListSelectionEvent; import javax.swing.plaf.basic.BasicArrowButton; import java.awt.BorderLayout; @@ -41,7 +39,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -166,24 +163,10 @@ public class SearchableAndCategorizableComboBox model = getModel(); - if (model instanceof MutableComboBoxModel mutableModel) { - - // Remove all existing elements - while (mutableModel.getSize() > 0) { - mutableModel.removeElementAt(0); - } - - // Add new elements - for (T item : allItems) { - mutableModel.addElement(item); - } - } else { - // If the model is not mutable, we need to set a new model - // This should be a rare case, as DefaultComboBoxModel is mutable - model = new DefaultComboBoxModel<>(new Vector<>(allItems)); - setupModelListener(model); + if (getModel() instanceof DefaultComboBoxModel) { + ComboBoxModel model = new DefaultComboBoxModel<>(new Vector<>(allItems)); setModel(model); + setupModelListener(model); } // Recreate the search fields only if they don't exist @@ -205,6 +188,21 @@ public class SearchableAndCategorizableComboBox model = getModel(); + if (model == null) { + return; + } + List items = new ArrayList<>(); + for (int i = 0; i < model.getSize(); i++) { + T item = model.getElementAt(i); + if (item != null) { + items.add(item); + } + } + updateItems(constructItemGroupMapFromList(items)); + } + private List extractItemsFromMap(Map> itemGroupMap) { Set uniqueItems = new HashSet<>(); // Use a Set to ensure uniqueness for (G group : itemGroupMap.keySet()) { @@ -498,6 +496,24 @@ public class SearchableAndCategorizableComboBox