Added way to delete motor configurations through long click in the configurations...
[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         public synchronized void deleteMotorConfig( Context context, String config ) {\r
99                 rocketDocument.getRocket().removeMotorConfigurationID(config);\r
100                 notifyMotorConfigChanged(context);\r
101         }\r
102         \r
103         /**\r
104          * @param rocketDocument the rocketDocument to set\r
105          */\r
106         public void setRocketDocument(OpenRocketDocument rocketDocument) {\r
107                 this.rocketDocument = rocketDocument;\r
108                 synchronized ( this ) {\r
109                         isModified = false;\r
110                 }\r
111         }\r
112 \r
113         public WarningSet getWarnings() {\r
114                 return warnings;\r
115         }\r
116 \r
117         public void setWarnings(WarningSet warnings) {\r
118                 this.warnings = warnings;\r
119         }\r
120 \r
121         public Uri getFileUri() {\r
122                 return fileUri;\r
123         }\r
124 \r
125         public void setFileUri(Uri fileUri) {\r
126                 this.fileUri = fileUri;\r
127         }\r
128 \r
129         public boolean isModified() {\r
130                 return this.isModified;\r
131         }\r
132 \r
133         public boolean canSave() {\r
134                 return this.isModified && this.runningSims.isEmpty();\r
135         }\r
136 \r
137         public void saveOpenRocketDocument() throws IOException {\r
138 \r
139                 // Translate the fileUri if it happens to be a .rkt file.\r
140 \r
141                 String filename = fileUri.getPath();\r
142 \r
143                 if ( ! filename.endsWith(".ork") ) {\r
144                         filename = filename.concat(".ork");\r
145                 }\r
146 \r
147                 OpenRocketSaver saver = new OpenRocketSaver();\r
148                 StorageOptions options = new StorageOptions();\r
149                 options.setCompressionEnabled(true);\r
150                 options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);\r
151                 saver.save(new File(filename),rocketDocument,options);\r
152                 isModified = false;\r
153         }\r
154 \r
155 }\r