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

View File

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

View File

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