Put the loadMotor() method back since it is useful to python bindings.
[debian/openrocket] / core / src / net / sf / openrocket / startup / Startup2.java
1 package net.sf.openrocket.startup;
2
3 import java.awt.GraphicsEnvironment;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.io.File;
7
8 import javax.swing.SwingUtilities;
9 import javax.swing.Timer;
10 import javax.swing.ToolTipManager;
11
12 import net.sf.openrocket.communication.UpdateInfo;
13 import net.sf.openrocket.communication.UpdateInfoRetriever;
14 import net.sf.openrocket.database.ComponentPresetDatabase;
15 import net.sf.openrocket.database.Databases;
16 import net.sf.openrocket.gui.dialogs.UpdateInfoDialog;
17 import net.sf.openrocket.gui.main.BasicFrame;
18 import net.sf.openrocket.gui.main.Splash;
19 import net.sf.openrocket.gui.main.SwingExceptionHandler;
20 import net.sf.openrocket.gui.util.GUIUtil;
21 import net.sf.openrocket.gui.util.SwingPreferences;
22 import net.sf.openrocket.logging.LogHelper;
23 import net.sf.openrocket.util.BuildProperties;
24
25 /**
26  * The second class in the OpenRocket startup sequence.  This class can assume the
27  * Application class to be properly set up, and can use any classes safely.
28  * 
29  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
30  */
31 public class Startup2 {
32         private static final LogHelper log = Application.getLogger();
33         
34
35         private static final String THRUSTCURVE_DIRECTORY = "datafiles/thrustcurves/";
36         
37         /**
38          * Run when starting up OpenRocket after Application has been set up.
39          * 
40          * @param args  command line arguments
41          */
42         static void runMain(final String[] args) throws Exception {
43                 
44                 log.info("Starting up OpenRocket version " + BuildProperties.getVersion());
45                 
46                 // Check that we're not running headless
47                 log.info("Checking for graphics head");
48                 checkHead();
49                 
50                 // Check that we're running a good version of a JRE
51                 log.info("Checking JRE compatibility");
52                 VersionHelper.checkVersion();
53                 VersionHelper.checkOpenJDK();
54                 
55                 // Run the actual startup method in the EDT since it can use progress dialogs etc.
56                 log.info("Moving startup to EDT");
57                 SwingUtilities.invokeAndWait(new Runnable() {
58                         @Override
59                         public void run() {
60                                 runInEDT(args);
61                         }
62                 });
63                 
64                 log.info("Startup complete");
65         }
66         
67         
68         /**
69          * Run in the EDT when starting up OpenRocket.
70          * 
71          * @param args  command line arguments
72          */
73         private static void runInEDT(String[] args) {
74                 
75                 // Initialize the splash screen with version info
76                 log.info("Initializing the splash screen");
77                 Splash.init();
78                 
79                 // Must be done after localization is initialized
80                 ComponentPresetDatabase componentPresetDao = new ComponentPresetDatabase();
81                 ConcurrentComponentPresetDatabaseLoader presetLoader = new ConcurrentComponentPresetDatabaseLoader( componentPresetDao );
82                 presetLoader.load();
83                 
84                 Application.setComponentPresetDao( componentPresetDao );
85                 
86                 // Setup the uncaught exception handler
87                 log.info("Registering exception handler");
88                 SwingExceptionHandler exceptionHandler = new SwingExceptionHandler();
89                 Application.setExceptionHandler(exceptionHandler);
90                 exceptionHandler.registerExceptionHandler();
91                 
92                 // Start update info fetching
93                 final UpdateInfoRetriever updateInfo;
94                 if ( Application.getPreferences().getCheckUpdates()) {
95                         log.info("Starting update check");
96                         updateInfo = new UpdateInfoRetriever();
97                         updateInfo.start();
98                 } else {
99                         log.info("Update check disabled");
100                         updateInfo = null;
101                 }
102                 
103                 // Set the best available look-and-feel
104                 log.info("Setting best LAF");
105                 GUIUtil.setBestLAF();
106                 
107                 // Set tooltip delay time.  Tooltips are used in MotorChooserDialog extensively.
108                 ToolTipManager.sharedInstance().setDismissDelay(30000);
109                 
110                 // Load defaults
111                 ((SwingPreferences) Application.getPreferences()).loadDefaultUnits();
112                 
113                 // Load motors etc.
114                 log.info("Loading databases");
115                 
116                 loadMotor();
117                 
118                 Databases.fakeMethod();
119                 
120                 try {
121                         presetLoader.await();
122                 } catch ( InterruptedException iex) {
123                         
124                 }
125
126                 // Starting action (load files or open new document)
127                 log.info("Opening main application window");
128                 if (!handleCommandLine(args)) {
129                         BasicFrame.newAction();
130                 }
131                 
132                 // Check whether update info has been fetched or whether it needs more time
133                 log.info("Checking update status");
134                 checkUpdateStatus(updateInfo);
135                 
136         }
137         
138         /**
139          * this method is useful for the python bindings.
140          */
141         public static void loadMotor() {
142                 ConcurrentLoadingThrustCurveMotorSetDatabase motorLoader = new ConcurrentLoadingThrustCurveMotorSetDatabase(THRUSTCURVE_DIRECTORY);
143                 motorLoader.startLoading();
144                 Application.setMotorSetDatabase(motorLoader);
145         }
146         
147         /**
148          * Check that the JRE is not running headless.
149          */
150         private static void checkHead() {
151                 
152                 if (GraphicsEnvironment.isHeadless()) {
153                         log.error("Application is headless.");
154                         System.err.println();
155                         System.err.println("OpenRocket cannot currently be run without the graphical " +
156                                         "user interface.");
157                         System.err.println();
158                         System.exit(1);
159                 }
160                 
161         }
162         
163         
164         private static void checkUpdateStatus(final UpdateInfoRetriever updateInfo) {
165                 if (updateInfo == null)
166                         return;
167                 
168                 int delay = 1000;
169                 if (!updateInfo.isRunning())
170                         delay = 100;
171                 
172                 final Timer timer = new Timer(delay, null);
173                 
174                 ActionListener listener = new ActionListener() {
175                         private int count = 5;
176                         
177                         @Override
178                         public void actionPerformed(ActionEvent e) {
179                                 if (!updateInfo.isRunning()) {
180                                         timer.stop();
181                                         
182                                         String current = BuildProperties.getVersion();
183                                         String last = Application.getPreferences().getString(Preferences.LAST_UPDATE, "");
184                                         
185                                         UpdateInfo info = updateInfo.getUpdateInfo();
186                                         if (info != null && info.getLatestVersion() != null &&
187                                                         !current.equals(info.getLatestVersion()) &&
188                                                         !last.equals(info.getLatestVersion())) {
189                                                 
190                                                 UpdateInfoDialog infoDialog = new UpdateInfoDialog(info);
191                                                 infoDialog.setVisible(true);
192                                                 if (infoDialog.isReminderSelected()) {
193                                                         Application.getPreferences().putString(Preferences.LAST_UPDATE, "");
194                                                 } else {
195                                                         Application.getPreferences().putString(Preferences.LAST_UPDATE, info.getLatestVersion());
196                                                 }
197                                         }
198                                 }
199                                 count--;
200                                 if (count <= 0)
201                                         timer.stop();
202                         }
203                 };
204                 timer.addActionListener(listener);
205                 timer.start();
206         }
207         
208         /**
209          * Handles arguments passed from the command line.  This may be used either
210          * when starting the first instance of OpenRocket or later when OpenRocket is
211          * executed again while running.
212          * 
213          * @param args  the command-line arguments.
214          * @return              whether a new frame was opened or similar user desired action was
215          *                              performed as a result.
216          */
217         private static boolean handleCommandLine(String[] args) {
218                 
219                 // Check command-line for files
220                 boolean opened = false;
221                 for (String file : args) {
222                         if (BasicFrame.open(new File(file), null)) {
223                                 opened = true;
224                         }
225                 }
226                 return opened;
227         }
228         
229 }