De-localize Material and FlightDataType objects when persisting in ork files. Added...
[debian/openrocket] / core / src / net / sf / openrocket / file / openrocket / savers / RocketComponentSaver.java
index bc58ff403f47e98de95ec30dce05348518e33433..e009f863770a0a415d47c5c3af53aa1396dd8e05 100644 (file)
@@ -9,6 +9,7 @@ import net.sf.openrocket.file.RocketSaver;
 import net.sf.openrocket.material.Material;
 import net.sf.openrocket.motor.Motor;
 import net.sf.openrocket.motor.ThrustCurveMotor;
+import net.sf.openrocket.preset.ComponentPreset;
 import net.sf.openrocket.rocketcomponent.ComponentAssembly;
 import net.sf.openrocket.rocketcomponent.MotorMount;
 import net.sf.openrocket.rocketcomponent.Rocket;
@@ -19,15 +20,22 @@ import net.sf.openrocket.util.LineStyle;
 
 
 public class RocketComponentSaver {
-       
+
        protected RocketComponentSaver() {
                // Prevent instantiation from outside the package
        }
-       
+
        protected void addParams(net.sf.openrocket.rocketcomponent.RocketComponent c, List<String> elements) {
                elements.add("<name>" + RocketSaver.escapeXML(c.getName()) + "</name>");
-               
-               
+
+               ComponentPreset preset = c.getPresetComponent();
+               if ( preset != null ) {
+                       elements.add("<preset type=\"" + preset.getType() +
+                                       "\" manufacturer=\"" + preset.getManufacturer().getSimpleName() +
+                                       "\" partno=\"" + preset.getPartNo() + "\" digest=\"" + preset.getDigest() +"\"/>");
+               }
+
+
                // Save color and line style if significant
                if (!(c instanceof Rocket || c instanceof ComponentAssembly)) {
                        Color color = c.getColor();
@@ -35,23 +43,23 @@ public class RocketComponentSaver {
                                elements.add("<color red=\"" + color.getRed() + "\" green=\"" + color.getGreen()
                                                + "\" blue=\"" + color.getBlue() + "\"/>");
                        }
-                       
+
                        LineStyle style = c.getLineStyle();
                        if (style != null) {
                                // Type names currently equivalent to the enum names except for case.
                                elements.add("<linestyle>" + style.name().toLowerCase(Locale.ENGLISH) + "</linestyle>");
                        }
                }
-               
-               
+
+
                // Save position unless "AFTER"
                if (c.getRelativePosition() != RocketComponent.Position.AFTER) {
                        // The type names are currently equivalent to the enum names except for case.
                        String type = c.getRelativePosition().name().toLowerCase(Locale.ENGLISH);
                        elements.add("<position type=\"" + type + "\">" + c.getPositionValue() + "</position>");
                }
-               
-               
+
+
                // Overrides
                boolean overridden = false;
                if (c.isMassOverridden()) {
@@ -66,26 +74,26 @@ public class RocketComponentSaver {
                        elements.add("<overridesubcomponents>" + c.getOverrideSubcomponents()
                                        + "</overridesubcomponents>");
                }
-               
-               
+
+
                // Comment
                if (c.getComment().length() > 0) {
                        elements.add("<comment>" + RocketSaver.escapeXML(c.getComment()) + "</comment>");
                }
-               
+
        }
-       
-       
-       
-       
+
+
+
+
        protected final String materialParam(Material mat) {
                return materialParam("material", mat);
        }
-       
-       
+
+
        protected final String materialParam(String tag, Material mat) {
                String str = "<" + tag;
-               
+
                switch (mat.getType()) {
                case LINE:
                        str += " type=\"line\"";
@@ -99,27 +107,27 @@ public class RocketComponentSaver {
                default:
                        throw new BugException("Unknown material type: " + mat.getType());
                }
-               
-               return str + " density=\"" + mat.getDensity() + "\">" + RocketSaver.escapeXML(mat.getName()) + "</" + tag + ">";
+
+               return str + " density=\"" + mat.getDensity() + "\" key=\"" + mat.getKey() + "\">" + RocketSaver.escapeXML(mat.getName()) + "</" + tag + ">";
        }
-       
-       
+
+
        protected final List<String> motorMountParams(MotorMount mount) {
                if (!mount.isMotorMount())
                        return Collections.emptyList();
-               
+
                String[] motorConfigIDs = ((RocketComponent) mount).getRocket().getMotorConfigurationIDs();
                List<String> elements = new ArrayList<String>();
-               
+
                elements.add("<motormount>");
-               
+
                for (String id : motorConfigIDs) {
                        Motor motor = mount.getMotor(id);
-                       
+
                        // Nothing is stored if no motor loaded
                        if (motor == null)
                                continue;
-                       
+
                        elements.add("  <motor configid=\"" + id + "\">");
                        if (motor.getMotorType() != Motor.Type.UNKNOWN) {
                                elements.add("    <type>" + motor.getMotorType().name().toLowerCase(Locale.ENGLISH) + "</type>");
@@ -133,27 +141,27 @@ public class RocketComponentSaver {
                        elements.add("    <designation>" + RocketSaver.escapeXML(motor.getDesignation()) + "</designation>");
                        elements.add("    <diameter>" + motor.getDiameter() + "</diameter>");
                        elements.add("    <length>" + motor.getLength() + "</length>");
-                       
+
                        // Motor delay
                        if (mount.getMotorDelay(id) == Motor.PLUGGED) {
                                elements.add("    <delay>none</delay>");
                        } else {
                                elements.add("    <delay>" + mount.getMotorDelay(id) + "</delay>");
                        }
-                       
+
                        elements.add("  </motor>");
                }
-               
+
                elements.add("  <ignitionevent>"
                                + mount.getIgnitionEvent().name().toLowerCase(Locale.ENGLISH).replace("_", "")
                                + "</ignitionevent>");
-               
+
                elements.add("  <ignitiondelay>" + mount.getIgnitionDelay() + "</ignitiondelay>");
                elements.add("  <overhang>" + mount.getMotorOverhang() + "</overhang>");
-               
+
                elements.add("</motormount>");
-               
+
                return elements;
        }
-       
+
 }