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.arch.SystemInfo;
13 import net.sf.openrocket.arch.SystemInfo.Platform;
14 import net.sf.openrocket.communication.UpdateInfo;
15 import net.sf.openrocket.communication.UpdateInfoRetriever;
16 import net.sf.openrocket.database.ComponentPresetDatabase;
17 import net.sf.openrocket.database.Databases;
18 import net.sf.openrocket.gui.dialogs.UpdateInfoDialog;
19 import net.sf.openrocket.gui.main.BasicFrame;
20 import net.sf.openrocket.gui.main.Splash;
21 import net.sf.openrocket.gui.main.SwingExceptionHandler;
22 import net.sf.openrocket.gui.util.GUIUtil;
23 import net.sf.openrocket.gui.util.SwingPreferences;
24 import net.sf.openrocket.logging.LogHelper;
25 import net.sf.openrocket.util.BuildProperties;
28 * The second class in the OpenRocket startup sequence. This class can assume the
29 * Application class to be properly set up, and can use any classes safely.
31 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
33 public class Startup2 {
34 private static final LogHelper log = Application.getLogger();
37 private static final String THRUSTCURVE_DIRECTORY = "datafiles/thrustcurves/";
40 * Run when starting up OpenRocket after Application has been set up.
42 * @param args command line arguments
44 static void runMain(final String[] args) throws Exception {
46 log.info("Starting up OpenRocket version " + BuildProperties.getVersion());
48 // Check that we're not running headless
49 log.info("Checking for graphics head");
52 // Check that we're running a good version of a JRE
53 log.info("Checking JRE compatibility");
54 VersionHelper.checkVersion();
55 VersionHelper.checkOpenJDK();
57 // If running on a MAC set up OSX UI Elements.
58 if ( SystemInfo.getPlatform() == Platform.MAC_OS ){
59 OSXStartup.setupOSX();
62 // Run the actual startup method in the EDT since it can use progress dialogs etc.
63 log.info("Moving startup to EDT");
64 SwingUtilities.invokeAndWait(new Runnable() {
71 log.info("Startup complete");
76 * Run in the EDT when starting up OpenRocket.
78 * @param args command line arguments
80 private static void runInEDT(String[] args) {
82 // Initialize the splash screen with version info
83 log.info("Initializing the splash screen");
86 // Must be done after localization is initialized
87 ComponentPresetDatabase componentPresetDao = new ComponentPresetDatabase(true) {
90 protected void load() {
91 ConcurrentComponentPresetDatabaseLoader presetLoader = new ConcurrentComponentPresetDatabaseLoader( this );
95 } catch ( InterruptedException iex) {
101 Application.setComponentPresetDao( componentPresetDao );
103 componentPresetDao.startLoading();
105 // Setup the uncaught exception handler
106 log.info("Registering exception handler");
107 SwingExceptionHandler exceptionHandler = new SwingExceptionHandler();
108 Application.setExceptionHandler(exceptionHandler);
109 exceptionHandler.registerExceptionHandler();
111 // Start update info fetching
112 final UpdateInfoRetriever updateInfo;
113 if ( Application.getPreferences().getCheckUpdates()) {
114 log.info("Starting update check");
115 updateInfo = new UpdateInfoRetriever();
118 log.info("Update check disabled");
122 // Set the best available look-and-feel
123 log.info("Setting best LAF");
124 GUIUtil.setBestLAF();
126 // Set tooltip delay time. Tooltips are used in MotorChooserDialog extensively.
127 ToolTipManager.sharedInstance().setDismissDelay(30000);
130 ((SwingPreferences) Application.getPreferences()).loadDefaultUnits();
133 log.info("Loading databases");
137 Databases.fakeMethod();
140 // Starting action (load files or open new document)
141 log.info("Opening main application window");
142 if (!handleCommandLine(args)) {
143 BasicFrame.newAction();
146 // Check whether update info has been fetched or whether it needs more time
147 log.info("Checking update status");
148 checkUpdateStatus(updateInfo);
153 * this method is useful for the python bindings.
155 public static void loadMotor() {
156 ConcurrentLoadingThrustCurveMotorSetDatabase motorLoader = new ConcurrentLoadingThrustCurveMotorSetDatabase(THRUSTCURVE_DIRECTORY);
157 motorLoader.startLoading();
158 Application.setMotorSetDatabase(motorLoader);
162 * Check that the JRE is not running headless.
164 private static void checkHead() {
166 if (GraphicsEnvironment.isHeadless()) {
167 log.error("Application is headless.");
168 System.err.println();
169 System.err.println("OpenRocket cannot currently be run without the graphical " +
171 System.err.println();
178 private static void checkUpdateStatus(final UpdateInfoRetriever updateInfo) {
179 if (updateInfo == null)
183 if (!updateInfo.isRunning())
186 final Timer timer = new Timer(delay, null);
188 ActionListener listener = new ActionListener() {
189 private int count = 5;
192 public void actionPerformed(ActionEvent e) {
193 if (!updateInfo.isRunning()) {
196 String current = BuildProperties.getVersion();
197 String last = Application.getPreferences().getString(Preferences.LAST_UPDATE, "");
199 UpdateInfo info = updateInfo.getUpdateInfo();
200 if (info != null && info.getLatestVersion() != null &&
201 !current.equals(info.getLatestVersion()) &&
202 !last.equals(info.getLatestVersion())) {
204 UpdateInfoDialog infoDialog = new UpdateInfoDialog(info);
205 infoDialog.setVisible(true);
206 if (infoDialog.isReminderSelected()) {
207 Application.getPreferences().putString(Preferences.LAST_UPDATE, "");
209 Application.getPreferences().putString(Preferences.LAST_UPDATE, info.getLatestVersion());
218 timer.addActionListener(listener);
223 * Handles arguments passed from the command line. This may be used either
224 * when starting the first instance of OpenRocket or later when OpenRocket is
225 * executed again while running.
227 * @param args the command-line arguments.
228 * @return whether a new frame was opened or similar user desired action was
229 * performed as a result.
231 private static boolean handleCommandLine(String[] args) {
233 // Check command-line for files
234 boolean opened = false;
235 for (String file : args) {
236 if (BasicFrame.open(new File(file), null)) {