001: package jimm.datavision;
002:
003: import jimm.util.I18N;
004: import jimm.util.XMLWriter;
005: import java.util.*;
006:
007: /*
008: * A section area holds an ordered list of {@see Section}s and knows its
009: * name and type.
010: *
011: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
012: */
013: public class SectionArea implements Writeable {
014:
015: public static final int REPORT_HEADER = 0;
016: public static final int REPORT_FOOTER = 1;
017: public static final int PAGE_HEADER = 2;
018: public static final int PAGE_FOOTER = 3;
019: public static final int DETAIL = 4;
020: public static final int GROUP_HEADER = 5;
021: public static final int GROUP_FOOTER = 6;
022:
023: /**
024: * These are I18N lookup keys for <code>REPORT_*</code> constants.
025: * The order of these keys must match the values of those constants.
026: */
027: protected static final String[] AREA_NAME_KEYS = {
028: "Report.report_header", "Report.report_footer",
029: "Report.page_header", "Report.page_footer",
030: "Report.detail", "Report.group_header",
031: "Report.group_footer" };
032:
033: List sections;
034: /** One of the <code>REPORT_*</code> constants. */
035: int area;
036:
037: /**
038: * Given the <code>REPORT_*</code> constant <var>area</var>,
039: * returns the section area name.
040: *
041: * @param area a <code>REPORT_*</code> constant
042: */
043: public static String nameFromArea(int area) {
044: return I18N.get(AREA_NAME_KEYS[area]);
045: }
046:
047: /**
048: * Constructor.
049: *
050: * @param area a <code>REPORT_*</code> constant
051: */
052: public SectionArea(int area) {
053: this .area = area;
054: sections = new ArrayList();
055: }
056:
057: public int getArea() {
058: return area;
059: }
060:
061: public int indexOf(Section s) {
062: return sections.indexOf(s);
063: }
064:
065: public Section get(int index) {
066: return (Section) sections.get(index);
067: }
068:
069: public Section first() {
070: return (Section) sections.get(0);
071: }
072:
073: /**
074: * Adds a section to our list and sets its name and other area-related
075: * information.
076: *
077: * @param s a section
078: */
079: public void add(Section s) {
080: sections.add(s);
081: imprint(s);
082: }
083:
084: /**
085: * Adds a section to our list and sets its name and other area-related
086: * information.
087: *
088: * @param s a section
089: */
090: public void add(int index, Section s) {
091: sections.add(index, s);
092: imprint(s);
093: }
094:
095: /**
096: * Adds a (possibly created) section after <var>afterThis</var> and
097: * returns the section. If <var>section</var> is <code>null</code>,
098: * a new section will be created.
099: *
100: * @param section the section to insert; if <code>null</code>, a new
101: * section will be created
102: * @param afterThis the new section will be inserted after this one
103: */
104: public Section insertAfter(Section section, Section afterThis) {
105: if (section == null) {
106: if (afterThis != null)
107: section = new Section(afterThis.getReport());
108: else
109: throw new IllegalArgumentException(
110: "SectionArea.insertAfter:"
111: + " both section and afterThis"
112: + " can't be null");
113: }
114:
115: sections.add(sections.indexOf(afterThis) + 1, section);
116: imprint(section);
117: return section;
118: }
119:
120: /**
121: * Modifies <var>section</var> so it knows that it's part of this area.
122: *
123: * @param section a section
124: */
125: protected void imprint(Section section) {
126: section.setArea(this );
127: }
128:
129: /**
130: * Returns <code>true</code> if this is a report detail section.
131: *
132: * @return <code>true</code> if this is a report detail section
133: */
134: public boolean isDetail() {
135: return area == DETAIL;
136: }
137:
138: /**
139: * Removes a section.
140: *
141: * @param s a report section
142: */
143: public void remove(Section s) {
144: sections.remove(s);
145: s.setArea(null);
146: }
147:
148: /**
149: * Returns <code>true</code> if <var>s</var> is one of our sections.
150: *
151: * @param s a section
152: * @return <code>true</code> if <var>s</var> is one of our sections
153: */
154: public boolean contains(Section s) {
155: return sections.contains(s);
156: }
157:
158: /**
159: * Returns the name of this area.
160: *
161: * @return the name of this area
162: */
163: public String getName() {
164: return nameFromArea(area);
165: }
166:
167: /**
168: * Returns an unmodifiable version of our list of sections.
169: *
170: * @return an unmodifiable version of our list of sections.
171: */
172: public List sections() {
173: return Collections.unmodifiableList(sections);
174: }
175:
176: public Iterator iterator() {
177: return sections.iterator();
178: }
179:
180: public int size() {
181: return sections.size();
182: }
183:
184: public boolean isEmpty() {
185: return sections.isEmpty();
186: }
187:
188: public void clear() {
189: for (Iterator iter = sections.iterator(); iter.hasNext();)
190: ((Section) iter.next()).setArea(null);
191: sections.clear();
192: }
193:
194: public void withSectionsDo(SectionWalker s) {
195: for (Iterator iter = sections.iterator(); iter.hasNext();)
196: s.step((Section) iter.next());
197: }
198:
199: public void writeXML(XMLWriter out) {
200: ListWriter.writeList(out, sections);
201: }
202:
203: }
|