2 * PositionDependentHandler.java
4 package net.sf.openrocket.file.rocksim;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.rocketcomponent.RocketComponent;
8 import org.xml.sax.SAXException;
10 import java.util.HashMap;
13 * An abstract base class that handles position dependencies for all lower level components that
16 public abstract class PositionDependentHandler<C extends RocketComponent> extends BaseHandler<C> {
18 /** Temporary position value. */
19 private Double positionValue;
21 /** Temporary position. */
22 private RocketComponent.Position position;
25 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
27 super.closeElement(element, attributes, content, warnings);
28 if ("Xb".equals(element)) {
29 positionValue = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH;
31 if ("LocationMode".equals(element)) {
32 position = RocksimLocationMode.fromCode(Integer.parseInt(
33 content)).asOpenRocket();
38 * This method sets the position information onto the component. Rocksim splits the location/position
39 * information into two disparate data elements. Both pieces of data are necessary to map into OpenRocket's
42 * @param element the element name
43 * @param attributes the attributes
44 * @param content the content of the element
45 * @param warnings the warning set to store warnings in.
46 * @throws org.xml.sax.SAXException not thrown
49 public void endHandler(String element, HashMap<String, String> attributes,
50 String content, WarningSet warnings) throws SAXException {
51 super.endHandler(element, attributes, content, warnings);
52 setRelativePosition(position);
53 setLocation(getComponent(), position, positionValue);
57 * Set the relative position onto the component. This cannot be done directly because setRelativePosition is not
58 * public in all components.
60 * @param position the OpenRocket position
62 protected abstract void setRelativePosition(RocketComponent.Position position);
65 * Set the position of a component.
67 * @param component the component
68 * @param position the relative position
69 * @param location the actual position value
71 public static void setLocation(RocketComponent component, RocketComponent.Position position, double location) {
72 if (position.equals(RocketComponent.Position.BOTTOM)) {
73 component.setPositionValue(-1d * location);
76 component.setPositionValue(location);