component config refactoring, localization fixes
[debian/openrocket] / src / net / sf / openrocket / gui / configdialog / ComponentConfigDialog.java
index 0ad6c93ac08d87f9b14699f8cbf148a8359a63fb..5bb3448bc5f8d0f698bc5b2925d2c28ec107ea46 100644 (file)
@@ -1,21 +1,21 @@
 package net.sf.openrocket.gui.configdialog;
 
 
-import java.awt.Point;
 import java.awt.Window;
-import java.awt.event.ComponentAdapter;
-import java.awt.event.ComponentEvent;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 
 import javax.swing.JDialog;
 
 import net.sf.openrocket.document.OpenRocketDocument;
+import net.sf.openrocket.l10n.Translator;
 import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
 import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
 import net.sf.openrocket.rocketcomponent.RocketComponent;
+import net.sf.openrocket.startup.Application;
+import net.sf.openrocket.util.BugException;
 import net.sf.openrocket.util.GUIUtil;
-import net.sf.openrocket.util.Prefs;
+import net.sf.openrocket.util.Reflection;
 
 /**
  * A dialog that contains the configuration elements of one component.
@@ -30,42 +30,29 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
        private static final String CONFIGDIALOGPACKAGE = "net.sf.openrocket.gui.configdialog";
        private static final String CONFIGDIALOGPOSTFIX = "Config";
        
-       
-       private static ComponentConfigDialog dialog = null;
 
+       private static ComponentConfigDialog dialog = null;
        
+
        private OpenRocketDocument document = null;
        private RocketComponent component = null;
        private RocketComponentConfig configurator = null;
        
        private final Window parent;
+       private static final Translator trans = Application.getTranslator();
        
-       private ComponentConfigDialog(Window parent, OpenRocketDocument document, 
+       private ComponentConfigDialog(Window parent, OpenRocketDocument document,
                        RocketComponent component) {
                super(parent);
                this.parent = parent;
                
                setComponent(document, component);
                
-               // Set window position according to preferences, and set prefs when moving
-               Point position = Prefs.getWindowPosition(this.getClass());
-               if (position == null)
-                       this.setLocationByPlatform(true);
-               else
-                       this.setLocation(position);
-
-               this.addComponentListener(new ComponentAdapter() {
-                       @Override
-                       public void componentMoved(ComponentEvent e) {
-                               Prefs.setWindowPosition(ComponentConfigDialog.this.getClass(), 
-                                               ComponentConfigDialog.this.getLocation());
-                       }
-               });
-               
                GUIUtil.setDisposableDialogOptions(this, null);
+               GUIUtil.rememberWindowPosition(this);
        }
        
-
+       
        /**
         * Set the component being configured.  The listening connections of the old configurator
         * will be removed and the new ones created.
@@ -76,10 +63,10 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
                if (this.document != null) {
                        this.document.getRocket().removeComponentChangeListener(this);
                }
-
+               
                if (configurator != null) {
                        // Remove listeners by setting all applicable models to null
-                       GUIUtil.setNullModels(configurator);  // null-safe
+                       GUIUtil.setNullModels(configurator); // null-safe
                }
                
                this.document = document;
@@ -90,11 +77,9 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
                this.setContentPane(configurator);
                configurator.updateFields();
                
-               setTitle(component.getComponentName()+" configuration");
-
-//             Dimension pref = getPreferredSize();
-//             Dimension real = getSize();
-//             if (pref.width > real.width || pref.height > real.height)
+               //// configuration
+               setTitle(trans.get("ComponentCfgDlg.configuration1") + " " + component.getComponentName() + " " + trans.get("ComponentCfgDlg.configuration"));
+               
                this.pack();
        }
        
@@ -102,32 +87,59 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
         * Return the configurator panel of the current component.
         */
        private RocketComponentConfig getDialogContents() {
-               Constructor<? extends RocketComponentConfig> c = 
-                       findDialogContentsConstructor(component);
+               Constructor<? extends RocketComponentConfig> c =
+                               findDialogContentsConstructor(component);
                if (c != null) {
                        try {
-                               return (RocketComponentConfig) c.newInstance(component);
+                               return c.newInstance(document, component);
                        } catch (InstantiationException e) {
-                               throw new RuntimeException("BUG in constructor reflection",e);
+                               throw new BugException("BUG in constructor reflection", e);
                        } catch (IllegalAccessException e) {
-                               throw new RuntimeException("BUG in constructor reflection",e);
+                               throw new BugException("BUG in constructor reflection", e);
                        } catch (InvocationTargetException e) {
-                               throw new RuntimeException("BUG in constructor reflection",e);
+                               throw Reflection.handleWrappedException(e);
                        }
                }
                
                // Should never be reached, since RocketComponentConfig should catch all
                // components without their own configurator.
-               throw new RuntimeException("Unable to find any configurator for "+component);
+               throw new BugException("Unable to find any configurator for " + component);
        }
-
+       
+       
+       private void closeDialog() {
+               this.setVisible(false);
+               this.dispose();
+               this.configurator.invalidateModels();
+       }
+       
+       
+       @Override
+       public void componentChanged(ComponentChangeEvent e) {
+               if (e.isTreeChange() || e.isUndoChange()) {
+                       
+                       // Hide dialog in case of tree or undo change
+                       dialog.closeDialog();
+                       
+               } else {
+                       /*
+                        * TODO: HIGH:  The line below has caused a NullPointerException (without null check)
+                        * How is this possible?  The null check was added to avoid this, but the
+                        * root cause should be analyzed.
+                        * [Openrocket-bugs] 2009-12-12 19:23:22 Automatic bug report for OpenRocket 0.9.5
+                        */
+                       if (configurator != null)
+                               configurator.updateFields();
+               }
+       }
+       
+       
        /**
         * Finds the Constructor of the given component's config dialog panel in 
         * CONFIGDIALOGPACKAGE.
         */
        @SuppressWarnings("unchecked")
-       private static Constructor<? extends RocketComponentConfig> 
-                       findDialogContentsConstructor(RocketComponent component) {
+       private static Constructor<? extends RocketComponentConfig> findDialogContentsConstructor(RocketComponent component) {
                Class<?> currentclass;
                String currentclassname;
                String configclassname;
@@ -141,23 +153,24 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
                        int index = currentclassname.lastIndexOf('.');
                        if (index >= 0)
                                currentclassname = currentclassname.substring(index + 1);
-                       configclassname = CONFIGDIALOGPACKAGE + "." + currentclassname + 
-                               CONFIGDIALOGPOSTFIX;
+                       configclassname = CONFIGDIALOGPACKAGE + "." + currentclassname +
+                                       CONFIGDIALOGPOSTFIX;
                        
                        try {
                                configclass = Class.forName(configclassname);
                                c = (Constructor<? extends RocketComponentConfig>)
-                                       configclass.getConstructor(RocketComponent.class);
+                                               configclass.getConstructor(OpenRocketDocument.class, RocketComponent.class);
                                return c;
-                       } catch (Exception ignore) { }
-
+                       } catch (Exception ignore) {
+                       }
+                       
                        currentclass = currentclass.getSuperclass();
                }
                return null;
        }
        
        
-       
+
 
        //////////  Static dialog  /////////
        
@@ -168,7 +181,7 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
         * @param document              the document to configure.
         * @param component             the component to configure.
         */
-       public static void showDialog(Window parent, OpenRocketDocument document, 
+       public static void showDialog(Window parent, OpenRocketDocument document,
                        RocketComponent component) {
                if (dialog != null)
                        dialog.dispose();
@@ -176,11 +189,12 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
                dialog = new ComponentConfigDialog(parent, document, component);
                dialog.setVisible(true);
                
-               document.addUndoPosition("Modify "+component.getComponentName());
+               ////Modify
+               document.addUndoPosition(trans.get("ComponentCfgDlg.Modify") + " " + component.getComponentName());
        }
        
        
-       /* package */ 
+       /* package */
        static void showDialog(RocketComponent component) {
                showDialog(dialog.parent, dialog.document, component);
        }
@@ -189,49 +203,17 @@ public class ComponentConfigDialog extends JDialog implements ComponentChangeLis
         * Hides the configuration dialog.  May be used even if not currently visible.
         */
        public static void hideDialog() {
-               if (dialog != null)
-                       dialog.setVisible(false);
-       }
-
-       
-       /**
-        * Add an undo position for the current document.  This is intended for use only
-        * by the currently open dialog.
-        * 
-        * @param description  Description of the undoable action
-        */
-       /*package*/ static void addUndoPosition(String description) {
-               if (dialog == null) {
-                       throw new IllegalStateException("Dialog not open, report bug!");
+               if (dialog != null) {
+                       dialog.closeDialog();
                }
-               dialog.document.addUndoPosition(description);
        }
        
-       /*package*/
-       static String getUndoDescription() {
-               if (dialog == null) {
-                       throw new IllegalStateException("Dialog not open, report bug!");
-               }
-               return dialog.document.getUndoDescription();
-       }
        
        /**
         * Returns whether the singleton configuration dialog is currently visible or not.
         */
        public static boolean isDialogVisible() {
-               return (dialog!=null) && (dialog.isVisible());
-       }
-
-
-       public void componentChanged(ComponentChangeEvent e) {
-               if (e.isTreeChange() || e.isUndoChange()) {
-                       
-                       // Hide dialog in case of tree or undo change
-                       dialog.setVisible(false);
-
-               } else {
-                       configurator.updateFields();
-               }
+               return (dialog != null) && (dialog.isVisible());
        }
        
 }