1 package net.sf.openrocket.startup;
3 import java.awt.GraphicsEnvironment;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
8 import javax.swing.SwingUtilities;
9 import javax.swing.Timer;
10 import javax.swing.ToolTipManager;
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;
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.
29 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
31 public class Startup2 {
32 private static final LogHelper log = Application.getLogger();
35 private static final String THRUSTCURVE_DIRECTORY = "datafiles/thrustcurves/";
38 * Run when starting up OpenRocket after Application has been set up.
40 * @param args command line arguments
42 static void runMain(final String[] args) throws Exception {
44 log.info("Starting up OpenRocket version " + BuildProperties.getVersion());
46 // Check that we're not running headless
47 log.info("Checking for graphics head");
50 // Check that we're running a good version of a JRE
51 log.info("Checking JRE compatibility");
52 VersionHelper.checkVersion();
53 VersionHelper.checkOpenJDK();
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() {
64 log.info("Startup complete");
69 * Run in the EDT when starting up OpenRocket.
71 * @param args command line arguments
73 private static void runInEDT(String[] args) {
75 // Initialize the splash screen with version info
76 log.info("Initializing the splash screen");
79 // Must be done after localization is initialized
80 ComponentPresetDatabase componentPresetDao = new ComponentPresetDatabase();
81 ConcurrentComponentPresetDatabaseLoader presetLoader = new ConcurrentComponentPresetDatabaseLoader( componentPresetDao );
84 Application.setComponentPresetDao( componentPresetDao );
86 // Setup the uncaught exception handler
87 log.info("Registering exception handler");
88 SwingExceptionHandler exceptionHandler = new SwingExceptionHandler();
89 Application.setExceptionHandler(exceptionHandler);
90 exceptionHandler.registerExceptionHandler();
92 // Start update info fetching
93 final UpdateInfoRetriever updateInfo;
94 if ( Application.getPreferences().getCheckUpdates()) {
95 log.info("Starting update check");
96 updateInfo = new UpdateInfoRetriever();
99 log.info("Update check disabled");
103 // Set the best available look-and-feel
104 log.info("Setting best LAF");
105 GUIUtil.setBestLAF();
107 // Set tooltip delay time. Tooltips are used in MotorChooserDialog extensively.
108 ToolTipManager.sharedInstance().setDismissDelay(30000);
111 ((SwingPreferences) Application.getPreferences()).loadDefaultUnits();
114 log.info("Loading databases");
118 Databases.fakeMethod();
121 presetLoader.await();
122 } catch ( InterruptedException iex) {
126 // Starting action (load files or open new document)
127 log.info("Opening main application window");
128 if (!handleCommandLine(args)) {
129 BasicFrame.newAction();
132 // Check whether update info has been fetched or whether it needs more time
133 log.info("Checking update status");
134 checkUpdateStatus(updateInfo);
139 * this method is useful for the python bindings.
141 public static void loadMotor() {
142 ConcurrentLoadingThrustCurveMotorSetDatabase motorLoader = new ConcurrentLoadingThrustCurveMotorSetDatabase(THRUSTCURVE_DIRECTORY);
143 motorLoader.startLoading();
144 Application.setMotorSetDatabase(motorLoader);
148 * Check that the JRE is not running headless.
150 private static void checkHead() {
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 " +
157 System.err.println();
164 private static void checkUpdateStatus(final UpdateInfoRetriever updateInfo) {
165 if (updateInfo == null)
169 if (!updateInfo.isRunning())
172 final Timer timer = new Timer(delay, null);
174 ActionListener listener = new ActionListener() {
175 private int count = 5;
178 public void actionPerformed(ActionEvent e) {
179 if (!updateInfo.isRunning()) {
182 String current = BuildProperties.getVersion();
183 String last = Application.getPreferences().getString(Preferences.LAST_UPDATE, "");
185 UpdateInfo info = updateInfo.getUpdateInfo();
186 if (info != null && info.getLatestVersion() != null &&
187 !current.equals(info.getLatestVersion()) &&
188 !last.equals(info.getLatestVersion())) {
190 UpdateInfoDialog infoDialog = new UpdateInfoDialog(info);
191 infoDialog.setVisible(true);
192 if (infoDialog.isReminderSelected()) {
193 Application.getPreferences().putString(Preferences.LAST_UPDATE, "");
195 Application.getPreferences().putString(Preferences.LAST_UPDATE, info.getLatestVersion());
204 timer.addActionListener(listener);
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.
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.
217 private static boolean handleCommandLine(String[] args) {
219 // Check command-line for files
220 boolean opened = false;
221 for (String file : args) {
222 if (BasicFrame.open(new File(file), null)) {