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.awt.event.ItemListener;
import java.util.Arrays; 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(); private static final Translator trans = Application.getTranslator();
//// Custom //// Custom
@ -46,18 +47,15 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
private final T[] typesY; private final T[] typesY;
protected PlotConfiguration<T, B> configuration; protected PlotConfiguration<T, B> configuration;
private JComboBox<PlotConfiguration<T, B>> configurationSelector; private final JComboBox<PlotConfiguration<T, B>> configurationSelector;
protected JComboBox<T> domainTypeSelector;
protected GroupableAndSearchableComboBox<G, T> domainTypeSelector;
private UnitSelector domainUnitSelector; private UnitSelector domainUnitSelector;
private final JPanel typeSelectorPanel;
private JPanel typeSelectorPanel;
protected int modifying = 0; protected int modifying = 0;
public PlotPanel(T[] typesX, T[] typesY, PlotConfiguration<T, B> customConfiguration, PlotConfiguration<T, B>[] presets, public PlotPanel(T[] typesX, T[] typesY, PlotConfiguration<T, B> customConfiguration, PlotConfiguration<T, B>[] presets,
PlotConfiguration<T, B> defaultConfiguration, PlotConfiguration<T, B> defaultConfiguration, Component[] extraWidgetsX, Component[] extraWidgetsY) {
Component[] extraWidgetsX, Component[] extraWidgetsY) {
super(new MigLayout("fill")); super(new MigLayout("fill"));
this.customConfiguration = customConfiguration; this.customConfiguration = customConfiguration;
@ -108,7 +106,13 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
//// X axis //// X axis
addXAxisSelector(typesX, extraWidgetsX);
//// Y axis selector panel
typeSelectorPanel = addYAxisSelector(typesY, extraWidgetsY);
}
protected void addXAxisSelector(T[] typesX, Component[] extraWidgetsX) {
//// X axis type: //// X axis type:
this.add(new JLabel(trans.get("simplotpanel.lbl.Xaxistype")), "spanx, split"); this.add(new JLabel(trans.get("simplotpanel.lbl.Xaxistype")), "spanx, split");
domainTypeSelector = new GroupableAndSearchableComboBox<>(Arrays.asList(typesX), trans.get("FlightDataComboBox.placeholder")); 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 { } else {
this.add(new JLabel(), "wrap unrel"); this.add(new JLabel(), "wrap unrel");
} }
}
protected JPanel addYAxisSelector(T[] typesY, Component[] extraWidgetsY) {
//// Y axis selector panel final JPanel typeSelectorPanel;
//// Y axis types: //// Y axis types:
this.add(new JLabel(trans.get("simplotpanel.lbl.Yaxistypes"))); JPanel yPanel = new JPanel(new MigLayout("fill, ins 0"));
//// Flight events: yPanel.add(new JLabel(trans.get("simplotpanel.lbl.Yaxistypes")), "wrap rel");
this.add(new JLabel(trans.get("simplotpanel.lbl.Flightevents")), "wrap rel");
typeSelectorPanel = new JPanel(new MigLayout("gapy rel")); typeSelectorPanel = new JPanel(new MigLayout("gapy rel"));
JScrollPane scroll = new JScrollPane(typeSelectorPanel); JScrollPane scroll = new JScrollPane(typeSelectorPanel);
int spanY = extraWidgetsY == null ? 1 : extraWidgetsY.length + 1; yPanel.add(scroll, "pushy, grow 100");
this.add(scroll, "spany " + spanY + ", pushy, wmin 400lp, grow 100, gapright para"); 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 // Extra Y widgets
if (extraWidgetsY != null) { if (extraWidgetsY != null) {
JPanel extraYPanel = new JPanel(new MigLayout("fill, ins 0"));
for (Component widgetsY : extraWidgetsY) { 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"); this.add(newYAxisBtn, "spanx, pushx, left");
return typeSelectorPanel;
} }
protected PlotConfiguration<T, B> getConfiguration() { protected PlotConfiguration<T, B> getConfiguration() {
@ -271,9 +282,11 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
protected void updatePlots() { protected void updatePlots() {
if (domainTypeSelector != null) {
domainTypeSelector.setSelectedItem(configuration.getDomainAxisType()); domainTypeSelector.setSelectedItem(configuration.getDomainAxisType());
domainUnitSelector.setUnitGroup(configuration.getDomainAxisType().getUnitGroup()); domainUnitSelector.setUnitGroup(configuration.getDomainAxisType().getUnitGroup());
domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit()); domainUnitSelector.setSelectedUnit(configuration.getDomainAxisUnit());
}
typeSelectorPanel.removeAll(); typeSelectorPanel.removeAll();
for (int i = 0; i < configuration.getTypeCount(); i++) { 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); Unit unit = configuration.getUnit(i);
int axis = configuration.getAxis(i); int axis = configuration.getAxis(i);
PlotTypeSelector<G, T> selector = new PlotTypeSelector<>(i, type, unit, axis, Arrays.asList(typesY)); S selector = createSelector(i, type, unit, axis);
int finalI = i; addSelectionListeners(selector, 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();
});
typeSelectorPanel.add(selector, "wrap"); typeSelectorPanel.add(selector, "wrap");
} }
@ -311,4 +304,39 @@ public class PlotPanel<T extends DataType & Groupable<G>, B extends DataBranch<T
typeSelectorPanel.validate(); typeSelectorPanel.validate();
typeSelectorPanel.repaint(); 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; import javax.swing.JPanel;
public class PlotTypeSelector<G extends Group, T extends Groupable<G> & UnitValue> extends 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 static final long serialVersionUID = 9056324972817542570L;
private final String[] POSITIONS = {Util.PlotAxisSelection.AUTO.getName(), private final String[] POSITIONS = {Util.PlotAxisSelection.AUTO.getName(),
Util.PlotAxisSelection.LEFT.getName(), Util.PlotAxisSelection.RIGHT.getName()}; Util.PlotAxisSelection.LEFT.getName(), Util.PlotAxisSelection.RIGHT.getName()};
private final int index; private final int index;
private final GroupableAndSearchableComboBox<G, T> typeSelector; protected final GroupableAndSearchableComboBox<G, T> typeSelector;
private final UnitSelector unitSelector; private final UnitSelector unitSelector;
private final JComboBox<String> axisSelector; private final JComboBox<String> axisSelector;
private final JButton removeButton; 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")); super(new MigLayout("ins 0"));
this.index = plotIndex; 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 = new JButton(Icons.EDIT_DELETE);
removeButton.setToolTipText("Remove this plot"); removeButton.setToolTipText("Remove this plot");
removeButton.setBorderPainted(false); 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"); 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.PlotConfiguration;
import info.openrocket.swing.gui.plot.PlotPanel; 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.SimulationPlotConfiguration;
import info.openrocket.swing.gui.plot.SimulationPlotDialog; import info.openrocket.swing.gui.plot.SimulationPlotDialog;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
@ -48,7 +49,8 @@ import info.openrocket.swing.gui.theme.UITheme;
* *
* @author Sampo Niskanen <sampo.niskanen@iki.fi> * @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 @Serial
private static final long serialVersionUID = -2227129713185477998L; private static final long serialVersionUID = -2227129713185477998L;
@ -113,9 +115,6 @@ public class SimulationPlotPanel extends PlotPanel<FlightDataType, FlightDataBra
//// Y axis //// Y axis
addFlightEventsSelectorWidgets(selectorPanel); addFlightEventsSelectorWidgets(selectorPanel);
this.add(new JPanel(), "growx");
updatePlots(); updatePlots();
} }
@ -137,8 +136,10 @@ public class SimulationPlotPanel extends PlotPanel<FlightDataType, FlightDataBra
Component[] extraWidgetsX = new Component[] {simPlotPanelDesc}; Component[] extraWidgetsX = new Component[] {simPlotPanelDesc};
// Create extra widgets for the Y axis // 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")); 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); return new SimulationPlotPanel(simulation, types, simPlotPanelDesc, extraWidgetsX, selectorPanel, extraWidgetsY);
} }