1 package net.sf.openrocket.gui.scalefigure;
4 import net.miginfocom.swing.MigLayout;
5 import net.sf.openrocket.aerodynamics.AerodynamicCalculator;
6 import net.sf.openrocket.aerodynamics.BarrowmanCalculator;
7 import net.sf.openrocket.aerodynamics.FlightConditions;
8 import net.sf.openrocket.aerodynamics.WarningSet;
9 import net.sf.openrocket.document.OpenRocketDocument;
10 import net.sf.openrocket.document.Simulation;
11 import net.sf.openrocket.gui.adaptors.DoubleModel;
12 import net.sf.openrocket.gui.adaptors.MotorConfigurationModel;
13 import net.sf.openrocket.gui.components.BasicSlider;
14 import net.sf.openrocket.gui.components.StageSelector;
15 import net.sf.openrocket.gui.components.UnitSelector;
16 import net.sf.openrocket.gui.configdialog.ComponentConfigDialog;
17 import net.sf.openrocket.gui.figureelements.CGCaret;
18 import net.sf.openrocket.gui.figureelements.CPCaret;
19 import net.sf.openrocket.gui.figureelements.Caret;
20 import net.sf.openrocket.gui.figureelements.RocketInfo;
21 import net.sf.openrocket.gui.main.SimulationWorker;
22 import net.sf.openrocket.gui.main.componenttree.ComponentTreeModel;
23 import net.sf.openrocket.l10n.Translator;
24 import net.sf.openrocket.masscalc.BasicMassCalculator;
25 import net.sf.openrocket.masscalc.MassCalculator;
26 import net.sf.openrocket.masscalc.MassCalculator.MassCalcType;
27 import net.sf.openrocket.rocketcomponent.Configuration;
28 import net.sf.openrocket.rocketcomponent.Rocket;
29 import net.sf.openrocket.rocketcomponent.RocketComponent;
30 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
31 import net.sf.openrocket.simulation.FlightData;
32 import net.sf.openrocket.simulation.listeners.SimulationListener;
33 import net.sf.openrocket.simulation.listeners.system.ApogeeEndListener;
34 import net.sf.openrocket.simulation.listeners.system.InterruptListener;
35 import net.sf.openrocket.startup.Application;
36 import net.sf.openrocket.unit.UnitGroup;
37 import net.sf.openrocket.util.ChangeSource;
38 import net.sf.openrocket.util.Chars;
39 import net.sf.openrocket.util.Coordinate;
40 import net.sf.openrocket.util.MathUtil;
41 import net.sf.openrocket.util.Prefs;
43 import javax.swing.AbstractAction;
44 import javax.swing.Action;
45 import javax.swing.JComboBox;
46 import javax.swing.JLabel;
47 import javax.swing.JPanel;
48 import javax.swing.JSlider;
49 import javax.swing.JToggleButton;
50 import javax.swing.JViewport;
51 import javax.swing.SwingUtilities;
52 import javax.swing.event.ChangeEvent;
53 import javax.swing.event.ChangeListener;
54 import javax.swing.event.TreeSelectionEvent;
55 import javax.swing.event.TreeSelectionListener;
56 import javax.swing.tree.TreePath;
57 import javax.swing.tree.TreeSelectionModel;
58 import java.awt.Dimension;
60 import java.awt.Point;
61 import java.awt.event.ActionEvent;
62 import java.awt.event.InputEvent;
63 import java.awt.event.MouseEvent;
64 import java.util.ArrayList;
65 import java.util.Collection;
66 import java.util.List;
67 import java.util.concurrent.Executor;
68 import java.util.concurrent.Executors;
69 import java.util.concurrent.ThreadFactory;
72 * A JPanel that contains a RocketFigure and buttons to manipulate the figure.
74 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
76 public class RocketPanel extends JPanel implements TreeSelectionListener, ChangeSource {
78 private static final Translator trans = Application.getTranslator();
79 private final RocketFigure figure;
80 private final ScaleScrollPane scrollPane;
82 private JLabel infoMessage;
84 private TreeSelectionModel selectionModel = null;
87 /* Calculation of CP and CG */
88 private AerodynamicCalculator aerodynamicCalculator;
89 private MassCalculator massCalculator;
92 private final OpenRocketDocument document;
93 private final Configuration configuration;
95 private Caret extraCP = null;
96 private Caret extraCG = null;
97 private RocketInfo extraText = null;
100 private double cpAOA = Double.NaN;
101 private double cpTheta = Double.NaN;
102 private double cpMach = Double.NaN;
103 private double cpRoll = Double.NaN;
105 // The functional ID of the rocket that was simulated
106 private int flightDataFunctionalID = -1;
107 private String flightDataMotorID = null;
110 private SimulationWorker backgroundSimulationWorker = null;
111 private boolean dirty = false;
113 private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
117 * The executor service used for running the background simulations.
118 * This uses a fixed-sized thread pool for all background simulations
119 * with all threads in daemon mode and with minimum priority.
121 private static final Executor backgroundSimulationExecutor;
123 backgroundSimulationExecutor = Executors.newFixedThreadPool(Prefs.getMaxThreadCount(),
124 new ThreadFactory() {
125 private ThreadFactory factory = Executors.defaultThreadFactory();
128 public Thread newThread(Runnable r) {
129 Thread t = factory.newThread(r);
131 t.setPriority(Thread.MIN_PRIORITY);
138 public RocketPanel(OpenRocketDocument document) {
140 this.document = document;
141 configuration = document.getDefaultConfiguration();
143 // TODO: FUTURE: calculator selection
144 aerodynamicCalculator = new BarrowmanCalculator();
145 massCalculator = new BasicMassCalculator();
147 // Create figure and custom scroll pane
148 figure = new RocketFigure(configuration);
150 scrollPane = new ScaleScrollPane(figure) {
152 public void mouseClicked(MouseEvent event) {
153 handleMouseClick(event);
156 scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
157 scrollPane.setFitting(true);
161 configuration.addChangeListener(new ChangeListener() {
163 public void stateChanged(ChangeEvent e) {
164 System.out.println("Configuration changed, calling updateFigure");
166 figure.updateFigure();
173 * Creates the layout and components of the panel.
175 private void createPanel() {
176 setLayout(new MigLayout("", "[shrink][grow]", "[shrink][shrink][grow][shrink]"));
178 setPreferredSize(new Dimension(800, 300));
184 FigureTypeAction action = new FigureTypeAction(RocketFigure.TYPE_SIDE);
186 action.putValue(Action.NAME, trans.get("RocketPanel.FigTypeAct.Sideview"));
188 action.putValue(Action.SHORT_DESCRIPTION, trans.get("RocketPanel.FigTypeAct.ttip.Sideview"));
189 JToggleButton toggle = new JToggleButton(action);
190 add(toggle, "spanx, split");
192 action = new FigureTypeAction(RocketFigure.TYPE_BACK);
194 action.putValue(Action.NAME, trans.get("RocketPanel.FigTypeAct.Backview"));
196 action.putValue(Action.SHORT_DESCRIPTION, trans.get("RocketPanel.FigTypeAct.ttip.Backview"));
197 toggle = new JToggleButton(action);
198 add(toggle, "gap rel");
201 // Zoom level selector
202 ScaleSelector scaleSelector = new ScaleSelector(scrollPane);
208 StageSelector stageSelector = new StageSelector(configuration);
209 add(stageSelector, "");
213 // Motor configuration selector
214 //// Motor configuration:
215 JLabel label = new JLabel(trans.get("RocketPanel.lbl.Motorcfg"));
216 label.setHorizontalAlignment(JLabel.RIGHT);
217 add(label, "growx, right");
218 add(new JComboBox(new MotorConfigurationModel(configuration)), "wrap");
224 // Create slider and scroll pane
226 DoubleModel theta = new DoubleModel(figure, "Rotation",
227 UnitGroup.UNITS_ANGLE, 0, 2 * Math.PI);
228 UnitSelector us = new UnitSelector(theta, true);
229 us.setHorizontalAlignment(JLabel.CENTER);
230 add(us, "alignx 50%, growx");
232 // Add the rocket figure
233 add(scrollPane, "grow, spany 2, wmin 300lp, hmin 100lp, wrap");
236 // Add rotation slider
237 // Minimum size to fit "360deg"
238 JLabel l = new JLabel("360" + Chars.DEGREE);
239 Dimension d = l.getPreferredSize();
241 add(new BasicSlider(theta.getSliderModel(0, 2 * Math.PI), JSlider.VERTICAL, true),
242 "ax 50%, wrap, width " + (d.width + 6) + "px:null:null, growy");
245 //// <html>Click to select Shift+click to select other Double-click to edit Click+drag to move
246 infoMessage = new JLabel(trans.get("RocketPanel.lbl.infoMessage"));
247 infoMessage.setFont(new Font("Sans Serif", Font.PLAIN, 9));
248 add(infoMessage, "skip, span, gapleft 25, wrap");
255 public RocketFigure getFigure() {
259 public AerodynamicCalculator getAerodynamicCalculator() {
260 return aerodynamicCalculator;
263 public Configuration getConfiguration() {
264 return configuration;
268 * Get the center of pressure figure element.
270 * @return center of pressure info
272 public Caret getExtraCP () {
277 * Get the center of gravity figure element.
279 * @return center of gravity info
281 public Caret getExtraCG () {
286 * Get the extra text figure element.
288 * @return extra text that contains info about the rocket design
290 public RocketInfo getExtraText () {
294 public void setSelectionModel(TreeSelectionModel m) {
295 if (selectionModel != null) {
296 selectionModel.removeTreeSelectionListener(this);
299 selectionModel.addTreeSelectionListener(this);
300 valueChanged((TreeSelectionEvent) null); // updates FigureParameters
306 * Return the angle of attack used in CP calculation. NaN signifies the default value
308 * @return the angle of attack used, or NaN.
310 public double getCPAOA() {
315 * Set the angle of attack to be used in CP calculation. A value of NaN signifies that
316 * the default AOA (zero) should be used.
317 * @param aoa the angle of attack to use, or NaN
319 public void setCPAOA(double aoa) {
320 if (MathUtil.equals(aoa, cpAOA) ||
321 (Double.isNaN(aoa) && Double.isNaN(cpAOA)))
325 figure.updateFigure();
329 public double getCPTheta() {
333 public void setCPTheta(double theta) {
334 if (MathUtil.equals(theta, cpTheta) ||
335 (Double.isNaN(theta) && Double.isNaN(cpTheta)))
338 if (!Double.isNaN(theta))
339 figure.setRotation(theta);
341 figure.updateFigure();
345 public double getCPMach() {
349 public void setCPMach(double mach) {
350 if (MathUtil.equals(mach, cpMach) ||
351 (Double.isNaN(mach) && Double.isNaN(cpMach)))
355 figure.updateFigure();
359 public double getCPRoll() {
363 public void setCPRoll(double roll) {
364 if (MathUtil.equals(roll, cpRoll) ||
365 (Double.isNaN(roll) && Double.isNaN(cpRoll)))
369 figure.updateFigure();
376 public void addChangeListener(ChangeListener listener) {
377 listeners.add(0, listener);
381 public void removeChangeListener(ChangeListener listener) {
382 listeners.remove(listener);
385 protected void fireChangeEvent() {
386 ChangeEvent e = new ChangeEvent(this);
387 ChangeListener[] list = listeners.toArray(new ChangeListener[0]);
388 for (ChangeListener l : list) {
397 * Handle clicking on figure shapes. The functioning is the following:
399 * Get the components clicked.
400 * If no component is clicked, do nothing.
401 * If the currently selected component is in the set, keep it,
402 * unless the selector specified is pressed. If it is pressed, cycle to
403 * the next component. Otherwise select the first component in the list.
405 public static final int CYCLE_SELECTION_MODIFIER = InputEvent.SHIFT_DOWN_MASK;
407 private void handleMouseClick(MouseEvent event) {
408 if (event.getButton() != MouseEvent.BUTTON1)
410 Point p0 = event.getPoint();
411 Point p1 = scrollPane.getViewport().getViewPosition();
415 RocketComponent[] clicked = figure.getComponentsByPoint(x, y);
417 // If no component is clicked, do nothing
418 if (clicked.length == 0)
421 // Check whether the currently selected component is in the clicked components.
422 TreePath path = selectionModel.getSelectionPath();
424 RocketComponent current = (RocketComponent) path.getLastPathComponent();
426 for (int i = 0; i < clicked.length; i++) {
427 if (clicked[i] == current) {
428 if (event.isShiftDown() && (event.getClickCount() == 1)) {
429 path = ComponentTreeModel.makeTreePath(clicked[(i + 1) % clicked.length]);
431 path = ComponentTreeModel.makeTreePath(clicked[i]);
438 // Currently selected component not clicked
440 if (event.isShiftDown() && event.getClickCount() == 1 && clicked.length > 1) {
441 path = ComponentTreeModel.makeTreePath(clicked[1]);
443 path = ComponentTreeModel.makeTreePath(clicked[0]);
447 // Set selection and check for double-click
448 selectionModel.setSelectionPath(path);
449 if (event.getClickCount() == 2) {
450 RocketComponent component = (RocketComponent) path.getLastPathComponent();
452 ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),
453 document, component);
461 * Updates the extra data included in the figure. Currently this includes
462 * the CP and CG carets.
464 private WarningSet warnings = new WarningSet();
466 private void updateExtras() {
470 // TODO: MEDIUM: User-definable conditions
471 FlightConditions conditions = new FlightConditions(configuration);
474 if (!Double.isNaN(cpMach)) {
475 conditions.setMach(cpMach);
476 extraText.setMach(cpMach);
478 conditions.setMach(Prefs.getDefaultMach());
479 extraText.setMach(Prefs.getDefaultMach());
482 if (!Double.isNaN(cpAOA)) {
483 conditions.setAOA(cpAOA);
485 conditions.setAOA(0);
487 extraText.setAOA(cpAOA);
489 if (!Double.isNaN(cpRoll)) {
490 conditions.setRollRate(cpRoll);
492 conditions.setRollRate(0);
495 if (!Double.isNaN(cpTheta)) {
496 conditions.setTheta(cpTheta);
497 cp = aerodynamicCalculator.getCP(configuration, conditions, warnings);
499 cp = aerodynamicCalculator.getWorstCP(configuration, conditions, warnings);
501 extraText.setTheta(cpTheta);
504 cg = massCalculator.getCG(configuration, MassCalcType.LAUNCH_MASS);
505 // System.out.println("CG computed as "+cg+ " CP as "+cp);
507 if (cp.weight > 0.000001)
512 if (cg.weight > 0.000001)
517 // Length bound is assumed to be tight
518 double length = 0, diameter = 0;
519 Collection<Coordinate> bounds = configuration.getBounds();
520 if (!bounds.isEmpty()) {
521 double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY;
522 for (Coordinate c : bounds) {
528 length = maxX - minX;
531 for (RocketComponent c : configuration) {
532 if (c instanceof SymmetricComponent) {
533 double d1 = ((SymmetricComponent) c).getForeRadius() * 2;
534 double d2 = ((SymmetricComponent) c).getAftRadius() * 2;
535 diameter = MathUtil.max(diameter, d1, d2);
539 extraText.setCG(cgx);
540 extraText.setCP(cpx);
541 extraText.setLength(length);
542 extraText.setDiameter(diameter);
543 extraText.setMass(cg.weight);
544 extraText.setWarnings(warnings);
547 if (figure.getType() == RocketFigure.TYPE_SIDE && length > 0) {
549 // TODO: LOW: Y-coordinate and rotation
550 extraCP.setPosition(cpx * RocketFigure.EXTRA_SCALE, 0);
551 extraCG.setPosition(cgx * RocketFigure.EXTRA_SCALE, 0);
555 extraCP.setPosition(Double.NaN, Double.NaN);
556 extraCG.setPosition(Double.NaN, Double.NaN);
561 //////// Flight simulation in background
563 // Check whether to compute or not
564 if (!Prefs.computeFlightInBackground()) {
565 extraText.setFlightData(null);
566 extraText.setCalculatingData(false);
567 stopBackgroundSimulation();
571 // Check whether data is already up to date
572 if (flightDataFunctionalID == configuration.getRocket().getFunctionalModID() &&
573 flightDataMotorID == configuration.getMotorConfigurationID()) {
577 flightDataFunctionalID = configuration.getRocket().getFunctionalModID();
578 flightDataMotorID = configuration.getMotorConfigurationID();
580 // Stop previous computation (if any)
581 stopBackgroundSimulation();
583 // Check that configuration has motors
584 if (!configuration.hasMotors()) {
585 extraText.setFlightData(FlightData.NaN_DATA);
586 extraText.setCalculatingData(false);
590 // Start calculation process
591 extraText.setCalculatingData(true);
593 Rocket duplicate = (Rocket) configuration.getRocket().copy();
594 Simulation simulation = Prefs.getBackgroundSimulation(duplicate);
595 simulation.getOptions().setMotorConfigurationID(
596 configuration.getMotorConfigurationID());
598 backgroundSimulationWorker = new BackgroundSimulationWorker(simulation);
599 backgroundSimulationExecutor.execute(backgroundSimulationWorker);
603 * Cancels the current background simulation worker, if any.
605 private void stopBackgroundSimulation() {
606 if (backgroundSimulationWorker != null) {
607 backgroundSimulationWorker.cancel(true);
608 backgroundSimulationWorker = null;
614 * A SimulationWorker that simulates the rocket flight in the background and
615 * sets the results to the extra text when finished. The worker can be cancelled
618 private class BackgroundSimulationWorker extends SimulationWorker {
620 public BackgroundSimulationWorker(Simulation sim) {
625 protected FlightData doInBackground() {
627 // Pause a little while to allow faster UI reaction
630 } catch (InterruptedException ignore) {
632 if (isCancelled() || backgroundSimulationWorker != this)
635 return super.doInBackground();
639 protected void simulationDone() {
640 // Do nothing if cancelled
641 if (isCancelled() || backgroundSimulationWorker != this)
644 backgroundSimulationWorker = null;
645 extraText.setFlightData(simulation.getSimulatedData());
646 extraText.setCalculatingData(false);
651 protected SimulationListener[] getExtraListeners() {
652 return new SimulationListener[] {
653 InterruptListener.INSTANCE,
654 ApogeeEndListener.INSTANCE };
658 protected void simulationInterrupted(Throwable t) {
659 // Do nothing on cancel, set N/A data otherwise
660 if (isCancelled() || backgroundSimulationWorker != this) // Double-check
663 backgroundSimulationWorker = null;
664 extraText.setFlightData(FlightData.NaN_DATA);
665 extraText.setCalculatingData(false);
673 * Adds the extra data to the figure. Currently this includes the CP and CG carets.
675 private void addExtras() {
676 figure.clearRelativeExtra();
677 extraCG = new CGCaret(0, 0);
678 extraCP = new CPCaret(0, 0);
679 extraText = new RocketInfo(configuration);
681 figure.addRelativeExtra(extraCP);
682 figure.addRelativeExtra(extraCG);
683 figure.addAbsoluteExtra(extraText);
688 * Updates the selection in the FigureParameters and repaints the figure.
689 * Ignores the event itself.
692 public void valueChanged(TreeSelectionEvent e) {
693 TreePath[] paths = selectionModel.getSelectionPaths();
695 figure.setSelection(null);
699 RocketComponent[] components = new RocketComponent[paths.length];
700 for (int i = 0; i < paths.length; i++)
701 components[i] = (RocketComponent) paths[i].getLastPathComponent();
702 figure.setSelection(components);
708 * An <code>Action</code> that shows whether the figure type is the type
709 * given in the constructor.
711 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
713 private class FigureTypeAction extends AbstractAction implements ChangeListener {
714 private final int type;
716 public FigureTypeAction(int type) {
719 figure.addChangeListener(this);
723 public void actionPerformed(ActionEvent e) {
724 boolean state = (Boolean) getValue(Action.SELECTED_KEY);
726 // This view has been selected
727 figure.setType(type);
734 public void stateChanged(ChangeEvent e) {
735 putValue(Action.SELECTED_KEY, figure.getType() == type);