2 * PositionDependentHandler.java
4 package net.sf.openrocket.file.rocksim.importt;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
8 import net.sf.openrocket.file.rocksim.RocksimLocationMode;
9 import net.sf.openrocket.rocketcomponent.RocketComponent;
10 import org.xml.sax.SAXException;
12 import java.util.HashMap;
15 * An abstract base class that handles position dependencies for all lower level components that
18 * @param <C> the specific position dependent RocketComponent subtype for which the concrete handler can create
20 public abstract class PositionDependentHandler<C extends RocketComponent> extends BaseHandler<C> {
22 /** Temporary position value. */
23 private Double positionValue = 0d;
25 /** Temporary position. */
26 private RocketComponent.Position position = RocketComponent.Position.TOP;
32 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
34 super.closeElement(element, attributes, content, warnings);
35 if (RocksimCommonConstants.XB.equals(element)) {
36 positionValue = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH;
38 if (RocksimCommonConstants.LOCATION_MODE.equals(element)) {
39 position = RocksimLocationMode.fromCode(Integer.parseInt(
40 content)).asOpenRocket();
45 * This method sets the position information onto the component. Rocksim splits the location/position
46 * information into two disparate data elements. Both pieces of data are necessary to map into OpenRocket's
49 * @param element the element name
50 * @param attributes the attributes
51 * @param content the content of the element
52 * @param warnings the warning set to store warnings in.
53 * @throws org.xml.sax.SAXException not thrown
56 public void endHandler(String element, HashMap<String, String> attributes,
57 String content, WarningSet warnings) throws SAXException {
58 super.endHandler(element, attributes, content, warnings);
59 setRelativePosition(position);
60 setLocation(getComponent(), position, positionValue);
64 * Set the relative position onto the component. This cannot be done directly because setRelativePosition is not
65 * public in all components.
67 * @param position the OpenRocket position
69 protected abstract void setRelativePosition(RocketComponent.Position position);
72 * Set the position of a component.
74 * @param component the component
75 * @param position the relative position
76 * @param location the actual position value
78 public static void setLocation(RocketComponent component, RocketComponent.Position position, double location) {
79 if (position.equals(RocketComponent.Position.BOTTOM)) {
80 component.setPositionValue(-1d * location);
83 component.setPositionValue(location);