preset component framework
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / ExternalComponent.java
index 0f19b43057a2596f1a9cad8ecff84f31a2640e98..23cea24074368f56983fd6fa6e20a2e8b8f122ab 100644 (file)
@@ -4,15 +4,18 @@ import java.util.List;
 
 import net.sf.openrocket.l10n.Translator;
 import net.sf.openrocket.material.Material;
+import net.sf.openrocket.material.Material.Type;
+import net.sf.openrocket.preset.ExternalComponentPreset;
+import net.sf.openrocket.preset.RocketComponentPreset;
 import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.unit.UnitGroup;
 import net.sf.openrocket.util.Prefs;
 
 /**
  * Class of components with well-defined physical appearance and which have an effect on
- * aerodynamic simulation.  They have material defined for them, and the mass of the component 
+ * aerodynamic simulation.  They have material defined for them, and the mass of the component
  * is calculated using the component's volume.
- * 
+ *
  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
  */
 
@@ -30,6 +33,7 @@ public abstract class ExternalComponent extends RocketComponent {
                //// Polished
                POLISHED("ExternalComponent.Polished", 2e-6);
                
+               private static final Translator trans = Application.getTranslator();
                private final String name;
                private final double roughnessSize;
                
@@ -44,7 +48,6 @@ public abstract class ExternalComponent extends RocketComponent {
                
                @Override
                public String toString() {
-                       final Translator trans = Application.getTranslator();
                        return trans.get(name) + " (" + UnitGroup.UNITS_ROUGHNESS.toStringUnit(roughnessSize) + ")";
                }
        }
@@ -111,6 +114,7 @@ public abstract class ExternalComponent extends RocketComponent {
                if (material.equals(mat))
                        return;
                material = mat;
+               clearPreset();
                fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
        }
        
@@ -126,7 +130,27 @@ public abstract class ExternalComponent extends RocketComponent {
        }
        
        
-
+       @Override
+       protected void loadFromPreset(RocketComponentPreset preset) {
+               super.loadFromPreset(preset);
+               
+               ExternalComponentPreset p = (ExternalComponentPreset) preset;
+               String materialName = p.getMaterialName();
+               double mass = p.getMass();
+               
+               double volume = getComponentVolume();
+               double density;
+               if (volume > 0.00001) {
+                       density = mass / volume;
+               } else {
+                       density = 1000;
+               }
+               
+               Material mat = Material.newMaterial(Type.BULK, materialName, density, true);
+               setMaterial(mat);
+       }
+       
+       
        @Override
        protected List<RocketComponent> copyFrom(RocketComponent c) {
                ExternalComponent src = (ExternalComponent) c;
@@ -135,14 +159,4 @@ public abstract class ExternalComponent extends RocketComponent {
                return super.copyFrom(c);
        }
        
-    /**
-     * Accept a visitor to this ExternalComponent in the component hierarchy.
-     * 
-     * @param theVisitor  the visitor that will be called back with a reference to this ExternalComponent
-     */
-    @Override 
-    public void accept (final ComponentVisitor theVisitor) {
-        theVisitor.visit(this);
-    }
-    
 }