Material localization support
[debian/openrocket] / core / src / net / sf / openrocket / preset / xml / BaseComponentDTO.java
index b39bc23ec4245a2cb3cb07e3e1d9be36d58a903e..ca8c18fde308db6247319b6dde0811783bac7f34 100644 (file)
@@ -1,13 +1,10 @@
-
 package net.sf.openrocket.preset.xml;
 
-import net.sf.openrocket.database.Databases;
-import net.sf.openrocket.material.Material;
-import net.sf.openrocket.motor.Manufacturer;
-import net.sf.openrocket.preset.ComponentPreset;
-import net.sf.openrocket.preset.InvalidComponentPresetException;
-import net.sf.openrocket.preset.TypedPropertyMap;
-import net.sf.openrocket.unit.UnitGroup;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.List;
 
 import javax.imageio.ImageIO;
 import javax.xml.bind.DatatypeConverter;
@@ -19,18 +16,21 @@ import javax.xml.bind.annotation.XmlInlineBinaryData;
 import javax.xml.bind.annotation.XmlValue;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
-import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.List;
+
+import net.sf.openrocket.database.Databases;
+import net.sf.openrocket.material.Material;
+import net.sf.openrocket.motor.Manufacturer;
+import net.sf.openrocket.preset.ComponentPreset;
+import net.sf.openrocket.preset.InvalidComponentPresetException;
+import net.sf.openrocket.preset.TypedPropertyMap;
+import net.sf.openrocket.unit.UnitGroup;
 
 /**
  * Base class for the external representation of all component presets.
  */
 @XmlAccessorType(XmlAccessType.FIELD)
 public abstract class BaseComponentDTO {
-
+       
        @XmlElement(name = "Manufacturer")
        private String manufacturer;
        @XmlElement(name = "PartNumber")
@@ -41,19 +41,19 @@ public abstract class BaseComponentDTO {
        private AnnotatedMaterialDTO material;
        @XmlElement(name = "Mass")
        private AnnotatedMassDTO mass;
-       @XmlElement(name="Filled")
+       @XmlElement(name = "Filled")
        private Boolean filled;
-    @XmlInlineBinaryData
-    @XmlJavaTypeAdapter(Base64Adapter.class)
-    @XmlElement(name = "Thumbnail")
-    private byte[] image;
-
+       @XmlInlineBinaryData
+       @XmlJavaTypeAdapter(Base64Adapter.class)
+       @XmlElement(name = "Thumbnail")
+       private byte[] image;
+       
        /**
         * Default constructor.
         */
        protected BaseComponentDTO() {
        }
-
+       
        /**
         * Constructor.
         *
@@ -64,216 +64,219 @@ public abstract class BaseComponentDTO {
        protected BaseComponentDTO(final ComponentPreset preset) {
                setManufacturer(preset.getManufacturer().getSimpleName());
                setPartNo(preset.getPartNo());
-               if ( preset.has(ComponentPreset.DESCRIPTION )) {
+               if (preset.has(ComponentPreset.DESCRIPTION)) {
                        setDescription(preset.get(ComponentPreset.DESCRIPTION));
                }
-               if ( preset.has(ComponentPreset.MATERIAL)) {
+               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));
+               if (preset.has(ComponentPreset.FILLED)) {
+                       setFilled(preset.get(ComponentPreset.FILLED));
+               }
+               if (preset.has(ComponentPreset.IMAGE)) {
+                       setImageData(preset.get(ComponentPreset.IMAGE));
                }
-        if (preset.has(ComponentPreset.IMAGE) ) {
-            setImageData(preset.get(ComponentPreset.IMAGE));
-        }
        }
-
+       
        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.getValue();
        }
-
+       
        public void setMass(final AnnotatedMassDTO theMass) {
                mass = theMass;
        }
-
+       
        public void setMass(final double theMass) {
                mass = new AnnotatedMassDTO(theMass);
        }
-
+       
        public Boolean getFilled() {
                return filled;
        }
-
+       
        public void setFilled(Boolean filled) {
                this.filled = filled;
        }
-
-    public byte[] getImageData() {
-        return image;
-    }
-
-    public void setImageData(final byte[] theImage) {
-        image = theImage;
-    }
-
-    public BufferedImage getImage() throws IOException {
-        if (image != null) {
-            return ImageIO.read(new ByteArrayInputStream(image));
-        }
-        return null;
-    }
-
-    public void setImage(BufferedImage theImage) throws IOException {
-        if (theImage != null) {
-            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-            ImageIO.write(theImage, "png", byteArrayOutputStream);
-            image = byteArrayOutputStream.toByteArray();
-        }
-    }
-
-    public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
-
+       
+       public byte[] getImageData() {
+               return image;
+       }
+       
+       public void setImageData(final byte[] theImage) {
+               image = theImage;
+       }
+       
+       public BufferedImage getImage() throws IOException {
+               if (image != null) {
+                       return ImageIO.read(new ByteArrayInputStream(image));
+               }
+               return null;
+       }
+       
+       public void setImage(BufferedImage theImage) throws IOException {
+               if (theImage != null) {
+                       final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+                       ImageIO.write(theImage, "png", byteArrayOutputStream);
+                       image = byteArrayOutputStream.toByteArray();
+               }
+       }
+       
+       public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
+       
        void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
                props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
                props.put(ComponentPreset.PARTNO, partNo);
-               if ( description != null ) {
+               if (description != null) {
                        props.put(ComponentPreset.DESCRIPTION, description);
                }
                Material m = find(materialList, material);
-               if ( m != null ) {
+               if (m != null) {
                        props.put(ComponentPreset.MATERIAL, find(materialList, material));
                }
-               if ( mass != null ) {
+               if (mass != null) {
                        props.put(ComponentPreset.MASS, getMass());
                }
-               if ( filled != null ) {
+               if (filled != null) {
                        props.put(ComponentPreset.FILLED, getFilled());
                }
-        if (image != null) {
-            props.put(ComponentPreset.IMAGE, image);
-        }
+               if (image != null) {
+                       props.put(ComponentPreset.IMAGE, image);
+               }
        }
-
+       
        protected Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
-               if ( dto == null ) {
+               if (dto == null) {
                        return null;
                }
                for (int i = 0; i < materialList.size(); i++) {
-                       MaterialDTO materialDTO =  materialList.get(i);
+                       MaterialDTO materialDTO = materialList.get(i);
                        if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
                                return materialDTO.asMaterial();
                        }
                }
-
+               
                // Don't have one, first check OR's database
-        Material m = Databases.findMaterial(dto.getORMaterialType(), dto.material);
-        if ( m != null ) {
-               return m;
-        }
-
-        return Material.newUserMaterial(dto.getORMaterialType(), dto.material, 0.0);
-
+               Material m = Databases.findMaterial(dto.getORMaterialType(), dto.material);
+               if (m != null) {
+                       return m;
+               }
+               
+               return Databases.findMaterial(dto.getORMaterialType(), dto.material, 0.0);
+               
        }
-
+       
        static class AnnotatedMaterialDTO {
                @XmlAttribute(name = "Type")
                private String type;
                @XmlValue
                private String material;
-
-               AnnotatedMaterialDTO() {}
-
+               
+               AnnotatedMaterialDTO() {
+               }
+               
                AnnotatedMaterialDTO(Material theMaterial) {
                        type = theMaterial.getType().name();
                        material = theMaterial.getName();
                }
                
                public Material.Type getORMaterialType() {
-                       if ( "BULK".equals(type) ) {
+                       if ("BULK".equals(type)) {
                                return Material.Type.BULK;
-                       } else if ( "SURFACE".equals(type) ) {
+                       } else if ("SURFACE".equals(type)) {
                                return Material.Type.SURFACE;
-                       } else if ( "LINE".equals(type) ) {
+                       } else if ("LINE".equals(type)) {
                                return Material.Type.LINE;
                        }
-                       throw new IllegalArgumentException( "Inavlid material type " + type +" specified for Component" );
+                       throw new IllegalArgumentException("Inavlid material type " + type + " specified for Component");
                }
        }
-
+       
        static class AnnotatedLengthDTO {
-               @XmlAttribute(name="Unit", required=false)
+               @XmlAttribute(name = "Unit", required = false)
                private String unitName = "m";
                @XmlValue
                private double length;
-
-               AnnotatedLengthDTO() {}
-
-               AnnotatedLengthDTO( double length ) {
+               
+               AnnotatedLengthDTO() {
+               }
+               
+               AnnotatedLengthDTO(double length) {
                        this.length = length;
                }
-
+               
                public double getValue() {
                        return UnitGroup.UNITS_LENGTH.getUnit(unitName).fromUnit(length);
                }
        }
-
+       
        static class AnnotatedMassDTO {
-               @XmlAttribute(name="Unit", required=false)
+               @XmlAttribute(name = "Unit", required = false)
                private String unitName = "kg";
                @XmlValue
                private double mass;
-
-               AnnotatedMassDTO() {}
-
-               AnnotatedMassDTO( double mass ) {
+               
+               AnnotatedMassDTO() {
+               }
+               
+               AnnotatedMassDTO(double mass) {
                        this.mass = mass;
                }
-
+               
                public double getValue() {
                        return UnitGroup.UNITS_MASS.getUnit(unitName).fromUnit(mass);
                }
        }
-
-    static class Base64Adapter extends XmlAdapter<String, byte[]> {
-       @Override
-        public byte[] unmarshal(String s) {
-            if (s == null) {
-                return null;
-            }
-            return DatatypeConverter.parseBase64Binary(s);
-        }
-
-       @Override
-        public String marshal(byte[] bytes) {
-            if (bytes == null) {
-                return null;
-            }
-            return DatatypeConverter.printBase64Binary(bytes);
-        }
-    }
+       
+       static class Base64Adapter extends XmlAdapter<String, byte[]> {
+               @Override
+               public byte[] unmarshal(String s) {
+                       if (s == null) {
+                               return null;
+                       }
+                       return DatatypeConverter.parseBase64Binary(s);
+               }
+               
+               @Override
+               public String marshal(byte[] bytes) {
+                       if (bytes == null) {
+                               return null;
+                       }
+                       return DatatypeConverter.printBase64Binary(bytes);
+               }
+       }
 }