[#2001] Add document save change on sim edit

This commit is contained in:
SiboVG 2023-02-01 15:32:06 +00:00
parent a7e094311b
commit 8d62dee370
7 changed files with 48 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.util.*; import java.util.*;
import net.sf.openrocket.rocketcomponent.*; import net.sf.openrocket.rocketcomponent.*;
import net.sf.openrocket.util.StateChangeListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -32,7 +33,7 @@ import net.sf.openrocket.util.ArrayList;
* *
* @author Sampo Niskanen <sampo.niskanen@iki.fi> * @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/ */
public class OpenRocketDocument implements ComponentChangeListener { public class OpenRocketDocument implements ComponentChangeListener, StateChangeListener {
private static final Logger log = LoggerFactory.getLogger(OpenRocketDocument.class); private static final Logger log = LoggerFactory.getLogger(OpenRocketDocument.class);
private final List<String> file_extensions = Arrays.asList("ork", "ork.gz", "rkt", "rkt.gz"); // Possible extensions of an OpenRocket document private final List<String> file_extensions = Arrays.asList("ork", "ork.gz", "rkt", "rkt.gz"); // Possible extensions of an OpenRocket document
/** /**
@ -86,6 +87,7 @@ public class OpenRocketDocument implements ComponentChangeListener {
private final ArrayList<UndoRedoListener> undoRedoListeners = new ArrayList<UndoRedoListener>(2); private final ArrayList<UndoRedoListener> undoRedoListeners = new ArrayList<UndoRedoListener>(2);
private File file = null; private File file = null;
private int modID = -1;
private int savedID = -1; private int savedID = -1;
private final StorageOptions storageOptions = new StorageOptions(); private final StorageOptions storageOptions = new StorageOptions();
@ -224,7 +226,7 @@ public class OpenRocketDocument implements ComponentChangeListener {
* @return if the current rocket is saved * @return if the current rocket is saved
*/ */
public boolean isSaved() { public boolean isSaved() {
return rocket.getModID() == savedID; return rocket.getModID() + modID == savedID;
} }
/** /**
@ -232,10 +234,10 @@ public class OpenRocketDocument implements ComponentChangeListener {
* @param saved if the current rocket or none will be set to save * @param saved if the current rocket or none will be set to save
*/ */
public void setSaved(boolean saved) { public void setSaved(boolean saved) {
if (saved == false) if (!saved)
this.savedID = -1; this.savedID = -1;
else else
this.savedID = rocket.getModID(); this.savedID = rocket.getModID() + modID;
} }
/** /**
@ -632,6 +634,12 @@ public class OpenRocketDocument implements ComponentChangeListener {
fireDocumentChangeEvent(new DocumentChangeEvent(e.getSource())); fireDocumentChangeEvent(new DocumentChangeEvent(e.getSource()));
} }
@Override
public void stateChanged(EventObject e) {
modID++;
fireDocumentChangeEvent(new DocumentChangeEvent(e.getSource()));
}
/** /**
* Sets the latest description * Sets the latest description
*/ */

View File

@ -70,6 +70,7 @@ public class Simulation implements ChangeSource, Cloneable {
private SafetyMutex mutex = SafetyMutex.newInstance(); private SafetyMutex mutex = SafetyMutex.newInstance();
private final OpenRocketDocument document;
private final Rocket rocket; private final Rocket rocket;
FlightConfigurationId configId = FlightConfigurationId.ERROR_FCID; FlightConfigurationId configId = FlightConfigurationId.ERROR_FCID;
@ -107,7 +108,8 @@ public class Simulation implements ChangeSource, Cloneable {
* *
* @param rocket the rocket associated with the simulation. * @param rocket the rocket associated with the simulation.
*/ */
public Simulation(Rocket rocket) { public Simulation(OpenRocketDocument document, Rocket rocket) {
this.document = document;
this.rocket = rocket; this.rocket = rocket;
this.status = Status.NOT_SIMULATED; this.status = Status.NOT_SIMULATED;
@ -118,10 +120,21 @@ public class Simulation implements ChangeSource, Cloneable {
setFlightConfigurationId(fcid); setFlightConfigurationId(fcid);
options.addChangeListener(new ConditionListener()); options.addChangeListener(new ConditionListener());
addChangeListener(document);
}
/**
* Create a new simulation for the rocket. Parent document should also be provided.
* The initial motor configuration is taken from the default rocket configuration.
*
* @param rocket the rocket associated with the simulation.
*/
public Simulation(Rocket rocket) {
this(null, rocket);
} }
public Simulation(Rocket rocket, Status status, String name, SimulationOptions options, public Simulation(OpenRocketDocument document, Rocket rocket, Status status, String name, SimulationOptions options,
List<SimulationExtension> extensions, FlightData data) { List<SimulationExtension> extensions, FlightData data) {
if (rocket == null) if (rocket == null)
@ -133,6 +146,7 @@ public class Simulation implements ChangeSource, Cloneable {
if (options == null) if (options == null)
throw new IllegalArgumentException("options cannot be null"); throw new IllegalArgumentException("options cannot be null");
this.document = document;
this.rocket = rocket; this.rocket = rocket;
if (status == Status.UPTODATE) { if (status == Status.UPTODATE) {
@ -151,6 +165,7 @@ public class Simulation implements ChangeSource, Cloneable {
this.setFlightConfigurationId(config.getFlightConfigurationID()); this.setFlightConfigurationId(config.getFlightConfigurationID());
options.addChangeListener(new ConditionListener()); options.addChangeListener(new ConditionListener());
addChangeListener(document);
if (extensions != null) { if (extensions != null) {
this.simulationExtensions.addAll(extensions); this.simulationExtensions.addAll(extensions);
@ -525,7 +540,7 @@ public class Simulation implements ChangeSource, Cloneable {
public Simulation duplicateSimulation(Rocket newRocket) { public Simulation duplicateSimulation(Rocket newRocket) {
mutex.lock("duplicateSimulation"); mutex.lock("duplicateSimulation");
try { try {
final Simulation newSim = new Simulation(newRocket); final Simulation newSim = new Simulation(this.document, newRocket);
newSim.name = this.name; newSim.name = this.name;
newSim.configId = this.configId; newSim.configId = this.configId;
newSim.options.copyFrom(this.options); newSim.options.copyFrom(this.options);

View File

@ -139,7 +139,7 @@ class SingleSimulationHandler extends AbstractElementHandler {
status = Status.NOT_SIMULATED; status = Status.NOT_SIMULATED;
} }
Simulation simulation = new Simulation(doc.getRocket(), status, name, Simulation simulation = new Simulation(doc, doc.getRocket(), status, name,
options, extensions, data); options, extensions, data);
simulation.setFlightConfigurationId( idToSet ); simulation.setFlightConfigurationId( idToSet );

View File

@ -128,7 +128,7 @@ public class DefaultSimulationModifierService implements SimulationModifierServi
Rocket rocket = document.getRocket(); Rocket rocket = document.getRocket();
// Simulation is used to calculate default min/max values // Simulation is used to calculate default min/max values
Simulation simulation = new Simulation(rocket); Simulation simulation = new Simulation(document, rocket);
for (RocketComponent c : rocket) { for (RocketComponent c : rocket) {

View File

@ -54,6 +54,8 @@ import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.document.OpenRocketDocumentFactory; import net.sf.openrocket.document.OpenRocketDocumentFactory;
import net.sf.openrocket.document.StorageOptions; import net.sf.openrocket.document.StorageOptions;
import net.sf.openrocket.document.StorageOptions.FileType; import net.sf.openrocket.document.StorageOptions.FileType;
import net.sf.openrocket.document.events.DocumentChangeEvent;
import net.sf.openrocket.document.events.DocumentChangeListener;
import net.sf.openrocket.file.GeneralRocketSaver; import net.sf.openrocket.file.GeneralRocketSaver;
import net.sf.openrocket.file.RocketLoadException; import net.sf.openrocket.file.RocketLoadException;
import net.sf.openrocket.gui.components.StyledLabel; import net.sf.openrocket.gui.components.StyledLabel;
@ -247,6 +249,13 @@ public class BasicFrame extends JFrame {
} }
}); });
document.addDocumentChangeListener(new DocumentChangeListener() {
@Override
public void documentChanged(DocumentChangeEvent e) {
setTitle();
}
});
setTitle(); setTitle();
this.pack(); this.pack();

View File

@ -222,7 +222,7 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
rocket.setSelectedConfiguration(config.getKey()); rocket.setSelectedConfiguration(config.getKey());
// create simulation for configuration // create simulation for configuration
Simulation newSim = new Simulation(rocket); Simulation newSim = new Simulation(document, rocket);
newSim.setName(doc.getNextSimulationName()); newSim.setName(doc.getNextSimulationName());
doc.addSimulation(newSim); doc.addSimulation(newSim);

View File

@ -267,7 +267,7 @@ public class SimulationPanel extends JPanel {
} }
private void newSimulation() { private void newSimulation() {
Simulation sim = new Simulation(document.getRocket()); Simulation sim = new Simulation(document, document.getRocket());
sim.setName(document.getNextSimulationName()); sim.setName(document.getNextSimulationName());
int n = document.getSimulationCount(); int n = document.getSimulationCount();