Added centering rings and engine blocks presets
[debian/openrocket] / core / src / net / sf / openrocket / preset / ComponentPreset.java
index c1d562856e726d6cefa85423601fa41bba80cebb..9ee7c0096fc1ce134359f387026cb1e6afed9fa5 100644 (file)
@@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.security.MessageDigest;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -12,9 +13,7 @@ import java.util.Map;
 
 import net.sf.openrocket.material.Material;
 import net.sf.openrocket.motor.Manufacturer;
-import net.sf.openrocket.rocketcomponent.BodyTube;
 import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
-import net.sf.openrocket.rocketcomponent.NoseCone;
 import net.sf.openrocket.rocketcomponent.Transition.Shape;
 import net.sf.openrocket.unit.UnitGroup;
 import net.sf.openrocket.util.BugException;
@@ -41,16 +40,59 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
                BODY_TUBE( new TypedKey<?>[] {
                                ComponentPreset.MANUFACTURER,
                                ComponentPreset.PARTNO,
-                               ComponentPreset.OUTER_DIAMETER,
+                               ComponentPreset.DESCRIPTION,
                                ComponentPreset.INNER_DIAMETER,
+                               ComponentPreset.OUTER_DIAMETER,
                                ComponentPreset.LENGTH} ),
                                
                NOSE_CONE( new TypedKey<?>[] {
                                ComponentPreset.MANUFACTURER,
                                ComponentPreset.PARTNO,
+                               ComponentPreset.DESCRIPTION,
+                               ComponentPreset.SHAPE,
+                               ComponentPreset.OUTER_DIAMETER,
+                               ComponentPreset.SHOULDER_DIAMETER,
+                               ComponentPreset.LENGTH} ),
+
+               TRANSITION( new TypedKey<?>[] {
+                               ComponentPreset.MANUFACTURER,
+                               ComponentPreset.PARTNO,
+                               ComponentPreset.DESCRIPTION,
                                ComponentPreset.SHAPE,
+                               ComponentPreset.FORE_OUTER_DIAMETER,
+                               ComponentPreset.OUTER_DIAMETER,
+                               ComponentPreset.LENGTH} ),
+                               
+               TUBE_COUPLER( new TypedKey<?>[] {
+                               ComponentPreset.MANUFACTURER,
+                               ComponentPreset.PARTNO,
+                               ComponentPreset.OUTER_DIAMETER,
+                               ComponentPreset.INNER_DIAMETER,
+                               ComponentPreset.LENGTH} ),
+                                               
+               BULK_HEAD( new TypedKey<?>[] {
+                               ComponentPreset.MANUFACTURER,
+                               ComponentPreset.PARTNO,
+                               ComponentPreset.DESCRIPTION,
+                               ComponentPreset.OUTER_DIAMETER,
+                               ComponentPreset.LENGTH} ),
+
+               CENTERING_RING( new TypedKey<?>[] {
+                               ComponentPreset.MANUFACTURER,
+                               ComponentPreset.PARTNO,
+                               ComponentPreset.DESCRIPTION,
+                               ComponentPreset.INNER_DIAMETER,
                                ComponentPreset.OUTER_DIAMETER,
-                               ComponentPreset.LENGTH} ) ;
+                               ComponentPreset.LENGTH} ),
+
+               ENGINE_BLOCK( new TypedKey<?>[] {
+                               ComponentPreset.MANUFACTURER,
+                               ComponentPreset.PARTNO,
+                               ComponentPreset.DESCRIPTION,
+                               ComponentPreset.INNER_DIAMETER,
+                               ComponentPreset.OUTER_DIAMETER,
+                               ComponentPreset.LENGTH} );
+
 
                Type[] compatibleTypes;
                TypedKey<?>[] displayedColumns;
@@ -91,6 +133,9 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
        public final static TypedKey<Double> OUTER_DIAMETER = new TypedKey<Double>("OuterDiameter", Double.class, UnitGroup.UNITS_LENGTH);
        public final static TypedKey<Double> SHOULDER_LENGTH = new TypedKey<Double>("ShoulderLength", Double.class, UnitGroup.UNITS_LENGTH);
        public final static TypedKey<Double> SHOULDER_DIAMETER = new TypedKey<Double>("ShoulderDiameter", Double.class, UnitGroup.UNITS_LENGTH);
+       public final static TypedKey<Double> FORE_SHOULDER_LENGTH = new TypedKey<Double>("ForeShoulderLength",Double.class, UnitGroup.UNITS_LENGTH);
+       public final static TypedKey<Double> FORE_SHOULDER_DIAMETER = new TypedKey<Double>("ForeShoulderDiameter",Double.class, UnitGroup.UNITS_LENGTH);
+       public final static TypedKey<Double> FORE_OUTER_DIAMETER = new TypedKey<Double>("ForeOuterDiameter", Double.class, UnitGroup.UNITS_LENGTH);
        public final static TypedKey<Shape> SHAPE = new TypedKey<Shape>("Shape", Shape.class);
        public final static TypedKey<Material> MATERIAL = new TypedKey<Material>("Material", Material.class);
        public final static TypedKey<Finish> FINISH = new TypedKey<Finish>("Finish", Finish.class);
@@ -109,6 +154,9 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
                keyMap.put(OUTER_DIAMETER.getName(), OUTER_DIAMETER);
                keyMap.put(SHOULDER_LENGTH.getName(), SHOULDER_LENGTH);
                keyMap.put(SHOULDER_DIAMETER.getName(), SHOULDER_DIAMETER);
+               keyMap.put(FORE_SHOULDER_LENGTH.getName(), FORE_SHOULDER_LENGTH);
+               keyMap.put(FORE_SHOULDER_DIAMETER.getName(), FORE_SHOULDER_DIAMETER);
+               keyMap.put(FORE_OUTER_DIAMETER.getName(), FORE_OUTER_DIAMETER);
                keyMap.put(SHAPE.getName(), SHAPE);
                keyMap.put(MATERIAL.getName(), MATERIAL);
                keyMap.put(FINISH.getName(), FINISH);
@@ -117,119 +165,28 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
                keyMap.put(MASS.getName(), MASS);
        }
 
-       public static ComponentPreset create( TypedPropertyMap props ) throws InvalidComponentPresetException {
-
-               ComponentPreset preset = new ComponentPreset();
-               // First do validation.
-               if ( !props.containsKey(TYPE)) {
-                       throw new InvalidComponentPresetException("No Type specified " + props.toString() );
-               }
-
-               if (!props.containsKey(MANUFACTURER)) {
-                       throw new InvalidComponentPresetException("No Manufacturer specified " + props.toString() );
-               }
-
-               if (!props.containsKey(PARTNO)) {
-                       throw new InvalidComponentPresetException("No PartNo specified " + props.toString() );
-               }
-
-               preset.properties.putAll(props);
-
-               // Should check for various bits of each of the types.
-               Type t = props.get(TYPE);
-               switch ( t ) {
-               case BODY_TUBE: {
-
-                       if ( !props.containsKey(LENGTH) ) {
-                               throw new InvalidComponentPresetException( "No Length specified for body tube preset " + props.toString());
-                       }
-
-                       BodyTube bt = new BodyTube();
-
-                       bt.setLength(props.get(LENGTH));
-
-                       // Need to verify contains 2 of OD, thickness, ID.  Compute the third.
-                       boolean hasOd = props.containsKey(OUTER_DIAMETER);
-                       boolean hasId = props.containsKey(INNER_DIAMETER);
-                       boolean hasThickness = props.containsKey(THICKNESS);
-
-                       if ( hasOd ) {
-                               double outerRadius = props.get(OUTER_DIAMETER)/2.0;
-                               double thickness = 0;
-                               bt.setOuterRadius( outerRadius );
-                               if ( hasId ) {
-                                       thickness = outerRadius - props.get(INNER_DIAMETER)/2.0;
-                               } else if ( hasThickness ) {
-                                       thickness = props.get(THICKNESS);
-                               } else {
-                                       throw new InvalidComponentPresetException("Body tube preset underspecified " + props.toString());
-                               }
-                               bt.setThickness( thickness );
-                       } else {
-                               if ( ! hasId && ! hasThickness ) {
-                                       throw new InvalidComponentPresetException("Body tube preset underspecified " + props.toString());
-                               }
-                               double innerRadius = props.get(INNER_DIAMETER)/2.0;
-                               double thickness = props.get(THICKNESS);
-                               bt.setOuterRadius(innerRadius + thickness);
-                               bt.setThickness(thickness);
-                       }
-
-                       preset.properties.put(OUTER_DIAMETER, bt.getOuterRadius() *2.0);
-                       preset.properties.put(INNER_DIAMETER, bt.getInnerRadius() *2.0);
-                       preset.properties.put(THICKNESS, bt.getThickness());
-
-                       // Need to translate Mass to Density.
-                       if ( props.containsKey(MASS) ) {
-                               String materialName = "TubeCustom";
-                               if ( props.containsKey(MATERIAL) ) {
-                                       materialName = props.get(MATERIAL).getName();
-                               }
-                               Material m = Material.newMaterial(Material.Type.BULK, materialName, props.get(MASS)/bt.getComponentVolume(), false);
-                               preset.properties.put(MATERIAL, m);
-                       }
-
-                       break;
-               }
-               case NOSE_CONE: {
-                       if ( !props.containsKey(LENGTH) ) {
-                               throw new InvalidComponentPresetException( "No Length specified for nose cone preset " + props.toString());
-                       }
-                       if ( !props.containsKey(SHAPE) ) {
-                               throw new InvalidComponentPresetException( "No Shape specified for nose cone preset " + props.toString());
-                       }
-                       if ( !props.containsKey(OUTER_DIAMETER) ) {
-                               throw new InvalidComponentPresetException( "No Outer Diameter specified for nose cone preset " + props.toString());
-                       }
-                       
-                       if ( props.containsKey(MASS) ) {
-                               // compute a density for this component
-                               double mass = props.get(MASS);
-                               NoseCone nc = new NoseCone();
-                               nc.loadPreset(preset);
-                               double density = mass / nc.getComponentVolume();
-
-                               String materialName = "NoseConeCustom";
-                               if ( props.containsKey(MATERIAL) ) {
-                                       materialName = props.get(MATERIAL).getName();
-                               }
-                               
-                               Material m = Material.newMaterial(Material.Type.BULK, materialName,density, false);
-                               preset.properties.put(MATERIAL, m);
-
-                       }
-                       break;
-               }
-               }
-
-               preset.computeDigest();
-
-               return preset;
-
-       }
-
-       // Private constructor to encourage use of factory.
-       private ComponentPreset() {
+       public final static List<TypedKey<?>> orderedKeyList = Arrays.<TypedKey<?>>asList(
+                       MANUFACTURER,
+                       PARTNO,
+                       DESCRIPTION,
+                       OUTER_DIAMETER,
+                       INNER_DIAMETER,
+                       LENGTH,
+                       SHOULDER_DIAMETER,
+                       SHOULDER_LENGTH,
+                       FORE_SHOULDER_DIAMETER,
+                       FORE_SHOULDER_LENGTH,
+                       SHAPE,
+                       THICKNESS,
+                       FILLED,
+                       MASS,
+                       FINISH,
+                       MATERIAL
+                       );
+       
+       
+       // package scope constructor to encourage use of factory.
+       ComponentPreset() {
        }
 
        /**
@@ -265,6 +222,26 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
                return properties.containsKey(key);
        }
 
+       /**
+        * Package scope so the ComponentPresetFactory can call it.
+        * @param other
+        */
+       void putAll(TypedPropertyMap other) {
+               if (other == null) {
+                       return;
+               }
+               properties.putAll(other);
+       }
+
+       /**
+        * Package scope so the ComponentPresetFactory can call it.
+        * @param key
+        * @param value
+        */
+       <T> void put( TypedKey<T> key, T value ) {
+               properties.put(key, value);
+       }
+       
        public <T> T get(TypedKey<T> key) {
                T value = properties.get(key);
                if (value == null) {
@@ -300,7 +277,10 @@ public class ComponentPreset implements Comparable<ComponentPreset> {
                return get(MANUFACTURER).toString() + "|" + get(PARTNO);
        }
 
-       private void computeDigest() {
+       /**
+        * Package scope so the factory can call it.
+        */
+       void computeDigest() {
 
                try {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();