2 * BaseVisitorStrategy.java
4 package net.sf.openrocket.gui.print.visitor;
6 import com.itextpdf.text.Document;
7 import com.itextpdf.text.pdf.PdfWriter;
8 import net.sf.openrocket.rocketcomponent.BodyComponent;
9 import net.sf.openrocket.rocketcomponent.BodyTube;
10 import net.sf.openrocket.rocketcomponent.ComponentVisitor;
11 import net.sf.openrocket.rocketcomponent.ComponentVisitorStrategy;
12 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
13 import net.sf.openrocket.rocketcomponent.ExternalComponent;
14 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
15 import net.sf.openrocket.rocketcomponent.InnerTube;
16 import net.sf.openrocket.rocketcomponent.LaunchLug;
17 import net.sf.openrocket.rocketcomponent.NoseCone;
18 import net.sf.openrocket.rocketcomponent.RadiusRingComponent;
19 import net.sf.openrocket.rocketcomponent.RingComponent;
20 import net.sf.openrocket.rocketcomponent.Rocket;
21 import net.sf.openrocket.rocketcomponent.RocketComponent;
22 import net.sf.openrocket.rocketcomponent.Stage;
23 import net.sf.openrocket.rocketcomponent.Transition;
24 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
26 import java.util.HashSet;
30 * This abstract class contains boilerplate functionality to support visiting the components of a rocket.
31 * It is a visitor strategy, not a visitor per se.
33 public abstract class BaseVisitorStrategy implements ComponentVisitorStrategy {
38 protected ComponentVisitor parent;
43 protected Document document;
46 * The direct iText writer.
48 protected PdfWriter writer;
51 * The stages selected.
53 protected Set<Integer> stages;
56 * State variable to track the level of hierarchy.
58 protected int level = 0;
61 * Default no-arg constructor.
63 public BaseVisitorStrategy () {
69 * @param doc the iText document
71 public BaseVisitorStrategy (Document doc) {
78 * @param doc the iText document
79 * @param theWriter an iText byte writer
81 public BaseVisitorStrategy (Document doc, PdfWriter theWriter) {
82 this(doc, theWriter, new HashSet<Integer>());
88 * @param doc the iText document
89 * @param theWriter an iText byte writer
90 * @param theStages a set of stage numbers
92 public BaseVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStages) {
99 * Determine if the visitor strategy's set of stage numbers (to print) contains the specified stage.
101 * @param stageNumber a stage number
103 * @return true if the visitor strategy contains the stage number provided
105 public boolean shouldVisitStage (int stageNumber) {
106 if (stages == null || stages.isEmpty()) {
110 for (final Integer stage : stages) {
111 if (stage == stageNumber) {
120 * Recurse through the given rocket component.
122 * @param root the root component; all children will be visited recursively
124 protected void goDeep (final RocketComponent root) {
125 RocketComponent[] rc = root.getChildren();
131 * Recurse through the given rocket component.
133 * @param theRc an array of rocket components; all children will be visited recursively
135 protected void goDeep (final RocketComponent[] theRc) {
137 for (RocketComponent rocketComponent : theRc) {
138 rocketComponent.accept(parent);
144 * Get the dimensions of the paper page.
146 * @return an internal Dimension
148 protected Dimension getPageSize() {
149 return new Dimension(document.getPageSize().getWidth(),
150 document.getPageSize().getHeight());
157 public void visit (final Rocket visitable) {
165 public void visit (final RocketComponent visitable) {
166 if (shouldVisitStage(visitable.getStageNumber())) {
175 public void visit (final Stage visitable) {
176 if (shouldVisitStage(visitable.getStageNumber())) {
185 public void visit (final ExternalComponent visitable) {
186 if (shouldVisitStage(visitable.getStageNumber())) {
195 public void visit (final BodyComponent visitable) {
196 if (shouldVisitStage(visitable.getStageNumber())) {
205 public void visit (final RingComponent visitable) {
206 if (shouldVisitStage(visitable.getStageNumber())) {
215 public void visit (final InnerTube visitable) {
216 if (shouldVisitStage(visitable.getStageNumber())) {
225 public void visit (final LaunchLug visitable) {
226 if (shouldVisitStage(visitable.getStageNumber())) {
235 public void visit (final Transition visitable) {
236 if (shouldVisitStage(visitable.getStageNumber())) {
245 public void visit (final RadiusRingComponent visitable) {
246 if (shouldVisitStage(visitable.getStageNumber())) {
255 public void visit (final NoseCone visitable) {
256 if (shouldVisitStage(visitable.getStageNumber())) {
265 public void visit (final BodyTube visitable) {
266 if (shouldVisitStage(visitable.getStageNumber())) {
275 public void visit (final TrapezoidFinSet visitable) {
276 if (shouldVisitStage(visitable.getStageNumber())) {
285 public void visit (final EllipticalFinSet visitable) {
286 if (shouldVisitStage(visitable.getStageNumber())) {
295 public void visit (final FreeformFinSet visitable) {
296 if (shouldVisitStage(visitable.getStageNumber())) {
305 public void setParent (final ComponentVisitor theParent) {
313 public void close () {
321 public Dimension(float w, float h) {
326 public float getWidth() {
330 public float getHeight() {