2 * PrintableContext.java
5 package net.sf.openrocket.gui.print;
7 import java.util.Iterator;
10 import java.util.TreeMap;
11 import java.util.TreeSet;
14 * Instances of this class are meant to keep track of what the user has selected to be printed.
16 public class PrintableContext implements Comparable<PrintableContext>, Iterable<PrintableContext> {
19 * The stage number. May be null for printables that have no stage meaning.
21 private Set<Integer> stageNumber;
24 * The type of thing to be printed.
26 private OpenRocketPrintable printable;
29 * Sort of a reverse map that tracks each type of printable item and the stages for which that item is to be printed.
31 private final Map<OpenRocketPrintable, Set<Integer>> previous = new TreeMap<OpenRocketPrintable, Set<Integer>>();
36 public PrintableContext() {
42 * @param theStageNumber the stage number of the printable; may be null if not applicable
43 * @param thePrintable the type of the thing to be printed
45 * @throws IllegalArgumentException thrown if thePrintable.isStageSpecific
47 private PrintableContext(final Set<Integer> theStageNumber, final OpenRocketPrintable thePrintable)
48 throws IllegalArgumentException {
49 if (thePrintable.isStageSpecific() && theStageNumber == null) {
50 throw new IllegalArgumentException("A stage number must be provided when a printable is stage specific.");
52 stageNumber = theStageNumber;
53 printable = thePrintable;
57 * Add a type of printable to a stage (number).
59 * @param theStageNumber the stage number
60 * @param thePrintable the printable to associate with the stage
62 public void add(final Integer theStageNumber, final OpenRocketPrintable thePrintable) {
63 Set<Integer> stages = previous.get(thePrintable);
65 stages = new TreeSet<Integer>();
66 previous.put(thePrintable, stages);
68 if (theStageNumber != null) {
69 stages.add(theStageNumber);
73 /** PrintableContext iterator. */
75 public Iterator<PrintableContext> iterator() {
76 return new Iterator<PrintableContext>() {
78 Iterator<OpenRocketPrintable> keyIter = previous.keySet().iterator();
81 public boolean hasNext() {
82 return keyIter.hasNext();
86 public PrintableContext next() {
87 final OpenRocketPrintable key = keyIter.next();
88 return new PrintableContext(previous.get(key), key);
92 public void remove() {
99 * Get the stage number, if it's applicable to the printable.
101 * @return the stage number
103 public Set<Integer> getStageNumber() {
110 * @return the printable
112 public OpenRocketPrintable getPrintable() {
117 public int compareTo(final PrintableContext other) {
118 return this.printable.getPrintOrder() - other.printable.getPrintOrder();