commit
c188f1cc96
@ -7,12 +7,7 @@ import info.openrocket.core.simulation.FlightDataTypeGroup;
|
|||||||
import info.openrocket.core.startup.Application;
|
import info.openrocket.core.startup.Application;
|
||||||
|
|
||||||
import javax.swing.JComboBox;
|
import javax.swing.JComboBox;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
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();
|
||||||
@ -20,33 +15,4 @@ public class FlightDataComboBox extends JComboBox<FlightDataType> {
|
|||||||
public static GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> createComboBox(List<FlightDataType> types) {
|
public static GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> createComboBox(List<FlightDataType> types) {
|
||||||
return new GroupableAndSearchableComboBox<>(types, trans.get("FlightDataComboBox.placeholder"));
|
return new GroupableAndSearchableComboBox<>(types, trans.get("FlightDataComboBox.placeholder"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a map of flight data group and corresponding flight data types.
|
|
||||||
* @param groups the groups
|
|
||||||
* @param types the types
|
|
||||||
* @return the map linking the types to their groups
|
|
||||||
*/
|
|
||||||
private static Map<FlightDataTypeGroup, List<FlightDataType>> createFlightDataGroupMap(
|
|
||||||
FlightDataTypeGroup[] groups, FlightDataType[] types) {
|
|
||||||
// Sort the groups based on priority (lower number = higher priority)
|
|
||||||
FlightDataTypeGroup[] sortedGroups = groups.clone();
|
|
||||||
Arrays.sort(sortedGroups, Comparator.comparingInt(FlightDataTypeGroup::getPriority));
|
|
||||||
|
|
||||||
Map<FlightDataTypeGroup, List<FlightDataType>> map = new LinkedHashMap<>();
|
|
||||||
for (FlightDataTypeGroup group : sortedGroups) {
|
|
||||||
List<FlightDataType> itemsForGroup = new ArrayList<>();
|
|
||||||
for (FlightDataType type : types) {
|
|
||||||
if (type.getGroup().equals(group)) {
|
|
||||||
itemsForGroup.add(type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Sort the types within each group based on priority
|
|
||||||
itemsForGroup.sort(Comparator.comparingInt(FlightDataType::getGroupPriority));
|
|
||||||
|
|
||||||
map.put(group, itemsForGroup);
|
|
||||||
}
|
|
||||||
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ import info.openrocket.core.preferences.ApplicationPreferences;
|
|||||||
import info.openrocket.core.unit.Unit;
|
import info.openrocket.core.unit.Unit;
|
||||||
import info.openrocket.core.util.Utils;
|
import info.openrocket.core.util.Utils;
|
||||||
|
|
||||||
|
import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
|
||||||
import net.miginfocom.swing.MigLayout;
|
import net.miginfocom.swing.MigLayout;
|
||||||
import info.openrocket.swing.gui.components.DescriptionArea;
|
import info.openrocket.swing.gui.components.DescriptionArea;
|
||||||
import info.openrocket.swing.gui.components.UnitSelector;
|
import info.openrocket.swing.gui.components.UnitSelector;
|
||||||
@ -101,7 +102,7 @@ public class SimulationPlotPanel extends JPanel {
|
|||||||
|
|
||||||
private JComboBox<PlotConfiguration> configurationSelector;
|
private JComboBox<PlotConfiguration> configurationSelector;
|
||||||
|
|
||||||
private JComboBox<FlightDataType> domainTypeSelector;
|
private GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> domainTypeSelector;
|
||||||
private UnitSelector domainUnitSelector;
|
private UnitSelector domainUnitSelector;
|
||||||
|
|
||||||
private JPanel typeSelectorPanel;
|
private JPanel typeSelectorPanel;
|
||||||
@ -487,7 +488,7 @@ public class SimulationPlotPanel extends JPanel {
|
|||||||
private final String[] POSITIONS = { AUTO_NAME, LEFT_NAME, RIGHT_NAME };
|
private final String[] POSITIONS = { AUTO_NAME, LEFT_NAME, RIGHT_NAME };
|
||||||
|
|
||||||
private final int index;
|
private final int index;
|
||||||
private final JComboBox<FlightDataType> typeSelector;
|
private final GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> typeSelector;
|
||||||
private UnitSelector unitSelector;
|
private UnitSelector unitSelector;
|
||||||
private JComboBox<String> axisSelector;
|
private JComboBox<String> axisSelector;
|
||||||
|
|
||||||
|
@ -152,6 +152,9 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
|
|||||||
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
label.setText(getDisplayString((T) value));
|
label.setText(getDisplayString((T) value));
|
||||||
|
} else {
|
||||||
|
// Handle the case when no item is selected
|
||||||
|
label.setText("");
|
||||||
}
|
}
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
@ -162,9 +165,11 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
|
|||||||
this.itemGroupMap = new LinkedHashMap<>(itemGroupMap); // Create a copy to avoid external modifications
|
this.itemGroupMap = new LinkedHashMap<>(itemGroupMap); // Create a copy to avoid external modifications
|
||||||
this.allItems = extractItemsFromMap(itemGroupMap);
|
this.allItems = extractItemsFromMap(itemGroupMap);
|
||||||
|
|
||||||
// Update the existing model instead of creating a new one
|
// Update the model
|
||||||
if (getModel() instanceof DefaultComboBoxModel<T>) {
|
if (getModel() instanceof DefaultComboBoxModel<T>) {
|
||||||
|
T selectedItem = (T) getModel().getSelectedItem();
|
||||||
ComboBoxModel<T> model = new DefaultComboBoxModel<>(new Vector<>(allItems));
|
ComboBoxModel<T> model = new DefaultComboBoxModel<>(new Vector<>(allItems));
|
||||||
|
model.setSelectedItem(selectedItem);
|
||||||
setModel(model);
|
setModel(model);
|
||||||
setupModelListener(model);
|
setupModelListener(model);
|
||||||
}
|
}
|
||||||
@ -267,7 +272,6 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
|
|||||||
itemMenu.setSelected(item == GroupableAndSearchableComboBox.this.getSelectedItem());
|
itemMenu.setSelected(item == GroupableAndSearchableComboBox.this.getSelectedItem());
|
||||||
itemMenu.addActionListener(e -> {
|
itemMenu.addActionListener(e -> {
|
||||||
setSelectedItem(item);
|
setSelectedItem(item);
|
||||||
fireActionEvent();
|
|
||||||
});
|
});
|
||||||
groupMenu.add(itemMenu);
|
groupMenu.add(itemMenu);
|
||||||
}
|
}
|
||||||
@ -365,7 +369,7 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
|
|||||||
public void setSelectedItem(Object anObject) {
|
public void setSelectedItem(Object anObject) {
|
||||||
// Hide the popups after selection
|
// Hide the popups after selection
|
||||||
hidePopups();
|
hidePopups();
|
||||||
super.setSelectedItem(anObject);
|
getModel().setSelectedItem(anObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void filter(String text) {
|
private void filter(String text) {
|
||||||
@ -439,7 +443,7 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setupModelListener(ComboBoxModel<T> model) {
|
private void setupModelListener(ComboBoxModel<T> model) {
|
||||||
if (model == null) {
|
/*if (model == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
model.addListDataListener(this);
|
model.addListDataListener(this);
|
||||||
@ -448,7 +452,7 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
|
|||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
model.setSelectedItem(GroupableAndSearchableComboBox.this.getSelectedItem());
|
model.setSelectedItem(GroupableAndSearchableComboBox.this.getSelectedItem());
|
||||||
}
|
}
|
||||||
});
|
});*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupSearchFieldListeners() {
|
private void setupSearchFieldListeners() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user