DGP - 1st printing
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / Rocket.java
index 709c7a59a21a1b2e672ec894428ef7aba6152448..a0c923ed2592219aaa14c0b4264cb4bf63db0b05 100644 (file)
@@ -1,5 +1,12 @@
 package net.sf.openrocket.rocketcomponent;
 
+import net.sf.openrocket.motor.Motor;
+import net.sf.openrocket.util.Chars;
+import net.sf.openrocket.util.Coordinate;
+import net.sf.openrocket.util.MathUtil;
+
+import javax.swing.event.ChangeListener;
+import javax.swing.event.EventListenerList;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -7,15 +14,8 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 
-import javax.swing.event.ChangeListener;
-import javax.swing.event.EventListenerList;
-
-import net.sf.openrocket.util.Coordinate;
-import net.sf.openrocket.util.MathUtil;
-
 
 /**
  * Base for all rocket components.  This is the "starting point" for all rocket trees.
@@ -72,8 +72,8 @@ public class Rocket extends RocketComponent {
        
        
        // Motor configuration list
-       private List<String> motorConfigurationIDs = new ArrayList<String>();
-       private Map<String, String> motorConfigurationNames = new HashMap<String, String>();
+       private ArrayList<String> motorConfigurationIDs = new ArrayList<String>();
+       private HashMap<String, String> motorConfigurationNames = new HashMap<String, String>();
        {
                motorConfigurationIDs.add(null);
        }
@@ -134,7 +134,6 @@ public class Rocket extends RocketComponent {
        }
        
        
-       
        /**
         * Return the non-negative modification ID of this rocket.  The ID is changed
         * every time any change occurs in the rocket.  This can be used to check 
@@ -267,22 +266,25 @@ public class Rocket extends RocketComponent {
        }
        
 
+       
+       
+       
        /**
         * Make a deep copy of the Rocket structure.  This is a helper method which simply 
         * casts the result of the superclass method to a Rocket.
         */
+       @SuppressWarnings("unchecked")
        @Override
        public Rocket copy() {
                Rocket copy = (Rocket)super.copy();
+               copy.motorConfigurationIDs = (ArrayList<String>) this.motorConfigurationIDs.clone();
+               copy.motorConfigurationNames = 
+                       (HashMap<String, String>) this.motorConfigurationNames.clone();
                copy.resetListeners();
+               
                return copy;
        }
        
-       
-       
-
-       
-       
        /**
         * Load the rocket structure from the source.  The method loads the fields of this
         * Rocket object and copies the references to siblings from the <code>source</code>.
@@ -293,6 +295,7 @@ public class Rocket extends RocketComponent {
         * and therefore fires an UNDO_EVENT, masked with all applicable mass/aerodynamic/tree
         * changes.
         */
+       @SuppressWarnings("unchecked")
        public void loadFrom(Rocket r) {
                super.copyFrom(r);
                
@@ -312,10 +315,15 @@ public class Rocket extends RocketComponent {
                this.refType = r.refType;
                this.customReferenceLength = r.customReferenceLength;
                
-               this.motorConfigurationIDs = r.motorConfigurationIDs;
-               this.motorConfigurationNames = r.motorConfigurationNames;
+               this.motorConfigurationIDs = (ArrayList<String>) r.motorConfigurationIDs.clone();
+               this.motorConfigurationNames = 
+                       (HashMap<String, String>) r.motorConfigurationNames.clone();
                this.perfectFinish = r.perfectFinish;
                
+               String id = defaultConfiguration.getMotorConfigurationID();
+               if (!this.motorConfigurationIDs.contains(id))
+                       defaultConfiguration.setMotorConfigurationID(null);
+               
                fireComponentChangeEvent(type);
        }
 
@@ -534,6 +542,45 @@ public class Rocket extends RocketComponent {
        }
 
        
+       /**
+        * Check whether <code>id</code> is a valid motor configuration ID.
+        * 
+        * @param id    the configuration ID.
+        * @return              whether a motor configuration with that ID exists.
+        */
+       public boolean isMotorConfigurationID(String id) {
+               return motorConfigurationIDs.contains(id);
+       }
+       
+       
+       
+       /**
+        * Check whether the given motor configuration ID has motors defined for it.
+        * 
+        * @param id    the motor configuration ID (may be invalid).
+        * @return              whether any motors are defined for it.
+        */
+       public boolean hasMotors(String id) {
+               if (id == null)
+                       return false;
+               
+               Iterator<RocketComponent> iterator = this.deepIterator();
+               while (iterator.hasNext()) {
+                       RocketComponent c = iterator.next();
+
+                       if (c instanceof MotorMount) {
+                               MotorMount mount = (MotorMount) c;
+                               if (!mount.isMotorMount())
+                                       continue;
+                               if (mount.getMotor(id) != null) {
+                                       return true;
+                               }
+                       }
+               }
+               return false;
+       }
+       
+       
        /**
         * Return the user-set name of the motor configuration.  If no name has been set,
         * returns an empty string (not null).
@@ -542,6 +589,8 @@ public class Rocket extends RocketComponent {
         * @return         the configuration name
         */
        public String getMotorConfigurationName(String id) {
+               if (!isMotorConfigurationID(id))
+                       return "";
                String s = motorConfigurationNames.get(id);
                if (s == null)
                        return "";
@@ -558,14 +607,29 @@ public class Rocket extends RocketComponent {
         */
        public void setMotorConfigurationName(String id, String name) {
                motorConfigurationNames.put(id,name);
-               fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
+               fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
        }
        
+       
+       /**
+        * Return either the motor configuration name (if set) or its description. 
+        * 
+        * @param id  the motor configuration ID.
+        * @return    a textual representation of the configuration
+        */
+       public String getMotorConfigurationNameOrDescription(String id) {
+               String name;
                
+               name = getMotorConfigurationName(id);
+               if (name != null  &&  !name.equals(""))
+                       return name;
+
+               return getMotorConfigurationDescription(id);
+       }
+       
        /**
-        * Return a description for the motor configuration.  This is either the 
-        * name previously set by {@link #setMotorConfigurationName(String, String)} or
-        * a string generated from the motor designations of the components.
+        * Return a description for the motor configuration, generated from the motor 
+        * designations of the components.
         * 
         * @param id  the motor configuration ID.
         * @return    a textual representation of the configuration
@@ -575,14 +639,6 @@ public class Rocket extends RocketComponent {
                String name;
                int motorCount = 0;
                
-               if (!motorConfigurationIDs.contains(id)) {
-                       throw new IllegalArgumentException("Motor configuration ID does not exist: "+id);
-               }
-               
-               name = motorConfigurationNames.get(id);
-               if (name != null  &&  !name.equals(""))
-                       return name;
-               
                // Generate the description
                
                // First iterate over each stage and store the designations of each motor
@@ -638,7 +694,7 @@ public class Rocket extends RocketComponent {
                                        if (previous != null) {
                                                String s = "";
                                                if (count > 1) {
-                                                       s = "" + count + "\u00d7" + previous;
+                                                       s = "" + count + Chars.TIMES + previous;
                                                } else {
                                                        s = previous;
                                                }
@@ -657,7 +713,7 @@ public class Rocket extends RocketComponent {
                        if (previous != null) {
                                String s = "";
                                if (count > 1) {
-                                       s = "" + count + "\u00d7" + previous;
+                                       s = "" + count + Chars.TIMES + previous;
                                } else {
                                        s = previous;
                                }
@@ -737,4 +793,14 @@ public class Rocket extends RocketComponent {
        public boolean isCompatible(Class<? extends RocketComponent> type) {
                return (Stage.class.isAssignableFrom(type));
        }
+
+    /**
+     * Accept a visitor to this Rocket in the component hierarchy.
+     * 
+     * @param theVisitor  the visitor that will be called back with a reference to this Rocket
+     */    
+       @Override 
+    public void accept (final ComponentVisitor theVisitor) {
+        theVisitor.visit(this);
+    }    
 }