committed Doug's Rocksim loader
[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 public abstract class PositionDependentHandler<C extends RocketComponent> extends BaseHandler<C> {
17
18     /** Temporary position value. */
19     private Double positionValue;
20     
21     /** Temporary position. */
22     private RocketComponent.Position position;
23
24     @Override
25     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
26             throws SAXException {
27         super.closeElement(element, attributes, content, warnings);
28         if ("Xb".equals(element)) {
29             positionValue = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH;
30         }
31         if ("LocationMode".equals(element)) {
32             position = RocksimLocationMode.fromCode(Integer.parseInt(
33                     content)).asOpenRocket();
34         }
35     }
36     
37     /**
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
40      * position model.
41      * 
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
47      */
48     @Override
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);
54     }
55
56     /**
57      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
58      * public in all components.
59      * 
60      * @param position  the OpenRocket position
61      */
62     protected abstract void setRelativePosition(RocketComponent.Position position);
63     
64     /**
65      * Set the position of a component.
66      * 
67      * @param component  the component
68      * @param position   the relative position
69      * @param location   the actual position value
70      */
71     public static void setLocation(RocketComponent component, RocketComponent.Position position, double location) {
72         if (position.equals(RocketComponent.Position.BOTTOM)) {
73             component.setPositionValue(-1d * location);
74         }
75         else {
76             component.setPositionValue(location);
77         }
78     }
79     
80 }