diff --git a/core/src/net/sf/openrocket/document/OpenRocketDocument.java b/core/src/net/sf/openrocket/document/OpenRocketDocument.java index 6173afefa..c1ea85f24 100644 --- a/core/src/net/sf/openrocket/document/OpenRocketDocument.java +++ b/core/src/net/sf/openrocket/document/OpenRocketDocument.java @@ -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 */ -public class OpenRocketDocument implements ComponentChangeListener { +public class OpenRocketDocument implements ComponentChangeListener, StateChangeListener { private static final Logger log = LoggerFactory.getLogger(OpenRocketDocument.class); private final List 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 undoRedoListeners = new ArrayList(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 */ diff --git a/core/src/net/sf/openrocket/document/Simulation.java b/core/src/net/sf/openrocket/document/Simulation.java index 8d4644450..a695a1165 100644 --- a/core/src/net/sf/openrocket/document/Simulation.java +++ b/core/src/net/sf/openrocket/document/Simulation.java @@ -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 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); diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/SingleSimulationHandler.java b/core/src/net/sf/openrocket/file/openrocket/importt/SingleSimulationHandler.java index c559a2fca..6483e09bc 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/SingleSimulationHandler.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/SingleSimulationHandler.java @@ -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 ); diff --git a/core/src/net/sf/openrocket/optimization/services/DefaultSimulationModifierService.java b/core/src/net/sf/openrocket/optimization/services/DefaultSimulationModifierService.java index 79f6e3ef8..ccf25dc7d 100644 --- a/core/src/net/sf/openrocket/optimization/services/DefaultSimulationModifierService.java +++ b/core/src/net/sf/openrocket/optimization/services/DefaultSimulationModifierService.java @@ -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) { diff --git a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java index a6599ce43..d505e00f1 100644 --- a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -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(); diff --git a/swing/src/net/sf/openrocket/gui/main/FlightConfigurationPanel.java b/swing/src/net/sf/openrocket/gui/main/FlightConfigurationPanel.java index edec986cc..878300b37 100644 --- a/swing/src/net/sf/openrocket/gui/main/FlightConfigurationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/FlightConfigurationPanel.java @@ -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); diff --git a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java index 1716cc016..b7fd17732 100644 --- a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java @@ -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();