Add option to select material group in material editor

This commit is contained in:
SiboVG 2024-07-24 22:48:21 +02:00
parent 5d45d075ce
commit 8dcb120cc5
3 changed files with 40 additions and 8 deletions

View File

@ -189,18 +189,35 @@ public class Databases {
* @param type the material type.
* @param baseName the base name of the material.
* @param density the density of the material.
* @param group the material group.
* @return the material object from the database or a new material.
*/
public static Material findMaterial(Material.Type type, String baseName, double density) {
public static Material findMaterial(Material.Type type, String baseName, double density, MaterialGroup group) {
Database<Material> db = getDatabase(type);
String name = trans.get("material", baseName);
for (Material m : db) {
if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) {
if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density) && m.getGroup() == group) {
return m;
}
}
return Material.newMaterial(type, name, density, true);
return Material.newMaterial(type, name, density, group, true);
}
/**
* Find a material from the database or return a new user defined material if the specified
* material with the specified density is not found.
* <p>
* This method will attempt to localize the material name to the current locale, or use
* the provided name if unable to do so.
*
* @param type the material type.
* @param baseName the base name of the material.
* @param density the density of the material.
* @return the material object from the database or a new material.
*/
public static Material findMaterial(Material.Type type, String baseName, double density) {
return findMaterial(type, baseName, density, null);
}
/**

View File

@ -939,6 +939,7 @@ custmatdlg.title.Custommaterial = Custom material
custmatdlg.lbl.Materialname = Material name:
custmatdlg.lbl.Materialtype = Material type:
custmatdlg.lbl.Materialdensity = Material density:
custmatdlg.lbl.MaterialGroup = Material group:
custmatdlg.checkbox.Addmaterial = Add material to application database

View File

@ -21,6 +21,7 @@ import javax.swing.JTextField;
import info.openrocket.core.database.Databases;
import info.openrocket.core.l10n.Translator;
import info.openrocket.core.material.Material;
import info.openrocket.core.material.MaterialGroup;
import info.openrocket.core.startup.Application;
import net.miginfocom.swing.MigLayout;
@ -39,10 +40,11 @@ public class CustomMaterialDialog extends JDialog {
private boolean okClicked = false;
private JComboBox<Material.Type> typeBox;
private JTextField nameField;
private final JTextField nameField;
private DoubleModel density;
private JSpinner densitySpinner;
private UnitSelector densityUnit;
private final JSpinner densitySpinner;
private final UnitSelector densityUnit;
private JComboBox<MaterialGroup> groupBox;
private JCheckBox addBox;
public CustomMaterialDialog(Window parent, Material material, boolean saveOption, boolean addToApplicationDatabase,
@ -118,6 +120,17 @@ public class CustomMaterialDialog extends JDialog {
updateDensityModel();
// Material group
panel.add(new JLabel(trans.get("custmatdlg.lbl.MaterialGroup")));
groupBox = new JComboBox<>(MaterialGroup.ALL_GROUPS);
if (material != null && !material.isUserDefined()) {
groupBox.setSelectedItem(material.getEquivalentGroup());
} else {
groupBox.setSelectedItem(MaterialGroup.CUSTOM);
}
panel.add(groupBox, "span, growx, wrap");
// Save option
if (saveOption) {
//// Add material to application database
@ -175,6 +188,7 @@ public class CustomMaterialDialog extends JDialog {
Material.Type type;
String name;
double materialDensity;
MaterialGroup group;
if (typeBox != null) {
type = (Material.Type) typeBox.getSelectedItem();
@ -183,10 +197,10 @@ public class CustomMaterialDialog extends JDialog {
}
name = nameField.getText().trim();
materialDensity = this.density.getValue();
group = (MaterialGroup) groupBox.getSelectedItem();
return Databases.findMaterial(type, name, materialDensity);
return Databases.findMaterial(type, name, materialDensity, group);
}