Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / startup / Startup2.java
index 11ceb19049f523e7a7741d09c61c664309d205f1..6b7b49cdfc962aa351b6321b77f44831a8006788 100644 (file)
@@ -4,31 +4,24 @@ import java.awt.GraphicsEnvironment;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.swing.SwingUtilities;
 import javax.swing.Timer;
 import javax.swing.ToolTipManager;
 
+import net.sf.openrocket.arch.SystemInfo;
+import net.sf.openrocket.arch.SystemInfo.Platform;
 import net.sf.openrocket.communication.UpdateInfo;
 import net.sf.openrocket.communication.UpdateInfoRetriever;
+import net.sf.openrocket.database.ComponentPresetDatabase;
 import net.sf.openrocket.database.Databases;
-import net.sf.openrocket.database.ThrustCurveMotorSet;
-import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
-import net.sf.openrocket.file.iterator.DirectoryIterator;
-import net.sf.openrocket.file.iterator.FileIterator;
-import net.sf.openrocket.file.motor.MotorLoaderHelper;
 import net.sf.openrocket.gui.dialogs.UpdateInfoDialog;
 import net.sf.openrocket.gui.main.BasicFrame;
 import net.sf.openrocket.gui.main.Splash;
 import net.sf.openrocket.gui.main.SwingExceptionHandler;
 import net.sf.openrocket.gui.util.GUIUtil;
-import net.sf.openrocket.gui.util.SimpleFileFilter;
 import net.sf.openrocket.gui.util.SwingPreferences;
 import net.sf.openrocket.logging.LogHelper;
-import net.sf.openrocket.motor.Motor;
-import net.sf.openrocket.motor.ThrustCurveMotor;
 import net.sf.openrocket.util.BuildProperties;
 
 /**
@@ -43,11 +36,6 @@ public class Startup2 {
 
        private static final String THRUSTCURVE_DIRECTORY = "datafiles/thrustcurves/";
        
-       /** Block motor loading for this many milliseconds */
-       private static AtomicInteger blockLoading = new AtomicInteger(Integer.MAX_VALUE);
-       
-       
-
        /**
         * Run when starting up OpenRocket after Application has been set up.
         * 
@@ -66,6 +54,11 @@ public class Startup2 {
                VersionHelper.checkVersion();
                VersionHelper.checkOpenJDK();
                
+               // If running on a MAC set up OSX UI Elements.
+               if ( SystemInfo.getPlatform() == Platform.MAC_OS ){
+                       OSXStartup.setupOSX();
+               }
+               
                // Run the actual startup method in the EDT since it can use progress dialogs etc.
                log.info("Moving startup to EDT");
                SwingUtilities.invokeAndWait(new Runnable() {
@@ -90,6 +83,25 @@ public class Startup2 {
                log.info("Initializing the splash screen");
                Splash.init();
                
+               // Must be done after localization is initialized
+               ComponentPresetDatabase componentPresetDao = new ComponentPresetDatabase(true) {
+
+                       @Override
+                       protected void load() {
+                               ConcurrentComponentPresetDatabaseLoader presetLoader = new ConcurrentComponentPresetDatabaseLoader( this );
+                               presetLoader.load();
+                               try {
+                                       presetLoader.await();
+                               } catch ( InterruptedException iex) {
+                                       
+                               }
+                       }
+                       
+               };
+               Application.setComponentPresetDao( componentPresetDao );
+
+               componentPresetDao.startLoading();
+               
                // Setup the uncaught exception handler
                log.info("Registering exception handler");
                SwingExceptionHandler exceptionHandler = new SwingExceptionHandler();
@@ -119,9 +131,12 @@ public class Startup2 {
                
                // Load motors etc.
                log.info("Loading databases");
+               
                loadMotor();
+               
                Databases.fakeMethod();
                
+
                // Starting action (load files or open new document)
                log.info("Opening main application window");
                if (!handleCommandLine(args)) {
@@ -132,10 +147,16 @@ public class Startup2 {
                log.info("Checking update status");
                checkUpdateStatus(updateInfo);
                
-               // Block motor loading for 1.5 seconds to allow window painting to be faster
-               blockLoading.set(1500);
        }
        
+       /**
+        * this method is useful for the python bindings.
+        */
+       public static void loadMotor() {
+               ConcurrentLoadingThrustCurveMotorSetDatabase motorLoader = new ConcurrentLoadingThrustCurveMotorSetDatabase(THRUSTCURVE_DIRECTORY);
+               motorLoader.startLoading();
+               Application.setMotorSetDatabase(motorLoader);
+       }
        
        /**
         * Check that the JRE is not running headless.
@@ -154,78 +175,6 @@ public class Startup2 {
        }
        
        
-       private static void loadMotor() {
-               
-               log.info("Starting motor loading from " + THRUSTCURVE_DIRECTORY + " in background thread.");
-               ThrustCurveMotorSetDatabase db = new ThrustCurveMotorSetDatabase(true) {
-                       
-                       @Override
-                       protected void loadMotors() {
-                               
-                               // Block loading until timeout occurs or database is taken into use
-                               log.info("Blocking motor loading while starting up");
-                               while (!inUse && blockLoading.addAndGet(-100) > 0) {
-                                       try {
-                                               Thread.sleep(100);
-                                       } catch (InterruptedException e) {
-                                       }
-                               }
-                               log.info("Blocking ended, inUse=" + inUse + " blockLoading=" + blockLoading.get());
-                               
-                               // Start loading
-                               log.info("Loading motors from " + THRUSTCURVE_DIRECTORY);
-                               long t0 = System.currentTimeMillis();
-                               int fileCount;
-                               int thrustCurveCount;
-                               
-                               // Load the packaged thrust curves
-                               List<Motor> list;
-                               FileIterator iterator = DirectoryIterator.findDirectory(THRUSTCURVE_DIRECTORY,
-                                                               new SimpleFileFilter("", false, "eng", "rse"));
-                               if (iterator == null) {
-                                       throw new IllegalStateException("Thrust curve directory " + THRUSTCURVE_DIRECTORY +
-                                                       "not found, distribution built wrong");
-                               }
-                               list = MotorLoaderHelper.load(iterator);
-                               for (Motor m : list) {
-                                       this.addMotor((ThrustCurveMotor) m);
-                               }
-                               fileCount = iterator.getFileCount();
-                               
-                               thrustCurveCount = list.size();
-                               
-                               // Load the user-defined thrust curves
-                               for (File file : ((SwingPreferences) Application.getPreferences()).getUserThrustCurveFiles()) {
-                                       log.info("Loading motors from " + file);
-                                       list = MotorLoaderHelper.load(file);
-                                       for (Motor m : list) {
-                                               this.addMotor((ThrustCurveMotor) m);
-                                       }
-                                       fileCount++;
-                                       thrustCurveCount += list.size();
-                               }
-                               
-                               long t1 = System.currentTimeMillis();
-                               
-                               // Count statistics
-                               int distinctMotorCount = 0;
-                               int distinctThrustCurveCount = 0;
-                               distinctMotorCount = motorSets.size();
-                               for (ThrustCurveMotorSet set : motorSets) {
-                                       distinctThrustCurveCount += set.getMotorCount();
-                               }
-                               log.info("Motor loading done, took " + (t1 - t0) + " ms to load "
-                                               + fileCount + " files/directories containing "
-                                               + thrustCurveCount + " thrust curves which contained "
-                                               + distinctMotorCount + " distinct motors with "
-                                               + distinctThrustCurveCount + " distinct thrust curves.");
-                       }
-                       
-               };
-               db.startLoading();
-               Application.setMotorSetDatabase(db);
-       }
-       
        private static void checkUpdateStatus(final UpdateInfoRetriever updateInfo) {
                if (updateInfo == null)
                        return;