Fix sim table highlight after tab switch

This commit is contained in:
SiboVG 2022-07-30 16:38:17 +02:00
parent a210661ccd
commit 68d2236654
2 changed files with 18 additions and 9 deletions

View File

@ -102,6 +102,7 @@ public class BasicFrame extends JFrame {
public static final int DESIGN_TAB = 0; public static final int DESIGN_TAB = 0;
public static final int FLIGHT_CONFIGURATION_TAB = 1; public static final int FLIGHT_CONFIGURATION_TAB = 1;
public static final int SIMULATION_TAB = 2; public static final int SIMULATION_TAB = 2;
private int previousTab = DESIGN_TAB;
/** /**
@ -1751,6 +1752,10 @@ public class BasicFrame extends JFrame {
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
JTabbedPane tabSource = (JTabbedPane) e.getSource(); JTabbedPane tabSource = (JTabbedPane) e.getSource();
int tab = tabSource.getSelectedIndex(); int tab = tabSource.getSelectedIndex();
if (previousTab == SIMULATION_TAB) {
simulationPanel.updatePreviousSelection();
}
previousTab = tab;
switch (tab) { switch (tab) {
case DESIGN_TAB: case DESIGN_TAB:
designPanel.takeTheSpotlight(); designPanel.takeTheSpotlight();

View File

@ -10,7 +10,6 @@ import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
@ -104,6 +103,8 @@ public class SimulationPanel extends JPanel {
private final SimulationAction duplicateSimulationAction; private final SimulationAction duplicateSimulationAction;
private final SimulationAction deleteSimulationAction; private final SimulationAction deleteSimulationAction;
private int[] previousSelection = null;
public SimulationPanel(OpenRocketDocument doc) { public SimulationPanel(OpenRocketDocument doc) {
super(new MigLayout("fill", "[grow][][][][][][grow]")); super(new MigLayout("fill", "[grow][][][][][][grow]"));
@ -266,6 +267,10 @@ public class SimulationPanel extends JPanel {
updateButtonStates(); updateButtonStates();
} }
public void updatePreviousSelection() {
this.previousSelection = simulationTable.getSelectedRows();
}
private void newSimulation() { private void newSimulation() {
Simulation sim = new Simulation(document.getRocket()); Simulation sim = new Simulation(document.getRocket());
sim.setName(document.getNextSimulationName()); sim.setName(document.getNextSimulationName());
@ -994,18 +999,17 @@ public class SimulationPanel extends JPanel {
} }
/** /**
* Focus on the simulation table * Focus on the simulation table and maintain the previous row selection(s).
*/ */
public void takeTheSpotlight() { public void takeTheSpotlight() {
simulationTable.requestFocusInWindow(); simulationTable.requestFocusInWindow();
int selection = simulationTable.getSelectionModel().getAnchorSelectionIndex(); if (previousSelection == null || previousSelection.length == 0) {
if (selection == -1) { simulationTable.setRowSelectionInterval(0, 0);
if (simulationTable.getRowCount() > 0) { } else {
selection = 0; simulationTable.clearSelection();
} else { for (int row : previousSelection) {
return; simulationTable.addRowSelectionInterval(row, row);
} }
} }
simulationTable.setRowSelectionInterval(selection, selection);
} }
} }