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