Minor adjustments to data types to make it more generic

This commit is contained in:
SiboVG 2024-08-20 18:13:54 +02:00
parent 3e00f31558
commit baaca0c64d
3 changed files with 91 additions and 51 deletions

View File

@ -28,7 +28,8 @@ import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T>, G extends Group> extends JPanel {
public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T>, G extends Group,
S extends PlotTypeSelector<G, T>> extends JPanel {
private static final Translator trans = Application.getTranslator();
//// Custom
@ -46,18 +47,15 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
private final T[] typesY;
protected PlotConfiguration<T, B> configuration;
private JComboBox<PlotConfiguration<T, B>> configurationSelector;
protected GroupableAndSearchableComboBox<G, T> domainTypeSelector;
private final JComboBox<PlotConfiguration<T, B>> configurationSelector;
protected JComboBox<T> domainTypeSelector;
private UnitSelector domainUnitSelector;
private JPanel typeSelectorPanel;
private final JPanel typeSelectorPanel;
protected int modifying = 0;
public PlotPanel(T[] typesX, T[] typesY, PlotConfiguration<T, B> customConfiguration, PlotConfiguration<T, B>[] presets,
PlotConfiguration<T, B> defaultConfiguration,
Component[] extraWidgetsX, Component[] extraWidgetsY) {
PlotConfiguration<T, B> defaultConfiguration, Component[] extraWidgetsX, Component[] extraWidgetsY) {
super(new MigLayout("fill"));
this.customConfiguration = customConfiguration;
@ -108,7 +106,13 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
//// X axis
addXAxisSelector(typesX, extraWidgetsX);
//// Y axis selector panel
typeSelectorPanel = addYAxisSelector(typesY, extraWidgetsY);
}
protected void addXAxisSelector(T[] typesX, Component[] extraWidgetsX) {
//// X axis type:
this.add(new JLabel(trans.get("simplotpanel.lbl.Xaxistype")), "spanx, split");
domainTypeSelector = new GroupableAndSearchableComboBox<>(Arrays.asList(typesX), trans.get("FlightDataComboBox.placeholder"));
@ -156,25 +160,31 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
} else {
this.add(new JLabel(), "wrap unrel");
}
}
//// Y axis selector panel
protected JPanel addYAxisSelector(T[] typesY, Component[] extraWidgetsY) {
final JPanel typeSelectorPanel;
//// Y axis types:
this.add(new JLabel(trans.get("simplotpanel.lbl.Yaxistypes")));
//// Flight events:
this.add(new JLabel(trans.get("simplotpanel.lbl.Flightevents")), "wrap rel");
JPanel yPanel = new JPanel(new MigLayout("fill, ins 0"));
yPanel.add(new JLabel(trans.get("simplotpanel.lbl.Yaxistypes")), "wrap rel");
typeSelectorPanel = new JPanel(new MigLayout("gapy rel"));
JScrollPane scroll = new JScrollPane(typeSelectorPanel);
int spanY = extraWidgetsY == null ? 1 : extraWidgetsY.length + 1;
this.add(scroll, "spany " + spanY + ", pushy, wmin 400lp, grow 100, gapright para");
yPanel.add(scroll, "pushy, grow 100");
if (extraWidgetsY != null) {
this.add(yPanel, "pushy, wmin 400lp, grow 100, gapright para");
} else {
this.add(yPanel, "pushy, spanx, wmin 400lp, grow 100, wrap");
}
// Extra Y widgets
if (extraWidgetsY != null) {
JPanel extraYPanel = new JPanel(new MigLayout("fill, ins 0"));
for (Component widgetsY : extraWidgetsY) {
this.add(widgetsY, "growx, wrap");
extraYPanel.add(widgetsY, "growx, wrap rel");
}
this.add(new JPanel(), "pushy, wrap"); // Fill up the rest of the vertical space
extraYPanel.add(new JPanel(), "pushy, grow 100"); // Fill up the rest of the vertical space
this.add(extraYPanel, "pushy, grow 100, wrap");
}
@ -225,6 +235,7 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
}
});
this.add(newYAxisBtn, "spanx, pushx, left");
return typeSelectorPanel;
}
protected PlotConfiguration<T, B> getConfiguration() {
@ -271,9 +282,11 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
protected void updatePlots() {
domainTypeSelector.setSelectedItem(configuration.getDomainAxisType());
domainUnitSelector.setUnitGroup(configuration.getDomainAxisType().getUnitGroup());
domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit());
if (domainTypeSelector != null) {
domainTypeSelector.setSelectedItem(configuration.getDomainAxisType());
domainUnitSelector.setUnitGroup(configuration.getDomainAxisType().getUnitGroup());
domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit());
}
typeSelectorPanel.removeAll();
for (int i = 0; i < configuration.getTypeCount(); i++) {
@ -281,29 +294,9 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
Unit unit = configuration.getUnit(i);
int axis = configuration.getAxis(i);
PlotTypeSelector<G, T> selector = new PlotTypeSelector<>(i, type, unit, axis, Arrays.asList(typesY));
int finalI = i;
selector.addTypeSelectionListener(e -> {
if (modifying > 0) return;
T selectedType = selector.getSelectedType();
configuration.setPlotDataType(finalI, selectedType);
selector.setUnitGroup(selectedType.getUnitGroup());
configuration.setPlotDataUnit(finalI, selector.getSelectedUnit());
setToCustom();
});
selector.addUnitSelectionListener(e -> {
if (modifying > 0) return;
configuration.setPlotDataUnit(finalI, selector.getSelectedUnit());
});
selector.addAxisSelectionListener(e -> {
if (modifying > 0) return;
configuration.setPlotDataAxis(finalI, selector.getSelectedAxis());
});
selector.addRemoveButtonListener(e -> {
configuration.removePlotDataType(finalI);
setToCustom();
updatePlots();
});
S selector = createSelector(i, type, unit, axis);
addSelectionListeners(selector, i);
typeSelectorPanel.add(selector, "wrap");
}
@ -311,4 +304,39 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
typeSelectorPanel.validate();
typeSelectorPanel.repaint();
}
protected S createSelector(int i, T type, Unit unit, int axis) {
return (S) new PlotTypeSelector<>(i, type, unit, axis, Arrays.asList(typesY));
}
private void addSelectionListeners(S selector, final int idx) {
// Type
selector.addTypeSelectionListener(e -> {
if (modifying > 0) return;
T selectedType = selector.getSelectedType();
configuration.setPlotDataType(idx, selectedType);
selector.setUnitGroup(selectedType.getUnitGroup());
configuration.setPlotDataUnit(idx, selector.getSelectedUnit());
setToCustom();
});
// Unit
selector.addUnitSelectionListener(e -> {
if (modifying > 0) return;
configuration.setPlotDataUnit(idx, selector.getSelectedUnit());
});
// Axis
selector.addAxisSelectionListener(e -> {
if (modifying > 0) return;
configuration.setPlotDataAxis(idx, selector.getSelectedAxis());
});
// Remove button
selector.addRemoveButtonListener(e -> {
configuration.removePlotDataType(idx);
setToCustom();
updatePlots();
});
}
}

View File

@ -23,19 +23,20 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
public class PlotTypeSelector<G extends Group, T extends Groupable<G> & UnitValue> extends JPanel {
private static final Translator trans = Application.getTranslator();
protected static final Translator trans = Application.getTranslator();
private static final long serialVersionUID = 9056324972817542570L;
private final String[] POSITIONS = {Util.PlotAxisSelection.AUTO.getName(),
Util.PlotAxisSelection.LEFT.getName(), Util.PlotAxisSelection.RIGHT.getName()};
private final int index;
private final GroupableAndSearchableComboBox<G, T> typeSelector;
protected final GroupableAndSearchableComboBox<G, T> typeSelector;
private final UnitSelector unitSelector;
private final JComboBox<String> axisSelector;
private final JButton removeButton;
public PlotTypeSelector(int plotIndex, T type, Unit unit, int position, List<T> availableTypes) {
public PlotTypeSelector(int plotIndex, T type, Unit unit, int position, List<T> availableTypes,
boolean addRemoveButton) {
super(new MigLayout("ins 0"));
this.index = plotIndex;
@ -59,6 +60,16 @@ public class PlotTypeSelector<G extends Group, T extends Groupable<G> & UnitValu
removeButton = new JButton(Icons.EDIT_DELETE);
removeButton.setToolTipText("Remove this plot");
removeButton.setBorderPainted(false);
if (addRemoveButton) {
addRemoveButton();
}
}
public PlotTypeSelector(int plotIndex, T type, Unit unit, int position, List<T> availableTypes) {
this(plotIndex, type, unit, position, availableTypes, true);
}
protected void addRemoveButton() {
this.add(removeButton, "gapright 0");
}

View File

@ -35,6 +35,7 @@ import info.openrocket.core.preferences.ApplicationPreferences;
import info.openrocket.swing.gui.plot.PlotConfiguration;
import info.openrocket.swing.gui.plot.PlotPanel;
import info.openrocket.swing.gui.plot.PlotTypeSelector;
import info.openrocket.swing.gui.plot.SimulationPlotConfiguration;
import info.openrocket.swing.gui.plot.SimulationPlotDialog;
import net.miginfocom.swing.MigLayout;
@ -48,7 +49,8 @@ import info.openrocket.swing.gui.theme.UITheme;
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
public class SimulationPlotPanel extends PlotPanel<FlightDataType, FlightDataBranch, FlightDataTypeGroup> {
public class SimulationPlotPanel extends PlotPanel<FlightDataType, FlightDataBranch, FlightDataTypeGroup,
PlotTypeSelector<FlightDataTypeGroup, FlightDataType>> {
@Serial
private static final long serialVersionUID = -2227129713185477998L;
@ -113,9 +115,6 @@ public class SimulationPlotPanel extends PlotPanel<FlightDataType, FlightDataBra
//// Y axis
addFlightEventsSelectorWidgets(selectorPanel);
this.add(new JPanel(), "growx");
updatePlots();
}
@ -137,8 +136,10 @@ public class SimulationPlotPanel extends PlotPanel<FlightDataType, FlightDataBra
Component[] extraWidgetsX = new Component[] {simPlotPanelDesc};
// Create extra widgets for the Y axis
//// Flight events:
JLabel label = new JLabel(trans.get("simplotpanel.lbl.Flightevents"));
JPanel selectorPanel = new JPanel(new MigLayout("ins 0"));
Component[] extraWidgetsY = new Component[] {selectorPanel};
Component[] extraWidgetsY = new Component[] {label, selectorPanel};
return new SimulationPlotPanel(simulation, types, simPlotPanelDesc, extraWidgetsX, selectorPanel, extraWidgetsY);
}