X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fpreset%2FComponentPreset.java;h=410470e73c04f798753f76f28c24c5fa5b5ced8f;hb=f476a440dd2a024628613dea28e6f08e80173e50;hp=3da15e2642baf76cb0d73ed622bbbfd28b5ea050;hpb=9c8be94f5fb1a088eeb030dee26a5ec92415c37c;p=debian%2Fopenrocket diff --git a/core/src/net/sf/openrocket/preset/ComponentPreset.java b/core/src/net/sf/openrocket/preset/ComponentPreset.java index 3da15e26..410470e7 100644 --- a/core/src/net/sf/openrocket/preset/ComponentPreset.java +++ b/core/src/net/sf/openrocket/preset/ComponentPreset.java @@ -14,6 +14,9 @@ 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; +import net.sf.openrocket.rocketcomponent.Transition.Shape; import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.TextUtil; @@ -36,37 +39,87 @@ public class ComponentPreset implements Comparable { private String digest = ""; public enum Type { - BODY_TUBE, - NOSE_CONE; + BODY_TUBE( new TypedKey[] { + ComponentPreset.MANUFACTURER, + ComponentPreset.PARTNO, + ComponentPreset.OUTER_DIAMETER, + ComponentPreset.INNER_DIAMETER, + ComponentPreset.LENGTH} ), + + NOSE_CONE( new TypedKey[] { + ComponentPreset.MANUFACTURER, + ComponentPreset.PARTNO, + ComponentPreset.DESCRIPTION, + ComponentPreset.SHAPE, + ComponentPreset.OUTER_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} ); Type[] compatibleTypes; + TypedKey[] displayedColumns; - Type () { + Type( TypedKey[] displayedColumns) { compatibleTypes = new Type[1]; compatibleTypes[0] = this; + this.displayedColumns = displayedColumns; } - Type( Type ... t ) { + Type( Type[] t, TypedKey[] displayedColumns ) { compatibleTypes = new Type[t.length+1]; compatibleTypes[0] = this; for( int i=0; i[] getDisplayedColumns() { + return displayedColumns; + } + } public final static TypedKey MANUFACTURER = new TypedKey("Manufacturer", Manufacturer.class); public final static TypedKey PARTNO = new TypedKey("PartNo",String.class); + public final static TypedKey DESCRIPTION = new TypedKey("Description", String.class); public final static TypedKey TYPE = new TypedKey("Type",Type.class); public final static TypedKey LENGTH = new TypedKey("Length", Double.class, UnitGroup.UNITS_LENGTH); public final static TypedKey INNER_DIAMETER = new TypedKey("InnerDiameter", Double.class, UnitGroup.UNITS_LENGTH); public final static TypedKey OUTER_DIAMETER = new TypedKey("OuterDiameter", Double.class, UnitGroup.UNITS_LENGTH); + public final static TypedKey SHOULDER_LENGTH = new TypedKey("ShoulderLength", Double.class, UnitGroup.UNITS_LENGTH); + public final static TypedKey SHOULDER_DIAMETER = new TypedKey("ShoulderDiameter", Double.class, UnitGroup.UNITS_LENGTH); + public final static TypedKey FORE_SHOULDER_LENGTH = new TypedKey("ForeShoulderLength",Double.class, UnitGroup.UNITS_LENGTH); + public final static TypedKey FORE_SHOULDER_DIAMETER = new TypedKey("ForeShoulderDiameter",Double.class, UnitGroup.UNITS_LENGTH); + public final static TypedKey FORE_OUTER_DIAMETER = new TypedKey("ForeOuterDiameter", Double.class, UnitGroup.UNITS_LENGTH); + public final static TypedKey SHAPE = new TypedKey("Shape", Shape.class); public final static TypedKey MATERIAL = new TypedKey("Material", Material.class); public final static TypedKey FINISH = new TypedKey("Finish", Finish.class); public final static TypedKey THICKNESS = new TypedKey("Thickness", Double.class, UnitGroup.UNITS_LENGTH); @@ -78,9 +131,16 @@ public class ComponentPreset implements Comparable { keyMap.put(MANUFACTURER.getName(), MANUFACTURER); keyMap.put(PARTNO.getName(), PARTNO); keyMap.put(TYPE.getName(), TYPE); + keyMap.put(DESCRIPTION.getName(), DESCRIPTION); keyMap.put(LENGTH.getName(), LENGTH); keyMap.put(INNER_DIAMETER.getName(), INNER_DIAMETER); 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); keyMap.put(THICKNESS.getName(), THICKNESS); @@ -88,93 +148,8 @@ public class ComponentPreset implements Comparable { 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: { - break; - } - } - - preset.computeDigest(); - - return preset; - - } - - // Private constructor to encourage use of factory. - private ComponentPreset() { + // package scope constructor to encourage use of factory. + ComponentPreset() { } /** @@ -210,6 +185,26 @@ public class ComponentPreset implements Comparable { 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 + */ + void put( TypedKey key, T value ) { + properties.put(key, value); + } + public T get(TypedKey key) { T value = properties.get(key); if (value == null) { @@ -245,7 +240,10 @@ public class ComponentPreset implements Comparable { return get(MANUFACTURER).toString() + "|" + get(PARTNO); } - private void computeDigest() { + /** + * Package scope so the factory can call it. + */ + void computeDigest() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -287,6 +285,10 @@ public class ComponentPreset implements Comparable { } else if ( key.getType() == Material.class ) { double d = ((Material)value).getDensity(); os.writeDouble(d); + } else if ( key.getType() == Shape.class ) { + // FIXME - this is ugly to use the ordinal but what else? + int i = ((Shape)value).ordinal(); + os.writeInt(i); } }