c6a30e001fe2cdb22201bddddc1b1bbc31c01765
[debian/openrocket] / src / net / sf / openrocket / gui / print / visitor / BaseVisitorStrategy.java
1 /*
2  * BaseVisitorStrategy.java
3  */
4 package net.sf.openrocket.gui.print.visitor;
5
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;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
30 /**
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.
33  */
34 public abstract class BaseVisitorStrategy implements ComponentVisitorStrategy {
35
36     /**
37      * The owning visitor.
38      */
39     protected ComponentVisitor parent;
40
41     /**
42      * The iText document.
43      */
44     protected Document document;
45
46     /**
47      * The direct iText writer.
48      */
49     protected PdfWriter writer;
50
51     /**
52      * The stages selected.
53      */
54     protected Set<Integer> stages;
55
56     /**
57      * State variable to track the level of hierarchy.
58      */
59     protected int level = 0;
60
61     /**
62      * Default no-arg constructor.
63      */
64     public BaseVisitorStrategy () {
65     }
66
67     /**
68      * Constructor.
69      *
70      * @param doc the iText document
71      */
72     public BaseVisitorStrategy (Document doc) {
73         this(doc, null);
74     }
75
76     /**
77      * Constructor.
78      *
79      * @param doc       the iText document
80      * @param theWriter an iText byte writer
81      */
82     public BaseVisitorStrategy (Document doc, PdfWriter theWriter) {
83         this(doc, theWriter, new HashSet<Integer>());
84     }
85
86     /**
87      * Constructor.
88      *
89      * @param doc       the iText document
90      * @param theWriter an iText byte writer
91      * @param theStages a set of stage numbers
92      */
93     public BaseVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStages) {
94         document = doc;
95         writer = theWriter;
96         stages = theStages;
97     }
98
99     /**
100      * Determine if the visitor strategy's set of stage numbers (to print) contains the specified stage.
101      *
102      * @param stageNumber a stage number
103      *
104      * @return true if the visitor strategy contains the stage number provided
105      */
106     public boolean shouldVisitStage (int stageNumber) {
107         if (stages == null || stages.isEmpty()) {
108             return false;
109         }
110
111         for (final Integer stage : stages) {
112             if (stage == stageNumber) {
113                 return true;
114             }
115         }
116
117         return false;
118     }
119
120     /**
121      * Recurse through the given rocket component.
122      *
123      * @param root the root component; all children will be visited recursively
124      */
125     protected void goDeep (final RocketComponent root) {
126         RocketComponent[] rc = root.getChildren();
127         goDeep(rc);
128     }
129
130
131     /**
132      * Recurse through the given rocket component.
133      *
134      * @param theRc an array of rocket components; all children will be visited recursively
135      */
136     protected void goDeep (final RocketComponent[] theRc) {
137         level++;
138         for (RocketComponent rocketComponent : theRc) {
139             rocketComponent.accept(parent);
140         }
141         level--;
142     }
143
144     /**
145      * Get the dimensions of the paper page.
146      *
147      * @return an internal Dimension
148      */
149     protected Dimension getPageSize () {
150         return new Dimension(document.getPageSize().getWidth(),
151                              document.getPageSize().getHeight());
152     }
153
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public void visit (final Rocket visitable) {
159         goDeep(visitable);
160     }
161
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public void visit (final RocketComponent visitable) {
167         if (shouldVisitStage(visitable.getStageNumber())) {
168             goDeep(visitable);
169         }
170     }
171
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public void visit (final Stage visitable) {
177         if (shouldVisitStage(visitable.getStageNumber())) {
178             goDeep(visitable);
179         }
180     }
181
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public void visit (final ExternalComponent visitable) {
187         if (shouldVisitStage(visitable.getStageNumber())) {
188             goDeep(visitable);
189         }
190     }
191
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public void visit (final BodyComponent visitable) {
197         if (shouldVisitStage(visitable.getStageNumber())) {
198             goDeep(visitable);
199         }
200     }
201
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public void visit (final RingComponent visitable) {
207         if (shouldVisitStage(visitable.getStageNumber())) {
208             goDeep(visitable);
209         }
210     }
211
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public void visit (final InnerTube visitable) {
217         if (shouldVisitStage(visitable.getStageNumber())) {
218             goDeep(visitable);
219         }
220     }
221
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public void visit (final LaunchLug visitable) {
227         if (shouldVisitStage(visitable.getStageNumber())) {
228             goDeep(visitable);
229         }
230     }
231
232     /**
233      * {@inheritDoc}
234      */
235     @Override
236     public void visit (final Transition visitable) {
237         if (shouldVisitStage(visitable.getStageNumber())) {
238             goDeep(visitable);
239         }
240     }
241
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public void visit (final RadiusRingComponent visitable) {
247         if (shouldVisitStage(visitable.getStageNumber())) {
248             goDeep(visitable);
249         }
250     }
251
252     /**
253      * {@inheritDoc}
254      */
255     @Override
256     public void visit (final MassObject visitable) {
257         if (shouldVisitStage(visitable.getStageNumber())) {
258             goDeep(visitable);
259         }
260     }
261
262     /**
263      * {@inheritDoc}
264      */
265     @Override
266     public void visit (final NoseCone visitable) {
267         if (shouldVisitStage(visitable.getStageNumber())) {
268             goDeep(visitable);
269         }
270     }
271
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public void visit (final BodyTube visitable) {
277         if (shouldVisitStage(visitable.getStageNumber())) {
278             goDeep(visitable);
279         }
280     }
281
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public void visit (final TrapezoidFinSet visitable) {
287         if (shouldVisitStage(visitable.getStageNumber())) {
288             goDeep(visitable);
289         }
290     }
291
292     /**
293      * {@inheritDoc}
294      */
295     @Override
296     public void visit (final EllipticalFinSet visitable) {
297         if (shouldVisitStage(visitable.getStageNumber())) {
298             goDeep(visitable);
299         }
300     }
301
302     /**
303      * {@inheritDoc}
304      */
305     @Override
306     public void visit (final FreeformFinSet visitable) {
307         if (shouldVisitStage(visitable.getStageNumber())) {
308             goDeep(visitable);
309         }
310     }
311
312     /**
313      * {@inheritDoc}
314      */
315     @Override
316     public void setParent (final ComponentVisitor theParent) {
317         parent = theParent;
318     }
319
320     /**
321      * {@inheritDoc}
322      */
323     @Override
324     public void close () {
325     }
326 }
327
328 class Dimension {
329     public float width;
330     public float height;
331
332     public Dimension (float w, float h) {
333         width = w;
334         height = h;
335     }
336
337     public float getWidth () {
338         return width;
339     }
340
341     public float getHeight () {
342         return height;
343     }
344 }