2 * PrintableContext.java
5 package net.sf.openrocket.gui.print;
10 * Instances of this class are meant to keep track of what the user has selected to be printed.
12 public class PrintableContext implements Comparable<PrintableContext>, Iterable<PrintableContext> {
15 * The stage number. May be null for printables that have no stage meaning.
17 private Set<Integer> stageNumber;
20 * The type of thing to be printed.
22 private OpenRocketPrintable printable;
25 * Sort of a reverse map that tracks each type of printable item and the stages for which that item is to be printed.
27 private final Map<OpenRocketPrintable, Set<Integer>> previous = new TreeMap<OpenRocketPrintable, Set<Integer>>();
32 public PrintableContext () {
38 * @param theStageNumber the stage number of the printable; may be null if not applicable
39 * @param thePrintable the type of the thing to be printed
41 * @throws IllegalArgumentException thrown if thePrintable.isStageSpecific
43 private PrintableContext (final Set<Integer> theStageNumber, final OpenRocketPrintable thePrintable)
44 throws IllegalArgumentException {
45 if (thePrintable.isStageSpecific() && theStageNumber == null) {
46 throw new IllegalArgumentException("A stage number must be provided when a printable is stage specific.");
48 stageNumber = theStageNumber;
49 printable = thePrintable;
53 * Add a type of printable to a stage (number).
55 * @param theStageNumber the stage number
56 * @param thePrintable the printable to associate with the stage
58 public void add (final Integer theStageNumber, final OpenRocketPrintable thePrintable) {
59 Set<Integer> stages = previous.get(thePrintable);
61 stages = new TreeSet<Integer>();
62 previous.put(thePrintable, stages);
64 if (theStageNumber != null) {
65 stages.add(theStageNumber);
69 /** PrintableContext iterator. */
70 public Iterator<PrintableContext> iterator () {
71 return new Iterator<PrintableContext>() {
73 Iterator<OpenRocketPrintable> keyIter = previous.keySet().iterator();
76 public boolean hasNext () {
77 return keyIter.hasNext();
81 public PrintableContext next () {
82 final OpenRocketPrintable key = keyIter.next();
83 return new PrintableContext(previous.get(key), key);
87 public void remove () {
94 * Get the stage number, if it's applicable to the printable.
96 * @return the stage number
98 public Set<Integer> getStageNumber () {
105 * @return the printable
107 public OpenRocketPrintable getPrintable () {
112 public int compareTo (final PrintableContext other) {
113 return this.printable.getPrintOrder() - other.printable.getPrintOrder();