version 1.1.9
[debian/openrocket] / src / net / sf / openrocket / gui / main / SimulationRunDialog.java
index 50b3f2c5b101b21a25ca1f8d9d3202118edddf3f..9ac4a3690f3c86fb9d3bd63d9295026bfed47576 100644 (file)
@@ -8,8 +8,6 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
-import java.io.CharArrayWriter;
-import java.io.PrintWriter;
 import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
@@ -25,6 +23,8 @@ import javax.swing.JProgressBar;
 import net.miginfocom.swing.MigLayout;
 import net.sf.openrocket.document.Simulation;
 import net.sf.openrocket.gui.dialogs.DetailDialog;
+import net.sf.openrocket.l10n.Translator;
+import net.sf.openrocket.logging.LogHelper;
 import net.sf.openrocket.rocketcomponent.Configuration;
 import net.sf.openrocket.rocketcomponent.MotorMount;
 import net.sf.openrocket.rocketcomponent.MotorMount.IgnitionEvent;
@@ -35,6 +35,7 @@ import net.sf.openrocket.simulation.exception.SimulationException;
 import net.sf.openrocket.simulation.exception.SimulationLaunchException;
 import net.sf.openrocket.simulation.listeners.AbstractSimulationListener;
 import net.sf.openrocket.simulation.listeners.SimulationListener;
+import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.unit.Unit;
 import net.sf.openrocket.unit.UnitGroup;
 import net.sf.openrocket.util.GUIUtil;
@@ -43,6 +44,10 @@ import net.sf.openrocket.util.Prefs;
 
 
 public class SimulationRunDialog extends JDialog {
+       private static final LogHelper log = Application.getLogger();
+       private static final Translator trans = Application.getTranslator();
+       
+
        /** Update the dialog status every this many ms */
        private static final long UPDATE_MS = 200;
        
@@ -65,7 +70,13 @@ public class SimulationRunDialog extends JDialog {
        private final JProgressBar progressBar;
        
 
+       /*
+        * NOTE:  Care must be used when accessing the simulation parameters, since they
+        * are being run in another thread.  Mutexes are used to avoid concurrent usage, which
+        * will result in an exception being thrown!
+        */
        private final Simulation[] simulations;
+       private final String[] simulationNames;
        private final SimulationWorker[] simulationWorkers;
        private final SimulationStatus[] simulationStatuses;
        private final double[] simulationMaxAltitude;
@@ -73,7 +84,8 @@ public class SimulationRunDialog extends JDialog {
        private final boolean[] simulationDone;
        
        public SimulationRunDialog(Window window, Simulation... simulations) {
-               super(window, "Running simulations...", Dialog.ModalityType.DOCUMENT_MODAL);
+               //// Running simulations...
+               super(window, trans.get("SimuRunDlg.title.RunSim"), Dialog.ModalityType.DOCUMENT_MODAL);
                
                if (simulations.length == 0) {
                        throw new IllegalArgumentException("Called with no simulations to run");
@@ -81,8 +93,15 @@ public class SimulationRunDialog extends JDialog {
                
                this.simulations = simulations;
                
+
+               // Randomize the simulation random seeds
+               for (Simulation sim : simulations) {
+                       sim.getOptions().randomizeSeed();
+               }
+               
                // Initialize the simulations
                int n = simulations.length;
+               simulationNames = new String[n];
                simulationWorkers = new SimulationWorker[n];
                simulationStatuses = new SimulationStatus[n];
                simulationMaxAltitude = new double[n];
@@ -90,6 +109,7 @@ public class SimulationRunDialog extends JDialog {
                simulationDone = new boolean[n];
                
                for (int i = 0; i < n; i++) {
+                       simulationNames[i] = simulations[i].getName();
                        simulationWorkers[i] = new InteractiveSimulationWorker(simulations[i], i);
                        executor.execute(simulationWorkers[i]);
                }
@@ -97,18 +117,21 @@ public class SimulationRunDialog extends JDialog {
                // Build the dialog
                JPanel panel = new JPanel(new MigLayout("fill", "[][grow]"));
                
-               simLabel = new JLabel("Running ...");
+               //// Running ...
+               simLabel = new JLabel(trans.get("SimuRunDlg.lbl.Running"));
                panel.add(simLabel, "spanx, wrap para");
-               
-               panel.add(new JLabel("Simulation time: "), "gapright para");
+               //// Simulation time: 
+               panel.add(new JLabel(trans.get("SimuRunDlg.lbl.Simutime") + " "), "gapright para");
                timeLabel = new JLabel("");
-               panel.add(timeLabel, "growx, wrap rel");
+               panel.add(timeLabel, "growx, wmin 200lp, wrap rel");
                
-               panel.add(new JLabel("Altitude: "));
+               //// Altitude:
+               panel.add(new JLabel(trans.get("SimuRunDlg.lbl.Altitude") + " "));
                altLabel = new JLabel("");
                panel.add(altLabel, "growx, wrap rel");
                
-               panel.add(new JLabel("Velocity: "));
+               //// Velocity:
+               panel.add(new JLabel(trans.get("SimuRunDlg.lbl.Velocity") + " "));
                velLabel = new JLabel("");
                panel.add(velLabel, "growx, wrap para");
                
@@ -117,7 +140,7 @@ public class SimulationRunDialog extends JDialog {
                
 
                // Add cancel button
-               JButton cancel = new JButton("Cancel");
+               JButton cancel = new JButton(trans.get("dlg.but.cancel"));
                cancel.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
@@ -174,7 +197,6 @@ public class SimulationRunDialog extends JDialog {
 
 
        private void updateProgress() {
-               System.out.println("updateProgress() called");
                int index;
                for (index = 0; index < simulations.length; index++) {
                        if (!simulationDone[index])
@@ -183,7 +205,7 @@ public class SimulationRunDialog extends JDialog {
                
                if (index >= simulations.length) {
                        // Everything is done, close the dialog
-                       System.out.println("Everything done.");
+                       log.debug("Everything done.");
                        this.dispose();
                        return;
                }
@@ -195,15 +217,15 @@ public class SimulationRunDialog extends JDialog {
                }
                progress /= simulationWorkers.length;
                progressBar.setValue(progress);
-               System.out.println("Progressbar value " + progress);
+               log.debug("Progressbar value " + progress);
                
                // Update the simulation fields
-               simLabel.setText("Running " + simulations[index].getName());
+               simLabel.setText("Running " + simulationNames[index]);
                if (simulationStatuses[index] == null) {
+                       log.debug("No simulation status data available, setting empty labels");
                        timeLabel.setText("");
                        altLabel.setText("");
                        velLabel.setText("");
-                       System.out.println("Empty labels, how sad.");
                        return;
                }
                
@@ -217,7 +239,6 @@ public class SimulationRunDialog extends JDialog {
                u = UnitGroup.UNITS_VELOCITY.getDefaultUnit();
                velLabel.setText(u.toStringUnit(simulationStatuses[index].getRocketVelocity().z) + " (max. " +
                                u.toStringUnit(simulationMaxVelocity[index]) + ")");
-               System.out.println("Set interesting labels.");
        }
        
        
@@ -255,7 +276,7 @@ public class SimulationRunDialog extends JDialog {
                        double launchBurn = 0;
                        double otherBurn = 0;
                        Configuration config = simulation.getConfiguration();
-                       String id = simulation.getConditions().getMotorConfigurationID();
+                       String id = simulation.getOptions().getMotorConfigurationID();
                        Iterator<MotorMount> iterator = config.motorIterator();
                        while (iterator.hasNext()) {
                                MotorMount m = iterator.next();
@@ -299,7 +320,7 @@ public class SimulationRunDialog extends JDialog {
                        
                        // 1. time = 0 ... burnoutTimeEstimate
                        if (simulationStage == -2 && status.getSimulationTime() < burnoutTimeEstimate) {
-                               System.out.println("Method 1:  t=" + status.getSimulationTime() + "  est=" + burnoutTimeEstimate);
+                               log.debug("Method 1:  t=" + status.getSimulationTime() + "  est=" + burnoutTimeEstimate);
                                setSimulationProgress(MathUtil.map(status.getSimulationTime(), 0, burnoutTimeEstimate,
                                                0.0, BURNOUT_PROGRESS));
                                updateProgress();
@@ -309,13 +330,12 @@ public class SimulationRunDialog extends JDialog {
                        if (simulationStage == -2) {
                                simulationStage++;
                                burnoutVelocity = MathUtil.max(status.getRocketVelocity().z, 0.1);
-                               System.out.println("CHANGING to Method 2, vel=" + burnoutVelocity);
+                               log.debug("CHANGING to Method 2, vel=" + burnoutVelocity);
                        }
                        
                        // 2. z-velocity from burnout velocity to zero
                        if (simulationStage == -1 && status.getRocketVelocity().z >= 0) {
-                               System.out.println("Method 2:  vel=" + status.getRocketVelocity().z + " burnout=" +
-                                               burnoutVelocity);
+                               log.debug("Method 2:  vel=" + status.getRocketVelocity().z + " burnout=" + burnoutVelocity);
                                setSimulationProgress(MathUtil.map(status.getRocketVelocity().z, burnoutVelocity, 0,
                                                BURNOUT_PROGRESS, APOGEE_PROGRESS));
                                updateProgress();
@@ -325,11 +345,12 @@ public class SimulationRunDialog extends JDialog {
                        if (simulationStage == -1 && status.getRocketVelocity().z < 0) {
                                simulationStage++;
                                apogeeAltitude = MathUtil.max(status.getRocketPosition().z, 1);
+                               log.debug("CHANGING to Method 3, apogee=" + apogeeAltitude);
                        }
                        
                        // 3. z-position from apogee to zero
                        // TODO: MEDIUM: several stages
-                       System.out.println("Method 3:  alt=" + status.getRocketPosition().z + "  apogee=" + apogeeAltitude);
+                       log.debug("Method 3:  alt=" + status.getRocketPosition().z + "  apogee=" + apogeeAltitude);
                        setSimulationProgress(MathUtil.map(status.getRocketPosition().z,
                                        apogeeAltitude, 0, APOGEE_PROGRESS, 1.0));
                        updateProgress();
@@ -341,7 +362,7 @@ public class SimulationRunDialog extends JDialog {
                @Override
                protected void simulationDone() {
                        simulationDone[index] = true;
-                       System.out.println("DONE, setting progress");
+                       log.debug("Simulation done");
                        setSimulationProgress(1.0);
                        updateProgress();
                }
@@ -359,18 +380,13 @@ public class SimulationRunDialog extends JDialog {
                                return; // Ignore cancellations
                        }
                        
-                       // Retrieve the stack trace in a textual form
-                       CharArrayWriter arrayWriter = new CharArrayWriter();
-                       arrayWriter.append(t.toString() + "\n" + "\n");
-                       t.printStackTrace(new PrintWriter(arrayWriter));
-                       String stackTrace = arrayWriter.toString();
-                       
                        // Analyze the exception type
                        if (t instanceof SimulationLaunchException) {
                                
                                DetailDialog.showDetailedMessageDialog(SimulationRunDialog.this,
                                                new Object[] {
-                                                               "Unable to simulate:",
+                                                               //// Unable to simulate:
+                                                               trans.get("SimuRunDlg.msg.Unabletosim"),
                                                                t.getMessage()
                                                },
                                                null, simulation.getName(), JOptionPane.ERROR_MESSAGE);
@@ -379,56 +395,25 @@ public class SimulationRunDialog extends JDialog {
                                
                                DetailDialog.showDetailedMessageDialog(SimulationRunDialog.this,
                                                new Object[] {
-                                                               "A error occurred during the simulation:",
+                                                               //// A error occurred during the simulation:
+                                                               trans.get("SimuRunDlg.msg.errorOccurred"),
                                                                t.getMessage()
                                                },
-                                               stackTrace, simulation.getName(), JOptionPane.ERROR_MESSAGE);
-                               
-                       } else if (t instanceof Exception) {
-                               
-                               // TODO: MEDIUM: Check the exception handling here...
-                               t.printStackTrace();
-                               DetailDialog.showDetailedMessageDialog(SimulationRunDialog.this,
-                                               new Object[] {
-                                                               "An exception occurred during the simulation:",
-                                                               t.getMessage(),
-                                                               simulation.getSimulationListeners().isEmpty() ?
-                                                                               "Please report this as a bug along with the details below." : ""
-                                               },
-                                               stackTrace, simulation.getName(), JOptionPane.ERROR_MESSAGE);
-                               
-                       } else if (t instanceof AssertionError) {
-                               
-                               t.printStackTrace();
-                               DetailDialog.showDetailedMessageDialog(SimulationRunDialog.this,
-                                               new Object[] {
-                                                               "A computation error occurred during the simulation.",
-                                                               "Please report this as a bug along with the details below."
-                                               },
-                                               stackTrace, simulation.getName(), JOptionPane.ERROR_MESSAGE);
+                                               null, simulation.getName(), JOptionPane.ERROR_MESSAGE);
                                
                        } else {
                                
-                               // Probably an Error
-                               DetailDialog.showDetailedMessageDialog(SimulationRunDialog.this,
-                                               new Object[] {
-                                                               "An unknown error was encountered during the simulation.",
-                                                               "The program may be unstable, you should save all your designs " +
-                                                                               "and restart OpenRocket now!"
-                                               },
-                                               stackTrace, simulation.getName(), JOptionPane.ERROR_MESSAGE);
+                               ExceptionHandler.handleErrorCondition("An exception occurred during the simulation", t);
                                
                        }
                        simulationDone();
                }
                
                
-
                private void setSimulationProgress(double p) {
-                       progress = Math.max(progress, (int) (100 * p + 0.5));
-                       progress = MathUtil.clamp(progress, 0, 100);
-                       System.out.println("Setting progress to " + progress + " (real " +
-                                       ((int) (100 * p + 0.5)) + ")");
+                       int exact = Math.max(progress, (int) (100 * p + 0.5));
+                       progress = MathUtil.clamp(exact, 0, 100);
+                       log.debug("Setting progress to " + progress + " (real " + exact + ")");
                        super.setProgress(progress);
                }
                
@@ -448,7 +433,7 @@ public class SimulationRunDialog extends JDialog {
                                case APOGEE:
                                        simulationStage = 0;
                                        apogeeAltitude = status.getRocketPosition().z;
-                                       System.out.println("APOGEE, setting progress");
+                                       log.debug("APOGEE, setting progress");
                                        setSimulationProgress(APOGEE_PROGRESS);
                                        publish(status);
                                        break;
@@ -458,7 +443,7 @@ public class SimulationRunDialog extends JDialog {
                                        break;
                                
                                case SIMULATION_END:
-                                       System.out.println("END, setting progress");
+                                       log.debug("END, setting progress");
                                        setSimulationProgress(1.0);
                                        break;
                                }