DGP - 1st printing
[debian/openrocket] / src / net / sf / openrocket / gui / print / visitor / MotorMountVisitorStrategy.java
1 /*
2  * MotorMountVisitor.java
3  */
4 package net.sf.openrocket.gui.print.visitor;
5
6 import com.itextpdf.text.Document;
7 import net.sf.openrocket.motor.Motor;
8 import net.sf.openrocket.rocketcomponent.BodyTube;
9 import net.sf.openrocket.rocketcomponent.InnerTube;
10 import net.sf.openrocket.rocketcomponent.MotorMount;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 /**
16  * A visitor strategy for finding data about motor configurations.  This visitor accumulates information about the
17  * motors currently 'installed' into each motor mount in the rocket.  When the visitor is complete, invoke {@link
18  * #getMotors()} to obtain the list of Motor instances that correspond to the motor configuration.
19  */
20 public class MotorMountVisitorStrategy extends BaseVisitorStrategy {
21
22     /**
23      * The motor configuration identifier.
24      */
25     private String mid;
26
27     /** The accumulating list of motors. */
28     private List<Motor> motors = new ArrayList<Motor>();
29
30     /**
31      * Constructor.
32      *
33      * @param doc           the iText document
34      * @param motorConfigID the motor configuration ID
35      */
36     public MotorMountVisitorStrategy (Document doc,
37                                       String motorConfigID) {
38         super(doc);
39         mid = motorConfigID;
40     }
41
42     /**
43      * Override the method that determines if the visiting should be going deep.
44      *
45      * @param stageNumber a stage number
46      *
47      * @return true, always
48      */
49     public boolean shouldVisitStage (int stageNumber) {
50         return true;
51     }
52
53     /**
54      * {@inheritDoc}
55      */
56     @Override
57     public void visit (final BodyTube visitable) {
58         if (visitable.isMotorMount()) {
59             doVisit(visitable);
60         }
61         else {
62             goDeep(visitable);
63         }
64     }
65
66     /**
67      * {@inheritDoc}
68      */
69     @Override
70     public void visit (final InnerTube visitable) {
71         if (visitable.isMotorMount()) {
72             doVisit(visitable);
73         }
74     }
75
76     /**
77      * The core behavior of this visitor.
78      *
79      * @param visitable the object to extract info about; a graphical image of the fin shape is drawn to the document
80      */
81     private void doVisit (final MotorMount visitable) {
82         final Motor motor = visitable.getMotor(mid);
83         if (motor != null) {
84             motors.add(motor);
85         }
86     }
87
88     /**
89      * Answer with the list of motors that have been accumulated from visiting all of the motor mount components in the
90      * rocket component hierarchy.
91      *
92      * @return a list of motors
93      */
94     public List<Motor> getMotors () {
95         return motors;
96     }
97
98 }
99
100