[#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 net.sf.openrocket.rocketcomponent.*;
import net.sf.openrocket.util.StateChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -32,7 +33,7 @@ import net.sf.openrocket.util.ArrayList;
*
* @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 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 File file = null;
private int modID = -1;
private int savedID = -1;
private final StorageOptions storageOptions = new StorageOptions();
@ -224,7 +226,7 @@ public class OpenRocketDocument implements ComponentChangeListener {
* @return if the current rocket is saved
*/
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
*/
public void setSaved(boolean saved) {
if (saved == false)
if (!saved)
this.savedID = -1;
else
this.savedID = rocket.getModID();
this.savedID = rocket.getModID() + modID;
}
/**
@ -632,6 +634,12 @@ public class OpenRocketDocument implements ComponentChangeListener {
fireDocumentChangeEvent(new DocumentChangeEvent(e.getSource()));
}
@Override
public void stateChanged(EventObject e) {
modID++;
fireDocumentChangeEvent(new DocumentChangeEvent(e.getSource()));
}
/**
* Sets the latest description
*/

View File

@ -69,7 +69,8 @@ public class Simulation implements ChangeSource, Cloneable {
private SafetyMutex mutex = SafetyMutex.newInstance();
private final OpenRocketDocument document;
private final Rocket rocket;
FlightConfigurationId configId = FlightConfigurationId.ERROR_FCID;
@ -107,21 +108,33 @@ public class Simulation implements ChangeSource, Cloneable {
*
* @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.status = Status.NOT_SIMULATED;
DefaultSimulationOptionFactory f = Application.getInjector().getInstance(DefaultSimulationOptionFactory.class);
options.copyConditionsFrom(f.getDefault());
FlightConfigurationId fcid = rocket.getSelectedConfiguration().getFlightConfigurationID();
setFlightConfigurationId(fcid);
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) {
if (rocket == null)
@ -132,7 +145,8 @@ public class Simulation implements ChangeSource, Cloneable {
throw new IllegalArgumentException("name cannot be null");
if (options == null)
throw new IllegalArgumentException("options cannot be null");
this.document = document;
this.rocket = rocket;
if (status == Status.UPTODATE) {
@ -151,6 +165,7 @@ public class Simulation implements ChangeSource, Cloneable {
this.setFlightConfigurationId(config.getFlightConfigurationID());
options.addChangeListener(new ConditionListener());
addChangeListener(document);
if (extensions != null) {
this.simulationExtensions.addAll(extensions);
@ -525,7 +540,7 @@ public class Simulation implements ChangeSource, Cloneable {
public Simulation duplicateSimulation(Rocket newRocket) {
mutex.lock("duplicateSimulation");
try {
final Simulation newSim = new Simulation(newRocket);
final Simulation newSim = new Simulation(this.document, newRocket);
newSim.name = this.name;
newSim.configId = this.configId;
newSim.options.copyFrom(this.options);

View File

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

View File

@ -128,7 +128,7 @@ public class DefaultSimulationModifierService implements SimulationModifierServi
Rocket rocket = document.getRocket();
// Simulation is used to calculate default min/max values
Simulation simulation = new Simulation(rocket);
Simulation simulation = new Simulation(document, 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.StorageOptions;
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.RocketLoadException;
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();
this.pack();

View File

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

View File

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