17e2929a02b46c36d478195021e411269af00529
[debian/openrocket] / android / src / net / sf / openrocket / android / CurrentRocket.java
1 package net.sf.openrocket.android;\r
2 \r
3 import static net.sf.openrocket.android.events.Events.*;\r
4 \r
5 import java.io.File;\r
6 import java.io.IOException;\r
7 import java.util.HashSet;\r
8 import java.util.Set;\r
9 \r
10 import net.sf.openrocket.aerodynamics.WarningSet;\r
11 import net.sf.openrocket.document.OpenRocketDocument;\r
12 import net.sf.openrocket.document.Simulation;\r
13 import net.sf.openrocket.document.StorageOptions;\r
14 import net.sf.openrocket.file.openrocket.OpenRocketSaver;\r
15 import net.sf.openrocket.rocketcomponent.Rocket;\r
16 import android.content.Context;\r
17 import android.content.Intent;\r
18 import android.net.Uri;\r
19 import android.support.v4.content.LocalBroadcastManager;\r
20 \r
21 public class CurrentRocket {\r
22 \r
23         private Uri fileUri;\r
24 \r
25         private OpenRocketDocument rocketDocument;\r
26         private WarningSet warnings;\r
27 \r
28         private boolean isModified = false;\r
29         private Set<Integer> runningSims = new HashSet<Integer>();\r
30 \r
31         /**\r
32          * @return the rocketDocument\r
33          */\r
34         public OpenRocketDocument getRocketDocument() {\r
35                 return rocketDocument;\r
36         }\r
37 \r
38         private void notifySimsChanged( Context context ) {\r
39                 Intent msg = new Intent(MESSAGE_ACTION);\r
40                 msg.putExtra(TYPE, SIMS_CHANGED);\r
41 \r
42                 LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
43         }\r
44 \r
45         private void notifySimComplete( Context context ) {\r
46                 Intent msg = new Intent(MESSAGE_ACTION);\r
47                 msg.putExtra(TYPE, SIM_COMPLETE);\r
48 \r
49                 LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
50         }\r
51 \r
52         private void notifyMotorConfigChanged( Context context ) {\r
53                 Intent msg = new Intent(MESSAGE_ACTION);\r
54                 msg.putExtra(TYPE, CONFIGS_CHANGED);\r
55 \r
56                 LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
57         }\r
58 \r
59         public synchronized void lockSimulation( Context context, int simulationId ) {\r
60                 runningSims.add(simulationId);\r
61                 // TODO - someday we might want to know about this:\r
62                 // notifySimsChanged( context );\r
63         }\r
64 \r
65         public synchronized void unlockSimulation( Context context, int simulationId ) {\r
66                 this.isModified = true;\r
67                 runningSims.remove(simulationId);\r
68                 notifySimComplete(context);\r
69         }\r
70 \r
71         public synchronized Set<Integer> lockedSimulations() {\r
72                 return new HashSet<Integer>(runningSims);\r
73         }\r
74 \r
75         public synchronized void addNewSimulation( Context context ) {\r
76                 isModified = true;\r
77                 Rocket rocket = rocketDocument.getRocket();\r
78                 // FIXME - hopefully the change to the Simulation object will be reverted soon.\r
79                 Simulation newSim = new Simulation(rocketDocument, rocket);\r
80                 newSim.setName(rocketDocument.getNextSimulationName());\r
81                 rocketDocument.addSimulation(newSim);\r
82                 notifySimsChanged(context);\r
83         }\r
84 \r
85         public synchronized void deleteSimulation( Context context, int simulationPos ) {\r
86                 isModified = true;\r
87                 rocketDocument.removeSimulation( simulationPos );\r
88                 notifySimsChanged(context);\r
89         }\r
90 \r
91         public synchronized String addNewMotorConfig( Context context ) {\r
92                 isModified = true;\r
93                 String configId = rocketDocument.getRocket().newMotorConfigurationID();\r
94                 notifyMotorConfigChanged(context);\r
95                 return configId;\r
96         }\r
97         \r
98         /**\r
99          * @param rocketDocument the rocketDocument to set\r
100          */\r
101         public void setRocketDocument(OpenRocketDocument rocketDocument) {\r
102                 this.rocketDocument = rocketDocument;\r
103                 synchronized ( this ) {\r
104                         isModified = false;\r
105                 }\r
106         }\r
107 \r
108         public WarningSet getWarnings() {\r
109                 return warnings;\r
110         }\r
111 \r
112         public void setWarnings(WarningSet warnings) {\r
113                 this.warnings = warnings;\r
114         }\r
115 \r
116         public Uri getFileUri() {\r
117                 return fileUri;\r
118         }\r
119 \r
120         public void setFileUri(Uri fileUri) {\r
121                 this.fileUri = fileUri;\r
122         }\r
123 \r
124         public boolean isModified() {\r
125                 return this.isModified;\r
126         }\r
127 \r
128         public boolean canSave() {\r
129                 return this.isModified && this.runningSims.isEmpty();\r
130         }\r
131 \r
132         public void saveOpenRocketDocument() throws IOException {\r
133 \r
134                 // Translate the fileUri if it happens to be a .rkt file.\r
135 \r
136                 String filename = fileUri.getPath();\r
137 \r
138                 if ( ! filename.endsWith(".ork") ) {\r
139                         filename = filename.concat(".ork");\r
140                 }\r
141 \r
142                 OpenRocketSaver saver = new OpenRocketSaver();\r
143                 StorageOptions options = new StorageOptions();\r
144                 options.setCompressionEnabled(true);\r
145                 options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);\r
146                 saver.save(new File(filename),rocketDocument,options);\r
147                 isModified = false;\r
148         }\r
149 \r
150 }\r