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;
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.
// 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);
}
}
-
/**
* 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
}
+
+
+
/**
* 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>.
* and therefore fires an UNDO_EVENT, masked with all applicable mass/aerodynamic/tree
* changes.
*/
+ @SuppressWarnings("unchecked")
public void loadFrom(Rocket r) {
super.copyFrom(r);
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);
}
}
+ /**
+ * 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).
* @return the configuration name
*/
public String getMotorConfigurationName(String id) {
+ if (!isMotorConfigurationID(id))
+ return "";
String s = motorConfigurationNames.get(id);
if (s == null)
return "";
*/
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
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
if (previous != null) {
String s = "";
if (count > 1) {
- s = "" + count + "\u00d7" + previous;
+ s = "" + count + Chars.TIMES + previous;
} else {
s = previous;
}
if (previous != null) {
String s = "";
if (count > 1) {
- s = "" + count + "\u00d7" + previous;
+ s = "" + count + Chars.TIMES + previous;
} else {
s = previous;
}
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);
+ }
}