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