4 package net.sf.openrocket.gui.print.visitor;
6 import net.sf.openrocket.rocketcomponent.BodyComponent;
7 import net.sf.openrocket.rocketcomponent.BodyTube;
8 import net.sf.openrocket.rocketcomponent.ComponentVisitor;
9 import net.sf.openrocket.rocketcomponent.ComponentVisitorStrategy;
10 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
11 import net.sf.openrocket.rocketcomponent.ExternalComponent;
12 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
13 import net.sf.openrocket.rocketcomponent.InnerTube;
14 import net.sf.openrocket.rocketcomponent.LaunchLug;
15 import net.sf.openrocket.rocketcomponent.MassObject;
16 import net.sf.openrocket.rocketcomponent.NoseCone;
17 import net.sf.openrocket.rocketcomponent.RadiusRingComponent;
18 import net.sf.openrocket.rocketcomponent.RingComponent;
19 import net.sf.openrocket.rocketcomponent.Rocket;
20 import net.sf.openrocket.rocketcomponent.RocketComponent;
21 import net.sf.openrocket.rocketcomponent.Stage;
22 import net.sf.openrocket.rocketcomponent.Transition;
23 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
25 import java.util.ArrayList;
26 import java.util.List;
29 * This visitor strategy accumulates Stage references in a Rocket hierarchy.
31 public class StageVisitorStrategy implements ComponentVisitorStrategy {
34 * The collection of stages, accumulated during a visitation.
36 private List<Double> stageComponents = new ArrayList<Double>();
38 private Double mass = 0d;
43 protected ComponentVisitor parent;
48 public StageVisitorStrategy () {
52 * Override the method that determines if the visiting should be going deep.
54 * @param stageNumber a stage number
56 * @return true, always
58 public boolean shouldVisitStage (int stageNumber) {
66 public void setParent (final ComponentVisitor theParent) {
74 public void visit (final Rocket visitable) {
82 public void visit (final Stage visitable) {
85 stageComponents.add(mass);
88 RocketComponent[] rc = visitable.getChildren();
96 public void visit (final RocketComponent visitable) {
97 mass += visitable.getMass();
102 * Recurse through the given rocket component.
104 * @param root the root component; all children will be visited recursively
106 protected void goDeep (final RocketComponent root) {
107 RocketComponent[] rc = root.getChildren();
113 * Recurse through the given rocket component.
115 * @param theRc an array of rocket components; all children will be visited recursively
117 protected void goDeep (final RocketComponent[] theRc) {
118 for (RocketComponent rocketComponent : theRc) {
119 rocketComponent.accept(parent);
128 public void visit (final ExternalComponent visitable) {
129 mass += visitable.getMass();
137 public void visit (final BodyComponent visitable) {
138 mass += visitable.getMass();
146 public void visit (final RingComponent visitable) {
147 mass += visitable.getMass();
155 public void visit (final InnerTube visitable) {
156 mass += visitable.getMass();
164 public void visit (final LaunchLug visitable) {
165 mass += visitable.getMass();
173 public void visit (final Transition visitable) {
174 mass += visitable.getMass();
182 public void visit (final RadiusRingComponent visitable) {
183 mass += visitable.getMass();
191 public void visit (final MassObject visitable) {
192 mass += visitable.getMass();
200 public void visit (final NoseCone visitable) {
201 mass += visitable.getMass();
209 public void visit (final BodyTube visitable) {
210 mass += visitable.getMass();
218 public void visit (final TrapezoidFinSet visitable) {
219 mass += visitable.getMass();
227 public void visit (final EllipticalFinSet visitable) {
228 mass += visitable.getMass();
236 public void visit (final FreeformFinSet visitable) {
237 mass += visitable.getMass();
242 * Get the list of stages, sort from Stage 1 .. Stage N.
244 * @return a sorted list of stages
246 public List<Double> getStages () {
247 return stageComponents;
251 * Close by setting the last stage.
253 public void close () {
255 stageComponents.add(mass);