Fix error when attempting actions on "extra" tabs
[sw/motorsim] / gui / com / billkuker / rocketry / motorsim / visual / MultiObjectEditor.java
index 6d17dd476c55d3b151afe0c8aa7e01782e61c44f..b9740b440f6ce3fce2c9587d5dfee53731058f3b 100644 (file)
@@ -53,6 +53,10 @@ public abstract class MultiObjectEditor<OBJECT, EDITOR extends Component> extend
                this.noun = " " + noun.trim();\r
        }\r
        \r
+       protected boolean has(OBJECT o){\r
+               return objectToEditor.containsKey(o);\r
+       }\r
+       \r
        protected void objectAdded(OBJECT o, EDITOR e){}\r
        \r
        protected void objectRemoved(OBJECT o, EDITOR e){}\r
@@ -87,18 +91,19 @@ public abstract class MultiObjectEditor<OBJECT, EDITOR extends Component> extend
        private void menuNew(ObjectCreator c){\r
                add(c.newObject());\r
        }\r
-       \r
+\r
        @SuppressWarnings("unchecked")\r
-       public EDITOR getSelectedEditor(){\r
-               try {\r
-                       return (EDITOR)super.getSelectedComponent();\r
-               } catch ( ClassCastException e ){\r
-                       return null;\r
-               }\r
+       public EDITOR getSelectedEditor() {\r
+               EDITOR e = (EDITOR) super.getSelectedComponent();\r
+               if (editorToObject.containsKey(e))\r
+                       return e;\r
+               return null;\r
        }\r
-       \r
+\r
        private void close(){\r
                EDITOR e = getSelectedEditor();\r
+               if ( e == null )\r
+                       return;\r
                OBJECT o = editorToObject.get(e);\r
                File f = editorToFile.get(e);\r
                \r
@@ -121,6 +126,8 @@ public abstract class MultiObjectEditor<OBJECT, EDITOR extends Component> extend
        \r
        private void saveDialog(){\r
                EDITOR e = getSelectedEditor();\r
+               if ( e == null )\r
+                       return;\r
                if ( !editorToFile.containsKey(e) ){\r
                        log.debug("Editor has no file, saving as...");\r
                        saveAsDialog();\r
@@ -137,6 +144,8 @@ public abstract class MultiObjectEditor<OBJECT, EDITOR extends Component> extend
        }\r
        private void saveAsDialog(){\r
                EDITOR e = getSelectedEditor();\r
+               if ( e == null )\r
+                       return;\r
                final FileDialog fd = new FileDialog(frame, "Save" + noun + " As", FileDialog.SAVE);\r
                fd.setVisible(true);\r
                if (fd.getFile() != null ) {\r
@@ -282,6 +291,16 @@ public abstract class MultiObjectEditor<OBJECT, EDITOR extends Component> extend
                objectAdded(o, e);\r
        }\r
        \r
+       protected final void add(final OBJECT o, final File f){\r
+               EDITOR e = createEditor(o);\r
+               objectToEditor.put(o, e);\r
+               editorToObject.put(e, o);\r
+               fileToEditor.put(f, e);\r
+               editorToFile.put(e, f);\r
+               addTab(f.getName(), e);\r
+               objectAdded(o, e);\r
+       }\r
+       \r
        public final void load(final File f) throws IOException{\r
                OBJECT o = loadFromFile(f);\r
                EDITOR e = createEditor(o);\r