2 * PartsListVisitorStrategy.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.*;
10 import java.util.HashMap;
11 import java.util.List;
16 * A visitor strategy for creating documentation about a parts list.
18 public class PartsListVisitorStrategy {
21 * Accumulator for parts data.
23 private Map<PartsAccumulator, PartsAccumulator> crap = new HashMap<PartsAccumulator, PartsAccumulator>();
28 protected Document document;
31 * The direct iText writer.
33 protected PdfWriter writer;
36 * The stages selected.
38 protected Set<Integer> stages;
42 * Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts.
44 * @param doc The iText document
45 * @param theWriter The direct iText writer
46 * @param theStagesToVisit The stages to be visited by this strategy
48 public PartsListVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) {
51 stages = theStagesToVisit;
56 * Print the parts detail.
58 * @param root the root component
60 public void doVisit (final RocketComponent root) {
61 goDeep(root.getChildren());
65 * Recurse through the given rocket component.
67 * @param theRc an array of rocket components; all children will be visited recursively
69 protected void goDeep (final List<RocketComponent> theRc) {
70 for (RocketComponent rocketComponent : theRc) {
71 doIt(rocketComponent);
78 private void doIt (final RocketComponent component) {
79 if (component instanceof InnerTube) {
80 final PartsAccumulator key = new PartsAccumulator(component);
81 PartsAccumulator pa = crap.get(key);
87 List<RocketComponent> rc = component.getChildren();
90 else if (component instanceof LaunchLug) {
91 final PartsAccumulator key = new PartsAccumulator(component);
92 PartsAccumulator pa = crap.get(key);
100 else if (component instanceof NoseCone) {
101 final PartsAccumulator key = new PartsAccumulator(component);
102 PartsAccumulator pa = crap.get(key);
108 List<RocketComponent> rc = component.getChildren();
111 else if (component instanceof Transition) {
112 final PartsAccumulator key = new PartsAccumulator(component);
113 PartsAccumulator pa = crap.get(key);
119 List<RocketComponent> rc = component.getChildren();
122 else if (component instanceof RadiusRingComponent) {
123 final PartsAccumulator key = new PartsAccumulator(component);
124 PartsAccumulator pa = crap.get(key);
130 List<RocketComponent> rc = component.getChildren();
133 else if (component instanceof RingComponent) {
134 final PartsAccumulator key = new PartsAccumulator(component);
135 PartsAccumulator pa = crap.get(key);
141 List<RocketComponent> rc = component.getChildren();
144 else if (component instanceof BodyTube) {
145 final PartsAccumulator key = new PartsAccumulator(component);
146 PartsAccumulator pa = crap.get(key);
152 List<RocketComponent> rc = component.getChildren();
155 else if (component instanceof TrapezoidFinSet) {
157 else if (component instanceof EllipticalFinSet) {
159 else if (component instanceof FreeformFinSet) {
167 public void close () {
168 for (PartsAccumulator partsAccumulator : crap.keySet()) {
169 System.err.println(partsAccumulator.component.getComponentName() + " " + partsAccumulator.quantity);
175 class PartsAccumulator {
179 RocketComponent component;
181 PartsAccumulator (RocketComponent theComponent) {
182 component = theComponent;
194 public boolean equals (final Object o1) {
199 RocketComponent that;
200 if (o1 instanceof net.sf.openrocket.gui.print.visitor.PartsAccumulator) {
201 that = ((net.sf.openrocket.gui.print.visitor.PartsAccumulator) o1).component;
203 else if (o1 instanceof RocketComponent) {
204 that = (RocketComponent) o1;
210 if (this.component.getClass().equals(that.getClass())) {
212 if (that.getLength() == this.component.getLength()) {
213 if (that.getMass() == this.component.getMass()) {
217 if (this.component instanceof Coaxial &&
218 that instanceof Coaxial) {
219 Coaxial cThis = (Coaxial) this.component;
220 Coaxial cThat = (Coaxial) that;
221 if (cThis.getInnerRadius() == cThat.getInnerRadius() &&
222 cThis.getOuterRadius() == cThat.getOuterRadius()) {
232 public int hashCode () {
233 return component.getComponentName().hashCode();