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.MassObject;
18 import net.sf.openrocket.rocketcomponent.NoseCone;
19 import net.sf.openrocket.rocketcomponent.RadiusRingComponent;
20 import net.sf.openrocket.rocketcomponent.RingComponent;
21 import net.sf.openrocket.rocketcomponent.Rocket;
22 import net.sf.openrocket.rocketcomponent.RocketComponent;
23 import net.sf.openrocket.rocketcomponent.Stage;
24 import net.sf.openrocket.rocketcomponent.Transition;
25 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
27 import java.util.HashSet;
31 * This abstract class contains boilerplate functionality to support visiting the components of a rocket. It is a
32 * visitor strategy, not a visitor per se.
34 public abstract class BaseVisitorStrategy implements ComponentVisitorStrategy {
39 protected ComponentVisitor parent;
44 protected Document document;
47 * The direct iText writer.
49 protected PdfWriter writer;
52 * The stages selected.
54 protected Set<Integer> stages;
57 * State variable to track the level of hierarchy.
59 protected int level = 0;
62 * Default no-arg constructor.
64 public BaseVisitorStrategy () {
70 * @param doc the iText document
72 public BaseVisitorStrategy (Document doc) {
79 * @param doc the iText document
80 * @param theWriter an iText byte writer
82 public BaseVisitorStrategy (Document doc, PdfWriter theWriter) {
83 this(doc, theWriter, new HashSet<Integer>());
89 * @param doc the iText document
90 * @param theWriter an iText byte writer
91 * @param theStages a set of stage numbers
93 public BaseVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStages) {
100 * Determine if the visitor strategy's set of stage numbers (to print) contains the specified stage.
102 * @param stageNumber a stage number
104 * @return true if the visitor strategy contains the stage number provided
106 public boolean shouldVisitStage (int stageNumber) {
107 if (stages == null || stages.isEmpty()) {
111 for (final Integer stage : stages) {
112 if (stage == stageNumber) {
121 * Recurse through the given rocket component.
123 * @param root the root component; all children will be visited recursively
125 protected void goDeep (final RocketComponent root) {
126 RocketComponent[] rc = root.getChildren();
132 * Recurse through the given rocket component.
134 * @param theRc an array of rocket components; all children will be visited recursively
136 protected void goDeep (final RocketComponent[] theRc) {
138 for (RocketComponent rocketComponent : theRc) {
139 rocketComponent.accept(parent);
145 * Get the dimensions of the paper page.
147 * @return an internal Dimension
149 protected Dimension getPageSize () {
150 return new Dimension(document.getPageSize().getWidth(),
151 document.getPageSize().getHeight());
158 public void visit (final Rocket visitable) {
166 public void visit (final RocketComponent visitable) {
167 if (shouldVisitStage(visitable.getStageNumber())) {
176 public void visit (final Stage visitable) {
177 if (shouldVisitStage(visitable.getStageNumber())) {
186 public void visit (final ExternalComponent visitable) {
187 if (shouldVisitStage(visitable.getStageNumber())) {
196 public void visit (final BodyComponent visitable) {
197 if (shouldVisitStage(visitable.getStageNumber())) {
206 public void visit (final RingComponent visitable) {
207 if (shouldVisitStage(visitable.getStageNumber())) {
216 public void visit (final InnerTube visitable) {
217 if (shouldVisitStage(visitable.getStageNumber())) {
226 public void visit (final LaunchLug visitable) {
227 if (shouldVisitStage(visitable.getStageNumber())) {
236 public void visit (final Transition visitable) {
237 if (shouldVisitStage(visitable.getStageNumber())) {
246 public void visit (final RadiusRingComponent visitable) {
247 if (shouldVisitStage(visitable.getStageNumber())) {
256 public void visit (final MassObject visitable) {
257 if (shouldVisitStage(visitable.getStageNumber())) {
266 public void visit (final NoseCone visitable) {
267 if (shouldVisitStage(visitable.getStageNumber())) {
276 public void visit (final BodyTube visitable) {
277 if (shouldVisitStage(visitable.getStageNumber())) {
286 public void visit (final TrapezoidFinSet visitable) {
287 if (shouldVisitStage(visitable.getStageNumber())) {
296 public void visit (final EllipticalFinSet visitable) {
297 if (shouldVisitStage(visitable.getStageNumber())) {
306 public void visit (final FreeformFinSet visitable) {
307 if (shouldVisitStage(visitable.getStageNumber())) {
316 public void setParent (final ComponentVisitor theParent) {
324 public void close () {
332 public Dimension (float w, float h) {
337 public float getWidth () {
341 public float getHeight () {