1 package net.sf.openrocket.android;
\r
4 import java.io.IOException;
\r
5 import java.util.HashSet;
\r
6 import java.util.Set;
\r
8 import net.sf.openrocket.aerodynamics.WarningSet;
\r
9 import net.sf.openrocket.document.OpenRocketDocument;
\r
10 import net.sf.openrocket.document.Simulation;
\r
11 import net.sf.openrocket.document.StorageOptions;
\r
12 import net.sf.openrocket.file.openrocket.OpenRocketSaver;
\r
13 import net.sf.openrocket.rocketcomponent.Rocket;
\r
14 import android.net.Uri;
\r
16 public class CurrentRocket {
\r
18 private Uri fileUri;
\r
20 private OpenRocketDocument rocketDocument;
\r
21 private WarningSet warnings;
\r
23 private RocketChangedEventHandler handler;
\r
25 private boolean isModified = false;
\r
26 private Set<Integer> runningSims = new HashSet<Integer>();
\r
28 public void setHandler( RocketChangedEventHandler handler ) {
\r
29 this.handler = handler;
\r
33 * @return the rocketDocument
\r
35 public OpenRocketDocument getRocketDocument() {
\r
36 return rocketDocument;
\r
39 public void notifySimsChanged() {
\r
40 synchronized ( this ) {
\r
43 if ( handler != null ) {
\r
44 handler.simsChangedMessage();
\r
48 public void notifySimComplete() {
\r
49 synchronized ( this ) {
\r
52 if ( handler != null ) {
\r
53 handler.simCompleteMessage();
\r
57 public synchronized void lockSimulation( int simulationId ) {
\r
58 runningSims.add(simulationId);
\r
61 public synchronized void unlockSimulation( int simulationId ) {
\r
62 runningSims.remove(simulationId);
\r
65 public synchronized Set<Integer> lockedSimulations() {
\r
66 return new HashSet<Integer>(runningSims);
\r
69 public void addNewSimulation() {
\r
70 Rocket rocket = rocketDocument.getRocket();
\r
71 // FIXME - hopefully the change to the Simulation object will be reverted soon.
\r
72 Simulation newSim = new Simulation(rocketDocument, rocket);
\r
73 newSim.setName(rocketDocument.getNextSimulationName());
\r
74 rocketDocument.addSimulation(newSim);
\r
75 notifySimsChanged();
\r
78 public void deleteSimulation( int simulationPos ) {
\r
79 rocketDocument.removeSimulation( simulationPos );
\r
80 notifySimsChanged();
\r
83 public String addNewMotorConfig() {
\r
84 synchronized ( this ) {
\r
87 String configId = rocketDocument.getRocket().newMotorConfigurationID();
\r
88 if ( handler != null ) {
\r
89 handler.configsChangedMessage();
\r
94 * @param rocketDocument the rocketDocument to set
\r
96 public void setRocketDocument(OpenRocketDocument rocketDocument) {
\r
97 this.rocketDocument = rocketDocument;
\r
98 synchronized ( this ) {
\r
103 public WarningSet getWarnings() {
\r
107 public void setWarnings(WarningSet warnings) {
\r
108 this.warnings = warnings;
\r
111 public Uri getFileUri() {
\r
115 public void setFileUri(Uri fileUri) {
\r
116 this.fileUri = fileUri;
\r
119 public boolean isModified() {
\r
120 return this.isModified;
\r
123 public boolean canSave() {
\r
124 return this.isModified && this.runningSims.isEmpty();
\r
127 public void saveOpenRocketDocument() throws IOException {
\r
129 // Translate the fileUri if it happens to be a .rkt file.
\r
131 String filename = fileUri.getPath();
\r
133 if ( ! filename.endsWith(".ork") ) {
\r
134 filename = filename.concat(".ork");
\r
137 OpenRocketSaver saver = new OpenRocketSaver();
\r
138 StorageOptions options = new StorageOptions();
\r
139 options.setCompressionEnabled(true);
\r
140 options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);
\r
141 saver.save(new File(filename),rocketDocument,options);
\r
142 isModified = false;
\r