23cea24074368f56983fd6fa6e20a2e8b8f122ab
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / ExternalComponent.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import java.util.List;
4
5 import net.sf.openrocket.l10n.Translator;
6 import net.sf.openrocket.material.Material;
7 import net.sf.openrocket.material.Material.Type;
8 import net.sf.openrocket.preset.ExternalComponentPreset;
9 import net.sf.openrocket.preset.RocketComponentPreset;
10 import net.sf.openrocket.startup.Application;
11 import net.sf.openrocket.unit.UnitGroup;
12 import net.sf.openrocket.util.Prefs;
13
14 /**
15  * Class of components with well-defined physical appearance and which have an effect on
16  * aerodynamic simulation.  They have material defined for them, and the mass of the component
17  * is calculated using the component's volume.
18  *
19  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20  */
21
22 public abstract class ExternalComponent extends RocketComponent {
23         
24         public enum Finish {
25                 //// Rough
26                 ROUGH("ExternalComponent.Rough", 500e-6),
27                 //// Unfinished
28                 UNFINISHED("ExternalComponent.Unfinished", 150e-6),
29                 //// Regular paint
30                 NORMAL("ExternalComponent.Regularpaint", 60e-6),
31                 //// Smooth paint
32                 SMOOTH("ExternalComponent.Smoothpaint", 20e-6),
33                 //// Polished
34                 POLISHED("ExternalComponent.Polished", 2e-6);
35                 
36                 private static final Translator trans = Application.getTranslator();
37                 private final String name;
38                 private final double roughnessSize;
39                 
40                 Finish(String name, double roughness) {
41                         this.name = name;
42                         this.roughnessSize = roughness;
43                 }
44                 
45                 public double getRoughnessSize() {
46                         return roughnessSize;
47                 }
48                 
49                 @Override
50                 public String toString() {
51                         return trans.get(name) + " (" + UnitGroup.UNITS_ROUGHNESS.toStringUnit(roughnessSize) + ")";
52                 }
53         }
54         
55         
56         /**
57          * The material of the component.
58          */
59         protected Material material = null;
60         
61         protected Finish finish = Finish.NORMAL;
62         
63         
64
65         /**
66          * Constructor that sets the relative position of the component.
67          */
68         public ExternalComponent(RocketComponent.Position relativePosition) {
69                 super(relativePosition);
70                 this.material = Prefs.getDefaultComponentMaterial(this.getClass(), Material.Type.BULK);
71         }
72         
73         /**
74          * Returns the volume of the component.  This value is used in calculating the mass
75          * of the object.
76          */
77         public abstract double getComponentVolume();
78         
79         /**
80          * Calculates the mass of the component as the product of the volume and interior density.
81          */
82         @Override
83         public double getComponentMass() {
84                 return material.getDensity() * getComponentVolume();
85         }
86         
87         /**
88          * ExternalComponent has aerodynamic effect, so return true.
89          */
90         @Override
91         public boolean isAerodynamic() {
92                 return true;
93         }
94         
95         /**
96          * ExternalComponent has effect on the mass, so return true.
97          */
98         @Override
99         public boolean isMassive() {
100                 return true;
101         }
102         
103         
104         public Material getMaterial() {
105                 return material;
106         }
107         
108         public void setMaterial(Material mat) {
109                 if (mat.getType() != Material.Type.BULK) {
110                         throw new IllegalArgumentException("ExternalComponent requires a bulk material" +
111                                         " type=" + mat.getType());
112                 }
113                 
114                 if (material.equals(mat))
115                         return;
116                 material = mat;
117                 clearPreset();
118                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
119         }
120         
121         public Finish getFinish() {
122                 return finish;
123         }
124         
125         public void setFinish(Finish finish) {
126                 if (this.finish == finish)
127                         return;
128                 this.finish = finish;
129                 fireComponentChangeEvent(ComponentChangeEvent.AERODYNAMIC_CHANGE);
130         }
131         
132         
133         @Override
134         protected void loadFromPreset(RocketComponentPreset preset) {
135                 super.loadFromPreset(preset);
136                 
137                 ExternalComponentPreset p = (ExternalComponentPreset) preset;
138                 String materialName = p.getMaterialName();
139                 double mass = p.getMass();
140                 
141                 double volume = getComponentVolume();
142                 double density;
143                 if (volume > 0.00001) {
144                         density = mass / volume;
145                 } else {
146                         density = 1000;
147                 }
148                 
149                 Material mat = Material.newMaterial(Type.BULK, materialName, density, true);
150                 setMaterial(mat);
151         }
152         
153         
154         @Override
155         protected List<RocketComponent> copyFrom(RocketComponent c) {
156                 ExternalComponent src = (ExternalComponent) c;
157                 this.finish = src.finish;
158                 this.material = src.material;
159                 return super.copyFrom(c);
160         }
161         
162 }