KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > SectionArea


1 package jimm.datavision;
2 import jimm.util.I18N;
3 import jimm.util.XMLWriter;
4 import java.util.*;
5
6 /*
7  * A section area holds an ordered list of {@see Section}s and knows its
8  * name and type.
9  *
10  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
11  */

12 public class SectionArea implements Writeable {
13
14 public static final int REPORT_HEADER = 0;
15 public static final int REPORT_FOOTER = 1;
16 public static final int PAGE_HEADER = 2;
17 public static final int PAGE_FOOTER = 3;
18 public static final int DETAIL = 4;
19 public static final int GROUP_HEADER = 5;
20 public static final int GROUP_FOOTER = 6;
21
22 /**
23  * These are I18N lookup keys for <code>REPORT_*</code> constants.
24  * The order of these keys must match the values of those constants.
25  */

26 protected static final String JavaDoc[] AREA_NAME_KEYS = {
27     "Report.report_header",
28     "Report.report_footer",
29     "Report.page_header",
30     "Report.page_footer",
31     "Report.detail",
32     "Report.group_header",
33     "Report.group_footer"
34 };
35
36 List sections;
37 /** One of the <code>REPORT_*</code> constants. */
38 int area;
39
40 /**
41  * Given the <code>REPORT_*</code> constant <var>area</var>,
42  * returns the section area name.
43  *
44  * @param area a <code>REPORT_*</code> constant
45  */

46 public static String JavaDoc nameFromArea(int area) {
47     return I18N.get(AREA_NAME_KEYS[area]);
48 }
49
50 /**
51  * Constructor.
52  *
53  * @param area a <code>REPORT_*</code> constant
54  */

55 public SectionArea(int area) {
56     this.area = area;
57     sections = new ArrayList();
58 }
59
60 public int getArea() {
61     return area;
62 }
63
64 public int indexOf(Section s) {
65     return sections.indexOf(s);
66 }
67
68 public Section get(int index) {
69     return (Section)sections.get(index);
70 }
71
72 public Section first() {
73     return (Section)sections.get(0);
74 }
75
76 /**
77  * Adds a section to our list and sets its name and other area-related
78  * information.
79  *
80  * @param s a section
81  */

82 public void add(Section s) {
83     sections.add(s);
84     imprint(s);
85 }
86
87 /**
88  * Adds a section to our list and sets its name and other area-related
89  * information.
90  *
91  * @param s a section
92  */

93 public void add(int index, Section s) {
94     sections.add(index, s);
95     imprint(s);
96 }
97
98 /**
99  * Adds a (possibly created) section after <var>afterThis</var> and
100  * returns the section. If <var>section</var> is <code>null</code>,
101  * a new section will be created.
102  *
103  * @param section the section to insert; if <code>null</code>, a new
104  * section will be created
105  * @param afterThis the new section will be inserted after this one
106  */

107 public Section insertAfter(Section section, Section afterThis) {
108     if (section == null) {
109     if (afterThis != null)
110         section = new Section(afterThis.getReport());
111     else
112         throw new IllegalArgumentException JavaDoc("SectionArea.insertAfter:" +
113                            " both section and afterThis" +
114                            " can't be null");
115     }
116
117     sections.add(sections.indexOf(afterThis) + 1, section);
118     imprint(section);
119     return section;
120 }
121
122 /**
123  * Modifies <var>section</var> so it knows that it's part of this area.
124  *
125  * @param section a section
126  */

127 protected void imprint(Section section) {
128     section.setArea(this);
129 }
130
131 /**
132  * Returns <code>true</code> if this is a report detail section.
133  *
134  * @return <code>true</code> if this is a report detail section
135  */

136 public boolean isDetail() {
137     return area == DETAIL;
138 }
139
140 /**
141  * Removes a section.
142  *
143  * @param s a report section
144  */

145 public void remove(Section s) {
146     sections.remove(s);
147     s.setArea(null);
148 }
149
150 /**
151  * Returns <code>true</code> if <var>s</var> is one of our sections.
152  *
153  * @param s a section
154  * @return <code>true</code> if <var>s</var> is one of our sections
155  */

156 public boolean contains(Section s) {
157     return sections.contains(s);
158 }
159
160 /**
161  * Returns the name of this area.
162  *
163  * @return the name of this area
164  */

165 public String JavaDoc getName() {
166     return nameFromArea(area);
167 }
168
169 /**
170  * Returns an unmodifiable version of our list of sections.
171  *
172  * @return an unmodifiable version of our list of sections.
173  */

174 public List sections() {
175     return Collections.unmodifiableList(sections);
176 }
177
178 public Iterator iterator() {
179     return sections.iterator();
180 }
181
182 public int size() {
183     return sections.size();
184 }
185
186 public boolean isEmpty() {
187     return sections.isEmpty();
188 }
189
190 public void clear() {
191     for (Iterator iter = sections.iterator(); iter.hasNext(); )
192     ((Section)iter.next()).setArea(null);
193     sections.clear();
194 }
195
196 public void withSectionsDo(SectionWalker s) {
197     for (Iterator iter = sections.iterator(); iter.hasNext(); )
198     s.step((Section)iter.next());
199 }
200
201 public void writeXML(XMLWriter out) {
202     ListWriter.writeList(out, sections);
203 }
204
205 }
206
Popular Tags