public class CustomMaterialDialog extends JDialog {
private final Material originalMaterial;
-
+
private boolean okClicked = false;
private JComboBox typeBox;
private JTextField nameField;
String title) {
this(parent, material, saveOption, title, null);
}
-
-
+
+
public CustomMaterialDialog(Window parent, Material material, boolean saveOption,
String title, String note) {
//// Custom material
super(parent, trans.get("custmatdlg.title.Custommaterial"), Dialog.ModalityType.APPLICATION_MODAL);
-
+
this.originalMaterial = material;
-
+
JPanel panel = new JPanel(new MigLayout("fill, gap rel unrel"));
-
-
+
+
// Add title and note
if (title != null) {
panel.add(new JLabel("<html><b>" + title + ":"),
if (note != null) {
panel.add(new StyledLabel(note, -1), "span, wrap para");
}
-
+
//// Material name
panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialname")));
nameField.setText(material.getName());
}
panel.add(nameField, "span, growx, wrap");
-
-
+
+
// Material type (if not known)
panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialtype")));
if (material == null) {
} else {
panel.add(new JLabel(material.getType().toString()), "span, growx, wrap");
}
-
-
+
+
// Material density:
panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialdensity")));
densitySpinner = new JSpinner();
panel.add(densityUnit, "w 30lp");
panel.add(new JPanel(), "growx, wrap");
updateDensityModel();
-
-
+
+
// Save option
if (saveOption) {
//// Add material to database
addBox = new JCheckBox(trans.get("custmatdlg.checkbox.Addmaterial"));
panel.add(addBox,"span, wrap");
}
-
+
//// OK button
JButton okButton = new JButton(trans.get("dlg.but.ok"));
-
+
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
panel.add(okButton,"span, split, tag ok");
-
+
//// Cancel
JButton closeButton = new JButton(trans.get("dlg.but.cancel"));
closeButton.addActionListener(new ActionListener() {
}
});
panel.add(closeButton,"tag cancel");
-
+
this.setContentPane(panel);
this.pack();
this.setLocationByPlatform(true);
GUIUtil.setDisposableDialogOptions(this, okButton);
}
-
-
+
+
public boolean getOkClicked() {
return okClicked;
}
-
-
+
+
public boolean isAddSelected() {
return addBox.isSelected();
}
-
-
+
+
public Material getMaterial() {
Material.Type type;
String name;
double density;
-
+
if (typeBox != null) {
type = (Material.Type) typeBox.getSelectedItem();
} else {
type = originalMaterial.getType();
}
-
+
name = nameField.getText().trim();
-
+
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() {
if (originalMaterial != null) {
if (density == null) {