committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / RocksimLocationMode.java
1 /*
2  * RocksimLocationMode.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import net.sf.openrocket.rocketcomponent.RocketComponent;
7
8 /**
9  * Models the relative position of parts on a rocket.  Maps from Rocksim's notion to OpenRocket's.
10  */
11 enum RocksimLocationMode {
12     FRONT_OF_OWNING_PART (0, RocketComponent.Position.TOP),
13     FROM_TIP_OF_NOSE     (1, RocketComponent.Position.ABSOLUTE),
14     BACK_OF_OWNING_PART  (2, RocketComponent.Position.BOTTOM);
15
16     /** The value Rocksim uses internally (and in the XML file). */
17     private final int ordinal;
18     
19     /** The OpenRocket position equivalent. */
20     private final RocketComponent.Position position;
21
22     /**
23      * Constructor.
24      * 
25      * @param idx   the rocksim enum value
26      * @param theOpenRocketPosition  the corresponding OpenRocket position
27      */
28     RocksimLocationMode(int idx, RocketComponent.Position theOpenRocketPosition) {
29         ordinal = idx;
30         position = theOpenRocketPosition;
31     }
32
33     /**
34      * Get the OpenRocket position.
35      * 
36      * @return  the position instance
37      */
38     public RocketComponent.Position asOpenRocket() {
39         return position;
40     }
41
42     /**
43      * Lookup an instance of this class from a rocksim enum value.
44      * 
45      * @param rocksimCode  the rocksim enum value
46      * 
47      * @return an instance of this enum
48      */
49     public static RocksimLocationMode fromCode(int rocksimCode) {
50         RocksimLocationMode[] values = values();
51         for (RocksimLocationMode value : values) {
52             if (value.ordinal == rocksimCode) {
53                 return value;
54             }
55         }
56         return FRONT_OF_OWNING_PART;
57     }
58
59 }