DGP - added isCompatible checks for all Rocksim components
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / LaunchLugHandler.java
1 /*
2  * LaunchLugHandler.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.LaunchLug;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
13
14 import java.util.HashMap;
15
16 /**
17  * The SAX handler for Rocksim Launch Lugs.
18  */
19 class LaunchLugHandler extends PositionDependentHandler<LaunchLug> {
20
21     /**
22      * The OpenRocket LaunchLug instance.
23      */
24     private final LaunchLug lug;
25
26     /**
27      * Constructor.
28      *
29      * @param c the parent
30      * @param warnings  the warning set
31      * 
32      * @throws IllegalArgumentException thrown if <code>c</code> is null
33      */
34     public LaunchLugHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
35         if (c == null) {
36             throw new IllegalArgumentException("The parent component of a launch lug may not be null.");
37         }
38         lug = new LaunchLug();
39         if (isCompatible(c, LaunchLug.class, warnings)) {
40             c.addChild(lug);
41         }
42     }
43
44     @Override
45     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
46         return PlainTextHandler.INSTANCE;
47     }
48
49     @Override
50     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
51             throws SAXException {
52         super.closeElement(element, attributes, content, warnings);
53
54         try {
55             if ("OD".equals(element)) {
56                 lug.setRadius(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
57             }
58             if ("ID".equals(element)) {
59                 lug.setInnerRadius(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
60             }
61             if ("Len".equals(element)) {
62                 lug.setLength(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
63             }
64             if ("Material".equals(element)) {
65                 setMaterialName(content);
66             }
67             if ("FinishCode".equals(element)) {
68                 lug.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
69             }
70         }
71         catch (NumberFormatException nfe) {
72             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
73         }
74     }
75
76     /**
77      * Get the LaunchLug component this handler is working upon.
78      * 
79      * @return a LaunchLug component
80      */
81     @Override
82     public LaunchLug getComponent() {
83         return lug;
84     }
85
86     /**
87      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
88      * public in all components.
89      * 
90      * @param position  the OpenRocket position
91      */
92     @Override
93     public void setRelativePosition(RocketComponent.Position position) {
94         lug.setRelativePosition(position);
95     }
96
97     /**
98      * Get the required type of material for this component.
99      *
100      * @return BULK
101      */
102     @Override
103     public Material.Type getMaterialType() {
104         return Material.Type.BULK;
105     }
106 }
107