Goddamn, this index issue caused me so much trouble

Fixes updating combobox after adding custom material
This commit is contained in:
SiboVG 2024-08-07 23:12:42 +02:00
parent 16bd21bb90
commit 48e9536769
2 changed files with 39 additions and 23 deletions

View File

@ -103,21 +103,21 @@ public class MaterialModel extends AbstractListModel<Material> implements
return; return;
Material material = dialog.getMaterial(); Material material = dialog.getMaterial();
this.setMethod.invoke(this.rocketComponent, material);
if (dialog.isAddSelected()) { if (dialog.isAddSelected()) {
this.applicationDatabase.add(material); this.applicationDatabase.add(material);
} else { } else {
material.setDocumentMaterial(true); material.setDocumentMaterial(true);
this.documentDatabase.add(material); this.documentDatabase.add(material);
} }
this.setMethod.invoke(this.rocketComponent, material);
} }
@Override @Override
public Material getElementAt(int index) { public Material getElementAt(int index) {
if (index < applicationDatabase.size()) { if (index < applicationDatabase.size()) {
return applicationDatabase.get(index); 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 documentDatabase.get(index - applicationDatabase.size());
} }
return null; return null;

View File

@ -16,10 +16,8 @@ import javax.swing.JMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.MutableComboBoxModel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.plaf.basic.BasicArrowButton; import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.BorderLayout; import java.awt.BorderLayout;
@ -41,7 +39,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -166,24 +163,10 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
this.allItems = extractItemsFromMap(itemGroupMap); this.allItems = extractItemsFromMap(itemGroupMap);
// Update the existing model instead of creating a new one // Update the existing model instead of creating a new one
ComboBoxModel<T> model = getModel(); if (getModel() instanceof DefaultComboBoxModel<T>) {
if (model instanceof MutableComboBoxModel<T> mutableModel) { ComboBoxModel<T> model = new DefaultComboBoxModel<>(new Vector<>(allItems));
// 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);
setModel(model); setModel(model);
setupModelListener(model);
} }
// Recreate the search fields only if they don't exist // Recreate the search fields only if they don't exist
@ -205,6 +188,21 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
repaint(); repaint();
} }
public void updateItemsFromModel() {
ComboBoxModel<T> model = getModel();
if (model == null) {
return;
}
List<T> 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<T> extractItemsFromMap(Map<G, List<T>> itemGroupMap) { private List<T> extractItemsFromMap(Map<G, List<T>> itemGroupMap) {
Set<T> uniqueItems = new HashSet<>(); // Use a Set to ensure uniqueness Set<T> uniqueItems = new HashSet<>(); // Use a Set to ensure uniqueness
for (G group : itemGroupMap.keySet()) { for (G group : itemGroupMap.keySet()) {
@ -498,6 +496,24 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
} }
} }
@Override
public void intervalAdded(ListDataEvent e) {
super.intervalAdded(e);
updateItemsFromModel();
}
@Override
public void intervalRemoved(ListDataEvent e) {
super.intervalRemoved(e);
updateItemsFromModel();
}
@Override
public void contentsChanged(ListDataEvent e) {
super.contentsChanged(e);
updateItemsFromModel();
}
private class SearchFieldKeyAdapter extends KeyAdapter { private class SearchFieldKeyAdapter extends KeyAdapter {
private final PlaceholderTextField primaryField; private final PlaceholderTextField primaryField;
private final PlaceholderTextField secondaryField; private final PlaceholderTextField secondaryField;