create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / database / Databases.java
1 package net.sf.openrocket.database;
2
3 import net.sf.openrocket.l10n.Translator;
4 import net.sf.openrocket.logging.LogHelper;
5 import net.sf.openrocket.material.Material;
6 import net.sf.openrocket.material.Material.Type;
7 import net.sf.openrocket.material.MaterialStorage;
8 import net.sf.openrocket.startup.Application;
9 import net.sf.openrocket.util.MathUtil;
10
11
12 /**
13  * A class that contains single instances of {@link Database} for specific purposes.
14  * 
15  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
16  */
17 public class Databases {
18         private static final LogHelper log = Application.getLogger();
19         private static final Translator trans = Application.getTranslator();
20         
21         
22         /* Static implementations of specific databases: */
23         
24         /**
25          * A database of bulk materials (with bulk densities).
26          */
27         public static final Database<Material> BULK_MATERIAL = new Database<Material>();
28         /**
29          * A database of surface materials (with surface densities).
30          */
31         public static final Database<Material> SURFACE_MATERIAL = new Database<Material>();
32         /**
33          * A database of linear material (with length densities).
34          */
35         public static final Database<Material> LINE_MATERIAL = new Database<Material>();
36         
37         
38         
39         static {
40                 
41                 // Add default materials
42                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Acrylic", 1190));
43                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Aluminum", 2700));
44                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Balsa", 170));
45                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Basswood", 500));
46                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Birch", 670));
47                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Brass", 8600));
48                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Cardboard", 680));
49                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Carbon fiber", 1780));
50                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Cork", 240));
51                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Depron (XPS)", 40));
52                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Fiberglass", 1850));
53                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Kraft phenolic", 950));
54                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Maple", 755));
55                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Paper (office)", 820));
56                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Pine", 530));
57                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Plywood (birch)", 630));
58                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Polycarbonate (Lexan)", 1200));
59                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Polystyrene", 1050));
60                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "PVC", 1390));
61                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Spruce", 450));
62                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Steel", 7850));
63                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Styrofoam (generic EPS)", 20));
64                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Styrofoam \"Blue foam\" (XPS)", 32));
65                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Titanium", 4500));
66                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Quantum tubing", 1050));
67                 BULK_MATERIAL.add(newMaterial(Material.Type.BULK, "Blue tube", 1300));
68                 
69                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Ripstop nylon", 0.067));
70                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Mylar", 0.021));
71                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Polyethylene (thin)", 0.015));
72                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Polyethylene (heavy)", 0.040));
73                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Silk", 0.060));
74                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Paper (office)", 0.080));
75                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Cellophane", 0.018));
76                 SURFACE_MATERIAL.add(newMaterial(Material.Type.SURFACE, "Cr\u00eape paper", 0.025));
77                 
78                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Thread (heavy-duty)", 0.0003));
79                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Elastic cord (round 2 mm, 1/16 in)", 0.0018));
80                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Elastic cord (flat 6 mm, 1/4 in)", 0.0043));
81                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Elastic cord (flat 12 mm, 1/2 in)", 0.008));
82                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Elastic cord (flat 19 mm, 3/4 in)", 0.0012));
83                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Elastic cord (flat 25 mm, 1 in)", 0.0016));
84                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Braided nylon (2 mm, 1/16 in)", 0.001));
85                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Braided nylon (3 mm, 1/8 in)", 0.0035));
86                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Tubular nylon (11 mm, 7/16 in)", 0.013));
87                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Tubular nylon (14 mm, 9/16 in)", 0.016));
88                 LINE_MATERIAL.add(newMaterial(Material.Type.LINE, "Tubular nylon (25 mm, 1 in)", 0.029));
89                 
90                 
91                 // Add user-defined materials
92                 for (Material m : Application.getPreferences().getUserMaterials()) {
93                         switch (m.getType()) {
94                         case LINE:
95                                 LINE_MATERIAL.add(m);
96                                 break;
97                         
98                         case SURFACE:
99                                 SURFACE_MATERIAL.add(m);
100                                 break;
101                         
102                         case BULK:
103                                 BULK_MATERIAL.add(m);
104                                 break;
105                         
106                         default:
107                                 log.warn("ERROR: Unknown material type " + m);
108                         }
109                 }
110                 
111                 // Add database storage listener
112                 MaterialStorage listener = new MaterialStorage();
113                 LINE_MATERIAL.addDatabaseListener(listener);
114                 SURFACE_MATERIAL.addDatabaseListener(listener);
115                 BULK_MATERIAL.addDatabaseListener(listener);
116         }
117         
118         
119         private static Material newMaterial(Type type, String baseName, double density) {
120                 String name = trans.get("material", baseName);
121                 return Material.newMaterial(type, name, density, false);
122         }
123         
124         
125         
126         
127         /*
128          * Used just for ensuring initialization of the class.
129          */
130         public static void fakeMethod() {
131                 
132         }
133         
134         /**
135          * Find a material from the database with the specified type and name.  Returns
136          * <code>null</code> if the specified material could not be found.
137          * <p>
138          * This method will attempt to localize the material name to the current locale, or use
139          * the provided name if unable to do so.
140          * 
141          * @param type          the material type.
142          * @param baseName      the material base name in the database.
143          * @return                      the material, or <code>null</code> if not found.
144          */
145         public static Material findMaterial(Material.Type type, String baseName) {
146                 Database<Material> db;
147                 switch (type) {
148                 case BULK:
149                         db = BULK_MATERIAL;
150                         break;
151                 case SURFACE:
152                         db = SURFACE_MATERIAL;
153                         break;
154                 case LINE:
155                         db = LINE_MATERIAL;
156                         break;
157                 default:
158                         throw new IllegalArgumentException("Illegal material type: " + type);
159                 }
160                 
161                 String name = trans.get("material", baseName);
162                 
163                 for (Material m : db) {
164                         if (m.getName().equalsIgnoreCase(name)) {
165                                 return m;
166                         }
167                 }
168                 return null;
169         }
170         
171         
172         /**
173          * Find a material from the database or return a new user defined material if the specified
174          * material with the specified density is not found.
175          * <p>
176          * This method will attempt to localize the material name to the current locale, or use
177          * the provided name if unable to do so.
178          * 
179          * @param type                  the material type.
180          * @param baseName                      the base name of the material.
181          * @param density               the density of the material.
182          * @return                              the material object from the database or a new material.
183          */
184         public static Material findMaterial(Material.Type type, String baseName, double density) {
185                 Database<Material> db;
186                 switch (type) {
187                 case BULK:
188                         db = BULK_MATERIAL;
189                         break;
190                 case SURFACE:
191                         db = SURFACE_MATERIAL;
192                         break;
193                 case LINE:
194                         db = LINE_MATERIAL;
195                         break;
196                 default:
197                         throw new IllegalArgumentException("Illegal material type: " + type);
198                 }
199                 
200                 String name = trans.get("material", baseName);
201                 
202                 for (Material m : db) {
203                         if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) {
204                                 return m;
205                         }
206                 }
207                 return Material.newMaterial(type, name, density, true);
208         }
209         
210 }