Synchronize table selection across motors, recovery & stage

This commit is contained in:
SiboVG 2022-07-30 02:46:38 +02:00
parent 687fb6f6e3
commit a210661ccd
2 changed files with 47 additions and 7 deletions

View File

@ -20,6 +20,7 @@ import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.document.Simulation; import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.gui.dialogs.flightconfiguration.RenameConfigDialog; import net.sf.openrocket.gui.dialogs.flightconfiguration.RenameConfigDialog;
import net.sf.openrocket.gui.main.flightconfigpanel.FlightConfigurablePanel;
import net.sf.openrocket.gui.main.flightconfigpanel.MotorConfigurationPanel; import net.sf.openrocket.gui.main.flightconfigpanel.MotorConfigurationPanel;
import net.sf.openrocket.gui.main.flightconfigpanel.RecoveryConfigurationPanel; import net.sf.openrocket.gui.main.flightconfigpanel.RecoveryConfigurationPanel;
import net.sf.openrocket.gui.main.flightconfigpanel.SeparationConfigurationPanel; import net.sf.openrocket.gui.main.flightconfigpanel.SeparationConfigurationPanel;
@ -113,23 +114,29 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
this.add(duplicateConfButton, "wrap"); this.add(duplicateConfButton, "wrap");
tabs.addChangeListener(new ChangeListener() { tabs.addChangeListener(new ChangeListener() {
private FlightConfigurablePanel<?> previousPanel = null;
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
// Trigger a selection of the motor/recovery/configuration item // Trigger a selection of the motor/recovery/configuration item
FlightConfigurablePanel<?> panel = null;
switch (tabs.getSelectedIndex()) { switch (tabs.getSelectedIndex()) {
case MOTOR_TAB_INDEX: case MOTOR_TAB_INDEX:
motorConfigurationPanel.updateButtonState(); panel = motorConfigurationPanel;
motorConfigurationPanel.takeTheSpotlight();
break; break;
case RECOVERY_TAB_INDEX: case RECOVERY_TAB_INDEX:
recoveryConfigurationPanel.updateButtonState(); panel = recoveryConfigurationPanel;
motorConfigurationPanel.takeTheSpotlight();
break; break;
case SEPARATION_TAB_INDEX: case SEPARATION_TAB_INDEX:
separationConfigurationPanel.updateButtonState(); panel = separationConfigurationPanel;
motorConfigurationPanel.takeTheSpotlight();
break; break;
} }
// Update the panel selection, focus, and button state
if (panel == null) return;
synchronizePanelSelection(previousPanel, panel);
panel.updateButtonState();
panel.takeTheSpotlight();
previousPanel = panel;
} }
}); });
@ -300,6 +307,16 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
} }
private void synchronizePanelSelection(FlightConfigurablePanel<?> source, FlightConfigurablePanel<?> target) {
if (source == null || target == null) return;
List<FlightConfigurationId> fids = source.getSelectedConfigurationIds();
if (fids == null || fids.isEmpty()) {
target.clearSelection();
} else {
target.setSelectedConfigurationIds(fids);
}
}
private List<FlightConfigurationId> getSelectedConfigurationIds() { private List<FlightConfigurationId> getSelectedConfigurationIds() {
switch (tabs.getSelectedIndex()) { switch (tabs.getSelectedIndex()) {
case MOTOR_TAB_INDEX: case MOTOR_TAB_INDEX:

View File

@ -175,7 +175,7 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
int row = table.rowAtPoint(e.getPoint()); int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint()); int col = table.columnAtPoint(e.getPoint());
if (row == -1 || col == -1) { if (row == -1 || col == -1) {
table.clearSelection(); clearSelection();
} }
} }
} }
@ -189,6 +189,10 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
*/ */
protected abstract JTable initializeTable(); protected abstract JTable initializeTable();
public void clearSelection() {
table.clearSelection();
}
protected T getSelectedComponent() { protected T getSelectedComponent() {
int col = table.convertColumnIndexToModel(table.getSelectedColumn()); int col = table.convertColumnIndexToModel(table.getSelectedColumn());
@ -269,6 +273,25 @@ public abstract class FlightConfigurablePanel<T extends FlightConfigurableCompon
return Ids; return Ids;
} }
/**
* Select the rows of the table that correspond to the given FlightConfigurationIds. The second column of the table
* will be used for the selection.
* @param fids flight configuration ids to select
*/
public void setSelectedConfigurationIds(List<FlightConfigurationId> fids) {
if (fids == null || fids.isEmpty() || table.getColumnCount() == 0) return;
for (FlightConfigurationId id : fids) {
if (id == FlightConfigurationId.DEFAULT_VALUE_FCID) continue;
for (int rowNum = 0; rowNum < table.getRowCount(); rowNum++) {
FlightConfigurationId rowFCID = rocket.getId(rowNum );
if (rowFCID.equals(id) && !table.isRowSelected(rowNum)) {
table.changeSelection(rowNum, 1, false, false);
}
}
}
}
protected abstract class FlightConfigurableCellRenderer extends DefaultTableCellRenderer { protected abstract class FlightConfigurableCellRenderer extends DefaultTableCellRenderer {
@Override @Override