DGP - added DensityType parsing for recovery devices
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / PositionDependentHandler.java
1 /*
2  * PositionDependentHandler.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.rocketcomponent.RocketComponent;
8 import org.xml.sax.SAXException;
9
10 import java.util.HashMap;
11
12 /**
13  * An abstract base class that handles position dependencies for all lower level components that
14  * are position aware.
15  *
16  * @param <C>   the specific position dependent RocketComponent subtype for which the concrete handler can create
17  */
18 public abstract class PositionDependentHandler<C extends RocketComponent> extends BaseHandler<C> {
19
20     /** Temporary position value. */
21     private Double positionValue;
22
23     /** Temporary position. */
24     private RocketComponent.Position position;
25
26     /**
27      * {@inheritDoc}
28      */
29     @Override
30     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
31             throws SAXException {
32         super.closeElement(element, attributes, content, warnings);
33         if ("Xb".equals(element)) {
34             positionValue = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH;
35         }
36         if ("LocationMode".equals(element)) {
37             position = RocksimLocationMode.fromCode(Integer.parseInt(
38                     content)).asOpenRocket();
39         }
40     }
41
42     /**
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
45      * position model.
46      *
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
52      */
53     @Override
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);
59     }
60
61     /**
62      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not
63      * public in all components.
64      *
65      * @param position  the OpenRocket position
66      */
67     protected abstract void setRelativePosition(RocketComponent.Position position);
68
69     /**
70      * Set the position of a component.
71      *
72      * @param component  the component
73      * @param position   the relative position
74      * @param location   the actual position value
75      */
76     public static void setLocation(RocketComponent component, RocketComponent.Position position, double location) {
77         if (position.equals(RocketComponent.Position.BOTTOM)) {
78             component.setPositionValue(-1d * location);
79         }
80         else {
81             component.setPositionValue(location);
82         }
83     }
84
85 }