[#1208] Add StageSelector button for ParallelStage

This commit is contained in:
SiboVG 2022-03-02 22:39:20 +01:00
parent 930881dae6
commit 06f220d6a9
2 changed files with 56 additions and 4 deletions

View File

@ -1589,12 +1589,38 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
return children.get(n);
}
/**
* Returns all the direct children of this component. The result is a clone of the children list and may be edited.
* @return direct children of this component.
*/
public final List<RocketComponent> getChildren() {
checkState();
this.checkComponentStructure();
return children.clone();
}
/**
* Returns all children of this component, as well as the children of the children etc.
* @return all the children (direct and indirect) of this component
*/
public final List<RocketComponent> getChildrenRecursive() {
checkState();
List<RocketComponent> children = getChildren();
if (children == null) {
return null;
}
for (RocketComponent child : new ArrayList<>(children)) {
List<RocketComponent> temp = child.getChildrenRecursive();
if (temp != null && temp.size() > 0) {
children.addAll(temp);
}
}
return children;
}
/**
* Returns the position of the child in this components child list, or -1 if the
@ -1683,11 +1709,32 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
while ( null != curComponent ) {
if( ComponentAssembly.class.isAssignableFrom( curComponent.getClass()))
return (ComponentAssembly) curComponent;
curComponent = curComponent.parent;
}
throw new IllegalStateException("getAssembly() called on hierarchy without a ComponentAssembly.");
}
/**
* Return all the component assemblies that are a child of this component
* @return list of ComponentAssembly components that are a child of this component
*/
public final List<RocketComponent> getChildAssemblies() {
checkState();
List<RocketComponent> children = getChildrenRecursive();
if (children == null) {
return null;
}
List<RocketComponent> result = new ArrayList<>();
for (RocketComponent child : children) {
if (child instanceof ComponentAssembly) {
result.add(child);
}
}
return result;
}
/**
* Return the stage number of the stage this component belongs to. The stages

View File

@ -12,6 +12,7 @@ import javax.swing.JToggleButton;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.gui.widgets.SelectColorToggleButton;
import net.sf.openrocket.rocketcomponent.AxialStage;
import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import net.sf.openrocket.rocketcomponent.Rocket;
@ -36,7 +37,11 @@ public class StageSelector extends JPanel implements StateChangeListener {
private void updateButtons( final FlightConfiguration configuration ) {
buttons.clear();
this.removeAll();
for(RocketComponent stage : configuration.getRocket().getChildren()){
List<RocketComponent> assemblies = configuration.getRocket().getChildAssemblies();
if (assemblies == null) {
return;
}
for (RocketComponent stage : assemblies) {
if (!(stage instanceof AxialStage)) continue;
JToggleButton button = new SelectColorToggleButton(new StageAction((AxialStage) stage));
button.setSelected(configuration.isStageActive(stage.getStageNumber()));
@ -50,7 +55,7 @@ public class StageSelector extends JPanel implements StateChangeListener {
@Override
public void stateChanged(EventObject eo) {
Object source = eo.getSource();
if ((source instanceof Rocket) || (source instanceof AxialStage)) {
if ((source instanceof Rocket) || (source instanceof AxialStage) || (source instanceof BodyTube)) {
Rocket rkt = (Rocket) ((RocketComponent) source).getRoot();
updateButtons( rkt.getSelectedConfiguration() );
}