Trying out an "Unfinished" view... Jury is out

This commit is contained in:
bkuker 2013-01-10 15:36:08 -05:00
parent 67c1a3c553
commit c1c4f99992
4 changed files with 213 additions and 215 deletions

View File

@ -47,7 +47,8 @@ RocketActions.MoveDownAct.ttip.Movedown = Move this component downwards.
RocketPanel.FigTypeAct.Sideview = Side view RocketPanel.FigTypeAct.Sideview = Side view
RocketPanel.FigTypeAct.Backview = Back view RocketPanel.FigTypeAct.Backview = Back view
RocketPanel.FigTypeAct.Figure3D = 3D Figure RocketPanel.FigTypeAct.Figure3D = 3D Figure
RocketPanel.FigTypeAct.Realistic3D = 3D Realistic RocketPanel.FigTypeAct.Finished = 3D Finished
RocketPanel.FigTypeAct.Unfinished = 3D Unfinished
RocketPanel.lbl.Flightcfg = Flight configuration: RocketPanel.lbl.Flightcfg = Flight configuration:

View File

@ -51,8 +51,9 @@ import com.jogamp.opengl.util.awt.Overlay;
*/ */
public class RocketFigure3d extends JPanel implements GLEventListener { public class RocketFigure3d extends JPanel implements GLEventListener {
public static final int TYPE_REALISTIC = 0; public static final int TYPE_FIGURE = 0;
public static final int TYPE_FIGURE = 1; public static final int TYPE_UNFINISHED = 1;
public static final int TYPE_FINISHED = 2;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final LogHelper log = Application.getLogger(); private static final LogHelper log = Application.getLogger();
@ -644,8 +645,10 @@ public class RocketFigure3d extends JPanel implements GLEventListener {
rr.dispose(drawable); rr.dispose(drawable);
if (t == TYPE_FIGURE) { if (t == TYPE_FIGURE) {
rr = new FigureRenderer(); rr = new FigureRenderer();
} else { } else if (t == TYPE_FINISHED) {
rr = new RealisticRenderer(document); rr = new RealisticRenderer(document);
} else if (t == TYPE_UNFINISHED) {
rr = new UnfinishedRenderer(document);
} }
rr.init(drawable); rr.init(drawable);
return false; return false;

View File

@ -87,71 +87,76 @@ import net.sf.openrocket.util.StateChangeListener;
*/ */
public class RocketPanel extends JPanel implements TreeSelectionListener, ChangeSource { public class RocketPanel extends JPanel implements TreeSelectionListener, ChangeSource {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
/*RocketPanel.FigTypeAct.Sideview = Side view public static enum VIEW_TYPE {
RocketPanel.FigTypeAct.Backview = Back view Sideview(false, RocketFigure.TYPE_SIDE),
RocketPanel.FigViewAct.3DFigure = 3D Figure Backview(false, RocketFigure.TYPE_BACK),
RocketPanel.FigViewAct.3DRealistic = 3D Realistic*/ Figure3D(true, RocketFigure3d.TYPE_FIGURE),
Unfinished(true, RocketFigure3d.TYPE_UNFINISHED),
private static enum VIEW_TYPE { Finished(true, RocketFigure3d.TYPE_FINISHED);
Sideview,
Backview, public final boolean is3d;
Figure3D, private final int type;
Realistic3D;
private VIEW_TYPE(final boolean is3d, final int type) {
this.is3d = is3d;
this.type = type;
};
@Override @Override
public String toString(){ public String toString() {
return trans.get("RocketPanel.FigTypeAct." + super.toString()); return trans.get("RocketPanel.FigTypeAct." + super.toString());
} }
} }
private boolean is3d; private boolean is3d;
private final RocketFigure figure; private final RocketFigure figure;
private final RocketFigure3d figure3d; private final RocketFigure3d figure3d;
private final ScaleScrollPane scrollPane; private final ScaleScrollPane scrollPane;
private final JPanel figureHolder; private final JPanel figureHolder;
private JLabel infoMessage; private JLabel infoMessage;
private TreeSelectionModel selectionModel = null; private TreeSelectionModel selectionModel = null;
private BasicSlider rotationSlider; private BasicSlider rotationSlider;
private ScaleSelector scaleSelector; private ScaleSelector scaleSelector;
/* Calculation of CP and CG */ /* Calculation of CP and CG */
private AerodynamicCalculator aerodynamicCalculator; private AerodynamicCalculator aerodynamicCalculator;
private MassCalculator massCalculator; private MassCalculator massCalculator;
private final OpenRocketDocument document; private final OpenRocketDocument document;
private final Configuration configuration; private final Configuration configuration;
private Caret extraCP = null; private Caret extraCP = null;
private Caret extraCG = null; private Caret extraCG = null;
private RocketInfo extraText = null; private RocketInfo extraText = null;
private double cpAOA = Double.NaN; private double cpAOA = Double.NaN;
private double cpTheta = Double.NaN; private double cpTheta = Double.NaN;
private double cpMach = Double.NaN; private double cpMach = Double.NaN;
private double cpRoll = Double.NaN; private double cpRoll = Double.NaN;
// The functional ID of the rocket that was simulated // The functional ID of the rocket that was simulated
private int flightDataFunctionalID = -1; private int flightDataFunctionalID = -1;
private String flightDataMotorID = null; private String flightDataMotorID = null;
private SimulationWorker backgroundSimulationWorker = null; private SimulationWorker backgroundSimulationWorker = null;
private List<EventListener> listeners = new ArrayList<EventListener>(); private List<EventListener> listeners = new ArrayList<EventListener>();
/** /**
* The executor service used for running the background simulations. * The executor service used for running the background simulations.
* This uses a fixed-sized thread pool for all background simulations * This uses a fixed-sized thread pool for all background simulations
@ -161,37 +166,37 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
static { static {
backgroundSimulationExecutor = Executors.newFixedThreadPool(SwingPreferences.getMaxThreadCount(), backgroundSimulationExecutor = Executors.newFixedThreadPool(SwingPreferences.getMaxThreadCount(),
new ThreadFactory() { new ThreadFactory() {
private ThreadFactory factory = Executors.defaultThreadFactory(); private ThreadFactory factory = Executors.defaultThreadFactory();
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread t = factory.newThread(r); Thread t = factory.newThread(r);
t.setDaemon(true); t.setDaemon(true);
t.setPriority(Thread.MIN_PRIORITY); t.setPriority(Thread.MIN_PRIORITY);
return t; return t;
} }
}); });
} }
public RocketPanel(OpenRocketDocument document) { public RocketPanel(OpenRocketDocument document) {
this.document = document; this.document = document;
configuration = document.getDefaultConfiguration(); configuration = document.getDefaultConfiguration();
// TODO: FUTURE: calculator selection // TODO: FUTURE: calculator selection
aerodynamicCalculator = new BarrowmanCalculator(); aerodynamicCalculator = new BarrowmanCalculator();
massCalculator = new BasicMassCalculator(); massCalculator = new BasicMassCalculator();
// Create figure and custom scroll pane // Create figure and custom scroll pane
figure = new RocketFigure(configuration); figure = new RocketFigure(configuration);
figure3d = new RocketFigure3d(document, configuration); figure3d = new RocketFigure3d(document, configuration);
figureHolder = new JPanel(new BorderLayout()); figureHolder = new JPanel(new BorderLayout());
scrollPane = new ScaleScrollPane(figure) { scrollPane = new ScaleScrollPane(figure) {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override @Override
public void mouseClicked(MouseEvent event) { public void mouseClicked(MouseEvent event) {
handleMouseClick(event); handleMouseClick(event);
@ -199,12 +204,12 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
}; };
scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE); scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
scrollPane.setFitting(true); scrollPane.setFitting(true);
createPanel(); createPanel();
is3d = true; is3d = true;
go2D(); go2D();
configuration.addChangeListener(new StateChangeListener() { configuration.addChangeListener(new StateChangeListener() {
@Override @Override
public void stateChanged(EventObject e) { public void stateChanged(EventObject e) {
@ -213,7 +218,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
updateFigures(); updateFigures();
} }
}); });
figure3d.addComponentSelectionListener(new RocketFigure3d.ComponentSelectionListener() { figure3d.addComponentSelectionListener(new RocketFigure3d.ComponentSelectionListener() {
@Override @Override
public void componentClicked(RocketComponent clicked[], MouseEvent event) { public void componentClicked(RocketComponent clicked[], MouseEvent event) {
@ -221,14 +226,14 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
} }
}); });
} }
private void updateFigures() { private void updateFigures() {
if (!is3d) if (!is3d)
figure.updateFigure(); figure.updateFigure();
else else
figure3d.updateFigure(); figure3d.updateFigure();
} }
private void go3D() { private void go3D() {
if (is3d) if (is3d)
return; return;
@ -237,13 +242,13 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
figureHolder.add(figure3d, BorderLayout.CENTER); figureHolder.add(figure3d, BorderLayout.CENTER);
rotationSlider.setEnabled(false); rotationSlider.setEnabled(false);
scaleSelector.setEnabled(false); scaleSelector.setEnabled(false);
revalidate(); revalidate();
figureHolder.revalidate(); figureHolder.revalidate();
figure3d.repaint(); figure3d.repaint();
} }
private void go2D() { private void go2D() {
if (!is3d) if (!is3d)
return; return;
@ -256,123 +261,112 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
figureHolder.revalidate(); figureHolder.revalidate();
figure.repaint(); figure.repaint();
} }
/** /**
* Creates the layout and components of the panel. * Creates the layout and components of the panel.
*/ */
private void createPanel() { private void createPanel() {
setLayout(new MigLayout("", "[shrink][grow]", "[shrink][shrink][grow][shrink]")); setLayout(new MigLayout("", "[shrink][grow]", "[shrink][shrink][grow][shrink]"));
setPreferredSize(new Dimension(800, 300)); setPreferredSize(new Dimension(800, 300));
// View Type Dropdown // View Type Dropdown
ComboBoxModel cm = new DefaultComboBoxModel(VIEW_TYPE.values()) { ComboBoxModel cm = new DefaultComboBoxModel(VIEW_TYPE.values()) {
@Override @Override
public void setSelectedItem(Object o) { public void setSelectedItem(Object o) {
super.setSelectedItem(o); super.setSelectedItem(o);
VIEW_TYPE v = (VIEW_TYPE) o; VIEW_TYPE v = (VIEW_TYPE) o;
switch (v) { if (v.is3d) {
case Sideview: figure3d.setType(v.type);
figure.setType(RocketFigure.TYPE_SIDE);
go2D();
break;
case Backview:
figure.setType(RocketFigure.TYPE_BACK);
go2D();
break;
case Realistic3D:
figure3d.setType(RocketFigure3d.TYPE_REALISTIC);
go3D(); go3D();
break; } else {
case Figure3D: figure.setType(v.type);
figure3d.setType(RocketFigure3d.TYPE_FIGURE); go2D();
go3D();
break;
} }
} }
}; };
add(new JLabel("View Type:"), "spanx, split"); add(new JLabel("View Type:"), "spanx, split");
add(new JComboBox(cm)); add(new JComboBox(cm));
// Zoom level selector // Zoom level selector
scaleSelector = new ScaleSelector(scrollPane); scaleSelector = new ScaleSelector(scrollPane);
add(scaleSelector); add(scaleSelector);
// Stage selector // Stage selector
StageSelector stageSelector = new StageSelector(configuration); StageSelector stageSelector = new StageSelector(configuration);
add(stageSelector); add(stageSelector);
// Flight configuration selector // Flight configuration selector
//// Flight configuration: //// Flight configuration:
JLabel label = new JLabel(trans.get("RocketPanel.lbl.Flightcfg")); JLabel label = new JLabel(trans.get("RocketPanel.lbl.Flightcfg"));
label.setHorizontalAlignment(JLabel.RIGHT); label.setHorizontalAlignment(JLabel.RIGHT);
add(label, "growx, right"); add(label, "growx, right");
add(new JComboBox(new FlightConfigurationModel(configuration)), ""); add(new JComboBox(new FlightConfigurationModel(configuration)), "");
//// Edit button //// Edit button
JButton button = new JButton(trans.get("RocketPanel.but.FlightcfgEdit")); JButton button = new JButton(trans.get("RocketPanel.but.FlightcfgEdit"));
button.addActionListener(new ActionListener() { button.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JDialog configDialog = new FlightConfigurationDialog(document.getRocket(),SwingUtilities.windowForComponent(RocketPanel.this)); JDialog configDialog = new FlightConfigurationDialog(document.getRocket(), SwingUtilities.windowForComponent(RocketPanel.this));
configDialog.show(); configDialog.show();
} }
}); });
add(button, "wrap"); add(button, "wrap");
// Create slider and scroll pane // Create slider and scroll pane
DoubleModel theta = new DoubleModel(figure, "Rotation", DoubleModel theta = new DoubleModel(figure, "Rotation",
UnitGroup.UNITS_ANGLE, 0, 2 * Math.PI); UnitGroup.UNITS_ANGLE, 0, 2 * Math.PI);
UnitSelector us = new UnitSelector(theta, true); UnitSelector us = new UnitSelector(theta, true);
us.setHorizontalAlignment(JLabel.CENTER); us.setHorizontalAlignment(JLabel.CENTER);
add(us, "alignx 50%, growx"); add(us, "alignx 50%, growx");
// Add the rocket figure // Add the rocket figure
add(figureHolder, "grow, spany 2, wmin 300lp, hmin 100lp, wrap"); add(figureHolder, "grow, spany 2, wmin 300lp, hmin 100lp, wrap");
// Add rotation slider // Add rotation slider
// Minimum size to fit "360deg" // Minimum size to fit "360deg"
JLabel l = new JLabel("360" + Chars.DEGREE); JLabel l = new JLabel("360" + Chars.DEGREE);
Dimension d = l.getPreferredSize(); Dimension d = l.getPreferredSize();
add(rotationSlider = new BasicSlider(theta.getSliderModel(0, 2 * Math.PI), JSlider.VERTICAL, true), add(rotationSlider = new BasicSlider(theta.getSliderModel(0, 2 * Math.PI), JSlider.VERTICAL, true),
"ax 50%, wrap, width " + (d.width + 6) + "px:null:null, growy"); "ax 50%, wrap, width " + (d.width + 6) + "px:null:null, growy");
//// <html>Click to select &nbsp;&nbsp; Shift+click to select other &nbsp;&nbsp; Double-click to edit &nbsp;&nbsp; Click+drag to move //// <html>Click to select &nbsp;&nbsp; Shift+click to select other &nbsp;&nbsp; Double-click to edit &nbsp;&nbsp; Click+drag to move
infoMessage = new JLabel(trans.get("RocketPanel.lbl.infoMessage")); infoMessage = new JLabel(trans.get("RocketPanel.lbl.infoMessage"));
infoMessage.setFont(new Font("Sans Serif", Font.PLAIN, 9)); infoMessage.setFont(new Font("Sans Serif", Font.PLAIN, 9));
add(infoMessage, "skip, span, gapleft 25, wrap"); add(infoMessage, "skip, span, gapleft 25, wrap");
addExtras(); addExtras();
} }
public RocketFigure getFigure() { public RocketFigure getFigure() {
return figure; return figure;
} }
public AerodynamicCalculator getAerodynamicCalculator() { public AerodynamicCalculator getAerodynamicCalculator() {
return aerodynamicCalculator; return aerodynamicCalculator;
} }
public Configuration getConfiguration() { public Configuration getConfiguration() {
return configuration; return configuration;
} }
/** /**
* Get the center of pressure figure element. * Get the center of pressure figure element.
* *
@ -381,7 +375,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
public Caret getExtraCP() { public Caret getExtraCP() {
return extraCP; return extraCP;
} }
/** /**
* Get the center of gravity figure element. * Get the center of gravity figure element.
* *
@ -390,7 +384,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
public Caret getExtraCG() { public Caret getExtraCG() {
return extraCG; return extraCG;
} }
/** /**
* Get the extra text figure element. * Get the extra text figure element.
* *
@ -399,7 +393,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
public RocketInfo getExtraText() { public RocketInfo getExtraText() {
return extraText; return extraText;
} }
public void setSelectionModel(TreeSelectionModel m) { public void setSelectionModel(TreeSelectionModel m) {
if (selectionModel != null) { if (selectionModel != null) {
selectionModel.removeTreeSelectionListener(this); selectionModel.removeTreeSelectionListener(this);
@ -408,9 +402,9 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
selectionModel.addTreeSelectionListener(this); selectionModel.addTreeSelectionListener(this);
valueChanged((TreeSelectionEvent) null); // updates FigureParameters valueChanged((TreeSelectionEvent) null); // updates FigureParameters
} }
/** /**
* Return the angle of attack used in CP calculation. NaN signifies the default value * Return the angle of attack used in CP calculation. NaN signifies the default value
* of zero. * of zero.
@ -419,7 +413,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
public double getCPAOA() { public double getCPAOA() {
return cpAOA; return cpAOA;
} }
/** /**
* Set the angle of attack to be used in CP calculation. A value of NaN signifies that * Set the angle of attack to be used in CP calculation. A value of NaN signifies that
* the default AOA (zero) should be used. * the default AOA (zero) should be used.
@ -434,11 +428,11 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
updateFigures(); updateFigures();
fireChangeEvent(); fireChangeEvent();
} }
public double getCPTheta() { public double getCPTheta() {
return cpTheta; return cpTheta;
} }
public void setCPTheta(double theta) { public void setCPTheta(double theta) {
if (MathUtil.equals(theta, cpTheta) || if (MathUtil.equals(theta, cpTheta) ||
(Double.isNaN(theta) && Double.isNaN(cpTheta))) (Double.isNaN(theta) && Double.isNaN(cpTheta)))
@ -450,11 +444,11 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
updateFigures(); updateFigures();
fireChangeEvent(); fireChangeEvent();
} }
public double getCPMach() { public double getCPMach() {
return cpMach; return cpMach;
} }
public void setCPMach(double mach) { public void setCPMach(double mach) {
if (MathUtil.equals(mach, cpMach) || if (MathUtil.equals(mach, cpMach) ||
(Double.isNaN(mach) && Double.isNaN(cpMach))) (Double.isNaN(mach) && Double.isNaN(cpMach)))
@ -464,11 +458,11 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
updateFigures(); updateFigures();
fireChangeEvent(); fireChangeEvent();
} }
public double getCPRoll() { public double getCPRoll() {
return cpRoll; return cpRoll;
} }
public void setCPRoll(double roll) { public void setCPRoll(double roll) {
if (MathUtil.equals(roll, cpRoll) || if (MathUtil.equals(roll, cpRoll) ||
(Double.isNaN(roll) && Double.isNaN(cpRoll))) (Double.isNaN(roll) && Double.isNaN(cpRoll)))
@ -478,31 +472,31 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
updateFigures(); updateFigures();
fireChangeEvent(); fireChangeEvent();
} }
@Override @Override
public void addChangeListener(EventListener listener) { public void addChangeListener(EventListener listener) {
listeners.add(0, listener); listeners.add(0, listener);
} }
@Override @Override
public void removeChangeListener(EventListener listener) { public void removeChangeListener(EventListener listener) {
listeners.remove(listener); listeners.remove(listener);
} }
protected void fireChangeEvent() { protected void fireChangeEvent() {
EventObject e = new EventObject(this); EventObject e = new EventObject(this);
for (EventListener l : listeners) { for (EventListener l : listeners) {
if ( l instanceof StateChangeListener ) { if (l instanceof StateChangeListener) {
((StateChangeListener)l).stateChanged(e); ((StateChangeListener) l).stateChanged(e);
} }
} }
} }
/** /**
* Handle clicking on figure shapes. The functioning is the following: * Handle clicking on figure shapes. The functioning is the following:
* *
@ -513,7 +507,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
* the next component. Otherwise select the first component in the list. * the next component. Otherwise select the first component in the list.
*/ */
public static final int CYCLE_SELECTION_MODIFIER = InputEvent.SHIFT_DOWN_MASK; public static final int CYCLE_SELECTION_MODIFIER = InputEvent.SHIFT_DOWN_MASK;
private void handleMouseClick(MouseEvent event) { private void handleMouseClick(MouseEvent event) {
if (event.getButton() != MouseEvent.BUTTON1) if (event.getButton() != MouseEvent.BUTTON1)
return; return;
@ -521,20 +515,20 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
Point p1 = scrollPane.getViewport().getViewPosition(); Point p1 = scrollPane.getViewport().getViewPosition();
int x = p0.x + p1.x; int x = p0.x + p1.x;
int y = p0.y + p1.y; int y = p0.y + p1.y;
RocketComponent[] clicked = figure.getComponentsByPoint(x, y); RocketComponent[] clicked = figure.getComponentsByPoint(x, y);
handleComponentClick(clicked, event); handleComponentClick(clicked, event);
} }
private void handleComponentClick(RocketComponent[] clicked, MouseEvent event){ private void handleComponentClick(RocketComponent[] clicked, MouseEvent event) {
// If no component is clicked, do nothing // If no component is clicked, do nothing
if (clicked.length == 0){ if (clicked.length == 0) {
selectionModel.setSelectionPath(null); selectionModel.setSelectionPath(null);
return; return;
} }
// Check whether the currently selected component is in the clicked components. // Check whether the currently selected component is in the clicked components.
TreePath path = selectionModel.getSelectionPath(); TreePath path = selectionModel.getSelectionPath();
if (path != null) { if (path != null) {
@ -551,7 +545,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
} }
} }
} }
// Currently selected component not clicked // Currently selected component not clicked
if (path == null) { if (path == null) {
if (event.isShiftDown() && event.getClickCount() == 1 && clicked.length > 1) { if (event.isShiftDown() && event.getClickCount() == 1 && clicked.length > 1) {
@ -560,34 +554,34 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
path = ComponentTreeModel.makeTreePath(clicked[0]); path = ComponentTreeModel.makeTreePath(clicked[0]);
} }
} }
// Set selection and check for double-click // Set selection and check for double-click
selectionModel.setSelectionPath(path); selectionModel.setSelectionPath(path);
if (event.getClickCount() == 2) { if (event.getClickCount() == 2) {
RocketComponent component = (RocketComponent) path.getLastPathComponent(); RocketComponent component = (RocketComponent) path.getLastPathComponent();
ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this), ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),
document, component); document, component);
} }
} }
/** /**
* Updates the extra data included in the figure. Currently this includes * Updates the extra data included in the figure. Currently this includes
* the CP and CG carets. * the CP and CG carets.
*/ */
private WarningSet warnings = new WarningSet(); private WarningSet warnings = new WarningSet();
private void updateExtras() { private void updateExtras() {
Coordinate cp, cg; Coordinate cp, cg;
double cpx, cgx; double cpx, cgx;
// TODO: MEDIUM: User-definable conditions // TODO: MEDIUM: User-definable conditions
FlightConditions conditions = new FlightConditions(configuration); FlightConditions conditions = new FlightConditions(configuration);
warnings.clear(); warnings.clear();
if (!Double.isNaN(cpMach)) { if (!Double.isNaN(cpMach)) {
conditions.setMach(cpMach); conditions.setMach(cpMach);
extraText.setMach(cpMach); extraText.setMach(cpMach);
@ -595,20 +589,20 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
conditions.setMach(Application.getPreferences().getDefaultMach()); conditions.setMach(Application.getPreferences().getDefaultMach());
extraText.setMach(Application.getPreferences().getDefaultMach()); extraText.setMach(Application.getPreferences().getDefaultMach());
} }
if (!Double.isNaN(cpAOA)) { if (!Double.isNaN(cpAOA)) {
conditions.setAOA(cpAOA); conditions.setAOA(cpAOA);
} else { } else {
conditions.setAOA(0); conditions.setAOA(0);
} }
extraText.setAOA(cpAOA); extraText.setAOA(cpAOA);
if (!Double.isNaN(cpRoll)) { if (!Double.isNaN(cpRoll)) {
conditions.setRollRate(cpRoll); conditions.setRollRate(cpRoll);
} else { } else {
conditions.setRollRate(0); conditions.setRollRate(0);
} }
if (!Double.isNaN(cpTheta)) { if (!Double.isNaN(cpTheta)) {
conditions.setTheta(cpTheta); conditions.setTheta(cpTheta);
cp = aerodynamicCalculator.getCP(configuration, conditions, warnings); cp = aerodynamicCalculator.getCP(configuration, conditions, warnings);
@ -616,24 +610,24 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
cp = aerodynamicCalculator.getWorstCP(configuration, conditions, warnings); cp = aerodynamicCalculator.getWorstCP(configuration, conditions, warnings);
} }
extraText.setTheta(cpTheta); extraText.setTheta(cpTheta);
cg = massCalculator.getCG(configuration, MassCalcType.LAUNCH_MASS); cg = massCalculator.getCG(configuration, MassCalcType.LAUNCH_MASS);
// System.out.println("CG computed as "+cg+ " CP as "+cp); // System.out.println("CG computed as "+cg+ " CP as "+cp);
if (cp.weight > 0.000001) if (cp.weight > 0.000001)
cpx = cp.x; cpx = cp.x;
else else
cpx = Double.NaN; cpx = Double.NaN;
if (cg.weight > 0.000001) if (cg.weight > 0.000001)
cgx = cg.x; cgx = cg.x;
else else
cgx = Double.NaN; cgx = Double.NaN;
figure3d.setCG(cg); figure3d.setCG(cg);
figure3d.setCP(cp); figure3d.setCP(cp);
// Length bound is assumed to be tight // Length bound is assumed to be tight
double length = 0, diameter = 0; double length = 0, diameter = 0;
Collection<Coordinate> bounds = configuration.getBounds(); Collection<Coordinate> bounds = configuration.getBounds();
@ -647,7 +641,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
} }
length = maxX - minX; length = maxX - minX;
} }
for (RocketComponent c : configuration) { for (RocketComponent c : configuration) {
if (c instanceof SymmetricComponent) { if (c instanceof SymmetricComponent) {
double d1 = ((SymmetricComponent) c).getForeRadius() * 2; double d1 = ((SymmetricComponent) c).getForeRadius() * 2;
@ -655,31 +649,31 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
diameter = MathUtil.max(diameter, d1, d2); diameter = MathUtil.max(diameter, d1, d2);
} }
} }
extraText.setCG(cgx); extraText.setCG(cgx);
extraText.setCP(cpx); extraText.setCP(cpx);
extraText.setLength(length); extraText.setLength(length);
extraText.setDiameter(diameter); extraText.setDiameter(diameter);
extraText.setMass(cg.weight); extraText.setMass(cg.weight);
extraText.setWarnings(warnings); extraText.setWarnings(warnings);
if (figure.getType() == RocketFigure.TYPE_SIDE && length > 0) { if (figure.getType() == RocketFigure.TYPE_SIDE && length > 0) {
// TODO: LOW: Y-coordinate and rotation // TODO: LOW: Y-coordinate and rotation
extraCP.setPosition(cpx * RocketFigure.EXTRA_SCALE, 0); extraCP.setPosition(cpx * RocketFigure.EXTRA_SCALE, 0);
extraCG.setPosition(cgx * RocketFigure.EXTRA_SCALE, 0); extraCG.setPosition(cgx * RocketFigure.EXTRA_SCALE, 0);
} else { } else {
extraCP.setPosition(Double.NaN, Double.NaN); extraCP.setPosition(Double.NaN, Double.NaN);
extraCG.setPosition(Double.NaN, Double.NaN); extraCG.setPosition(Double.NaN, Double.NaN);
} }
//////// Flight simulation in background //////// Flight simulation in background
// Check whether to compute or not // Check whether to compute or not
if (!((SwingPreferences) Application.getPreferences()).computeFlightInBackground()) { if (!((SwingPreferences) Application.getPreferences()).computeFlightInBackground()) {
extraText.setFlightData(null); extraText.setFlightData(null);
@ -687,38 +681,38 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
stopBackgroundSimulation(); stopBackgroundSimulation();
return; return;
} }
// Check whether data is already up to date // Check whether data is already up to date
if (flightDataFunctionalID == configuration.getRocket().getFunctionalModID() && if (flightDataFunctionalID == configuration.getRocket().getFunctionalModID() &&
flightDataMotorID == configuration.getFlightConfigurationID()) { flightDataMotorID == configuration.getFlightConfigurationID()) {
return; return;
} }
flightDataFunctionalID = configuration.getRocket().getFunctionalModID(); flightDataFunctionalID = configuration.getRocket().getFunctionalModID();
flightDataMotorID = configuration.getFlightConfigurationID(); flightDataMotorID = configuration.getFlightConfigurationID();
// Stop previous computation (if any) // Stop previous computation (if any)
stopBackgroundSimulation(); stopBackgroundSimulation();
// Check that configuration has motors // Check that configuration has motors
if (!configuration.hasMotors()) { if (!configuration.hasMotors()) {
extraText.setFlightData(FlightData.NaN_DATA); extraText.setFlightData(FlightData.NaN_DATA);
extraText.setCalculatingData(false); extraText.setCalculatingData(false);
return; return;
} }
// Start calculation process // Start calculation process
extraText.setCalculatingData(true); extraText.setCalculatingData(true);
Rocket duplicate = (Rocket) configuration.getRocket().copy(); Rocket duplicate = (Rocket) configuration.getRocket().copy();
Simulation simulation = ((SwingPreferences)Application.getPreferences()).getBackgroundSimulation(duplicate); Simulation simulation = ((SwingPreferences) Application.getPreferences()).getBackgroundSimulation(duplicate);
simulation.getOptions().setMotorConfigurationID( simulation.getOptions().setMotorConfigurationID(
configuration.getFlightConfigurationID()); configuration.getFlightConfigurationID());
backgroundSimulationWorker = new BackgroundSimulationWorker(document, simulation); backgroundSimulationWorker = new BackgroundSimulationWorker(document, simulation);
backgroundSimulationExecutor.execute(backgroundSimulationWorker); backgroundSimulationExecutor.execute(backgroundSimulationWorker);
} }
/** /**
* Cancels the current background simulation worker, if any. * Cancels the current background simulation worker, if any.
*/ */
@ -728,26 +722,26 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
backgroundSimulationWorker = null; backgroundSimulationWorker = null;
} }
} }
/** /**
* A SimulationWorker that simulates the rocket flight in the background and * A SimulationWorker that simulates the rocket flight in the background and
* sets the results to the extra text when finished. The worker can be cancelled * sets the results to the extra text when finished. The worker can be cancelled
* if necessary. * if necessary.
*/ */
private class BackgroundSimulationWorker extends SimulationWorker { private class BackgroundSimulationWorker extends SimulationWorker {
private final CustomExpressionSimulationListener exprListener; private final CustomExpressionSimulationListener exprListener;
public BackgroundSimulationWorker(OpenRocketDocument doc, Simulation sim) { public BackgroundSimulationWorker(OpenRocketDocument doc, Simulation sim) {
super(sim); super(sim);
List<CustomExpression> exprs = doc.getCustomExpressions(); List<CustomExpression> exprs = doc.getCustomExpressions();
exprListener = new CustomExpressionSimulationListener(exprs); exprListener = new CustomExpressionSimulationListener(exprs);
} }
@Override @Override
protected FlightData doInBackground() { protected FlightData doInBackground() {
// Pause a little while to allow faster UI reaction // Pause a little while to allow faster UI reaction
try { try {
Thread.sleep(300); Thread.sleep(300);
@ -755,38 +749,38 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
} }
if (isCancelled() || backgroundSimulationWorker != this) if (isCancelled() || backgroundSimulationWorker != this)
return null; return null;
return super.doInBackground(); return super.doInBackground();
} }
@Override @Override
protected void simulationDone() { protected void simulationDone() {
// Do nothing if cancelled // Do nothing if cancelled
if (isCancelled() || backgroundSimulationWorker != this) if (isCancelled() || backgroundSimulationWorker != this)
return; return;
backgroundSimulationWorker = null; backgroundSimulationWorker = null;
extraText.setFlightData(simulation.getSimulatedData()); extraText.setFlightData(simulation.getSimulatedData());
extraText.setCalculatingData(false); extraText.setCalculatingData(false);
figure.repaint(); figure.repaint();
figure3d.repaint(); figure3d.repaint();
} }
@Override @Override
protected SimulationListener[] getExtraListeners() { protected SimulationListener[] getExtraListeners() {
return new SimulationListener[] { return new SimulationListener[] {
InterruptListener.INSTANCE, InterruptListener.INSTANCE,
ApogeeEndListener.INSTANCE, ApogeeEndListener.INSTANCE,
exprListener}; exprListener };
} }
@Override @Override
protected void simulationInterrupted(Throwable t) { protected void simulationInterrupted(Throwable t) {
// Do nothing on cancel, set N/A data otherwise // Do nothing on cancel, set N/A data otherwise
if (isCancelled() || backgroundSimulationWorker != this) // Double-check if (isCancelled() || backgroundSimulationWorker != this) // Double-check
return; return;
backgroundSimulationWorker = null; backgroundSimulationWorker = null;
extraText.setFlightData(FlightData.NaN_DATA); extraText.setFlightData(FlightData.NaN_DATA);
extraText.setCalculatingData(false); extraText.setCalculatingData(false);
@ -794,9 +788,9 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
figure3d.repaint(); figure3d.repaint();
} }
} }
/** /**
* Adds the extra data to the figure. Currently this includes the CP and CG carets. * Adds the extra data to the figure. Currently this includes the CP and CG carets.
*/ */
@ -805,21 +799,21 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
extraCP = new CPCaret(0, 0); extraCP = new CPCaret(0, 0);
extraText = new RocketInfo(configuration); extraText = new RocketInfo(configuration);
updateExtras(); updateExtras();
figure.clearRelativeExtra(); figure.clearRelativeExtra();
figure.addRelativeExtra(extraCP); figure.addRelativeExtra(extraCP);
figure.addRelativeExtra(extraCG); figure.addRelativeExtra(extraCG);
figure.addAbsoluteExtra(extraText); figure.addAbsoluteExtra(extraText);
figure3d.clearRelativeExtra(); figure3d.clearRelativeExtra();
//figure3d.addRelativeExtra(extraCP); //figure3d.addRelativeExtra(extraCP);
//figure3d.addRelativeExtra(extraCG); //figure3d.addRelativeExtra(extraCG);
figure3d.addAbsoluteExtra(extraText); figure3d.addAbsoluteExtra(extraText);
} }
/** /**
* Updates the selection in the FigureParameters and repaints the figure. * Updates the selection in the FigureParameters and repaints the figure.
* Ignores the event itself. * Ignores the event itself.
@ -832,17 +826,17 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
figure3d.setSelection(null); figure3d.setSelection(null);
return; return;
} }
RocketComponent[] components = new RocketComponent[paths.length]; RocketComponent[] components = new RocketComponent[paths.length];
for (int i = 0; i < paths.length; i++) for (int i = 0; i < paths.length; i++)
components[i] = (RocketComponent) paths[i].getLastPathComponent(); components[i] = (RocketComponent) paths[i].getLastPathComponent();
figure.setSelection(components); figure.setSelection(components);
figure3d.setSelection(components); figure3d.setSelection(components);
} }
/** /**
* An <code>Action</code> that shows whether the figure type is the type * An <code>Action</code> that shows whether the figure type is the type
* given in the constructor. * given in the constructor.
@ -852,13 +846,13 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
private class FigureTypeAction extends AbstractAction implements StateChangeListener { private class FigureTypeAction extends AbstractAction implements StateChangeListener {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final int type; private final int type;
public FigureTypeAction(int type) { public FigureTypeAction(int type) {
this.type = type; this.type = type;
stateChanged(null); stateChanged(null);
figure.addChangeListener(this); figure.addChangeListener(this);
} }
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean state = (Boolean) getValue(Action.SELECTED_KEY); boolean state = (Boolean) getValue(Action.SELECTED_KEY);
@ -870,11 +864,11 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
} }
stateChanged(null); stateChanged(null);
} }
@Override @Override
public void stateChanged(EventObject e) { public void stateChanged(EventObject e) {
putValue(Action.SELECTED_KEY, figure.getType() == type && !is3d); putValue(Action.SELECTED_KEY, figure.getType() == type && !is3d);
} }
} }
} }