merge fixing

This commit is contained in:
Sampo Niskanen 2011-01-09 09:03:38 +00:00
parent e8abfbd306
commit 683c11072a
4 changed files with 1190 additions and 1188 deletions

View File

@ -3,8 +3,10 @@
*/ */
package net.sf.openrocket.gui.print.visitor; package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document; import java.util.HashSet;
import com.itextpdf.text.pdf.PdfWriter; import java.util.List;
import java.util.Set;
import net.sf.openrocket.rocketcomponent.BodyComponent; import net.sf.openrocket.rocketcomponent.BodyComponent;
import net.sf.openrocket.rocketcomponent.BodyTube; import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.ComponentVisitor; import net.sf.openrocket.rocketcomponent.ComponentVisitor;
@ -24,321 +26,321 @@ import net.sf.openrocket.rocketcomponent.Stage;
import net.sf.openrocket.rocketcomponent.Transition; import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.rocketcomponent.TrapezoidFinSet; import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
import java.util.HashSet; import com.itextpdf.text.Document;
import java.util.Set; import com.itextpdf.text.pdf.PdfWriter;
/** /**
* This abstract class contains boilerplate functionality to support visiting the components of a rocket. It is a * This abstract class contains boilerplate functionality to support visiting the components of a rocket. It is a
* visitor strategy, not a visitor per se. * visitor strategy, not a visitor per se.
*/ */
public abstract class BaseVisitorStrategy implements ComponentVisitorStrategy { public abstract class BaseVisitorStrategy implements ComponentVisitorStrategy {
/** /**
* The owning visitor. * The owning visitor.
*/ */
protected ComponentVisitor parent; protected ComponentVisitor parent;
/** /**
* The iText document. * The iText document.
*/ */
protected Document document; protected Document document;
/** /**
* The direct iText writer. * The direct iText writer.
*/ */
protected PdfWriter writer; protected PdfWriter writer;
/** /**
* The stages selected. * The stages selected.
*/ */
protected Set<Integer> stages; protected Set<Integer> stages;
/** /**
* State variable to track the level of hierarchy. * State variable to track the level of hierarchy.
*/ */
protected int level = 0; protected int level = 0;
/** /**
* Default no-arg constructor. * Default no-arg constructor.
*/ */
public BaseVisitorStrategy () { public BaseVisitorStrategy() {
} }
/** /**
* Constructor. * Constructor.
* *
* @param doc the iText document * @param doc the iText document
*/ */
public BaseVisitorStrategy (Document doc) { public BaseVisitorStrategy(Document doc) {
this(doc, null); this(doc, null);
} }
/** /**
* Constructor. * Constructor.
* *
* @param doc the iText document * @param doc the iText document
* @param theWriter an iText byte writer * @param theWriter an iText byte writer
*/ */
public BaseVisitorStrategy (Document doc, PdfWriter theWriter) { public BaseVisitorStrategy(Document doc, PdfWriter theWriter) {
this(doc, theWriter, new HashSet<Integer>()); this(doc, theWriter, new HashSet<Integer>());
} }
/** /**
* Constructor. * Constructor.
* *
* @param doc the iText document * @param doc the iText document
* @param theWriter an iText byte writer * @param theWriter an iText byte writer
* @param theStages a set of stage numbers * @param theStages a set of stage numbers
*/ */
public BaseVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStages) { public BaseVisitorStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStages) {
document = doc; document = doc;
writer = theWriter; writer = theWriter;
stages = theStages; stages = theStages;
} }
/** /**
* Determine if the visitor strategy's set of stage numbers (to print) contains the specified stage. * Determine if the visitor strategy's set of stage numbers (to print) contains the specified stage.
* *
* @param stageNumber a stage number * @param stageNumber a stage number
* *
* @return true if the visitor strategy contains the stage number provided * @return true if the visitor strategy contains the stage number provided
*/ */
public boolean shouldVisitStage (int stageNumber) { public boolean shouldVisitStage(int stageNumber) {
if (stages == null || stages.isEmpty()) { if (stages == null || stages.isEmpty()) {
return false; return false;
} }
for (final Integer stage : stages) { for (final Integer stage : stages) {
if (stage == stageNumber) { if (stage == stageNumber) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Recurse through the given rocket component. * Recurse through the given rocket component.
* *
* @param root the root component; all children will be visited recursively * @param root the root component; all children will be visited recursively
*/ */
protected void goDeep (final RocketComponent root) { protected void goDeep(final RocketComponent root) {
RocketComponent[] rc = root.getChildren(); List<RocketComponent> rc = root.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* Recurse through the given rocket component. * Recurse through the given rocket component.
* *
* @param theRc an array of rocket components; all children will be visited recursively * @param theRc an array of rocket components; all children will be visited recursively
*/ */
protected void goDeep (final RocketComponent[] theRc) { protected void goDeep(final List<RocketComponent> theRc) {
level++; level++;
for (RocketComponent rocketComponent : theRc) { for (RocketComponent rocketComponent : theRc) {
rocketComponent.accept(parent); rocketComponent.accept(parent);
} }
level--; level--;
} }
/** /**
* Get the dimensions of the paper page. * Get the dimensions of the paper page.
* *
* @return an internal Dimension * @return an internal Dimension
*/ */
protected Dimension getPageSize () { protected Dimension getPageSize() {
return new Dimension(document.getPageSize().getWidth(), return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight()); document.getPageSize().getHeight());
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Rocket visitable) { public void visit(final Rocket visitable) {
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RocketComponent visitable) { public void visit(final RocketComponent visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Stage visitable) { public void visit(final Stage visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final ExternalComponent visitable) { public void visit(final ExternalComponent visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final BodyComponent visitable) { public void visit(final BodyComponent visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RingComponent visitable) { public void visit(final RingComponent visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final InnerTube visitable) { public void visit(final InnerTube visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final LaunchLug visitable) { public void visit(final LaunchLug visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Transition visitable) { public void visit(final Transition visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RadiusRingComponent visitable) { public void visit(final RadiusRingComponent visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final MassObject visitable) { public void visit(final MassObject visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final NoseCone visitable) { public void visit(final NoseCone visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final BodyTube visitable) { public void visit(final BodyTube visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final TrapezoidFinSet visitable) { public void visit(final TrapezoidFinSet visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final EllipticalFinSet visitable) { public void visit(final EllipticalFinSet visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final FreeformFinSet visitable) { public void visit(final FreeformFinSet visitable) {
if (shouldVisitStage(visitable.getStageNumber())) { if (shouldVisitStage(visitable.getStageNumber())) {
goDeep(visitable); goDeep(visitable);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void setParent (final ComponentVisitor theParent) { public void setParent(final ComponentVisitor theParent) {
parent = theParent; parent = theParent;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void close () { public void close() {
} }
} }
class Dimension { class Dimension {
public float width; public float width;
public float height; public float height;
public Dimension (float w, float h) { public Dimension(float w, float h) {
width = w; width = w;
height = h; height = h;
} }
public float getWidth () { public float getWidth() {
return width; return width;
} }
public float getHeight () { public float getHeight() {
return height; return height;
} }
} }

View File

@ -3,18 +3,14 @@
*/ */
package net.sf.openrocket.gui.print.visitor; package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.BadElementException; import java.io.IOException;
import com.itextpdf.text.Chunk; import java.text.NumberFormat;
import com.itextpdf.text.Document; import java.util.Collection;
import com.itextpdf.text.DocumentException; import java.util.List;
import com.itextpdf.text.Element; import java.util.Set;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image; import javax.swing.ImageIcon;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.main.ComponentIcons; import net.sf.openrocket.gui.main.ComponentIcons;
import net.sf.openrocket.gui.print.ITextHelper; import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.gui.print.PrintUtilities; import net.sf.openrocket.gui.print.PrintUtilities;
@ -41,417 +37,418 @@ import net.sf.openrocket.unit.Unit;
import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.Coordinate; import net.sf.openrocket.util.Coordinate;
import javax.swing.ImageIcon; import com.itextpdf.text.BadElementException;
import java.io.IOException; import com.itextpdf.text.Chunk;
import java.text.NumberFormat; import com.itextpdf.text.Document;
import java.util.Collection; import com.itextpdf.text.DocumentException;
import java.util.Set; import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/** /**
* A visitor strategy for creating documentation about parts details. * A visitor strategy for creating documentation about parts details.
*/ */
public class PartsDetailVisitorStrategy extends BaseVisitorStrategy { public class PartsDetailVisitorStrategy extends BaseVisitorStrategy {
/** /**
* The number of columns in the table. * The number of columns in the table.
*/ */
private static final int TABLE_COLUMNS = 7; private static final int TABLE_COLUMNS = 7;
/** /**
* The parts detail is represented as an iText table. * The parts detail is represented as an iText table.
*/ */
PdfPTable grid; PdfPTable grid;
/** /**
* Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts. * Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts.
* *
* @param doc The iText document * @param doc The iText document
* @param theWriter The direct iText writer * @param theWriter The direct iText writer
* @param theStagesToVisit The stages to be visited by this strategy * @param theStagesToVisit The stages to be visited by this strategy
*/ */
public PartsDetailVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) { public PartsDetailVisitorStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) {
super(doc, theWriter, theStagesToVisit); super(doc, theWriter, theStagesToVisit);
PrintUtilities.addText(doc, PrintUtilities.BIG_BOLD, "Parts Detail"); PrintUtilities.addText(doc, PrintUtilities.BIG_BOLD, "Parts Detail");
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Stage visitable) { public void visit(final Stage visitable) {
try { try {
if (grid != null) { if (grid != null) {
document.add(grid); document.add(grid);
} }
document.add(ITextHelper.createPhrase(visitable.getName())); document.add(ITextHelper.createPhrase(visitable.getName()));
grid = new PdfPTable(TABLE_COLUMNS); grid = new PdfPTable(TABLE_COLUMNS);
grid.setWidthPercentage(100); grid.setWidthPercentage(100);
grid.setHorizontalAlignment(Element.ALIGN_LEFT); grid.setHorizontalAlignment(Element.ALIGN_LEFT);
} } catch (DocumentException e) {
catch (DocumentException e) { }
}
List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final ExternalComponent visitable) {
public void visit (final ExternalComponent visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(ITextHelper.createCell());
grid.addCell(ITextHelper.createCell()); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass()));
List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final BodyComponent visitable) {
public void visit (final BodyComponent visitable) { grid.addCell(visitable.getName());
grid.addCell(visitable.getName()); grid.completeRow();
grid.completeRow(); List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final RingComponent visitable) {
public void visit (final RingComponent visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true)); grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(createOuterDiaCell(visitable));
grid.addCell(createOuterDiaCell(visitable)); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass()));
List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final InnerTube visitable) {
public void visit (final InnerTube visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); final PdfPCell pCell = createNameCell(visitable.getName(), true);
final PdfPCell pCell = createNameCell(visitable.getName(), true); grid.addCell(pCell);
grid.addCell(pCell); grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(createOuterDiaCell(visitable));
grid.addCell(createOuterDiaCell(visitable)); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass()));
List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final LaunchLug visitable) {
public void visit (final LaunchLug visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(createOuterDiaCell(visitable));
grid.addCell(createOuterDiaCell(visitable)); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass())); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final Transition visitable) {
public void visit (final Transition visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true)); grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial()));
Chunk fore = new Chunk("Fore Dia: " + appendLength(visitable.getForeRadius() * 2));
Chunk fore = new Chunk("Fore Dia: " + appendLength(visitable.getForeRadius() * 2)); fore.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
fore.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); Chunk aft = new Chunk("Aft Dia: " + appendLength(visitable.getAftRadius() * 2));
Chunk aft = new Chunk("Aft Dia: " + appendLength(visitable.getAftRadius() * 2)); aft.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
aft.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); final PdfPCell cell = ITextHelper.createCell();
final PdfPCell cell = ITextHelper.createCell(); cell.addElement(fore);
cell.addElement(fore); cell.addElement(aft);
cell.addElement(aft); grid.addCell(cell);
grid.addCell(cell); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass()));
List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final RadiusRingComponent visitable) {
public void visit (final RadiusRingComponent visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true)); grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(createOuterDiaCell(visitable));
grid.addCell(createOuterDiaCell(visitable)); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass())); List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final MassObject visitable) {
public void visit (final MassObject visitable) { PdfPCell cell = ITextHelper.createCell();
PdfPCell cell = ITextHelper.createCell(); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setPaddingBottom(12f);
cell.setPaddingBottom(12f);
grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); final PdfPCell nameCell = createNameCell(visitable.getName(), true);
final PdfPCell nameCell = createNameCell(visitable.getName(), true); nameCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
nameCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); nameCell.setPaddingBottom(12f);
nameCell.setPaddingBottom(12f); grid.addCell(nameCell);
grid.addCell(nameCell); grid.addCell(cell);
grid.addCell(cell); grid.addCell(cell);
grid.addCell(cell); grid.addCell(cell);
grid.addCell(cell); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass())); List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final NoseCone visitable) {
public void visit (final NoseCone visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true)); grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(ITextHelper.createCell(visitable.getType().getName(), PdfPCell.BOTTOM));
grid.addCell(ITextHelper.createCell(visitable.getType().getName(), PdfPCell.BOTTOM)); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass())); List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final BodyTube visitable) {
public void visit (final BodyTube visitable) { grid.addCell(iconToImage(visitable));
grid.addCell(iconToImage(visitable)); grid.addCell(createNameCell(visitable.getName(), true));
grid.addCell(createNameCell(visitable.getName(), true)); grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(createMaterialCell(visitable.getMaterial())); grid.addCell(createOuterDiaCell(visitable));
grid.addCell(createOuterDiaCell(visitable)); grid.addCell(createLengthCell(visitable.getLength()));
grid.addCell(createLengthCell(visitable.getLength())); grid.addCell(createMassCell(visitable.getMass()));
grid.addCell(createMassCell(visitable.getMass())); List<RocketComponent> rc = visitable.getChildren();
RocketComponent[] rc = visitable.getChildren(); goDeep(rc);
goDeep(rc); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final TrapezoidFinSet visitable) {
public void visit (final TrapezoidFinSet visitable) { visitFins(visitable);
visitFins(visitable); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final EllipticalFinSet visitable) {
public void visit (final EllipticalFinSet visitable) { visitFins(visitable);
visitFins(visitable); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void visit(final FreeformFinSet visitable) {
public void visit (final FreeformFinSet visitable) { visitFins(visitable);
visitFins(visitable); }
}
/**
/** * {@inheritDoc}
* {@inheritDoc} */
*/ @Override
@Override public void close() {
public void close () { try {
try { if (grid != null) {
if (grid != null) { document.add(grid);
document.add(grid); }
} } catch (DocumentException e) {
} e.printStackTrace();
catch (DocumentException e) { }
e.printStackTrace(); }
}
} private PdfPCell createOuterDiaCell(final Coaxial visitable) {
PdfPCell result = new PdfPCell();
private PdfPCell createOuterDiaCell (final Coaxial visitable) { Phrase p = new Phrase();
PdfPCell result = new PdfPCell(); p.setLeading(12f);
Phrase p = new Phrase(); result.setVerticalAlignment(Element.ALIGN_TOP);
p.setLeading(12f); result.setBorder(Rectangle.BOTTOM);
result.setVerticalAlignment(Element.ALIGN_TOP); Chunk c = new Chunk();
result.setBorder(Rectangle.BOTTOM); c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
Chunk c = new Chunk(); c.append("Dia");
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); p.add(c);
c.append("Dia");
p.add(c); c = new Chunk();
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.SMALL_FONT_SIZE));
c = new Chunk(); c.append("out");
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.SMALL_FONT_SIZE)); p.add(c);
c.append("out");
p.add(c); c = new Chunk();
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
c = new Chunk(); c.append(" " + appendLength(visitable.getOuterRadius() * 2));
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); p.add(c);
c.append(" " + appendLength(visitable.getOuterRadius() * 2)); createInnerDiaCell(visitable, result);
p.add(c); result.addElement(p);
createInnerDiaCell(visitable, result); return result;
result.addElement(p); }
return result;
} private void createInnerDiaCell(final Coaxial visitable, PdfPCell cell) {
Phrase p = new Phrase();
private void createInnerDiaCell (final Coaxial visitable, PdfPCell cell) { p.setLeading(14f);
Phrase p = new Phrase(); Chunk c = new Chunk();
p.setLeading(14f); c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
Chunk c = new Chunk(); c.append("Dia");
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); p.add(c);
c.append("Dia");
p.add(c); c = new Chunk();
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.SMALL_FONT_SIZE));
c = new Chunk(); c.append("in ");
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.SMALL_FONT_SIZE)); p.add(c);
c.append("in ");
p.add(c); c = new Chunk();
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
c = new Chunk(); c.append(" " + appendLength(visitable.getInnerRadius() * 2));
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); p.add(c);
c.append(" " + appendLength(visitable.getInnerRadius() * 2)); cell.addElement(p);
p.add(c); }
cell.addElement(p);
} private void visitFins(FinSet visitable) {
private void visitFins (FinSet visitable) { Image img = null;
java.awt.Image awtImage = new PrintableFinSet(visitable).createImage();
Image img = null;
java.awt.Image awtImage = new PrintableFinSet(visitable).createImage(); Collection<Coordinate> x = visitable.getComponentBounds();
Collection<Coordinate> x = visitable.getComponentBounds(); try {
img = Image.getInstance(writer, awtImage, 0.25f);
try { } catch (BadElementException e) {
img = Image.getInstance(writer, awtImage, 0.25f); e.printStackTrace();
} } catch (IOException e) {
catch (BadElementException e) { e.printStackTrace();
e.printStackTrace(); }
}
catch (IOException e) { grid.addCell(iconToImage(visitable));
e.printStackTrace(); grid.addCell(createNameCell(visitable.getName() + " (" + visitable.getFinCount() + ")", true));
} grid.addCell(createMaterialCell(visitable.getMaterial()));
grid.addCell(ITextHelper.createCell("Thick: " + appendLength(visitable.getThickness()), PdfPCell.BOTTOM));
grid.addCell(iconToImage(visitable)); final PdfPCell pCell = new PdfPCell();
grid.addCell(createNameCell(visitable.getName() + " (" + visitable.getFinCount() + ")", true)); pCell.setBorder(Rectangle.BOTTOM);
grid.addCell(createMaterialCell(visitable.getMaterial())); pCell.addElement(img);
grid.addCell(ITextHelper.createCell("Thick: " + appendLength(visitable.getThickness()), PdfPCell.BOTTOM));
final PdfPCell pCell = new PdfPCell(); grid.addCell(ITextHelper.createCell());
pCell.setBorder(Rectangle.BOTTOM); grid.addCell(createMassCell(visitable.getMass()));
pCell.addElement(img);
List<RocketComponent> rc = visitable.getChildren();
grid.addCell(ITextHelper.createCell()); goDeep(rc);
grid.addCell(createMassCell(visitable.getMass())); }
RocketComponent[] rc = visitable.getChildren(); protected PdfPCell createLengthCell(double length) {
goDeep(rc); return ITextHelper.createCell("Len: " + appendLength(length), PdfPCell.BOTTOM);
} }
protected PdfPCell createLengthCell (double length) { protected PdfPCell createMassCell(double mass) {
return ITextHelper.createCell("Len: " + appendLength(length), PdfPCell.BOTTOM); return ITextHelper.createCell("Mass: " + appendMass(mass), PdfPCell.BOTTOM);
} }
protected PdfPCell createMassCell (double mass) { protected PdfPCell createNameCell(String v, boolean withIndent) {
return ITextHelper.createCell("Mass: " + appendMass(mass), PdfPCell.BOTTOM); PdfPCell result = new PdfPCell();
} result.setBorder(Rectangle.BOTTOM);
Chunk c = new Chunk();
protected PdfPCell createNameCell (String v, boolean withIndent) { c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
PdfPCell result = new PdfPCell(); if (withIndent) {
result.setBorder(Rectangle.BOTTOM); for (int x = 0; x < (level - 2) * 10; x++) {
Chunk c = new Chunk(); c.append(" ");
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); }
if (withIndent) { }
for (int x = 0; x < (level - 2) * 10; x++) { c.append(v);
c.append(" "); result.setColspan(2);
} result.addElement(c);
} return result;
c.append(v); }
result.setColspan(2);
result.addElement(c); protected PdfPCell createMaterialCell(Material material) {
return result; PdfPCell cell = ITextHelper.createCell();
} cell.setLeading(13f, 0);
protected PdfPCell createMaterialCell (Material material) { Chunk c = new Chunk();
PdfPCell cell = ITextHelper.createCell(); c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE));
cell.setLeading(13f, 0); c.append(appendMaterial(material));
cell.addElement(c);
Chunk c = new Chunk(); Chunk density = new Chunk();
c.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.NORMAL_FONT_SIZE)); density.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.SMALL_FONT_SIZE));
c.append(appendMaterial(material)); density.append(appendMaterialDensity(material));
cell.addElement(c); cell.addElement(density);
Chunk density = new Chunk(); return cell;
density.setFont(new Font(Font.FontFamily.HELVETICA, PrintUtilities.SMALL_FONT_SIZE)); }
density.append(appendMaterialDensity(material));
cell.addElement(density); protected PdfPCell iconToImage(final RocketComponent visitable) {
return cell; final ImageIcon icon = (ImageIcon) ComponentIcons.getLargeIcon(visitable.getClass());
} try {
Image im = Image.getInstance(icon.getImage(), null);
protected PdfPCell iconToImage (final RocketComponent visitable) { im.scaleToFit(icon.getIconWidth() * 0.6f, icon.getIconHeight() * 0.6f);
final ImageIcon icon = (ImageIcon) ComponentIcons.getLargeIcon(visitable.getClass()); PdfPCell cell = new PdfPCell(im);
try { cell.setFixedHeight(icon.getIconHeight() * 0.6f);
Image im = Image.getInstance(icon.getImage(), null); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
im.scaleToFit(icon.getIconWidth() * 0.6f, icon.getIconHeight() * 0.6f); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
PdfPCell cell = new PdfPCell(im); cell.setBorder(PdfPCell.NO_BORDER);
cell.setFixedHeight(icon.getIconHeight() * 0.6f); return cell;
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); } catch (BadElementException e) {
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); } catch (IOException e) {
cell.setBorder(PdfPCell.NO_BORDER); }
return cell; return null;
} }
catch (BadElementException e) {
} protected String appendLength(double length) {
catch (IOException e) { final Unit defaultUnit = UnitGroup.UNITS_LENGTH.getDefaultUnit();
} return NumberFormat.getNumberInstance().format(defaultUnit.toUnit(length)) + defaultUnit.toString();
return null; }
}
protected String appendMass(double mass) {
protected String appendLength (double length) { final Unit defaultUnit = UnitGroup.UNITS_MASS.getDefaultUnit();
final Unit defaultUnit = UnitGroup.UNITS_LENGTH.getDefaultUnit(); return NumberFormat.getNumberInstance().format(defaultUnit.toUnit(mass)) + defaultUnit.toString();
return NumberFormat.getNumberInstance().format(defaultUnit.toUnit(length)) + defaultUnit.toString(); }
}
protected String appendMaterial(Material material) {
protected String appendMass (double mass) { return material.getName();
final Unit defaultUnit = UnitGroup.UNITS_MASS.getDefaultUnit(); }
return NumberFormat.getNumberInstance().format(defaultUnit.toUnit(mass)) + defaultUnit.toString();
} protected String appendMaterialDensity(Material material) {
return " (" + material.getType().getUnitGroup().getDefaultUnit().toStringUnit(material.getDensity()) + ")";
protected String appendMaterial (Material material) { }
return material.getName();
}
protected String appendMaterialDensity (Material material) {
return " (" + material.getType().getUnitGroup().getDefaultUnit().toStringUnit(material.getDensity()) + ")";
}
} }

View File

@ -3,8 +3,11 @@
*/ */
package net.sf.openrocket.gui.print.visitor; package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document; import java.util.HashMap;
import com.itextpdf.text.pdf.PdfWriter; import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.openrocket.rocketcomponent.BodyTube; import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.Coaxial; import net.sf.openrocket.rocketcomponent.Coaxial;
import net.sf.openrocket.rocketcomponent.ComponentVisitor; import net.sf.openrocket.rocketcomponent.ComponentVisitor;
@ -19,238 +22,237 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.Transition; import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.rocketcomponent.TrapezoidFinSet; import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
import java.util.HashMap; import com.itextpdf.text.Document;
import java.util.Map; import com.itextpdf.text.pdf.PdfWriter;
import java.util.Set;
/** /**
* A visitor strategy for creating documentation about a parts list. * A visitor strategy for creating documentation about a parts list.
*/ */
public class PartsListVisitorStrategy extends BaseVisitorStrategy { public class PartsListVisitorStrategy extends BaseVisitorStrategy {
/** /**
* Accumulator for parts data. * Accumulator for parts data.
*/ */
private Map<PartsAccumulator, PartsAccumulator> crap = new HashMap<PartsAccumulator, PartsAccumulator>(); private Map<PartsAccumulator, PartsAccumulator> crap = new HashMap<PartsAccumulator, PartsAccumulator>();
/** /**
* Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts. * Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts.
* *
* @param doc The iText document * @param doc The iText document
* @param theWriter The direct iText writer * @param theWriter The direct iText writer
* @param theStagesToVisit The stages to be visited by this strategy * @param theStagesToVisit The stages to be visited by this strategy
*/ */
public PartsListVisitorStrategy (Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) { public PartsListVisitorStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) {
super(doc, theWriter, theStagesToVisit); super(doc, theWriter, theStagesToVisit);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RingComponent visitable) { public void visit(final RingComponent visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final InnerTube visitable) { public void visit(final InnerTube visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final LaunchLug visitable) { public void visit(final LaunchLug visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Transition visitable) { public void visit(final Transition visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RadiusRingComponent visitable) { public void visit(final RadiusRingComponent visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final NoseCone visitable) { public void visit(final NoseCone visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final BodyTube visitable) { public void visit(final BodyTube visitable) {
final PartsAccumulator key = new PartsAccumulator(visitable); final PartsAccumulator key = new PartsAccumulator(visitable);
PartsAccumulator pa = crap.get(key); PartsAccumulator pa = crap.get(key);
if (pa == null) { if (pa == null) {
pa = key; pa = key;
crap.put(pa, pa); crap.put(pa, pa);
} }
pa.increment(); pa.increment();
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final TrapezoidFinSet visitable) { public void visit(final TrapezoidFinSet visitable) {
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final EllipticalFinSet visitable) { public void visit(final EllipticalFinSet visitable) {
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final FreeformFinSet visitable) { public void visit(final FreeformFinSet visitable) {
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void setParent (final ComponentVisitor theParent) { public void setParent(final ComponentVisitor theParent) {
parent = theParent; parent = theParent;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void close () { public void close() {
for (PartsAccumulator partsAccumulator : crap.keySet()) { for (PartsAccumulator partsAccumulator : crap.keySet()) {
System.err.println(partsAccumulator.component.getComponentName() + " " + partsAccumulator.quantity); System.err.println(partsAccumulator.component.getComponentName() + " " + partsAccumulator.quantity);
} }
} }
} }
class PartsAccumulator { class PartsAccumulator {
int quantity = 0; int quantity = 0;
RocketComponent component; RocketComponent component;
PartsAccumulator (RocketComponent theComponent) { PartsAccumulator(RocketComponent theComponent) {
component = theComponent; component = theComponent;
} }
void increment() { void increment() {
quantity++; quantity++;
} }
int quantity() { int quantity() {
return quantity; return quantity;
} }
public boolean equals (final Object o1) { @Override
if (this == o1) { public boolean equals(final Object o1) {
return true; if (this == o1) {
} return true;
}
RocketComponent that;
if (o1 instanceof net.sf.openrocket.gui.print.visitor.PartsAccumulator) { RocketComponent that;
that = ((net.sf.openrocket.gui.print.visitor.PartsAccumulator)o1).component; if (o1 instanceof net.sf.openrocket.gui.print.visitor.PartsAccumulator) {
} that = ((net.sf.openrocket.gui.print.visitor.PartsAccumulator) o1).component;
else if (o1 instanceof RocketComponent) { } else if (o1 instanceof RocketComponent) {
that = (RocketComponent)o1; that = (RocketComponent) o1;
} } else {
else { return false;
return false; }
}
if (this.component.getClass().equals(that.getClass())) {
if (this.component.getClass().equals(that.getClass())) { //If
//If if (that.getLength() == this.component.getLength()) {
if (that.getLength() == this.component.getLength()) { if (that.getMass() == this.component.getMass()) {
if (that.getMass() == this.component.getMass()) { return true;
return true; }
} }
} if (this.component instanceof Coaxial &&
if (this.component instanceof Coaxial && that instanceof Coaxial) {
that instanceof Coaxial) { Coaxial cThis = (Coaxial) this.component;
Coaxial cThis = (Coaxial)this.component; Coaxial cThat = (Coaxial) that;
Coaxial cThat = (Coaxial)that; if (cThis.getInnerRadius() == cThat.getInnerRadius() &&
if (cThis.getInnerRadius() == cThat.getInnerRadius() && cThis.getOuterRadius() == cThat.getOuterRadius()) {
cThis.getOuterRadius() == cThat.getOuterRadius()) { return true;
return true; }
} }
} return false;
return false; }
} return false;
return false; }
}
@Override
public int hashCode() { public int hashCode() {
return component.getComponentName().hashCode(); return component.getComponentName().hashCode();
} }
} }

View File

@ -3,6 +3,9 @@
*/ */
package net.sf.openrocket.gui.print.visitor; package net.sf.openrocket.gui.print.visitor;
import java.util.ArrayList;
import java.util.List;
import net.sf.openrocket.rocketcomponent.BodyComponent; import net.sf.openrocket.rocketcomponent.BodyComponent;
import net.sf.openrocket.rocketcomponent.BodyTube; import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.ComponentVisitor; import net.sf.openrocket.rocketcomponent.ComponentVisitor;
@ -22,238 +25,236 @@ import net.sf.openrocket.rocketcomponent.Stage;
import net.sf.openrocket.rocketcomponent.Transition; import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.rocketcomponent.TrapezoidFinSet; import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
import java.util.ArrayList;
import java.util.List;
/** /**
* This visitor strategy accumulates Stage references in a Rocket hierarchy. * This visitor strategy accumulates Stage references in a Rocket hierarchy.
*/ */
public class StageVisitorStrategy implements ComponentVisitorStrategy { public class StageVisitorStrategy implements ComponentVisitorStrategy {
/** /**
* The collection of stages, accumulated during a visitation. * The collection of stages, accumulated during a visitation.
*/ */
private List<Double> stageComponents = new ArrayList<Double>(); private List<Double> stageComponents = new ArrayList<Double>();
private Double mass = 0d; private Double mass = 0d;
/** /**
* The owning visitor. * The owning visitor.
*/ */
protected ComponentVisitor parent; protected ComponentVisitor parent;
/** /**
* Constructor. * Constructor.
*/ */
public StageVisitorStrategy () { public StageVisitorStrategy() {
} }
/** /**
* Override the method that determines if the visiting should be going deep. * Override the method that determines if the visiting should be going deep.
* *
* @param stageNumber a stage number * @param stageNumber a stage number
* *
* @return true, always * @return true, always
*/ */
public boolean shouldVisitStage (int stageNumber) { public boolean shouldVisitStage(int stageNumber) {
return true; return true;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void setParent (final ComponentVisitor theParent) { public void setParent(final ComponentVisitor theParent) {
parent = theParent; parent = theParent;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Rocket visitable) { public void visit(final Rocket visitable) {
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Stage visitable) { public void visit(final Stage visitable) {
if (mass > 0d) { if (mass > 0d) {
stageComponents.add(mass); stageComponents.add(mass);
} }
mass = 0d; mass = 0d;
RocketComponent[] rc = visitable.getChildren(); List<RocketComponent> rc = visitable.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RocketComponent visitable) { public void visit(final RocketComponent visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* Recurse through the given rocket component. * Recurse through the given rocket component.
* *
* @param root the root component; all children will be visited recursively * @param root the root component; all children will be visited recursively
*/ */
protected void goDeep (final RocketComponent root) { protected void goDeep(final RocketComponent root) {
RocketComponent[] rc = root.getChildren(); List<RocketComponent> rc = root.getChildren();
goDeep(rc); goDeep(rc);
} }
/** /**
* Recurse through the given rocket component. * Recurse through the given rocket component.
* *
* @param theRc an array of rocket components; all children will be visited recursively * @param theRc an array of rocket components; all children will be visited recursively
*/ */
protected void goDeep (final RocketComponent[] theRc) { protected void goDeep(final List<RocketComponent> theRc) {
for (RocketComponent rocketComponent : theRc) { for (RocketComponent rocketComponent : theRc) {
rocketComponent.accept(parent); rocketComponent.accept(parent);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final ExternalComponent visitable) { public void visit(final ExternalComponent visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final BodyComponent visitable) { public void visit(final BodyComponent visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RingComponent visitable) { public void visit(final RingComponent visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final InnerTube visitable) { public void visit(final InnerTube visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final LaunchLug visitable) { public void visit(final LaunchLug visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final Transition visitable) { public void visit(final Transition visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final RadiusRingComponent visitable) { public void visit(final RadiusRingComponent visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final MassObject visitable) { public void visit(final MassObject visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final NoseCone visitable) { public void visit(final NoseCone visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final BodyTube visitable) { public void visit(final BodyTube visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final TrapezoidFinSet visitable) { public void visit(final TrapezoidFinSet visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final EllipticalFinSet visitable) { public void visit(final EllipticalFinSet visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void visit (final FreeformFinSet visitable) { public void visit(final FreeformFinSet visitable) {
mass += visitable.getMass(); mass += visitable.getMass();
goDeep(visitable); goDeep(visitable);
} }
/** /**
* Get the list of stages, sort from Stage 1 .. Stage N. * Get the list of stages, sort from Stage 1 .. Stage N.
* *
* @return a sorted list of stages * @return a sorted list of stages
*/ */
public List<Double> getStages () { public List<Double> getStages() {
return stageComponents; return stageComponents;
} }
/** /**
* Close by setting the last stage. * Close by setting the last stage.
*/ */
public void close () { @Override
if (mass > 0d) { public void close() {
stageComponents.add(mass); if (mass > 0d) {
} stageComponents.add(mass);
} }
}
} }