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 * @param <C> the specific position dependent RocketComponent subtype for which the concrete handler can create
18 public abstract class PositionDependentHandler<C extends RocketComponent> extends BaseHandler<C> {
20 /** Temporary position value. */
21 private Double positionValue;
23 /** Temporary position. */
24 private RocketComponent.Position position;
30 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
32 super.closeElement(element, attributes, content, warnings);
33 if ("Xb".equals(element)) {
34 positionValue = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH;
36 if ("LocationMode".equals(element)) {
37 position = RocksimLocationMode.fromCode(Integer.parseInt(
38 content)).asOpenRocket();
43 * This method sets the position information onto the component. Rocksim splits the location/position
44 * information into two disparate data elements. Both pieces of data are necessary to map into OpenRocket's
47 * @param element the element name
48 * @param attributes the attributes
49 * @param content the content of the element
50 * @param warnings the warning set to store warnings in.
51 * @throws org.xml.sax.SAXException not thrown
54 public void endHandler(String element, HashMap<String, String> attributes,
55 String content, WarningSet warnings) throws SAXException {
56 super.endHandler(element, attributes, content, warnings);
57 setRelativePosition(position);
58 setLocation(getComponent(), position, positionValue);
62 * Set the relative position onto the component. This cannot be done directly because setRelativePosition is not
63 * public in all components.
65 * @param position the OpenRocket position
67 protected abstract void setRelativePosition(RocketComponent.Position position);
70 * Set the position of a component.
72 * @param component the component
73 * @param position the relative position
74 * @param location the actual position value
76 public static void setLocation(RocketComponent component, RocketComponent.Position position, double location) {
77 if (position.equals(RocketComponent.Position.BOTTOM)) {
78 component.setPositionValue(-1d * location);
81 component.setPositionValue(location);