create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / startup / Application.java
1 package net.sf.openrocket.startup;
2
3 import net.sf.openrocket.database.ComponentPresetDao;
4 import net.sf.openrocket.database.MotorDatabase;
5 import net.sf.openrocket.l10n.ClassBasedTranslator;
6 import net.sf.openrocket.l10n.DebugTranslator;
7 import net.sf.openrocket.l10n.ExceptionSuppressingTranslator;
8 import net.sf.openrocket.l10n.Translator;
9 import net.sf.openrocket.logging.LogHelper;
10 import net.sf.openrocket.logging.LogLevel;
11 import net.sf.openrocket.logging.LogLevelBufferLogger;
12 import net.sf.openrocket.logging.PrintStreamLogger;
13
14 /**
15  * A class that provides singleton instances / beans for other classes to utilize.
16  * 
17  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18  */
19 public final class Application {
20         
21         private static LogHelper logger;
22         private static LogLevelBufferLogger logBuffer;
23         
24         private static Translator baseTranslator = new DebugTranslator(null);
25         
26         private static MotorDatabase motorSetDatabase;
27         
28         private static ComponentPresetDao componentPresetDao;
29
30         private static Preferences preferences;
31         
32         private static ExceptionHandler exceptionHandler;
33
34         // Initialize the logger to something sane for testing without executing Startup
35         static {
36                 setLogOutputLevel(LogLevel.DEBUG);
37         }
38         
39         /**
40          * Return whether to use additional safety code checks.
41          */
42         public static boolean useSafetyChecks() {
43                 // Currently default to false unless openrocket.debug.safetycheck is defined
44                 String s = System.getProperty("openrocket.debug.safetycheck");
45                 if (s != null && !(s.equalsIgnoreCase("false") || s.equalsIgnoreCase("off"))) {
46                         return true;
47                 }
48                 return false;
49         }
50         
51         /**
52          * Retrieve the logger to be used in logging.  By default this returns
53          * a logger that outputs to stdout/stderr even if not separately initialized,
54          * useful for development and debugging.
55          */
56         public static LogHelper getLogger() {
57                 return logger;
58         }
59         
60         /**
61          * Set the logger to be used in logging.  Note that calling this will only have effect
62          * on not-yet loaded classes, as the instance is stored in a static variable.
63          */
64         public static void setLogger(LogHelper logger) {
65                 Application.logger = logger;
66         }
67         
68         
69
70         /**
71          * Return the log buffer.
72          * 
73          * @return the logBuffer or null if not initialized
74          */
75         public static LogLevelBufferLogger getLogBuffer() {
76                 return logBuffer;
77         }
78         
79         /**
80          * Set the log buffer logger.  The logger must be separately configured
81          * to receive the logging.
82          */
83         public static void setLogBuffer(LogLevelBufferLogger logBuffer) {
84                 Application.logBuffer = logBuffer;
85         }
86         
87         
88         /**
89          * Set the logging to output the specified log level and upwards to standard output.
90          * 
91          * @param level         the minimum logging level to output.
92          */
93         public static void setLogOutputLevel(LogLevel level) {
94                 logger = new PrintStreamLogger();
95                 for (LogLevel l : LogLevel.values()) {
96                         if (l.atLeast(level)) {
97                                 ((PrintStreamLogger) logger).setOutput(l, System.out);
98                         }
99                 }
100                 
101         }
102         
103         
104         /**
105          * Return the translator to use for obtaining translated strings.
106          * @return      a translator.
107          */
108         public static Translator getTranslator() {
109                 Translator t = baseTranslator;
110                 t = new ClassBasedTranslator(t, 1);
111                 t = new ExceptionSuppressingTranslator(t);
112                 return t;
113         }
114         
115         /**
116          * Set the translator used in obtaining translated strings.
117          * @param translator    the translator to set.
118          */
119         public static void setBaseTranslator(Translator translator) {
120                 Application.baseTranslator = translator;
121         }
122         
123
124         /**
125          * @return the preferences
126          */
127         public static Preferences getPreferences() {
128                 return preferences;
129         }
130
131         /**
132          * @param preferences the preferences to set
133          */
134         public static void setPreferences(Preferences preferences) {
135                 Application.preferences = preferences;
136         }
137
138         /**
139          * @return the exceptionHandler
140          */
141         public static ExceptionHandler getExceptionHandler() {
142                 return exceptionHandler;
143         }
144
145         /**
146          * @param exceptionHandler the exceptionHandler to set
147          */
148         public static void setExceptionHandler(ExceptionHandler exceptionHandler) {
149                 Application.exceptionHandler = exceptionHandler;
150         }
151
152         /**
153          * Return the database of all thrust curves loaded into the system.
154          */
155         public static MotorDatabase getMotorSetDatabase() {
156                 return motorSetDatabase;
157         }
158         
159         /**
160          * Set the database of thrust curves loaded into the system.
161          */
162         public static void setMotorSetDatabase(MotorDatabase motorSetDatabase) {
163                 Application.motorSetDatabase = motorSetDatabase;
164         }
165
166         public static ComponentPresetDao getComponentPresetDao() {
167                 return componentPresetDao;
168                 
169         }
170
171         public static void setComponentPresetDao(ComponentPresetDao componentPresetDao) {
172                 Application.componentPresetDao = componentPresetDao;
173         }
174         
175
176 }