From 3e3b5dc65578abfbc2b009528cfe3540ee50a918 Mon Sep 17 00:00:00 2001 From: Daniel_M_Williams Date: Sat, 2 May 2020 11:56:40 -0400 Subject: [PATCH] [fix] SymmetricComponent.get{Next|Prev}SymmetricComponent simply retrieves next symmetric *sibling*. --- .../rocketcomponent/RocketComponent.java | 46 +++++++++---------- .../rocketcomponent/SymmetricComponent.java | 44 +++++++++--------- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java b/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java index fc20dc813..ad1c57a82 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java +++ b/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java @@ -1630,9 +1630,26 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab } return null; } - - - // TODO: Move these methods elsewhere (used only in SymmetricComponent) + + public final RocketComponent getNextComponent() { + checkState(); + if (getChildCount() > 0) + return getChild(0); + + RocketComponent current = this; + RocketComponent nextParent = this.parent; + + while (nextParent != null) { + int pos = nextParent.getChildPosition(current); + if (pos < nextParent.getChildCount() - 1) + return nextParent.getChild(pos + 1); + + current = nextParent; + nextParent = current.parent; + } + return null; + } + public final RocketComponent getPreviousComponent() { checkState(); this.checkComponentStructure(); @@ -1662,28 +1679,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab c = c.getChild(c.getChildCount() - 1); return c; } - - // TODO: Move these methods elsewhere (used only in SymmetricComponent) - public final RocketComponent getNextComponent() { - checkState(); - if (getChildCount() > 0) - return getChild(0); - - RocketComponent current = this; - RocketComponent nextParent = this.parent; - - while (nextParent != null) { - int pos = nextParent.getChildPosition(current); - if (pos < nextParent.getChildCount() - 1) - return nextParent.getChild(pos + 1); - - current = nextParent; - nextParent = current.parent; - } - return null; - } - - + /////////// Event handling ////////// // // Listener lists are provided by the root Rocket component, diff --git a/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java b/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java index 64958596e..a0ae39c16 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java +++ b/core/src/net/sf/openrocket/rocketcomponent/SymmetricComponent.java @@ -566,17 +566,17 @@ public abstract class SymmetricComponent extends BodyComponent implements Radial * @return the previous SymmetricComponent, or null. */ public final SymmetricComponent getPreviousSymmetricComponent() { - RocketComponent c; - for (c = this.getPreviousComponent(); c != null; c = c.getPreviousComponent()) { - if (c instanceof PodSet) { - return null; - } - if (c instanceof SymmetricComponent) { - return (SymmetricComponent) c; - } - if (!(c instanceof AxialStage) && - (c.axialMethod == AxialMethod.AFTER)) { - return null; // Bad component type as "parent" + if(null == this.parent) { + return null; + } + + int pos = parent.getChildPosition(this); + while( 0 < pos ) { + --pos; + final RocketComponent comp = parent.getChild(pos); + + if (comp instanceof SymmetricComponent) { + return (SymmetricComponent) comp; } } return null; @@ -588,17 +588,19 @@ public abstract class SymmetricComponent extends BodyComponent implements Radial * @return the next SymmetricComponent, or null. */ public final SymmetricComponent getNextSymmetricComponent() { - RocketComponent c; - for (c = this.getNextComponent(); c != null; c = c.getNextComponent()) { - if (c instanceof PodSet) { - return null; + if(null == this.parent) { + return null; + } + + int pos = parent.getChildPosition(this); + ++pos; + while( pos < parent.getChildCount() ) { + final RocketComponent comp = parent.getChild(pos); + ++pos; + + if (comp instanceof SymmetricComponent) { + return (SymmetricComponent) comp; } - if (c instanceof SymmetricComponent) { - return (SymmetricComponent) c; - } - if (!(c instanceof AxialStage) && - (c.axialMethod == AxialMethod.AFTER)) - return null; // Bad component type as "parent" } return null; }