When creating a new user material from an existing material (ie, key), make certain to use the name associated with the material because the key might not be localizable.

This commit is contained in:
Kevin Ruland 2012-08-04 08:37:39 +00:00
parent f9c450c79c
commit a373126c41
3 changed files with 73 additions and 63 deletions

View File

@ -14,9 +14,9 @@ import net.sf.openrocket.util.MathUtil;
*/ */
public class Databases { public class Databases {
private static final LogHelper log = Application.getLogger(); private static final LogHelper log = Application.getLogger();
/* Static implementations of specific databases: */ /* Static implementations of specific databases: */
/** /**
* A database of bulk materials (with bulk densities). * A database of bulk materials (with bulk densities).
*/ */
@ -29,11 +29,11 @@ public class Databases {
* A database of linear material (with length densities). * A database of linear material (with length densities).
*/ */
public static final Database<Material> LINE_MATERIAL = new Database<Material>(); public static final Database<Material> LINE_MATERIAL = new Database<Material>();
static { static {
// Add default materials // Add default materials
BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Acrylic", 1190)); BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Acrylic", 1190));
BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Aluminum", 2700)); BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Aluminum", 2700));
@ -62,7 +62,7 @@ public class Databases {
BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Titanium", 4500)); BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Titanium", 4500));
BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Quantumtubing", 1050)); BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Quantumtubing", 1050));
BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"BlueTube", 1300)); BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"BlueTube", 1300));
SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Ripstopnylon", 0.067)); SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Ripstopnylon", 0.067));
SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Mylar", 0.021)); SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Mylar", 0.021));
SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Polyethylenethin", 0.015)); SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Polyethylenethin", 0.015));
@ -71,7 +71,7 @@ public class Databases {
SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Paperoffice", 0.080)); SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Paperoffice", 0.080));
SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Cellophane", 0.018)); SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Cellophane", 0.018));
SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Crepepaper", 0.025)); SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Crepepaper", 0.025));
//// Thread (heavy-duty) //// Thread (heavy-duty)
LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Threadheavy-duty", 0.0003)); LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Threadheavy-duty", 0.0003));
//// Elastic cord (round 2mm, 1/16 in) //// Elastic cord (round 2mm, 1/16 in)
@ -94,44 +94,44 @@ public class Databases {
LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon14mm", 0.016)); LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon14mm", 0.016));
//// Tubular nylon (25 mm, 1 in) //// Tubular nylon (25 mm, 1 in)
LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon25mm", 0.029)); LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon25mm", 0.029));
// Add user-defined materials // Add user-defined materials
for (Material m : Application.getPreferences().getUserMaterials()) { for (Material m : Application.getPreferences().getUserMaterials()) {
switch (m.getType()) { switch (m.getType()) {
case LINE: case LINE:
LINE_MATERIAL.add(m); LINE_MATERIAL.add(m);
break; break;
case SURFACE: case SURFACE:
SURFACE_MATERIAL.add(m); SURFACE_MATERIAL.add(m);
break; break;
case BULK: case BULK:
BULK_MATERIAL.add(m); BULK_MATERIAL.add(m);
break; break;
default: default:
log.warn("ERROR: Unknown material type " + m); log.warn("ERROR: Unknown material type " + m);
} }
} }
// Add database storage listener // Add database storage listener
MaterialStorage listener = new MaterialStorage(); MaterialStorage listener = new MaterialStorage();
LINE_MATERIAL.addDatabaseListener(listener); LINE_MATERIAL.addDatabaseListener(listener);
SURFACE_MATERIAL.addDatabaseListener(listener); SURFACE_MATERIAL.addDatabaseListener(listener);
BULK_MATERIAL.addDatabaseListener(listener); BULK_MATERIAL.addDatabaseListener(listener);
} }
/* /*
* Used just for ensuring initialization of the class. * Used just for ensuring initialization of the class.
*/ */
public static void fakeMethod() { public static void fakeMethod() {
} }
/** /**
* Find a material from the database with the specified type and name. Returns * Find a material from the database with the specified type and name. Returns
* <code>null</code> if the specified material could not be found. * <code>null</code> if the specified material could not be found.
@ -155,7 +155,7 @@ public class Databases {
default: default:
throw new IllegalArgumentException("Illegal material type: " + type); throw new IllegalArgumentException("Illegal material type: " + type);
} }
for (Material m : db) { for (Material m : db) {
if (m.getName().equalsIgnoreCase(name)) { if (m.getName().equalsIgnoreCase(name)) {
return m; return m;
@ -163,8 +163,8 @@ public class Databases {
} }
return null; return null;
} }
/** /**
* Find a material from the database or return a new user defined material if the specified * Find a material from the database or return a new user defined material if the specified
* material with the specified density is not found. * material with the specified density is not found.
@ -189,17 +189,22 @@ public class Databases {
default: default:
throw new IllegalArgumentException("Illegal material type: " + type); throw new IllegalArgumentException("Illegal material type: " + type);
} }
Material bestMatch = null; Material bestMatch = null;
// Alter the search mechanism to handle older specifications. // Alter the search mechanism to handle older specifications.
// If a key is specified, then we match on key, if one is found. // If a key is specified, then we match on key, if one is found.
// Otherwise we return the material which matches on name. // Otherwise we return the material which matches on name.
// this requires us to loop through the entire db at least once to look for the key. // this requires us to loop through the entire db at least once to look for the key.
for (Material m : db) { for (Material m : db) {
// perfect match based on key. // perfect match based on key.
if ( key != null && m.getKey().equals(key) && MathUtil.equals(m.getDensity(), density) ) { if ( key != null && m.getKey().equals(key) ) {
return m; // Exact match
if ( MathUtil.equals(m.getDensity(), density) ) {
return m;
}
// Custom material with standard name
return Material.newUserMaterialWithKey(type, key, m.getName(), density);
} }
if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) { if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) {
bestMatch = m; bestMatch = m;
@ -210,6 +215,6 @@ public class Databases {
} }
return Material.newUserMaterial(type, name, density); return Material.newUserMaterial(type, name, density);
} }
} }

View File

@ -26,7 +26,7 @@ import net.sf.openrocket.startup.Application;
public class CustomMaterialDialog extends JDialog { public class CustomMaterialDialog extends JDialog {
private final Material originalMaterial; private final Material originalMaterial;
private boolean okClicked = false; private boolean okClicked = false;
private JComboBox typeBox; private JComboBox typeBox;
private JTextField nameField; private JTextField nameField;
@ -40,18 +40,18 @@ public class CustomMaterialDialog extends JDialog {
String title) { String title) {
this(parent, material, saveOption, title, null); this(parent, material, saveOption, title, null);
} }
public CustomMaterialDialog(Window parent, Material material, boolean saveOption, public CustomMaterialDialog(Window parent, Material material, boolean saveOption,
String title, String note) { String title, String note) {
//// Custom material //// Custom material
super(parent, trans.get("custmatdlg.title.Custommaterial"), Dialog.ModalityType.APPLICATION_MODAL); super(parent, trans.get("custmatdlg.title.Custommaterial"), Dialog.ModalityType.APPLICATION_MODAL);
this.originalMaterial = material; this.originalMaterial = material;
JPanel panel = new JPanel(new MigLayout("fill, gap rel unrel")); JPanel panel = new JPanel(new MigLayout("fill, gap rel unrel"));
// Add title and note // Add title and note
if (title != null) { if (title != null) {
panel.add(new JLabel("<html><b>" + title + ":"), panel.add(new JLabel("<html><b>" + title + ":"),
@ -60,7 +60,7 @@ public class CustomMaterialDialog extends JDialog {
if (note != null) { if (note != null) {
panel.add(new StyledLabel(note, -1), "span, wrap para"); panel.add(new StyledLabel(note, -1), "span, wrap para");
} }
//// Material name //// Material name
panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialname"))); panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialname")));
@ -69,8 +69,8 @@ public class CustomMaterialDialog extends JDialog {
nameField.setText(material.getName()); nameField.setText(material.getName());
} }
panel.add(nameField, "span, growx, wrap"); panel.add(nameField, "span, growx, wrap");
// Material type (if not known) // Material type (if not known)
panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialtype"))); panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialtype")));
if (material == null) { if (material == null) {
@ -87,8 +87,8 @@ public class CustomMaterialDialog extends JDialog {
} else { } else {
panel.add(new JLabel(material.getType().toString()), "span, growx, wrap"); panel.add(new JLabel(material.getType().toString()), "span, growx, wrap");
} }
// Material density: // Material density:
panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialdensity"))); panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialdensity")));
densitySpinner = new JSpinner(); densitySpinner = new JSpinner();
@ -97,18 +97,18 @@ public class CustomMaterialDialog extends JDialog {
panel.add(densityUnit, "w 30lp"); panel.add(densityUnit, "w 30lp");
panel.add(new JPanel(), "growx, wrap"); panel.add(new JPanel(), "growx, wrap");
updateDensityModel(); updateDensityModel();
// Save option // Save option
if (saveOption) { if (saveOption) {
//// Add material to database //// Add material to database
addBox = new JCheckBox(trans.get("custmatdlg.checkbox.Addmaterial")); addBox = new JCheckBox(trans.get("custmatdlg.checkbox.Addmaterial"));
panel.add(addBox,"span, wrap"); panel.add(addBox,"span, wrap");
} }
//// OK button //// OK button
JButton okButton = new JButton(trans.get("dlg.but.ok")); JButton okButton = new JButton(trans.get("dlg.but.ok"));
okButton.addActionListener(new ActionListener() { okButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -117,7 +117,7 @@ public class CustomMaterialDialog extends JDialog {
} }
}); });
panel.add(okButton,"span, split, tag ok"); panel.add(okButton,"span, split, tag ok");
//// Cancel //// Cancel
JButton closeButton = new JButton(trans.get("dlg.but.cancel")); JButton closeButton = new JButton(trans.get("dlg.but.cancel"));
closeButton.addActionListener(new ActionListener() { closeButton.addActionListener(new ActionListener() {
@ -128,43 +128,48 @@ public class CustomMaterialDialog extends JDialog {
} }
}); });
panel.add(closeButton,"tag cancel"); panel.add(closeButton,"tag cancel");
this.setContentPane(panel); this.setContentPane(panel);
this.pack(); this.pack();
this.setLocationByPlatform(true); this.setLocationByPlatform(true);
GUIUtil.setDisposableDialogOptions(this, okButton); GUIUtil.setDisposableDialogOptions(this, okButton);
} }
public boolean getOkClicked() { public boolean getOkClicked() {
return okClicked; return okClicked;
} }
public boolean isAddSelected() { public boolean isAddSelected() {
return addBox.isSelected(); return addBox.isSelected();
} }
public Material getMaterial() { public Material getMaterial() {
Material.Type type; Material.Type type;
String name; String name;
double density; double density;
if (typeBox != null) { if (typeBox != null) {
type = (Material.Type) typeBox.getSelectedItem(); type = (Material.Type) typeBox.getSelectedItem();
} else { } else {
type = originalMaterial.getType(); type = originalMaterial.getType();
} }
name = nameField.getText().trim(); name = nameField.getText().trim();
density = this.density.getValue(); density = this.density.getValue();
return Material.newUserMaterial(type, name, density); // If the name has not changed from the original name and we started with a system material.
if ( name.equals( originalMaterial.getName()) ) {
return Material.newUserMaterialWithKey(type, originalMaterial.getKey(), originalMaterial.getName(), density);
} else {
return Material.newUserMaterial(type, name, density);
}
} }
private void updateDensityModel() { private void updateDensityModel() {
if (originalMaterial != null) { if (originalMaterial != null) {
if (density == null) { if (density == null) {

View File

@ -218,16 +218,16 @@ public abstract class Material implements Comparable<Material> {
/** /**
* Return a new user defined material of the specified type and localizable key. * Return a new user defined material of the specified type and localizable key.
*/ */
public static Material newUserMaterialWithKey(Type type, String key, double density) { public static Material newUserMaterialWithKey(Type type, String key, String name, double density) {
switch (type) { switch (type) {
case LINE: case LINE:
return new Material.Line(null, key, density, true); return new Material.Line(name, key, density, true);
case SURFACE: case SURFACE:
return new Material.Surface(null, key, density, true); return new Material.Surface(name, key, density, true);
case BULK: case BULK:
return new Material.Bulk(null, key, density, true); return new Material.Bulk(name, key, density, true);
default: default:
throw new IllegalArgumentException("Unknown material type: " + type); throw new IllegalArgumentException("Unknown material type: " + type);