[fixes #927] Only update all sims when in simulation tab

This commit is contained in:
Sibo Van Gool 2021-07-18 03:14:28 +02:00
parent d74df08e04
commit a57d6a9b53
2 changed files with 30 additions and 19 deletions

View File

@ -221,7 +221,7 @@ public class BasicFrame extends JFrame {
// Bottom segment, rocket figure // Bottom segment, rocket figure
rocketpanel = new RocketPanel(document); rocketpanel = new RocketPanel(document, this);
vertical.setBottomComponent(rocketpanel); vertical.setBottomComponent(rocketpanel);
rocketpanel.setSelectionModel(tree.getSelectionModel()); rocketpanel.setSelectionModel(tree.getSelectionModel());
@ -1099,6 +1099,9 @@ public class BasicFrame extends JFrame {
tabbedPane.setSelectedIndex(tab); tabbedPane.setSelectedIndex(tab);
} }
public int getSelectedTab() {
return tabbedPane.getSelectedIndex();
}
private void openAction() { private void openAction() {

View File

@ -16,14 +16,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import javax.swing.ComboBoxModel; import javax.swing.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionEvent;
@ -50,6 +43,7 @@ import net.sf.openrocket.gui.figureelements.CGCaret;
import net.sf.openrocket.gui.figureelements.CPCaret; import net.sf.openrocket.gui.figureelements.CPCaret;
import net.sf.openrocket.gui.figureelements.Caret; import net.sf.openrocket.gui.figureelements.Caret;
import net.sf.openrocket.gui.figureelements.RocketInfo; import net.sf.openrocket.gui.figureelements.RocketInfo;
import net.sf.openrocket.gui.main.BasicFrame;
import net.sf.openrocket.gui.main.componenttree.ComponentTreeModel; import net.sf.openrocket.gui.main.componenttree.ComponentTreeModel;
import net.sf.openrocket.gui.simulation.SimulationWorker; import net.sf.openrocket.gui.simulation.SimulationWorker;
import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.SwingPreferences;
@ -63,7 +57,6 @@ import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.SymmetricComponent; import net.sf.openrocket.rocketcomponent.SymmetricComponent;
import net.sf.openrocket.simulation.BasicEventSimulationEngine;
import net.sf.openrocket.simulation.FlightData; import net.sf.openrocket.simulation.FlightData;
import net.sf.openrocket.simulation.customexpression.CustomExpression; import net.sf.openrocket.simulation.customexpression.CustomExpression;
import net.sf.openrocket.simulation.customexpression.CustomExpressionSimulationListener; import net.sf.openrocket.simulation.customexpression.CustomExpressionSimulationListener;
@ -153,6 +146,9 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
private List<EventListener> listeners = new ArrayList<EventListener>(); private List<EventListener> listeners = new ArrayList<EventListener>();
// Store the basic frame to know which tab is selected (Rocket design, Motors & Configuration, Flight simulations)
private final BasicFrame basicFrame;
/** /**
* The executor service used for running the background simulations. * The executor service used for running the background simulations.
@ -180,7 +176,12 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
} }
public RocketPanel(OpenRocketDocument document) { public RocketPanel(OpenRocketDocument document) {
this(document, null);
}
public RocketPanel(OpenRocketDocument document, BasicFrame basicFrame) {
this.document = document; this.document = document;
this.basicFrame = basicFrame;
Rocket rkt = document.getRocket(); Rocket rkt = document.getRocket();
@ -688,20 +689,25 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
return; return;
} }
// Start calculation process // Update simulations
if (Application.getPreferences().getAutoRunSimulations()) { if (Application.getPreferences().getAutoRunSimulations()) {
// Update only current flight config simulation when you are not in the simulations tab
updateSims(this.basicFrame != null && this.basicFrame.getSelectedTab() == BasicFrame.SIMULATION_TAB);
}
else {
// Always update the simulation of the current configuration
updateSims(false); updateSims(false);
} }
} }
/** /**
* Updates the simulations. If *currentConfig* is true, only update the simulation of the current flight * Updates the simulations. If *currentConfig* is false, only update the simulation of the current flight
* configuration. If it is false, update all the simulations. * configuration. If it is true, update all the simulations.
* *
* @param currentConfig flag to check whether to update all the simulations (false) or only the current * @param updateAllSims flag to check whether to update all the simulations (true) or only the current
* flight config sim (true) * flight config sim (false)
*/ */
private void updateSims(boolean currentConfig) { private void updateSims(boolean updateAllSims) {
// Stop previous computation (if any) // Stop previous computation (if any)
stopBackgroundSimulation(); stopBackgroundSimulation();
@ -713,14 +719,15 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
List<Simulation> sims = new LinkedList<>(); List<Simulation> sims = new LinkedList<>();
for (Simulation sim : document.getSimulations()) { for (Simulation sim : document.getSimulations()) {
// Find a Simulation based on the current flight configuration // Find a Simulation based on the current flight configuration
if (currentConfig) { if (!updateAllSims) {
if (sim.getFlightConfigurationId().compareTo(curID) == 0) { if (sim.getFlightConfigurationId().compareTo(curID) == 0) {
sims.add(sim); sims.add(sim);
break; break;
} }
} }
else else {
sims.add(sim); sims.add(sim);
}
} }
runBackgroundSimulations(sims, duplicate); runBackgroundSimulations(sims, duplicate);
} }
@ -780,6 +787,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
@Override @Override
protected FlightData doInBackground() { protected FlightData doInBackground() {
extraText.setCalculatingData(true);
// Pause a little while to allow faster UI reaction // Pause a little while to allow faster UI reaction
try { try {
Thread.sleep(300); Thread.sleep(300);