Refactor to GroupableAndSearchableComboBox

This commit is contained in:
SiboVG 2024-08-09 18:24:31 +02:00
parent 383bb4e4bb
commit d6cb2e84cc
3 changed files with 15 additions and 16 deletions

View File

@ -4,7 +4,7 @@ import info.openrocket.core.material.MaterialGroup;
import info.openrocket.core.util.Invalidatable; import info.openrocket.core.util.Invalidatable;
import info.openrocket.swing.gui.dialogs.preferences.PreferencesDialog; import info.openrocket.swing.gui.dialogs.preferences.PreferencesDialog;
import info.openrocket.swing.gui.main.BasicFrame; import info.openrocket.swing.gui.main.BasicFrame;
import info.openrocket.swing.gui.widgets.SearchableAndGroupableComboBox; import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import info.openrocket.core.document.OpenRocketDocument; import info.openrocket.core.document.OpenRocketDocument;
@ -36,7 +36,7 @@ import java.util.List;
public class MaterialPanel extends JPanel implements Invalidatable, InvalidatingWidget { public class MaterialPanel extends JPanel implements Invalidatable, InvalidatingWidget {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
private final List<Invalidatable> invalidatables = new ArrayList<>(); private final List<Invalidatable> invalidatables = new ArrayList<>();
private SearchableAndGroupableComboBox<MaterialGroup, Material> materialCombo = null; private GroupableAndSearchableComboBox<MaterialGroup, Material> materialCombo = null;
public MaterialPanel(RocketComponent component, OpenRocketDocument document, public MaterialPanel(RocketComponent component, OpenRocketDocument document,
Material.Type type, String materialString, String finishString, Material.Type type, String materialString, String finishString,
@ -160,9 +160,9 @@ public class MaterialPanel extends JPanel implements Invalidatable, Invalidating
public static class MaterialComboBox extends JComboBox<Material> { public static class MaterialComboBox extends JComboBox<Material> {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
public static SearchableAndGroupableComboBox<MaterialGroup, Material> createComboBox( public static GroupableAndSearchableComboBox<MaterialGroup, Material> createComboBox(
MaterialModel mm, Component... extraCategoryWidgets) { MaterialModel mm, Component... extraCategoryWidgets) {
return new SearchableAndGroupableComboBox<>(mm, return new GroupableAndSearchableComboBox<>(mm,
trans.get("MaterialPanel.MaterialComboBox.placeholder"), extraCategoryWidgets) { trans.get("MaterialPanel.MaterialComboBox.placeholder"), extraCategoryWidgets) {
@Override @Override
public String getDisplayString(Material item) { public String getDisplayString(Material item) {

View File

@ -1,6 +1,6 @@
package info.openrocket.swing.gui.simulation; package info.openrocket.swing.gui.simulation;
import info.openrocket.swing.gui.widgets.SearchableAndGroupableComboBox; import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import info.openrocket.core.l10n.Translator; import info.openrocket.core.l10n.Translator;
import info.openrocket.core.simulation.FlightDataType; import info.openrocket.core.simulation.FlightDataType;
import info.openrocket.core.simulation.FlightDataTypeGroup; import info.openrocket.core.simulation.FlightDataTypeGroup;
@ -17,8 +17,8 @@ import java.util.Map;
public class FlightDataComboBox extends JComboBox<FlightDataType> { public class FlightDataComboBox extends JComboBox<FlightDataType> {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
public static SearchableAndGroupableComboBox<FlightDataTypeGroup, FlightDataType> createComboBox(List<FlightDataType> types) { public static GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> createComboBox(List<FlightDataType> types) {
return new SearchableAndGroupableComboBox<>(types, trans.get("FlightDataComboBox.placeholder")); return new GroupableAndSearchableComboBox<>(types, trans.get("FlightDataComboBox.placeholder"));
} }
/** /**

View File

@ -14,7 +14,6 @@ import javax.swing.JComboBox;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@ -60,7 +59,7 @@ import java.util.Vector;
* *
* @author Sibo Van Gool <sibo.vangool@hotmail.com> * @author Sibo Van Gool <sibo.vangool@hotmail.com>
*/ */
public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable<G>> extends JComboBox<T> { public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable<G>> extends JComboBox<T> {
private static final String CHECKMARK = "\u2713"; private static final String CHECKMARK = "\u2713";
private static final int CHECKMARK_X_OFFSET = 5; private static final int CHECKMARK_X_OFFSET = 5;
private static final int CHECKMARK_Y_OFFSET = 5; private static final int CHECKMARK_Y_OFFSET = 5;
@ -89,7 +88,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
* @param placeHolderText the placeholder text for the search field (when no text is entered) * @param placeHolderText the placeholder text for the search field (when no text is entered)
* @param extraGroupPopupWidgets extra widgets to add to the groups popup. Each widget will be added as a separate menu item. * @param extraGroupPopupWidgets extra widgets to add to the groups popup. Each widget will be added as a separate menu item.
*/ */
public SearchableAndGroupableComboBox(ComboBoxModel<T> model, String placeHolderText, public GroupableAndSearchableComboBox(ComboBoxModel<T> model, String placeHolderText,
Component... extraGroupPopupWidgets) { Component... extraGroupPopupWidgets) {
super(model != null ? model : new DefaultComboBoxModel<>()); super(model != null ? model : new DefaultComboBoxModel<>());
List<T> items = new ArrayList<>(); List<T> items = new ArrayList<>();
@ -100,7 +99,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
init(model, constructItemGroupMapFromList(items), placeHolderText, extraGroupPopupWidgets); init(model, constructItemGroupMapFromList(items), placeHolderText, extraGroupPopupWidgets);
} }
public SearchableAndGroupableComboBox(List<T> allItems, String placeHolderText, Component... extraGroupPopupWidgets) { public GroupableAndSearchableComboBox(List<T> allItems, String placeHolderText, Component... extraGroupPopupWidgets) {
super(); super();
init(null, constructItemGroupMapFromList(allItems), placeHolderText, extraGroupPopupWidgets); init(null, constructItemGroupMapFromList(allItems), placeHolderText, extraGroupPopupWidgets);
@ -124,7 +123,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
private static void initColors() { private static void initColors() {
updateColors(); updateColors();
UITheme.Theme.addUIThemeChangeListener(SearchableAndGroupableComboBox::updateColors); UITheme.Theme.addUIThemeChangeListener(GroupableAndSearchableComboBox::updateColors);
} }
private static void updateColors() { private static void updateColors() {
@ -226,7 +225,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
// If the group contains the selected item, draw a checkbox // If the group contains the selected item, draw a checkbox
if (containsSelectedItem(group, (T) SearchableAndGroupableComboBox.this.getSelectedItem())) { if (containsSelectedItem(group, (T) GroupableAndSearchableComboBox.this.getSelectedItem())) {
g.drawString(CHECKMARK, CHECKMARK_X_OFFSET, getHeight() - CHECKMARK_Y_OFFSET); g.drawString(CHECKMARK, CHECKMARK_X_OFFSET, getHeight() - CHECKMARK_Y_OFFSET);
} }
} }
@ -236,7 +235,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
if (itemsForGroup != null) { if (itemsForGroup != null) {
for (T item : itemsForGroup) { for (T item : itemsForGroup) {
JCheckBoxMenuItem itemMenu = new JCheckBoxMenuItem(getDisplayString(item)); JCheckBoxMenuItem itemMenu = new JCheckBoxMenuItem(getDisplayString(item));
itemMenu.setSelected(item == SearchableAndGroupableComboBox.this.getSelectedItem()); itemMenu.setSelected(item == GroupableAndSearchableComboBox.this.getSelectedItem());
itemMenu.addActionListener(e -> { itemMenu.addActionListener(e -> {
setSelectedItem(item); setSelectedItem(item);
fireActionEvent(); fireActionEvent();
@ -297,7 +296,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
public void run() { public void run() {
T selectedItem = filteredList.getSelectedValue(); T selectedItem = filteredList.getSelectedValue();
if (selectedItem != null) { if (selectedItem != null) {
SearchableAndGroupableComboBox.this.setSelectedItem(selectedItem); GroupableAndSearchableComboBox.this.setSelectedItem(selectedItem);
} }
} }
}); });
@ -415,7 +414,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
addActionListener(new ActionListener() { addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
model.setSelectedItem(SearchableAndGroupableComboBox.this.getSelectedItem()); model.setSelectedItem(GroupableAndSearchableComboBox.this.getSelectedItem());
} }
}); });
} }