package net.sf.openrocket.gui.main;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Toolkit;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+import javax.swing.Action;
+import javax.swing.BorderFactory;
+import javax.swing.InputMap;
+import javax.swing.JButton;
+import javax.swing.JComponent;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JPopupMenu;
+import javax.swing.JScrollPane;
+import javax.swing.JSpinner;
+import javax.swing.JSplitPane;
+import javax.swing.JTabbedPane;
+import javax.swing.JTextField;
+import javax.swing.KeyStroke;
+import javax.swing.ListSelectionModel;
+import javax.swing.ScrollPaneConstants;
+import javax.swing.SwingUtilities;
+import javax.swing.border.BevelBorder;
+import javax.swing.border.TitledBorder;
+import javax.swing.event.TreeSelectionEvent;
+import javax.swing.event.TreeSelectionListener;
+import javax.swing.tree.DefaultTreeSelectionModel;
+import javax.swing.tree.TreePath;
+import javax.swing.tree.TreeSelectionModel;
+
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.util.Reflection;
import net.sf.openrocket.util.TestRockets;
-import javax.swing.*;
-import javax.swing.border.BevelBorder;
-import javax.swing.border.TitledBorder;
-import javax.swing.event.TreeSelectionEvent;
-import javax.swing.event.TreeSelectionListener;
-import javax.swing.tree.DefaultTreeSelectionModel;
-import javax.swing.tree.TreePath;
-import javax.swing.tree.TreeSelectionModel;
-import java.awt.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.KeyEvent;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.net.URLDecoder;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.ExecutionException;
-
public class BasicFrame extends JFrame {
private static final LogHelper log = Application.getLogger();
public static final int COMPONENT_TAB = 0;
public static final int SIMULATION_TAB = 1;
-
+
/**
* List of currently open frames. When the list goes empty
* it is time to exit the application.
*/
private static final ArrayList<BasicFrame> frames = new ArrayList<BasicFrame>();
-
+
/**
* Whether "New" and "Open" should replace this frame.
* Should be set to false on the first rocket modification.
*/
private boolean replaceable = false;
-
-
+
+
private final OpenRocketDocument document;
private final Rocket rocket;
private final RocketActions actions;
-
+
/**
* Sole constructor. Creates a new frame based on the supplied document
* and adds it to the current frames list.
this.rocket = document.getRocket();
this.rocket.getDefaultConfiguration().setAllStages();
-
+
// Set replaceable flag to false at first modification
rocket.addComponentChangeListener(new ComponentChangeListener() {
@Override
}
});
-
+
// Create the component tree selection model that will be used
componentSelectionModel = new DefaultTreeSelectionModel();
componentSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
selectionModel.attachComponentTreeSelectionModel(componentSelectionModel);
selectionModel.attachSimulationListSelectionModel(simulationSelectionModel);
-
+
actions = new RocketActions(document, selectionModel, this);
-
+
log.debug("Constructing the BasicFrame UI");
// The main vertical split pane
vertical.setResizeWeight(0.5);
this.add(vertical);
-
+
// The top tabbed pane
tabbedPane = new JTabbedPane();
//// Rocket design
vertical.setTopComponent(tabbedPane);
-
-
+
+
// Bottom segment, rocket figure
rocketpanel = new RocketPanel(document);
rocketpanel.setSelectionModel(tree.getSelectionModel());
-
+
createMenu();
-
+
rocket.addComponentChangeListener(new ComponentChangeListener() {
@Override
public void componentChanged(ComponentChangeEvent e) {
setTitle();
this.pack();
-
+
// Set initial window size
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
size.width = size.width * 9 / 10;
JSplitPane horizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
horizontal.setResizeWeight(0.5);
-
+
// Upper-left segment, component tree
JPanel panel = new JPanel(new MigLayout("fill, flowy", "", "[grow]"));
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK), null);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK), null);
-
-
+
+
// Double-click opens config dialog
MouseListener ml = new MouseAdapter() {
@Override
JScrollPane scroll = new JScrollPane(tree);
panel.add(scroll, "spany, grow, wrap");
-
+
// Buttons
JButton button = new JButton(actions.getMoveUpAction());
panel.add(button, "sizegroup buttons, aligny 65%");
horizontal.setLeftComponent(panel);
-
+
// Upper-right segment, component addition buttons
panel = new JPanel(new MigLayout("fill, insets 0", "[0::]"));
}
-
+
/**
* Return the currently selected rocket component, or <code>null</code> if none selected.
*/
});
menu.add(item);
-
+
menu.addSeparator();
//// Close
});
menu.add(item);
-
-
+
+
//// Edit
menu = new JMenu(trans.get("main.menu.edit"));
menu.setMnemonic(KeyEvent.VK_E);
menu.getAccessibleContext().setAccessibleDescription(trans.get("BasicFrame.menu.Rocketedt"));
menubar.add(menu);
-
+
Action action = UndoRedoAction.newUndoAction(document);
item = new JMenuItem(action);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.edit.undo.desc"));
menu.add(item);
-
+
action = UndoRedoAction.newRedoAction(document);
item = new JMenuItem(action);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
menu.addSeparator();
-
+
item = new JMenuItem(actions.getCutAction());
menu.add(item);
menu.addSeparator();
-
-
+
+
item = new JMenuItem(trans.get("main.menu.edit.resize"));
item.setIcon(Icons.EDIT_SCALE);
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.edit.resize.desc"));
});
menu.add(item);
-
-
+
+
//// Preferences
item = new JMenuItem(trans.get("main.menu.edit.preferences"));
item.setIcon(Icons.PREFERENCES);
});
menu.add(item);
-
-
-
+
+
+
//// Analyze
menu = new JMenu(trans.get("main.menu.analyze"));
menu.setMnemonic(KeyEvent.VK_A);
});
menu.add(item);
-
+
item = new JMenuItem(trans.get("main.menu.analyze.optimization"), KeyEvent.VK_O);
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.analyze.optimization.desc"));
item.addActionListener(new ActionListener() {
});
menu.add(item);
-
-
+
+
//// Debug
// (shown if openrocket.debug.menu is defined)
if (System.getProperty("openrocket.debug.menu") != null) {
menubar.add(makeDebugMenu());
}
-
-
+
+
//// Help
menu = new JMenu(trans.get("main.menu.help"));
menu.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.desc"));
menubar.add(menu);
-
+
// Guided tours
item = new JMenuItem(trans.get("main.menu.help.tours"), KeyEvent.VK_L);
- // TODO: Icon
+ item.setIcon(Icons.HELP_TOURS);
item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.tours.desc"));
item.addActionListener(new ActionListener() {
@Override
menu.addSeparator();
-
- //// License
- item = new JMenuItem(trans.get("main.menu.help.license"), KeyEvent.VK_L);
- item.setIcon(Icons.HELP_LICENSE);
- item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.license.desc"));
- item.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- log.user("License selected");
- new LicenseDialog(BasicFrame.this).setVisible(true);
- }
- });
- menu.add(item);
-
- menu.addSeparator();
-
//// Bug report
item = new JMenuItem(trans.get("main.menu.help.bugReport"), KeyEvent.VK_B);
item.setIcon(Icons.HELP_BUG_REPORT);
menu.addSeparator();
+
+ //// License
+ item = new JMenuItem(trans.get("main.menu.help.license"), KeyEvent.VK_L);
+ item.setIcon(Icons.HELP_LICENSE);
+ item.getAccessibleContext().setAccessibleDescription(trans.get("main.menu.help.license.desc"));
+ item.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ log.user("License selected");
+ new LicenseDialog(BasicFrame.this).setVisible(true);
+ }
+ });
+ menu.add(item);
+
+
//// About
item = new JMenuItem(trans.get("main.menu.help.about"), KeyEvent.VK_A);
item.setIcon(Icons.HELP_ABOUT);
});
menu.add(item);
-
+
this.setJMenuBar(menubar);
}
/*
* This menu is intentionally left untranslated.
*/
-
+
//// Debug menu
menu = new JMenu("Debug");
//// OpenRocket debugging tasks
int sel = JOptionPane.showOptionDialog(BasicFrame.this, new Object[] {
"Input text key to generate random rocket:",
field
- }, "Generate random test rocket", JOptionPane.DEFAULT_OPTION,
+ }, "Generate random test rocket", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new Object[] {
"Random", "OK"
- }, "OK");
+ }, "OK");
Rocket r;
if (sel == 0) {
});
menu.add(item);
-
-
+
+
item = new JMenuItem("Create 'Iso-Haisu'");
item.addActionListener(new ActionListener() {
@Override
});
menu.add(item);
-
+
item = new JMenuItem("Create 'Big Blue'");
item.addActionListener(new ActionListener() {
@Override
menu.addSeparator();
-
+
item = new JMenuItem("Memory statistics");
item.addActionListener(new ActionListener() {
@Override
stats[2] = String.format(" Used memory: %.1f MB (%.0f%%)", used / 1024.0 / 1024.0, 100.0 * used / max);
stats[3] = String.format(" Free memory: %.1f MB (%.0f%%)", free / 1024.0 / 1024.0, 100.0 * free / max);
-
+
DetailDialog.showDetailedMessageDialog(BasicFrame.this, stats, sb.toString(),
"Memory statistics", JOptionPane.INFORMATION_MESSAGE);
}
});
menu.add(item);
-
+
menu.addSeparator();
//// Exception here
});
menu.add(item);
-
+
menu.addSeparator();
-
+
item = new JMenuItem("Test popup");
item.addActionListener(new ActionListener() {
@Override
});
menu.add(item);
-
-
-
+
+
+
return menu;
}
}
-
+
private void openAction() {
JFileChooser chooser = new JFileChooser();
filename = filename.substring(filename.lastIndexOf('/') + 1);
}
-
+
// Open the file
log.info("Opening file from url=" + url + " filename=" + filename);
try {
return false;
}
-
+
// Handle the document
OpenRocketDocument doc = null;
try {
throw new BugException("Document loader returned null");
}
-
+
// Show warnings
WarningSet warnings = worker.getRocketLoader().getWarnings();
if (!warnings.isEmpty()) {
trans.get("BasicFrame.WarningDialog.title"), warnings);
}
-
+
// Set document state
doc.setFile(file);
doc.setSaved(true);
-
+
// Open the frame
log.debug("Opening new frame with the document");
BasicFrame frame = new BasicFrame(doc);
return true;
}
-
- /**
- * "Save" action. If the design is new, then this is identical to "Save As", with a default file filter for .ork.
- * If the rocket being edited previously was opened from a .ork file, then it will be saved immediately to the same
- * file. But clicking on 'Save' for an existing design file with a .rkt will bring up a confirmation dialog because
- * it's potentially a destructive write (loss of some fidelity if it's truly an original Rocksim generated file).
- *
- * @return true if the file was saved, false otherwise
- */
+
+ /**
+ * "Save" action. If the design is new, then this is identical to "Save As", with a default file filter for .ork.
+ * If the rocket being edited previously was opened from a .ork file, then it will be saved immediately to the same
+ * file. But clicking on 'Save' for an existing design file with a .rkt will bring up a confirmation dialog because
+ * it's potentially a destructive write (loss of some fidelity if it's truly an original Rocksim generated file).
+ *
+ * @return true if the file was saved, false otherwise
+ */
private boolean saveAction() {
File file = document.getFile();
if (file == null) {
log.info("Saving document to " + file);
if (FileHelper.ROCKSIM_DESIGN_FILTER.accept(file)) {
- return saveAsRocksim(file);
+ return saveAsRocksim(file);
}
return saveAs(file);
}
-
- /**
- * "Save As" action.
- *
- * Never should a .rkt file contain an OpenRocket content, or an .ork file contain a Rocksim design. Regardless of
- * what extension the user has chosen, it would violate the Principle of Least Astonishment to do otherwise
- * (and we want to make doing the wrong thing really hard to do). So always force the appropriate extension.
- *
- * This can result in some odd looking filenames (MyDesign.rkt.ork, MyDesign.rkt.ork.rkt, etc.) if the user is
- * not paying attention, but the user can control that by modifying the filename in the dialog.
- *
- * @return true if the file was saved, false otherwise
- */
+
+ /**
+ * "Save As" action.
+ *
+ * Never should a .rkt file contain an OpenRocket content, or an .ork file contain a Rocksim design. Regardless of
+ * what extension the user has chosen, it would violate the Principle of Least Astonishment to do otherwise
+ * (and we want to make doing the wrong thing really hard to do). So always force the appropriate extension.
+ *
+ * This can result in some odd looking filenames (MyDesign.rkt.ork, MyDesign.rkt.ork.rkt, etc.) if the user is
+ * not paying attention, but the user can control that by modifying the filename in the dialog.
+ *
+ * @return true if the file was saved, false otherwise
+ */
private boolean saveAsAction() {
File file = null;
-
+
StorageOptionChooser storageChooser =
new StorageOptionChooser(document, document.getDefaultStorageOptions());
final JFileChooser chooser = new JFileChooser();
- chooser.addChoosableFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
- chooser.addChoosableFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
-
- //Force the file filter to match the file extension that was opened. Will default to OR if the file is null.
- if (FileHelper.ROCKSIM_DESIGN_FILTER.accept(document.getFile())) {
- chooser.setFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
- }
- else {
- chooser.setFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
- }
+ chooser.addChoosableFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
+ chooser.addChoosableFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
+
+ //Force the file filter to match the file extension that was opened. Will default to OR if the file is null.
+ if (FileHelper.ROCKSIM_DESIGN_FILTER.accept(document.getFile())) {
+ chooser.setFileFilter(FileHelper.ROCKSIM_DESIGN_FILTER);
+ }
+ else {
+ chooser.setFileFilter(FileHelper.OPENROCKET_DESIGN_FILTER);
+ }
chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory());
chooser.setAccessory(storageChooser);
if (document.getFile() != null) {
chooser.setSelectedFile(document.getFile());
- }
-
+ }
+
int option = chooser.showSaveDialog(BasicFrame.this);
if (option != JFileChooser.APPROVE_OPTION) {
log.user("User decided not to save, option=" + option);
((SwingPreferences) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory());
storageChooser.storeOptions(document.getDefaultStorageOptions());
-
- if (chooser.getFileFilter().equals(FileHelper.ROCKSIM_DESIGN_FILTER)) {
- return saveAsRocksim(file);
- }
- else {
- file = FileHelper.forceExtension(file, "ork");
- return FileHelper.confirmWrite(file, this) && saveAs(file);
- }
+
+ if (chooser.getFileFilter().equals(FileHelper.ROCKSIM_DESIGN_FILTER)) {
+ return saveAsRocksim(file);
+ }
+ else {
+ file = FileHelper.forceExtension(file, "ork");
+ return FileHelper.confirmWrite(file, this) && saveAs(file);
+ }
}
-
- /**
- * Perform the writing of the design to the given file in Rocksim format.
- *
- * @param file the chosen file
- *
- * @return true if the file was written
- */
- private boolean saveAsRocksim(File file) {
- file = FileHelper.forceExtension(file, "rkt");
- if (!FileHelper.confirmWrite(file, this)) {
- return false;
- }
-
- try {
- new RocksimSaver().save(file, document);
- return true;
- } catch (IOException e) {
- return false;
- }
- }
-
- /**
- * Perform the writing of the design to the given file in OpenRocket format.
- *
- * @param file the chosen file
- *
- * @return true if the file was written
- */
- private boolean saveAs(File file) {
+
+ /**
+ * Perform the writing of the design to the given file in Rocksim format.
+ *
+ * @param file the chosen file
+ *
+ * @return true if the file was written
+ */
+ private boolean saveAsRocksim(File file) {
+ file = FileHelper.forceExtension(file, "rkt");
+ if (!FileHelper.confirmWrite(file, this)) {
+ return false;
+ }
+
+ try {
+ new RocksimSaver().save(file, document);
+ return true;
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Perform the writing of the design to the given file in OpenRocket format.
+ *
+ * @param file the chosen file
+ *
+ * @return true if the file was written
+ */
+ private boolean saveAs(File file) {
log.info("Saving document as " + file);
boolean saved = false;
return false;
}
-
+
SaveFileWorker worker = new SaveFileWorker(document, file, ROCKET_SAVER);
if (!SwingWorkerDialog.runWorker(this, "Saving file",
}
-
+
/**
*
*/
}
-
+
/**
* Find a currently open BasicFrame containing the specified rocket. This method
* can be used to map a Rocket to a BasicFrame from GUI methods.
*/
private ArrayList<RocketComponent> children = new ArrayList<RocketComponent>();
-
+
//////// Parameters common to all components:
/**
*/
protected double position = 0;
-
+
// Color of the component, null means to use the default color
private Color color = null;
private LineStyle lineStyle = null;
-
+
// Override mass/CG
private double overrideMass = 0;
private boolean massOverriden = false;
private boolean overrideSubcomponents = false;
-
+
// User-given name of the component
private String name = null;
// Preset component this component is based upon
private ComponentPreset presetComponent = null;
-
+
/**
* Used to invalidate the component after calling {@link #copyFrom(RocketComponent)}.
*/
//// NOTE !!! All fields must be copied in the method copyFrom()! ////
-
-
+
+
/**
* Default constructor. Sets the name of the component to the component's static name
* and the relative position of the component.
//////////// Methods that must be implemented ////////////
-
+
/**
* Static component name. The name may not vary of the parameters, it must be static.
*/
*/
public abstract Coordinate getComponentCG(); // CG of non-overridden component
-
+
/**
* Return the longitudinal (around the y- or z-axis) unitary moment of inertia.
* The unitary moment of inertia is the moment of inertia with the assumption that
}
-
+
/**
* Return a collection of bounding coordinates. The coordinates must be such that
* the component is fully enclosed in their convex hull.
public abstract boolean isMassive();
-
-
-
+
+
+
//////////// Methods that may be overridden ////////////
-
+
/**
* Shift the coordinates in the array corresponding to radial movement. A component
* that has a radial position must shift the coordinates in this array suitably.
}
-
-
+
+
/**
* Return the user-provided name of the component, or the component base
* name if the user-provided name is empty. This can be used in the UI.
}
-
+
/**
* Make a deep copy of the rocket component tree structure from this component
* downwards while maintaining the component ID's. The purpose of this method is
////////////// Methods that may not be overridden ////////////
-
-
+
+
////////// Common parameter setting/getting //////////
/**
}
-
-
+
+
/**
* Get the current override mass. The mass is not necessarily in use
* at the moment.
}
-
-
-
+
+
+
/**
* Return the current override CG. The CG is not necessarily overridden.
*
}
-
+
/**
* Return whether the mass and/or CG override overrides all subcomponent values
* as well. The default implementation is a normal getter/setter implementation,
}
-
-
+
+
/**
* Get the user-defined name of the component.
*/
}
-
+
/**
* Return the preset component that this component is based upon.
*
if (preset.getComponentClass() != this.getClass()) {
throw new IllegalArgumentException("Attempting to load preset of type " + preset.getComponentClass()
- + " into component of type " + this.getClass());
+ + " into component of type " + this.getClass());
}
RocketComponent root = getRoot();
}
-
+
/**
* Returns the unique ID of the component.
*
}
-
-
+
+
/**
* Get the characteristic length of the component, for example the length of a body tube
* of the length of the root chord of a fin. This is used in positioning the component
this.relativePosition = position;
fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
}
-
-
- /**
- * Determine position relative to given position argument. Note: This is a side-effect free method. No state
- * is modified.
- *
- * @param thePosition the relative position to be used as the basis for the computation
- * @param relativeTo the position is computed relative the the given component
- *
- * @return double position of the component relative to the parent, with respect to <code>position</code>
- */
- public double asPositionValue (Position thePosition, RocketComponent relativeTo) {
- double result = this.position;
- if (relativeTo != null) {
- double thisPos = this.toRelative(Coordinate.NUL, relativeTo)[0].x;
-
- switch (thePosition) {
- case ABSOLUTE:
- result = this.toAbsolute(Coordinate.NUL)[0].x;
- break;
- case TOP:
- result = thisPos;
- break;
- case MIDDLE:
- result = thisPos - (relativeTo.length - this.length) / 2;
- break;
- case BOTTOM:
- result = thisPos - (relativeTo.length - this.length);
- break;
- default:
- throw new BugException("Unknown position type: " + thePosition);
- }
- }
- return result;
- }
-
+
+
+ /**
+ * Determine position relative to given position argument. Note: This is a side-effect free method. No state
+ * is modified.
+ *
+ * @param thePosition the relative position to be used as the basis for the computation
+ * @param relativeTo the position is computed relative the the given component
+ *
+ * @return double position of the component relative to the parent, with respect to <code>position</code>
+ */
+ public double asPositionValue(Position thePosition, RocketComponent relativeTo) {
+ double result = this.position;
+ if (relativeTo != null) {
+ double thisPos = this.toRelative(Coordinate.NUL, relativeTo)[0].x;
+
+ switch (thePosition) {
+ case ABSOLUTE:
+ result = this.toAbsolute(Coordinate.NUL)[0].x;
+ break;
+ case TOP:
+ result = thisPos;
+ break;
+ case MIDDLE:
+ result = thisPos - (relativeTo.length - this.length) / 2;
+ break;
+ case BOTTOM:
+ result = thisPos - (relativeTo.length - this.length);
+ break;
+ default:
+ throw new BugException("Unknown position type: " + thePosition);
+ }
+ }
+ return result;
+ }
+
/**
* Get the position value of the component. The exact meaning of the value is
* dependent on the current relative positioning.
}
-
+
/////////// Coordinate changes ///////////
/**
}
-
+
/////////// Total mass and CG calculation ////////////
/**
}
-
+
/////////// Children handling ///////////
-
+
/**
* Adds a child to the rocket component tree. The component is added to the end
* of the component's child list. This is a helper method that calls
*/
public void addChild(RocketComponent component, int index) {
checkState();
+
if (component.parent != null) {
throw new IllegalArgumentException("component " + component.getComponentName() +
" is already in a tree");
}
+
+ // Ensure that the no loops are created in component tree [A -> X -> Y -> B, B.addChild(A)]
+ if (this.getRoot().equals(component)) {
+ throw new IllegalStateException("Component " + component.getComponentName() +
+ " is a parent of " + this.getComponentName() + ", attempting to create cycle in tree.");
+ }
+
if (!isCompatible(component)) {
throw new IllegalStateException("Component " + component.getComponentName() +
" not currently compatible with component " + getComponentName());
fireAddRemoveEvent(component);
}
-
/**
* Removes a child from the rocket component tree.
*
}
-
-
+
+
/**
* Move a child to another position.
*
}
-
+
/**
* Returns an iterator that iterates over all children and sub-children.
* <p>
}
-
-
-
+
+
+
/**
* Compare component equality based on the ID of this component. Only the
* ID and class type is used for a basis of comparison.
}
-
+
@Override
public int hashCode() {
return id.hashCode();
}
-
+
//////////// Helper methods for subclasses
-
-
-
+
+
+
/**
* Helper method to add rotationally symmetric bounds at the specified coordinates.
* The X-axis value is <code>x</code> and the radius at the specified position is
protected static final double ringMass(double outerRadius, double innerRadius,
double length, double density) {
return Math.PI * (MathUtil.pow2(outerRadius) - MathUtil.pow2(innerRadius)) *
- length * density;
+ length * density;
}
protected static final double ringLongitudinalUnitInertia(double outerRadius,
}
-
+
//////////// OTHER
-
+
/**
* Loads the RocketComponent fields from the given component. This method is meant
* for in-place replacement of a component. It is used with the undo/redo