From defe0287ee31a0fcf66a029d26cb2411a68fd096 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Thu, 26 Apr 2012 18:18:00 +0000 Subject: [PATCH] Added filled property. Made Filled and Mass optional by using objects instead of primitives. Changed the serialization process so only properties set in the ComponentPreset are serialized. Change the deserialization process so if properties are not specified in the xml file, they are not assigned. git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@616 180e2498-e6e9-4542-8430-84ac67f01cd8 --- .../preset/xml/BaseComponentDTO.java | 243 ++++++++++-------- 1 file changed, 135 insertions(+), 108 deletions(-) diff --git a/core/src/net/sf/openrocket/preset/xml/BaseComponentDTO.java b/core/src/net/sf/openrocket/preset/xml/BaseComponentDTO.java index 2c07d890..b646f7f7 100644 --- a/core/src/net/sf/openrocket/preset/xml/BaseComponentDTO.java +++ b/core/src/net/sf/openrocket/preset/xml/BaseComponentDTO.java @@ -21,112 +21,139 @@ import java.util.List; @XmlAccessorType(XmlAccessType.FIELD) public abstract class BaseComponentDTO { - @XmlElement(name = "Manufacturer") - private String manufacturer; - @XmlElement(name = "PartNumber") - private String partNo; - @XmlElement(name = "Description") - private String description; - @XmlElement(name = "Material") - private AnnotatedMaterialDTO material; - @XmlElement(name = "Mass") - private double mass; - - /** - * Default constructor. - */ - protected BaseComponentDTO() { - } - - /** - * Constructor. - * - * @param preset the preset to use to pull data values out of - * - * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset - */ - protected BaseComponentDTO(final ComponentPreset preset) { - setManufacturer(preset.getManufacturer().getSimpleName()); - setPartNo(preset.getPartNo()); - setDescription(preset.get(ComponentPreset.DESCRIPTION)); - setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL))); - if (preset.has(ComponentPreset.MASS)) { - setMass(preset.get(ComponentPreset.MASS)); - } - } - - public String getManufacturer() { - return manufacturer; - } - - public void setManufacturer(final String theManufacturer) { - manufacturer = theManufacturer; - } - - public String getPartNo() { - return partNo; - } - - public void setPartNo(final String thePartNo) { - partNo = thePartNo; - } - - public String getDescription() { - return description; - } - - public void setDescription(final String theDescription) { - description = theDescription; - } - - public AnnotatedMaterialDTO getMaterial() { - return material; - } - - public void setMaterial(final AnnotatedMaterialDTO theMaterial) { - material = theMaterial; - } - - public double getMass() { - return mass; - } - - public void setMass(final double theMass) { - mass = theMass; - } - - public abstract ComponentPreset asComponentPreset(List materials) throws InvalidComponentPresetException; - - void addProps(TypedPropertyMap props, List materialList) { - props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer)); - props.put(ComponentPreset.PARTNO, partNo); - props.put(ComponentPreset.DESCRIPTION, description); - props.put(ComponentPreset.MATERIAL, find(materialList, material)); - props.put(ComponentPreset.MASS, mass); - } - - private Material find(List materialList, AnnotatedMaterialDTO dto) { - for (int i = 0; i < materialList.size(); i++) { - MaterialDTO materialDTO = materialList.get(i); - if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) { - return materialDTO.asMaterial(); - } - } - //Otherwise fallback and look at factory default materials. - return Databases.findMaterial(Material.Type.valueOf(material.type), material.material); - } - - static class AnnotatedMaterialDTO { - @XmlAttribute(name = "Type") - private String type; - @XmlValue - private String material; - - AnnotatedMaterialDTO() {} - - AnnotatedMaterialDTO(Material theMaterial) { - type = theMaterial.getType().name(); - material = theMaterial.getName(); - } - } + @XmlElement(name = "Manufacturer") + private String manufacturer; + @XmlElement(name = "PartNumber") + private String partNo; + @XmlElement(name = "Description") + private String description; + @XmlElement(name = "Material") + private AnnotatedMaterialDTO material; + @XmlElement(name = "Mass") + private Double mass; + @XmlElement(name="Filled") + private Boolean filled; + + /** + * Default constructor. + */ + protected BaseComponentDTO() { + } + + /** + * Constructor. + * + * @param preset the preset to use to pull data values out of + * + * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset + */ + protected BaseComponentDTO(final ComponentPreset preset) { + setManufacturer(preset.getManufacturer().getSimpleName()); + setPartNo(preset.getPartNo()); + if ( preset.has(ComponentPreset.DESCRIPTION )) { + setDescription(preset.get(ComponentPreset.DESCRIPTION)); + } + if ( preset.has(ComponentPreset.MATERIAL)) { + setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL))); + } + if (preset.has(ComponentPreset.MASS)) { + setMass(preset.get(ComponentPreset.MASS)); + } + if ( preset.has(ComponentPreset.FILLED) ) { + setFilled( preset.get(ComponentPreset.FILLED)); + } + } + + public String getManufacturer() { + return manufacturer; + } + + public void setManufacturer(final String theManufacturer) { + manufacturer = theManufacturer; + } + + public String getPartNo() { + return partNo; + } + + public void setPartNo(final String thePartNo) { + partNo = thePartNo; + } + + public String getDescription() { + return description; + } + + public void setDescription(final String theDescription) { + description = theDescription; + } + + public AnnotatedMaterialDTO getMaterial() { + return material; + } + + public void setMaterial(final AnnotatedMaterialDTO theMaterial) { + material = theMaterial; + } + + public double getMass() { + return mass; + } + + public void setMass(final double theMass) { + mass = theMass; + } + + public Boolean getFilled() { + return filled; + } + + public void setFilled(Boolean filled) { + this.filled = filled; + } + + public abstract ComponentPreset asComponentPreset(List materials) throws InvalidComponentPresetException; + + void addProps(TypedPropertyMap props, List materialList) { + props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer)); + props.put(ComponentPreset.PARTNO, partNo); + if ( description != null ) { + props.put(ComponentPreset.DESCRIPTION, description); + } + Material m = find(materialList, material); + if ( m != null ) { + props.put(ComponentPreset.MATERIAL, find(materialList, material)); + } + if ( mass != null ) { + props.put(ComponentPreset.MASS, mass); + } + if ( filled != null ) { + props.put(ComponentPreset.FILLED, filled); + } + } + + private Material find(List materialList, AnnotatedMaterialDTO dto) { + for (int i = 0; i < materialList.size(); i++) { + MaterialDTO materialDTO = materialList.get(i); + if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) { + return materialDTO.asMaterial(); + } + } + //Otherwise fallback and look at factory default materials. + return Databases.findMaterial(Material.Type.valueOf(material.type), material.material); + } + + static class AnnotatedMaterialDTO { + @XmlAttribute(name = "Type") + private String type; + @XmlValue + private String material; + + AnnotatedMaterialDTO() {} + + AnnotatedMaterialDTO(Material theMaterial) { + type = theMaterial.getType().name(); + material = theMaterial.getName(); + } + } } -- 2.47.2