When creating a new user material from an existing material (ie, key), make certain...
[debian/openrocket] / core / src / net / sf / openrocket / gui / dialogs / CustomMaterialDialog.java
index 8172e1fe08bb28f10ebf5c0c24cc84554129fe2a..ed1d97cf1a0516f5662d084be2688a387b5babf9 100644 (file)
@@ -26,7 +26,7 @@ import net.sf.openrocket.startup.Application;
 public class CustomMaterialDialog extends JDialog {
 
        private final Material originalMaterial;
-       
+
        private boolean okClicked = false;
        private JComboBox typeBox;
        private JTextField nameField;
@@ -40,18 +40,18 @@ public class CustomMaterialDialog extends JDialog {
                        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 + ":"), 
@@ -60,7 +60,7 @@ public class CustomMaterialDialog extends JDialog {
                if (note != null) {
                        panel.add(new StyledLabel(note, -1), "span, wrap para");
                }
-               
+
 
                //// Material name
                panel.add(new JLabel(trans.get("custmatdlg.lbl.Materialname")));
@@ -69,8 +69,8 @@ public class CustomMaterialDialog extends JDialog {
                        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) {
@@ -87,8 +87,8 @@ public class CustomMaterialDialog extends JDialog {
                } 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();
@@ -97,18 +97,18 @@ public class CustomMaterialDialog extends JDialog {
                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) {
@@ -117,7 +117,7 @@ public class CustomMaterialDialog extends JDialog {
                        }
                });
                panel.add(okButton,"span, split, tag ok");
-               
+
                ////  Cancel
                JButton closeButton = new JButton(trans.get("dlg.but.cancel"));
                closeButton.addActionListener(new ActionListener() {
@@ -128,43 +128,48 @@ public class CustomMaterialDialog extends JDialog {
                        }
                });
                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) {