1 package net.sf.openrocket.gui.scalefigure;
4 import java.awt.Dimension;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.InputEvent;
9 import java.awt.event.MouseEvent;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.concurrent.Executor;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.ThreadFactory;
17 import javax.swing.AbstractAction;
18 import javax.swing.Action;
19 import javax.swing.JComboBox;
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
22 import javax.swing.JSlider;
23 import javax.swing.JToggleButton;
24 import javax.swing.JViewport;
25 import javax.swing.SwingUtilities;
26 import javax.swing.event.ChangeEvent;
27 import javax.swing.event.ChangeListener;
28 import javax.swing.event.TreeSelectionEvent;
29 import javax.swing.event.TreeSelectionListener;
30 import javax.swing.tree.TreePath;
31 import javax.swing.tree.TreeSelectionModel;
33 import net.miginfocom.swing.MigLayout;
34 import net.sf.openrocket.aerodynamics.AerodynamicCalculator;
35 import net.sf.openrocket.aerodynamics.BarrowmanCalculator;
36 import net.sf.openrocket.aerodynamics.FlightConditions;
37 import net.sf.openrocket.aerodynamics.WarningSet;
38 import net.sf.openrocket.document.OpenRocketDocument;
39 import net.sf.openrocket.document.Simulation;
40 import net.sf.openrocket.gui.adaptors.DoubleModel;
41 import net.sf.openrocket.gui.adaptors.MotorConfigurationModel;
42 import net.sf.openrocket.gui.components.BasicSlider;
43 import net.sf.openrocket.gui.components.StageSelector;
44 import net.sf.openrocket.gui.components.UnitSelector;
45 import net.sf.openrocket.gui.configdialog.ComponentConfigDialog;
46 import net.sf.openrocket.gui.figureelements.CGCaret;
47 import net.sf.openrocket.gui.figureelements.CPCaret;
48 import net.sf.openrocket.gui.figureelements.Caret;
49 import net.sf.openrocket.gui.figureelements.RocketInfo;
50 import net.sf.openrocket.gui.main.SimulationWorker;
51 import net.sf.openrocket.gui.main.componenttree.ComponentTreeModel;
52 import net.sf.openrocket.l10n.Translator;
53 import net.sf.openrocket.masscalc.BasicMassCalculator;
54 import net.sf.openrocket.masscalc.MassCalculator;
55 import net.sf.openrocket.masscalc.MassCalculator.MassCalcType;
56 import net.sf.openrocket.rocketcomponent.Configuration;
57 import net.sf.openrocket.rocketcomponent.Rocket;
58 import net.sf.openrocket.rocketcomponent.RocketComponent;
59 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
60 import net.sf.openrocket.simulation.FlightData;
61 import net.sf.openrocket.simulation.listeners.SimulationListener;
62 import net.sf.openrocket.simulation.listeners.system.ApogeeEndListener;
63 import net.sf.openrocket.simulation.listeners.system.InterruptListener;
64 import net.sf.openrocket.startup.Application;
65 import net.sf.openrocket.unit.UnitGroup;
66 import net.sf.openrocket.util.ChangeSource;
67 import net.sf.openrocket.util.Chars;
68 import net.sf.openrocket.util.Coordinate;
69 import net.sf.openrocket.util.MathUtil;
70 import net.sf.openrocket.util.Prefs;
73 * A JPanel that contains a RocketFigure and buttons to manipulate the figure.
75 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
77 public class RocketPanel extends JPanel implements TreeSelectionListener, ChangeSource {
79 private static final Translator trans = Application.getTranslator();
80 private final RocketFigure figure;
81 private final ScaleScrollPane scrollPane;
83 private JLabel infoMessage;
85 private TreeSelectionModel selectionModel = null;
88 /* Calculation of CP and CG */
89 private AerodynamicCalculator aerodynamicCalculator;
90 private MassCalculator massCalculator;
93 private final OpenRocketDocument document;
94 private final Configuration configuration;
96 private Caret extraCP = null;
97 private Caret extraCG = null;
98 private RocketInfo extraText = null;
101 private double cpAOA = Double.NaN;
102 private double cpTheta = Double.NaN;
103 private double cpMach = Double.NaN;
104 private double cpRoll = Double.NaN;
106 // The functional ID of the rocket that was simulated
107 private int flightDataFunctionalID = -1;
108 private String flightDataMotorID = null;
111 private SimulationWorker backgroundSimulationWorker = null;
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");
256 public RocketFigure getFigure() {
260 public AerodynamicCalculator getAerodynamicCalculator() {
261 return aerodynamicCalculator;
264 public Configuration getConfiguration() {
265 return configuration;
269 * Get the center of pressure figure element.
271 * @return center of pressure info
273 public Caret getExtraCP() {
278 * Get the center of gravity figure element.
280 * @return center of gravity info
282 public Caret getExtraCG() {
287 * Get the extra text figure element.
289 * @return extra text that contains info about the rocket design
291 public RocketInfo getExtraText() {
295 public void setSelectionModel(TreeSelectionModel m) {
296 if (selectionModel != null) {
297 selectionModel.removeTreeSelectionListener(this);
300 selectionModel.addTreeSelectionListener(this);
301 valueChanged((TreeSelectionEvent) null); // updates FigureParameters
307 * Return the angle of attack used in CP calculation. NaN signifies the default value
309 * @return the angle of attack used, or NaN.
311 public double getCPAOA() {
316 * Set the angle of attack to be used in CP calculation. A value of NaN signifies that
317 * the default AOA (zero) should be used.
318 * @param aoa the angle of attack to use, or NaN
320 public void setCPAOA(double aoa) {
321 if (MathUtil.equals(aoa, cpAOA) ||
322 (Double.isNaN(aoa) && Double.isNaN(cpAOA)))
326 figure.updateFigure();
330 public double getCPTheta() {
334 public void setCPTheta(double theta) {
335 if (MathUtil.equals(theta, cpTheta) ||
336 (Double.isNaN(theta) && Double.isNaN(cpTheta)))
339 if (!Double.isNaN(theta))
340 figure.setRotation(theta);
342 figure.updateFigure();
346 public double getCPMach() {
350 public void setCPMach(double mach) {
351 if (MathUtil.equals(mach, cpMach) ||
352 (Double.isNaN(mach) && Double.isNaN(cpMach)))
356 figure.updateFigure();
360 public double getCPRoll() {
364 public void setCPRoll(double roll) {
365 if (MathUtil.equals(roll, cpRoll) ||
366 (Double.isNaN(roll) && Double.isNaN(cpRoll)))
370 figure.updateFigure();
377 public void addChangeListener(ChangeListener listener) {
378 listeners.add(0, listener);
382 public void removeChangeListener(ChangeListener listener) {
383 listeners.remove(listener);
386 protected void fireChangeEvent() {
387 ChangeEvent e = new ChangeEvent(this);
388 ChangeListener[] list = listeners.toArray(new ChangeListener[0]);
389 for (ChangeListener l : list) {
398 * Handle clicking on figure shapes. The functioning is the following:
400 * Get the components clicked.
401 * If no component is clicked, do nothing.
402 * If the currently selected component is in the set, keep it,
403 * unless the selector specified is pressed. If it is pressed, cycle to
404 * the next component. Otherwise select the first component in the list.
406 public static final int CYCLE_SELECTION_MODIFIER = InputEvent.SHIFT_DOWN_MASK;
408 private void handleMouseClick(MouseEvent event) {
409 if (event.getButton() != MouseEvent.BUTTON1)
411 Point p0 = event.getPoint();
412 Point p1 = scrollPane.getViewport().getViewPosition();
416 RocketComponent[] clicked = figure.getComponentsByPoint(x, y);
418 // If no component is clicked, do nothing
419 if (clicked.length == 0)
422 // Check whether the currently selected component is in the clicked components.
423 TreePath path = selectionModel.getSelectionPath();
425 RocketComponent current = (RocketComponent) path.getLastPathComponent();
427 for (int i = 0; i < clicked.length; i++) {
428 if (clicked[i] == current) {
429 if (event.isShiftDown() && (event.getClickCount() == 1)) {
430 path = ComponentTreeModel.makeTreePath(clicked[(i + 1) % clicked.length]);
432 path = ComponentTreeModel.makeTreePath(clicked[i]);
439 // Currently selected component not clicked
441 if (event.isShiftDown() && event.getClickCount() == 1 && clicked.length > 1) {
442 path = ComponentTreeModel.makeTreePath(clicked[1]);
444 path = ComponentTreeModel.makeTreePath(clicked[0]);
448 // Set selection and check for double-click
449 selectionModel.setSelectionPath(path);
450 if (event.getClickCount() == 2) {
451 RocketComponent component = (RocketComponent) path.getLastPathComponent();
453 ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),
454 document, component);
462 * Updates the extra data included in the figure. Currently this includes
463 * the CP and CG carets.
465 private WarningSet warnings = new WarningSet();
467 private void updateExtras() {
471 // TODO: MEDIUM: User-definable conditions
472 FlightConditions conditions = new FlightConditions(configuration);
475 if (!Double.isNaN(cpMach)) {
476 conditions.setMach(cpMach);
477 extraText.setMach(cpMach);
479 conditions.setMach(Prefs.getDefaultMach());
480 extraText.setMach(Prefs.getDefaultMach());
483 if (!Double.isNaN(cpAOA)) {
484 conditions.setAOA(cpAOA);
486 conditions.setAOA(0);
488 extraText.setAOA(cpAOA);
490 if (!Double.isNaN(cpRoll)) {
491 conditions.setRollRate(cpRoll);
493 conditions.setRollRate(0);
496 if (!Double.isNaN(cpTheta)) {
497 conditions.setTheta(cpTheta);
498 cp = aerodynamicCalculator.getCP(configuration, conditions, warnings);
500 cp = aerodynamicCalculator.getWorstCP(configuration, conditions, warnings);
502 extraText.setTheta(cpTheta);
505 cg = massCalculator.getCG(configuration, MassCalcType.LAUNCH_MASS);
506 // System.out.println("CG computed as "+cg+ " CP as "+cp);
508 if (cp.weight > 0.000001)
513 if (cg.weight > 0.000001)
518 // Length bound is assumed to be tight
519 double length = 0, diameter = 0;
520 Collection<Coordinate> bounds = configuration.getBounds();
521 if (!bounds.isEmpty()) {
522 double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY;
523 for (Coordinate c : bounds) {
529 length = maxX - minX;
532 for (RocketComponent c : configuration) {
533 if (c instanceof SymmetricComponent) {
534 double d1 = ((SymmetricComponent) c).getForeRadius() * 2;
535 double d2 = ((SymmetricComponent) c).getAftRadius() * 2;
536 diameter = MathUtil.max(diameter, d1, d2);
540 extraText.setCG(cgx);
541 extraText.setCP(cpx);
542 extraText.setLength(length);
543 extraText.setDiameter(diameter);
544 extraText.setMass(cg.weight);
545 extraText.setWarnings(warnings);
548 if (figure.getType() == RocketFigure.TYPE_SIDE && length > 0) {
550 // TODO: LOW: Y-coordinate and rotation
551 extraCP.setPosition(cpx * RocketFigure.EXTRA_SCALE, 0);
552 extraCG.setPosition(cgx * RocketFigure.EXTRA_SCALE, 0);
556 extraCP.setPosition(Double.NaN, Double.NaN);
557 extraCG.setPosition(Double.NaN, Double.NaN);
562 //////// Flight simulation in background
564 // Check whether to compute or not
565 if (!Prefs.computeFlightInBackground()) {
566 extraText.setFlightData(null);
567 extraText.setCalculatingData(false);
568 stopBackgroundSimulation();
572 // Check whether data is already up to date
573 if (flightDataFunctionalID == configuration.getRocket().getFunctionalModID() &&
574 flightDataMotorID == configuration.getMotorConfigurationID()) {
578 flightDataFunctionalID = configuration.getRocket().getFunctionalModID();
579 flightDataMotorID = configuration.getMotorConfigurationID();
581 // Stop previous computation (if any)
582 stopBackgroundSimulation();
584 // Check that configuration has motors
585 if (!configuration.hasMotors()) {
586 extraText.setFlightData(FlightData.NaN_DATA);
587 extraText.setCalculatingData(false);
591 // Start calculation process
592 extraText.setCalculatingData(true);
594 Rocket duplicate = (Rocket) configuration.getRocket().copy();
595 Simulation simulation = Prefs.getBackgroundSimulation(duplicate);
596 simulation.getOptions().setMotorConfigurationID(
597 configuration.getMotorConfigurationID());
599 backgroundSimulationWorker = new BackgroundSimulationWorker(simulation);
600 backgroundSimulationExecutor.execute(backgroundSimulationWorker);
604 * Cancels the current background simulation worker, if any.
606 private void stopBackgroundSimulation() {
607 if (backgroundSimulationWorker != null) {
608 backgroundSimulationWorker.cancel(true);
609 backgroundSimulationWorker = null;
615 * A SimulationWorker that simulates the rocket flight in the background and
616 * sets the results to the extra text when finished. The worker can be cancelled
619 private class BackgroundSimulationWorker extends SimulationWorker {
621 public BackgroundSimulationWorker(Simulation sim) {
626 protected FlightData doInBackground() {
628 // Pause a little while to allow faster UI reaction
631 } catch (InterruptedException ignore) {
633 if (isCancelled() || backgroundSimulationWorker != this)
636 return super.doInBackground();
640 protected void simulationDone() {
641 // Do nothing if cancelled
642 if (isCancelled() || backgroundSimulationWorker != this)
645 backgroundSimulationWorker = null;
646 extraText.setFlightData(simulation.getSimulatedData());
647 extraText.setCalculatingData(false);
652 protected SimulationListener[] getExtraListeners() {
653 return new SimulationListener[] {
654 InterruptListener.INSTANCE,
655 ApogeeEndListener.INSTANCE };
659 protected void simulationInterrupted(Throwable t) {
660 // Do nothing on cancel, set N/A data otherwise
661 if (isCancelled() || backgroundSimulationWorker != this) // Double-check
664 backgroundSimulationWorker = null;
665 extraText.setFlightData(FlightData.NaN_DATA);
666 extraText.setCalculatingData(false);
674 * Adds the extra data to the figure. Currently this includes the CP and CG carets.
676 private void addExtras() {
677 figure.clearRelativeExtra();
678 extraCG = new CGCaret(0, 0);
679 extraCP = new CPCaret(0, 0);
680 extraText = new RocketInfo(configuration);
682 figure.addRelativeExtra(extraCP);
683 figure.addRelativeExtra(extraCG);
684 figure.addAbsoluteExtra(extraText);
689 * Updates the selection in the FigureParameters and repaints the figure.
690 * Ignores the event itself.
693 public void valueChanged(TreeSelectionEvent e) {
694 TreePath[] paths = selectionModel.getSelectionPaths();
696 figure.setSelection(null);
700 RocketComponent[] components = new RocketComponent[paths.length];
701 for (int i = 0; i < paths.length; i++)
702 components[i] = (RocketComponent) paths[i].getLastPathComponent();
703 figure.setSelection(components);
709 * An <code>Action</code> that shows whether the figure type is the type
710 * given in the constructor.
712 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
714 private class FigureTypeAction extends AbstractAction implements ChangeListener {
715 private final int type;
717 public FigureTypeAction(int type) {
720 figure.addChangeListener(this);
724 public void actionPerformed(ActionEvent e) {
725 boolean state = (Boolean) getValue(Action.SELECTED_KEY);
727 // This view has been selected
728 figure.setType(type);
735 public void stateChanged(ChangeEvent e) {
736 putValue(Action.SELECTED_KEY, figure.getType() == type);