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