Merged l10n branch to trunk
[debian/openrocket] / src / net / sf / openrocket / gui / print / PrintableContext.java
1 /*
2  * PrintableContext.java
3  *
4  */
5 package net.sf.openrocket.gui.print;
6
7 import java.util.*;
8
9 /**
10  * Instances of this class are meant to keep track of what the user has selected to be printed.
11  */
12 public class PrintableContext implements Comparable<PrintableContext>, Iterable<PrintableContext> {
13
14     /**
15      * The stage number.  May be null for printables that have no stage meaning.
16      */
17     private Set<Integer> stageNumber;
18
19     /**
20      * The type of thing to be printed.
21      */
22     private OpenRocketPrintable printable;
23
24     /**
25      * Sort of a reverse map that tracks each type of printable item and the stages for which that item is to be printed.
26      */
27     private final Map<OpenRocketPrintable, Set<Integer>> previous = new TreeMap<OpenRocketPrintable, Set<Integer>>();
28
29     /**
30      * Constructor.
31      */
32     public PrintableContext () {
33     }
34
35     /**
36      * Constructor.
37      *
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
40      *
41      * @throws IllegalArgumentException thrown if thePrintable.isStageSpecific
42      */
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.");
47         }
48         stageNumber = theStageNumber;
49         printable = thePrintable;
50     }
51
52     /**
53      * Add a type of printable to a stage (number).
54      *
55      * @param theStageNumber  the stage number
56      * @param thePrintable    the printable to associate with the stage
57      */
58     public void add (final Integer theStageNumber, final OpenRocketPrintable thePrintable) {
59         Set<Integer> stages = previous.get(thePrintable);
60         if (stages == null) {
61             stages = new TreeSet<Integer>();
62             previous.put(thePrintable, stages);
63         }
64         if (theStageNumber != null) {
65             stages.add(theStageNumber);
66         }
67     }
68
69     /** PrintableContext iterator. */
70     public Iterator<PrintableContext> iterator () {
71         return new Iterator<PrintableContext>() {
72
73             Iterator<OpenRocketPrintable> keyIter = previous.keySet().iterator();
74
75             @Override
76             public boolean hasNext () {
77                 return keyIter.hasNext();
78             }
79
80             @Override
81             public PrintableContext next () {
82                 final OpenRocketPrintable key = keyIter.next();
83                 return new PrintableContext(previous.get(key), key);
84             }
85
86             @Override
87             public void remove () {
88             }
89         };
90
91     }
92
93     /**
94      * Get the stage number, if it's applicable to the printable.
95      *
96      * @return the stage number
97      */
98     public Set<Integer> getStageNumber () {
99         return stageNumber;
100     }
101
102     /**
103      * Get the printable.
104      *
105      * @return the printable
106      */
107     public OpenRocketPrintable getPrintable () {
108         return printable;
109     }
110
111     @Override
112     public int compareTo (final PrintableContext other) {
113         return this.printable.getPrintOrder() - other.printable.getPrintOrder();
114     }
115
116 }