KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > SortedLayoutEngine


1 package jimm.datavision.layout;
2 import jimm.datavision.*;
3 import jimm.datavision.field.Field;
4 import jimm.datavision.field.ImageField;
5 import java.io.PrintWriter JavaDoc;
6 import java.util.*;
7
8 /**
9  * A sorted layout engine outputs the fields within each section in order
10  * of their y then x coordinates. Another way of putting it: the fields are
11  * sorted top to bottom, then left to right. This ensures, for example,
12  * that character-based layout engines such as <code>CharSepLE</code> and
13  * <code>HTMLLE</code> will display fields in the correct left-to-right
14  * order.
15  *
16  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
17  */

18 public abstract class SortedLayoutEngine extends LayoutEngine {
19
20 protected HashMap sectionFields;
21 protected Comparator comp;
22
23 /**
24  * Constructor.
25  */

26 public SortedLayoutEngine() {
27     this(null);
28 }
29
30 /**
31  * Constructor.
32  *
33  * @param out output print writer
34  */

35 public SortedLayoutEngine(PrintWriter JavaDoc out) {
36     super(out);
37     sectionFields = new HashMap();
38
39     // Sorts fields by their y coordinates, then their x coordinates.
40
comp = new Comparator() {
41     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
42         double y1 = ((Field)o1).getBounds().y;
43         double y2 = ((Field)o2).getBounds().y;
44         if (y1 == y2) {
45         double x1 = ((Field)o1).getBounds().x;
46         double x2 = ((Field)o2).getBounds().x;
47         return (x1 < x2) ? -1 : ((x1 > x2) ? 1 : 0);
48         }
49         else
50         return (y1 < y2) ? -1 : ((y1 > y2) ? 1 : 0);
51     }
52     };
53 }
54
55 /**
56  * This override iterates over a list of fields that have been sorted
57  * by their y and x coordinates. Put another way, the fields are output
58  * top to bottom, left to right.
59  *
60  * @param sect a section
61  */

62 protected void doOutputSection(Section sect) {
63     Object JavaDoc[] fields = (Object JavaDoc[])sectionFields.get(sect);
64     if (fields == null)
65     fields = buildSectionFields(sect);
66
67     if (fields != null) {
68     // Output the fields in the section
69
for (int i = 0; i < fields.length; ++i) {
70         Field f = (Field)fields[i];
71         if (f.isVisible()) {
72         if (f instanceof ImageField)
73             outputImage((ImageField)f);
74         else
75             outputField(f);
76         }
77     }
78     }
79     // Output the lines
80
for (Iterator iter = sect.lines(); iter.hasNext(); ) {
81     Line l = (Line)iter.next();
82     if (l.isVisible()) outputLine(l);
83     }
84 }
85
86 /**
87  * Creates, saves, and returns an array of fields sorted by their y and x
88  * coordinates (y first, then x). Another way of putting it: the fields are
89  * sorted top to bottom, then left to right.
90  *
91  * @param sect a report section
92  * @return a sorted array of fields
93  */

94 protected Object JavaDoc[] buildSectionFields(Section sect) {
95     int numFields = sect.numFields();
96     if (numFields == 0)
97     return null;
98
99     Object JavaDoc[] fields = sect.fieldsSortedBy(comp);
100     sectionFields.put(sect, fields);
101
102     return fields;
103 }
104
105 }
106
Popular Tags