Updates for 0.9.5
[debian/openrocket] / src / net / sf / openrocket / gui / adaptors / DoubleModel.java
index 0004dbc4659ceb4ad38497830422a3707bc8bd3a..6616b81c9cac7b4e54a17181f4ba80cee9b0b398 100644 (file)
@@ -17,8 +17,10 @@ import javax.swing.event.ChangeListener;
 
 import net.sf.openrocket.unit.Unit;
 import net.sf.openrocket.unit.UnitGroup;
+import net.sf.openrocket.util.BugException;
 import net.sf.openrocket.util.ChangeSource;
 import net.sf.openrocket.util.MathUtil;
+import net.sf.openrocket.util.Reflection;
 
 
 /**
@@ -37,6 +39,8 @@ import net.sf.openrocket.util.MathUtil;
 
 public class DoubleModel implements ChangeListener, ChangeSource {
        private static final boolean DEBUG_LISTENERS = false;
+       
+       public static final DoubleModel ZERO = new DoubleModel(0);
 
        //////////// JSpinner Model ////////////
        
@@ -377,16 +381,17 @@ public class DoubleModel implements ChangeListener, ChangeSource {
                }
 
                // Implement a wrapper to the ChangeListeners
-               ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
+               ArrayList<PropertyChangeListener> propertyChangeListeners = 
+                       new ArrayList<PropertyChangeListener>();
                @Override
                public void addPropertyChangeListener(PropertyChangeListener listener) {
-                       listeners.add(listener);
+                       propertyChangeListeners.add(listener);
                        DoubleModel.this.addChangeListener(this);
                }
                @Override
                public void removePropertyChangeListener(PropertyChangeListener listener) {
-                       listeners.remove(listener);
-                       if (listeners.isEmpty())
+                       propertyChangeListeners.remove(listener);
+                       if (propertyChangeListeners.isEmpty())
                                DoubleModel.this.removeChangeListener(this);
                }
                // If the value has changed, generate an event to the listeners
@@ -397,7 +402,7 @@ public class DoubleModel implements ChangeListener, ChangeSource {
                        PropertyChangeEvent event = new PropertyChangeEvent(this,Action.SELECTED_KEY,
                                        oldValue,newValue);
                        oldValue = newValue;
-                       Object[] l = listeners.toArray();
+                       Object[] l = propertyChangeListeners.toArray();
                        for (int i=0; i<l.length; i++) {
                                ((PropertyChangeListener)l[i]).propertyChange(event);
                        }
@@ -586,13 +591,12 @@ public class DoubleModel implements ChangeListener, ChangeSource {
                try {
                        return (Double)getMethod.invoke(source)*multiplier;
                } catch (IllegalArgumentException e) {
-                       e.printStackTrace();
+                       throw new BugException("BUG: Unable to invoke getMethod of "+this, e);
                } catch (IllegalAccessException e) {
-                       e.printStackTrace();
+                       throw new BugException("BUG: Unable to invoke getMethod of "+this, e);
                } catch (InvocationTargetException e) {
-                       e.printStackTrace();
+                       throw Reflection.handleInvocationTargetException(e);
                }
-               return lastValue;  // Should not occur
        }
        
        /**
@@ -612,15 +616,13 @@ public class DoubleModel implements ChangeListener, ChangeSource {
 
                try {
                        setMethod.invoke(source, v/multiplier);
-                       return;
                } catch (IllegalArgumentException e) {
-                       e.printStackTrace();
+                       throw new BugException("BUG: Unable to invoke setMethod of "+this, e);
                } catch (IllegalAccessException e) {
-                       e.printStackTrace();
+                       throw new BugException("BUG: Unable to invoke setMethod of "+this, e);
                } catch (InvocationTargetException e) {
-                       e.printStackTrace();
+                       throw Reflection.handleInvocationTargetException(e);
                }
-               fireStateChanged();  // Should not occur
        }
 
        
@@ -642,13 +644,12 @@ public class DoubleModel implements ChangeListener, ChangeSource {
                try {
                        return (Boolean)getAutoMethod.invoke(source);
                } catch (IllegalArgumentException e) {
-                       e.printStackTrace();
+                       throw new BugException("Method call failed", e);
                } catch (IllegalAccessException e) {
-                       e.printStackTrace();
+                       throw new BugException("Method call failed", e);
                } catch (InvocationTargetException e) {
-                       e.printStackTrace();
+                       throw Reflection.handleInvocationTargetException(e);
                }
-               return false;  // Should not occur
        }
        
        /**
@@ -661,18 +662,16 @@ public class DoubleModel implements ChangeListener, ChangeSource {
                        return;
                }
                
+               lastAutomatic = auto;
                try {
-                       lastAutomatic = auto;
                        setAutoMethod.invoke(source, auto);
-                       return;
                } catch (IllegalArgumentException e) {
-                       e.printStackTrace();
+                       throw new BugException(e);
                } catch (IllegalAccessException e) {
-                       e.printStackTrace();
+                       throw new BugException(e);
                } catch (InvocationTargetException e) {
-                       e.printStackTrace();
+                       throw Reflection.handleInvocationTargetException(e);
                }
-               fireStateChanged();  // Should not occur
        }
        
 
@@ -708,7 +707,7 @@ public class DoubleModel implements ChangeListener, ChangeSource {
        
        
        /**
-        * Add a listener to the model.  Adds the model as a listener to the Component if this
+        * Add a listener to the model.  Adds the model as a listener to the value source if this
         * is the first listener.
         * @param l Listener to add.
         */