Rework the CustomExpression evaluation to use SimulationListeners. Removed the OpenR...
[debian/openrocket] / android / src / net / sf / openrocket / android / CurrentRocket.java
index 187d46e2f40d52d2b21ed9577befa40e43d92b71..36446063955e82b39a326779b03bf061e96acaa7 100644 (file)
@@ -1,14 +1,22 @@
 package net.sf.openrocket.android;\r
 \r
+import static net.sf.openrocket.android.events.Events.*;\r
+\r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
 \r
 import net.sf.openrocket.aerodynamics.WarningSet;\r
 import net.sf.openrocket.document.OpenRocketDocument;\r
 import net.sf.openrocket.document.Simulation;\r
+import net.sf.openrocket.document.StorageOptions;\r
 import net.sf.openrocket.file.openrocket.OpenRocketSaver;\r
 import net.sf.openrocket.rocketcomponent.Rocket;\r
+import android.content.Context;\r
+import android.content.Intent;\r
 import android.net.Uri;\r
+import android.support.v4.content.LocalBroadcastManager;\r
 \r
 public class CurrentRocket {\r
 \r
@@ -17,12 +25,9 @@ public class CurrentRocket {
        private OpenRocketDocument rocketDocument;\r
        private WarningSet warnings;\r
 \r
-       private RocketChangedEventHandler handler;\r
-       \r
-       public void setHandler( RocketChangedEventHandler handler ) {\r
-               this.handler = handler;\r
-       }\r
-       \r
+       private boolean isModified = false;\r
+       private Set<Integer> runningSims = new HashSet<Integer>();\r
+\r
        /**\r
         * @return the rocketDocument\r
         */\r
@@ -30,38 +35,78 @@ public class CurrentRocket {
                return rocketDocument;\r
        }\r
 \r
-       public void notifySimsChanged() {\r
-               if ( handler != null ) {\r
-                       handler.simsChangedMessage();\r
-               }\r
+       private void notifySimsChanged( Context context ) {\r
+               Intent msg = new Intent(MESSAGE_ACTION);\r
+               msg.putExtra(TYPE, SIMS_CHANGED);\r
+\r
+               LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
+       }\r
+\r
+       private void notifySimComplete( Context context ) {\r
+               Intent msg = new Intent(MESSAGE_ACTION);\r
+               msg.putExtra(TYPE, SIM_COMPLETE);\r
+\r
+               LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
        }\r
 \r
-       public void addNewSimulation() {\r
+       private void notifyMotorConfigChanged( Context context ) {\r
+               Intent msg = new Intent(MESSAGE_ACTION);\r
+               msg.putExtra(TYPE, CONFIGS_CHANGED);\r
+\r
+               LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
+       }\r
+\r
+       public synchronized void lockSimulation( Context context, int simulationId ) {\r
+               runningSims.add(simulationId);\r
+               // TODO - someday we might want to know about this:\r
+               // notifySimsChanged( context );\r
+       }\r
+\r
+       public synchronized void unlockSimulation( Context context, int simulationId ) {\r
+               this.isModified = true;\r
+               runningSims.remove(simulationId);\r
+               notifySimComplete(context);\r
+       }\r
+\r
+       public synchronized Set<Integer> lockedSimulations() {\r
+               return new HashSet<Integer>(runningSims);\r
+       }\r
+\r
+       public synchronized void addNewSimulation( Context context ) {\r
+               isModified = true;\r
                Rocket rocket = rocketDocument.getRocket();\r
-               // FIXME - hopefully the change to the Simulation object will be reverted soon.\r
-               Simulation newSim = new Simulation(rocketDocument, rocket);\r
+               Simulation newSim = new Simulation(rocket);\r
                newSim.setName(rocketDocument.getNextSimulationName());\r
                rocketDocument.addSimulation(newSim);\r
-               notifySimsChanged();\r
+               notifySimsChanged(context);\r
        }\r
-       \r
-       public void deleteSimulation( int simulationPos ) {\r
+\r
+       public synchronized void deleteSimulation( Context context, int simulationPos ) {\r
+               isModified = true;\r
                rocketDocument.removeSimulation( simulationPos );\r
-               notifySimsChanged();\r
+               notifySimsChanged(context);\r
        }\r
-       \r
-       public String addNewMotorConfig() {\r
+\r
+       public synchronized String addNewMotorConfig( Context context ) {\r
+               isModified = true;\r
                String configId = rocketDocument.getRocket().newMotorConfigurationID();\r
-               if ( handler != null ) {\r
-                       handler.configsChangedMessage();\r
-               }\r
+               notifyMotorConfigChanged(context);\r
                return configId;\r
        }\r
+       \r
+       public synchronized void deleteMotorConfig( Context context, String config ) {\r
+               rocketDocument.getRocket().removeMotorConfigurationID(config);\r
+               notifyMotorConfigChanged(context);\r
+       }\r
+       \r
        /**\r
         * @param rocketDocument the rocketDocument to set\r
         */\r
        public void setRocketDocument(OpenRocketDocument rocketDocument) {\r
                this.rocketDocument = rocketDocument;\r
+               synchronized ( this ) {\r
+                       isModified = false;\r
+               }\r
        }\r
 \r
        public WarningSet getWarnings() {\r
@@ -80,11 +125,30 @@ public class CurrentRocket {
                this.fileUri = fileUri;\r
        }\r
 \r
-       public void saveOpenRocketDocument() throws IOException {\r
-               OpenRocketSaver saver = new OpenRocketSaver();\r
-               saver.save(new File(fileUri.getPath()),rocketDocument);\r
+       public boolean isModified() {\r
+               return this.isModified;\r
+       }\r
 \r
+       public boolean canSave() {\r
+               return this.isModified && this.runningSims.isEmpty();\r
        }\r
 \r
+       public void saveOpenRocketDocument() throws IOException {\r
+\r
+               // Translate the fileUri if it happens to be a .rkt file.\r
+\r
+               String filename = fileUri.getPath();\r
+\r
+               if ( ! filename.endsWith(".ork") ) {\r
+                       filename = filename.concat(".ork");\r
+               }\r
+\r
+               OpenRocketSaver saver = new OpenRocketSaver();\r
+               StorageOptions options = new StorageOptions();\r
+               options.setCompressionEnabled(true);\r
+               options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);\r
+               saver.save(new File(filename),rocketDocument,options);\r
+               isModified = false;\r
+       }\r
 \r
 }\r