DGP - added isCompatible checks for all Rocksim components
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / MassObjectHandler.java
1 /*
2  * MassObjectHandler.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.simplesax.ElementHandler;
8 import net.sf.openrocket.file.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.MassComponent;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
13
14 import java.util.HashMap;
15
16 /**
17  * A SAX handler for Rocksim's MassObject XML type.
18  */
19 class MassObjectHandler extends PositionDependentHandler<MassComponent> {
20
21     /** 
22      * The Rocksim Mass length fudge factor.  Rocksim completely exaggerates the length of a mass object to the point
23      * that it looks ridiculous in OpenRocket.  This fudge factor is here merely to get the typical mass object to
24      * render in the OpenRocket UI with it's bounds mostly inside it's parent.  The odd thing about it is that 
25      * Rocksim does not expose the length of a mass object in the UI and actually treats mass objects as point objects -
26      * not 3 or even 2 dimensional.
27      */
28     public static final int MASS_LEN_FUDGE_FACTOR = 100;
29
30     /**
31      * The OpenRocket MassComponent - counterpart to the RS MassObject.
32      */
33     private final MassComponent mass;
34
35     /**
36      * Constructor.
37      *l
38      * @param c the parent component
39      * @param warnings  the warning set
40      * 
41      * @throws IllegalArgumentException  thrown if <code>c</code> is null
42      */
43     public MassObjectHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
44         if (c == null) {
45             throw new IllegalArgumentException("The parent component of a mass component may not be null.");
46         }
47         mass = new MassComponent();
48         if (isCompatible(c, MassComponent.class, warnings)) {
49             c.addChild(mass);
50         }
51     }
52
53     @Override
54     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
55         return PlainTextHandler.INSTANCE;
56     }
57
58     @Override
59     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
60             throws SAXException {
61         super.closeElement(element, attributes, content, warnings);
62         try {
63             if ("Len".equals(element)) {
64                 mass.setLength(Double.parseDouble(content) / (RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH * MASS_LEN_FUDGE_FACTOR));
65             }
66             if ("KnownMass".equals(element)) {
67                 mass.setComponentMass(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_MASS);
68             }
69             if ("KnownCG".equals(element)) {
70                 //Setting the CG of the Mass Object to 0 is important because of the different ways that Rocksim and
71                 //OpenRocket treat mass objects.  Rocksim treats them as points (even though the data file contains a
72                 //length) and because Rocksim sets the CG of the mass object to really be relative to the front of
73                 //the parent.  But that value is already assumed in the position and position value for the component.
74                 //Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object.
75                 super.setCG(0); 
76             }
77         }
78         catch (NumberFormatException nfe) {
79             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
80         }
81     }
82
83     /**
84      * Get the component this handler is working upon.
85      *
86      * @return a component
87      */
88     @Override
89     public MassComponent getComponent() {
90         return mass;
91     }
92
93     /**
94      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not
95      * public in all components.
96      *
97      * @param position the OpenRocket position
98      */
99     public void setRelativePosition(RocketComponent.Position position) {
100         mass.setRelativePosition(position);
101     }
102
103     /**
104      * Get the required type of material for this component.  Does not apply to MassComponents.
105      *
106      * @return BULK
107      */
108     @Override
109     public Material.Type getMaterialType() {
110         return Material.Type.BULK;
111     }
112
113 }