[Bugfix] Configuration.getActiveComponents() fixed

This commit is contained in:
Daniel_M_Williams 2015-09-17 09:16:47 -04:00
parent 5f3bbc3103
commit 1d327e6dbb
2 changed files with 31 additions and 7 deletions

View File

@ -1,10 +1,12 @@
package net.sf.openrocket.rocketcomponent; package net.sf.openrocket.rocketcomponent;
import java.util.ArrayDeque;
import java.util.Collection; import java.util.Collection;
import java.util.EventListener; import java.util.EventListener;
import java.util.EventObject; import java.util.EventObject;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Queue;
import net.sf.openrocket.motor.MotorInstance; import net.sf.openrocket.motor.MotorInstance;
import net.sf.openrocket.motor.MotorInstanceConfiguration; import net.sf.openrocket.motor.MotorInstanceConfiguration;
@ -127,10 +129,9 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public void toggleStage(final int stageNumber) { public void toggleStage(final int stageNumber) {
if ((0 <= stageNumber) && (stageMap.containsKey(stageNumber))) { if ((0 <= stageNumber) && (stageMap.containsKey(stageNumber))) {
StageFlags flags = stageMap.get(stageNumber); StageFlags flags = stageMap.get(stageNumber);
log.error("debug: toggling stage " + stageNumber + " to " + !flags.active);
flags.active = !flags.active; flags.active = !flags.active;
//log.error("debug: toggling stage " + stageNumber + " to " + !flags.active + " " + this.toDebug());
fireChangeEvent(); fireChangeEvent();
return; return;
} }
@ -154,11 +155,22 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
return stageMap.get(stageNumber).active; return stageMap.get(stageNumber).active;
} }
public List<RocketComponent> getActiveComponents() { public Collection<RocketComponent> getActiveComponents() {
Queue<RocketComponent> toProcess = new ArrayDeque<RocketComponent>(this.rocket.getChildren());
ArrayList<RocketComponent> toReturn = new ArrayList<RocketComponent>(); ArrayList<RocketComponent> toReturn = new ArrayList<RocketComponent>();
for (StageFlags curFlags : this.stageMap.values()) {
if (curFlags.active) { while (!toProcess.isEmpty()) {
toReturn.add(curFlags.stage); RocketComponent comp = toProcess.poll();
if (comp instanceof AxialStage) {
if (!isStageActive(comp.getStageNumber())) {
continue;
}
}
toReturn.add(comp);
for (RocketComponent child : comp.getChildren()) {
toProcess.offer(child);
} }
} }
@ -313,6 +325,17 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
// DEBUG / DEVEL // DEBUG / DEVEL
public String toDebug() { public String toDebug() {
StringBuilder buf = new StringBuilder();
buf.append(String.format("["));
for (StageFlags flags : this.stageMap.values()) {
buf.append(String.format(" %d", (flags.active ? 1 : 0)));
}
buf.append("]\n");
return buf.toString();
}
// DEBUG / DEVEL
public String toDebugDetail() {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buf.append(String.format("\nDumping stage config: \n")); buf.append(String.format("\nDumping stage config: \n"));
for (StageFlags flags : this.stageMap.values()) { for (StageFlags flags : this.stageMap.values()) {
@ -352,6 +375,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY; double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY;
for (RocketComponent component : this.getActiveComponents()) { for (RocketComponent component : this.getActiveComponents()) {
System.err.println("..bounds checking component: " + component.getName());
for (Coordinate coord : component.getComponentBounds()) { for (Coordinate coord : component.getComponentBounds()) {
cachedBounds.add(coord); cachedBounds.add(coord);
if (coord.x < minX) if (coord.x < minX)

View File

@ -271,7 +271,7 @@ public class ConfigurationTest extends BaseTestCase {
// test explicitly setting all stages up to second stage active // test explicitly setting all stages up to second stage active
config.setOnlyStage(1); config.setOnlyStage(1);
assertThat(config.toDebug() + "Setting single stage active: ", config.isStageActive(1), equalTo(true)); assertThat(config.toDebugDetail() + "Setting single stage active: ", config.isStageActive(1), equalTo(true));
config.clearOnlyStage(0); config.clearOnlyStage(0);
assertThat(" deactivate stage #0: ", config.isStageActive(0), equalTo(false)); assertThat(" deactivate stage #0: ", config.isStageActive(0), equalTo(false));