Merge pull request #2185 from SiboVG/issue-2183

[#2183] Don't track stages when copying
This commit is contained in:
Sibo Van Gool 2023-04-20 14:19:04 +02:00 committed by GitHub
commit 6608725a47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 27 deletions

View File

@ -1679,6 +1679,22 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
/////////// Children handling /////////// /////////// Children handling ///////////
/**
* Adds a child to the rocket component tree. The component is added to the end
* of the component's child list. This is a helper method that calls
* {@link #addChild(RocketComponent,int)}.
*
* @param component The component to add.
* @param trackStage If component is a stage, this check will decide whether the rocket should track that stage (add it to the stageList etc.)
* @throws IllegalArgumentException if the component is already part of some
* component tree.
* @see #addChild(RocketComponent,int)
*/
public final void addChild(RocketComponent component, boolean trackStage) {
checkState();
addChild(component, children.size(), trackStage);
}
/** /**
* Adds a child to the rocket component tree. The component is added to the end * Adds a child to the rocket component tree. The component is added to the end
* of the component's child list. This is a helper method that calls * of the component's child list. This is a helper method that calls
@ -1690,8 +1706,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
* @see #addChild(RocketComponent,int) * @see #addChild(RocketComponent,int)
*/ */
public final void addChild(RocketComponent component) { public final void addChild(RocketComponent component) {
checkState(); addChild(component, true);
addChild(component, children.size());
} }
/** /**
@ -1703,10 +1718,11 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
* *
* @param component The component to add. * @param component The component to add.
* @param index Position to add component to. * @param index Position to add component to.
* @param trackStage If component is a stage, this check will decide whether the rocket should track that stage (add it to the stageList etc.)
* @throws IllegalArgumentException If the component is already part of * @throws IllegalArgumentException If the component is already part of
* some component tree. * some component tree.
*/ */
public void addChild(RocketComponent component, int index) { public void addChild(RocketComponent component, int index, boolean trackStage) {
checkState(); checkState();
if (component.parent != null) { if (component.parent != null) {
@ -1758,7 +1774,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
} }
} }
if (component instanceof AxialStage) { if (trackStage && (component instanceof AxialStage)) {
AxialStage nStage = (AxialStage) component; AxialStage nStage = (AxialStage) component;
this.getRocket().trackStage(nStage); this.getRocket().trackStage(nStage);
} }
@ -1769,6 +1785,36 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
fireAddRemoveEvent(component); fireAddRemoveEvent(component);
} }
/**
* Adds a child to the rocket component tree. The component is added to
* the given position of the component's child list.
* <p>
* This method may be overridden to enforce more strict component addition rules.
* The tests should be performed first and then this method called.
*
* @param component The component to add.
* @param index Position to add component to.
* @throws IllegalArgumentException If the component is already part of
* some component tree.
*/
public void addChild(RocketComponent component, int index) {
addChild(component, index, true);
}
/**
* Removes a child from the rocket component tree.
* (redirect to the removed-by-component
*
* @param n remove the n'th child.
* @param trackStage If component is a stage, this check will decide whether the rocket should track that stage (remove it to the stageList etc.)
* @throws IndexOutOfBoundsException if n is out of bounds
*/
public final void removeChild(int n, boolean trackStage) {
checkState();
RocketComponent component = this.getChild(n);
this.removeChild(component, trackStage);
}
/** /**
* Removes a child from the rocket component tree. * Removes a child from the rocket component tree.
* (redirect to the removed-by-component * (redirect to the removed-by-component
@ -1777,9 +1823,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
* @throws IndexOutOfBoundsException if n is out of bounds * @throws IndexOutOfBoundsException if n is out of bounds
*/ */
public final void removeChild(int n) { public final void removeChild(int n) {
checkState(); removeChild(n, true);
RocketComponent component = this.getChild(n);
this.removeChild(component);
} }
/** /**
@ -1787,9 +1831,10 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
* is not present as a child. * is not present as a child.
* *
* @param component the component to remove * @param component the component to remove
* @param trackStage If component is a stage, this check will decide whether the rocket should track that stage (remove it to the stageList etc.)
* @return whether the component was a child * @return whether the component was a child
*/ */
public final boolean removeChild(RocketComponent component) { public final boolean removeChild(RocketComponent component, boolean trackStage) {
checkState(); checkState();
component.checkComponentStructure(); component.checkComponentStructure();
@ -1812,6 +1857,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
} }
} }
if (trackStage) {
if (component instanceof AxialStage) { if (component instanceof AxialStage) {
AxialStage stage = (AxialStage) component; AxialStage stage = (AxialStage) component;
this.getRocket().forgetStage(stage); this.getRocket().forgetStage(stage);
@ -1821,6 +1867,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
for (AxialStage stage : component.getSubStages()) { for (AxialStage stage : component.getSubStages()) {
this.getRocket().forgetStage(stage); this.getRocket().forgetStage(stage);
} }
}
this.checkComponentStructure(); this.checkComponentStructure();
component.checkComponentStructure(); component.checkComponentStructure();
@ -1833,6 +1880,17 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
return false; return false;
} }
/**
* Removes a child from the rocket component tree. Does nothing if the component
* is not present as a child.
*
* @param component the component to remove
* @return whether the component was a child
*/
public final boolean removeChild(RocketComponent component) {
return removeChild(component, true);
}

View File

@ -101,7 +101,7 @@ public final class OpenRocketClipboard {
if (someChildrenSelected) { if (someChildrenSelected) {
for (RocketComponent child : component.getChildren()) { for (RocketComponent child : component.getChildren()) {
if (!clipboardComponents.contains(child)) { if (!clipboardComponents.contains(child)) {
component.removeChild(child); component.removeChild(child, false);
} else { } else {
clipboardComponents.remove(child); clipboardComponents.remove(child);
filterClipboardComponents(child.getChildren()); filterClipboardComponents(child.getChildren());

View File

@ -337,7 +337,7 @@ public class RocketActions {
RocketComponent originalParent = components.get(i).getParent(); RocketComponent originalParent = components.get(i).getParent();
int originalParentIdx = components.indexOf(originalParent); int originalParentIdx = components.indexOf(originalParent);
result.get(originalParentIdx).addChild(result.get(i)); result.get(originalParentIdx).addChild(result.get(i), false);
} else if (RocketComponent.listContainsParent(components, components.get(i))){ } else if (RocketComponent.listContainsParent(components, components.get(i))){
RocketComponent originalParent = components.get(i); RocketComponent originalParent = components.get(i);
while (originalParent != components.get(i)) { while (originalParent != components.get(i)) {
@ -346,7 +346,7 @@ public class RocketActions {
} }
} }
int originalParentIdx = components.indexOf(originalParent); int originalParentIdx = components.indexOf(originalParent);
result.get(originalParentIdx).addChild(result.get(i)); result.get(originalParentIdx).addChild(result.get(i), false);
} }
} }