KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > Section


1 package jimm.datavision;
2 import jimm.datavision.field.Field;
3 import jimm.datavision.field.FormulaField;
4 import jimm.util.XMLWriter;
5 import java.util.*;
6
7 /**
8  * A section of a report contains a group of fields and suppression
9  * information. Sections include page headers and footers, report headers
10  * and footers, group headers and footers, and details. Sections contain
11  * elements (fields and lines).
12  *
13  * @see Report
14  * @see Field
15  * @see Line
16  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
17  */

18 public class Section
19     extends Observable
20     implements Writeable, Observer
21 {
22
23 protected static final double DEFAULT_HEIGHT = 20;
24
25 protected Report report;
26 protected SectionArea area;
27 protected double minHeight;
28 protected ArrayList fields;
29 protected ArrayList lines;
30 protected SuppressionProc suppressionProc;
31 protected boolean pageBreak;
32
33 /**
34  * Constructor.
35  *
36  * @param r the report containing this section
37  */

38 public Section(Report r) {
39     report = r;
40     minHeight = DEFAULT_HEIGHT;
41     fields = new ArrayList();
42     lines = new ArrayList();
43     suppressionProc = new SuppressionProc(report);
44 }
45
46 public void update(Observable o, Object JavaDoc arg) {
47     setChanged();
48     notifyObservers(arg);
49 }
50
51 /**
52  * Returns the report containing this section.
53  */

54 public Report getReport() { return report; }
55
56 /**
57  * Returns the min height of this section.
58  *
59  * @return the min height
60  */

61 public double getMinHeight() { return minHeight; }
62
63 /**
64  * Sets the min height.
65  *
66  * @param newMinHeight the new min height
67  */

68 public void setMinHeight(double newMinHeight) {
69     if (minHeight != newMinHeight) {
70     minHeight = newMinHeight;
71     setChanged();
72     notifyObservers();
73     }
74 }
75
76 /**
77  * Returns the height of this section: not the minimum height defined
78  * when the report was designed but rather the height necessary to
79  * output this section. The height returned is the maximum of
80  * <var>minHeight</var> and the highest y coordinate of any field.
81  *
82  * @return the height this section will need to be output
83  */

84 public double getOutputHeight() {
85     double h = minHeight;
86     for (Iterator iter = fields.iterator(); iter.hasNext(); ) {
87     Field f = (Field)iter.next();
88     double y = f.getBounds().y + f.getOutputHeight();
89     if (y > h) h = y;
90     }
91     return h;
92 }
93
94 /**
95  * Returns the width of this section.
96  *
97  * @return the width
98  */

99 public double getWidth() { return report.getPaperFormat().getWidth(); }
100
101 /**
102  * Returns the area this section is contained within.
103  *
104  * @return the area this section is contained within
105  */

106 public SectionArea getArea() { return area; }
107
108 /**
109  * Sets the area this section is contained within and notifies any observers
110  * of the change.
111  *
112  * @param area a section area
113  */

114 public void setArea(SectionArea area) {
115     if (area != this.area) {
116     this.area = area;
117     setChanged();
118     notifyObservers();
119     }
120 }
121
122 /**
123  * Returns the name of this section.
124  *
125  * @return the section name
126  */

127 public String JavaDoc getName() { return (area == null) ? null : area.getName(); }
128
129 /**
130  * Given an id, returns the field within this section that has that id.
131  * If no field with the specified id exists, returns <code>null</code>.
132  *
133  * @return a field, or <code>null</code> if no field with the specified
134  * id exists in this section
135  */

136 public Field findField(Object JavaDoc id) {
137     Long JavaDoc lid;
138     if (id instanceof String JavaDoc)
139     lid = new Long JavaDoc((String JavaDoc)id);
140     else
141     lid = (Long JavaDoc)id;
142
143     for (Iterator iter = fields.iterator(); iter.hasNext(); ) {
144     Field f = (Field)iter.next();
145     if (f.getId().equals(lid))
146         return f;
147     }
148     return null;
149 }
150
151 /**
152  * Adds a field to this section.
153  *
154  * @param f a field
155  */

156 public void addField(Field f) {
157     fields.add(f);
158     f.addObserver(this);
159     f.setSection(this);
160     setChanged();
161     notifyObservers();
162 }
163
164 /**
165  * Removes a field from this section.
166  *
167  * @param f field
168  */

169 public void removeField(Field f) {
170     if (fields.contains(f)) {
171     fields.remove(f);
172     f.deleteObserver(this);
173     f.setSection(null);
174     setChanged();
175     notifyObservers();
176     }
177 }
178
179 /**
180  * Returns an iterator over all fields in this section.
181  *
182  * @return an iterator
183  */

184 public Iterator fields() { return fields.iterator(); }
185
186 /**
187  * Returns an array of this section's fields sorted by <var>comp</var>.
188  *
189  * @param comp the comparator to use for sorting
190  * @return an array of fields sorted by <var>comp</var>
191  */

192 public Object JavaDoc[] fieldsSortedBy(Comparator comp) {
193     Object JavaDoc[] sorted = fields.toArray();
194     Arrays.sort(sorted, comp);
195     return sorted;
196 }
197
198 /**
199  * Returns the number of fields in this section.
200  *
201  * @return the number of fields in this section
202  */

203 public int numFields() { return fields.size(); }
204
205 /**
206  * Adds a line to this section.
207  *
208  * @param l a line
209  */

210 public void addLine(Line l) {
211     lines.add(l);
212     setChanged();
213     notifyObservers();
214 }
215
216 /**
217  * Removes a line from this section.
218  *
219  * @param f line
220  */

221 public void removeLine(Line f) {
222     lines.remove(f);
223     setChanged();
224     notifyObservers();
225 }
226
227 /**
228  * Returns an iterator over all lines in this section.
229  *
230  * @return an iterator
231  */

232 public Iterator lines() { return lines.iterator(); }
233
234 public boolean isHidden() {
235     return suppressionProc.isHidden();
236 }
237
238 /**
239  * Returns <code>true</code> if this is a report detail section.
240  *
241  * @return <code>true</code> if this is a report detail section
242  */

243 public boolean isDetail() { return area.isDetail(); }
244
245 /**
246  * Returns the boolean page break flag.
247  *
248  * @return <code>true</code> if we should start a new page before this
249  * section.
250  */

251 public boolean hasPageBreak() { return pageBreak; }
252
253 /**
254  * Sets the page break flag.
255  *
256  * @param flag new value
257  */

258 public void setPageBreak(boolean flag) { pageBreak = flag; }
259
260 /**
261  * Returns the supression proc this section uses
262  *
263  * @return a suppression proc
264  */

265 public SuppressionProc getSuppressionProc() { return suppressionProc; }
266
267 /**
268  * Returns <code>true</code> if the specified field is inside this section.
269  *
270  * @param f a field
271  * @return <code>true</code> if the field is within this section
272  */

273 public boolean contains(Field f) {
274     return fields.contains(f);
275 }
276
277 /**
278  * Returns <code>true</code> if the specified field exists within this
279  * section either directly (as a field) or indirectly (as a formula used
280  * by an aggregate, user column, or formula or by the suppression proc).
281  *
282  * @param f a field
283  * @return <code>true</code> if the specified field is referenced within
284  * this section
285  */

286 public boolean containsReferenceTo(Field f) {
287     for (Iterator iter = fields(); iter.hasNext(); ) {
288     Field field = (Field)iter.next();
289     if (field == f || field.refersTo(f))
290         return true;
291     }
292     return suppressionProc.refersTo(f);
293 }
294
295 /**
296  * Returns <code>true</code> if the specified formula exists within this
297  * section either directly (as a formula field) or indirectly (as a formula
298  * used by an aggregate, user column, or formula or by the suppression proc).
299  *
300  * @param f a formula
301  * @return <code>true</code> if the specified formula is referenced within
302  * this section
303  */

304 public boolean containsReferenceTo(Formula f) {
305     for (Iterator iter = fields(); iter.hasNext(); ) {
306     Field field = (Field)iter.next();
307     if (field.refersTo(f))
308         return true;
309     }
310     return suppressionProc.refersTo(f);
311 }
312
313 /**
314  * Returns <code>true</code> if the specified user column exists within this
315  * section either directly (as a user column field) or indirectly (as a user
316  * column used by an aggregate, a formula, or by the suppression proc).
317  *
318  * @param uc a user column
319  * @return <code>true</code> if the specified formula is referenced within
320  * this section
321  */

322 public boolean containsReferenceTo(UserColumn uc) {
323     for (Iterator iter = fields(); iter.hasNext(); ) {
324     Field field = (Field)iter.next();
325     if (field.refersTo(uc))
326         return true;
327     }
328     return suppressionProc.refersTo(uc);
329 }
330
331 /**
332  * Returns <code>true</code> if the specified parameter exists within this
333  * section either directly (as a parameter field) or indirectly (as a parameter
334  * used by an aggregate, a formula, or by the suppression proc).
335  *
336  * @param p a parameter
337  * @return <code>true</code> if the specified formula is referenced within
338  * this section
339  */

340 public boolean containsReferenceTo(Parameter p) {
341     for (Iterator iter = fields(); iter.hasNext(); ) {
342     Field field = (Field)iter.next();
343     if (field.refersTo(p))
344         return true;
345     }
346     return suppressionProc.refersTo(p);
347 }
348
349 /**
350  * Forces all of the formulas used in this section to be evaluated.
351  * See the comment for <code>LayoutEngine.groupHeaders</code> for why
352  * this method is necessary.
353  *
354  * @see jimm.datavision.layout.LayoutEngine#groupHeaders
355  */

356 public void evaluateFormulas() {
357     for (Iterator iter = fields(); iter.hasNext(); ) {
358     Field field = (Field)iter.next();
359     if (field instanceof FormulaField)
360         field.getValue();
361     }
362 }
363
364 /**
365  * Return <code>true</code> if this section should be printed, given this
366  * particular row of data and our supressed state and suppression proc.
367  *
368  * @return <code>true</code> if the data should be displayed
369  */

370 public boolean isVisibleForCurrentRow() {
371     return !suppressionProc.suppress();
372 }
373
374 /**
375  * Writes this section and all it contains as an XML tag.
376  *
377  * @param out a writer that knows how to write XML
378  */

379 public void writeXML(XMLWriter out) {
380     out.startElement("section");
381     out.attr("height", minHeight);
382     if (pageBreak) out.attr("pagebreak", pageBreak);
383
384     ListWriter.writeList(out, fields);
385     ListWriter.writeList(out, lines);
386     suppressionProc.writeXML(out);
387
388     out.endElement();
389 }
390
391 }
392
Popular Tags