Checkpoint commit with many different changes. Made the Configuration and Simulation...
[debian/openrocket] / android / src / net / sf / openrocket / android / Application.java
1 package net.sf.openrocket.android;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.util.Locale;\r
6 \r
7 import net.sf.openrocket.aerodynamics.WarningSet;\r
8 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
9 import net.sf.openrocket.database.ComponentPresetDatabase;\r
10 import net.sf.openrocket.document.OpenRocketDocument;\r
11 import net.sf.openrocket.document.Simulation;\r
12 import net.sf.openrocket.file.openrocket.OpenRocketSaver;\r
13 import net.sf.openrocket.l10n.DebugTranslator;\r
14 import net.sf.openrocket.l10n.ResourceBundleTranslator;\r
15 import net.sf.openrocket.l10n.Translator;\r
16 import net.sf.openrocket.rocketcomponent.Rocket;\r
17 import android.content.pm.ApplicationInfo;\r
18 import android.net.Uri;\r
19 import android.preference.PreferenceManager;\r
20 \r
21 public class Application extends android.app.Application {\r
22 \r
23         private OpenRocketDocument rocketDocument;\r
24         private Uri fileUri;\r
25 \r
26         private WarningSet warnings;\r
27 \r
28         // Big B boolean so I can synchronize on it.\r
29         private static Boolean initialized = false;\r
30 \r
31         public void initialize() {\r
32                 synchronized (initialized) {\r
33                         if ( initialized == true ) {\r
34                                 return;\r
35                         }\r
36 \r
37                         // Android does not have a default sax parser set.  This needs to be defined first.\r
38                         System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");\r
39 \r
40                         net.sf.openrocket.startup.Application.setLogger( new AndroidLogWrapper.LogHelper() );\r
41 \r
42                         net.sf.openrocket.startup.Application.setPreferences( new PreferencesAdapter() );\r
43 \r
44                         net.sf.openrocket.startup.Application.setComponentPresetDao( new ComponentPresetDatabase() );\r
45 \r
46                         MotorDatabaseAdapter db = new MotorDatabaseAdapter(this);\r
47 \r
48                         net.sf.openrocket.startup.Application.setMotorSetDatabase(db);\r
49 \r
50                         Translator t;\r
51                         t = new ResourceBundleTranslator("l10n.messages");\r
52                         if (Locale.getDefault().getLanguage().equals("xx")) {\r
53                                 t = new DebugTranslator(t);\r
54                         }\r
55 \r
56                         net.sf.openrocket.startup.Application.setBaseTranslator(t);\r
57 \r
58                         initialized = true;\r
59                 }\r
60         }\r
61 \r
62         public Application() {\r
63         }\r
64 \r
65         /* (non-Javadoc)\r
66          * @see android.app.Application#onCreate()\r
67          */\r
68         @Override\r
69         public void onCreate() {\r
70                 super.onCreate();\r
71                 initialize();\r
72                 boolean isDebuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));\r
73                 AndroidLogWrapper.setLogEnabled(isDebuggable);\r
74                 PreferencesActivity.initializePreferences(this, PreferenceManager.getDefaultSharedPreferences(this));\r
75         }\r
76 \r
77         private RocketChangedEventHandler handler;\r
78         \r
79         public void setHandler( RocketChangedEventHandler handler ) {\r
80                 this.handler = handler;\r
81         }\r
82         \r
83         /**\r
84          * @return the rocketDocument\r
85          */\r
86         public OpenRocketDocument getRocketDocument() {\r
87                 return rocketDocument;\r
88         }\r
89 \r
90         public void addNewSimulation() {\r
91                 Rocket rocket = rocketDocument.getRocket();\r
92                 Simulation newSim = new Simulation(rocket);\r
93                 newSim.setName(rocketDocument.getNextSimulationName());\r
94                 rocketDocument.addSimulation(newSim);\r
95                 if ( handler != null ) {\r
96                         handler.simsChangedMessage();\r
97                 }\r
98         }\r
99         \r
100         public void deleteSimulation( int simulationPos ) {\r
101                 rocketDocument.removeSimulation( simulationPos );\r
102                 if ( handler != null ) {\r
103                         handler.simsChangedMessage();\r
104                 }\r
105         }\r
106         \r
107         public String addNewMotorConfig() {\r
108                 String configId = rocketDocument.getRocket().newMotorConfigurationID();\r
109                 if ( handler != null ) {\r
110                         handler.configsChangedMessage();\r
111                 }\r
112                 return configId;\r
113         }\r
114         /**\r
115          * @param rocketDocument the rocketDocument to set\r
116          */\r
117         public void setRocketDocument(OpenRocketDocument rocketDocument) {\r
118                 this.rocketDocument = rocketDocument;\r
119         }\r
120 \r
121         public WarningSet getWarnings() {\r
122                 return warnings;\r
123         }\r
124 \r
125         public void setWarnings(WarningSet warnings) {\r
126                 this.warnings = warnings;\r
127         }\r
128 \r
129         public Uri getFileUri() {\r
130                 return fileUri;\r
131         }\r
132 \r
133         public void setFileUri(Uri fileUri) {\r
134                 this.fileUri = fileUri;\r
135         }\r
136 \r
137         public void saveOpenRocketDocument() throws IOException {\r
138                 OpenRocketSaver saver = new OpenRocketSaver();\r
139                 saver.save(new File(fileUri.getPath()),rocketDocument);\r
140 \r
141         }\r
142 }\r