KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > Group


1 package jimm.datavision;
2 import jimm.datavision.field.Field;
3 import jimm.datavision.source.DataSource;
4 import jimm.datavision.source.sql.SQLQuery;
5 import jimm.util.XMLWriter;
6
7 /**
8  * A group uses a {@link Selectable} object to define record grouping. A group
9  * may contain multiple header and footer sections.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public class Group implements Writeable {
14
15 /**
16  * Sort records by the group's selectable in ascending order (the default).
17  */

18 public static final int SORT_ASCENDING = 0;
19 /**
20  * Sort records by the group's selectable in descending order.
21  */

22 public static final int SORT_DESCENDING = 1;
23
24 protected Report report;
25 protected SectionArea headers;
26 protected SectionArea footers;
27 protected Selectable selectable;
28 protected int sortOrder;
29 protected Object JavaDoc value; // Current row value
30
protected boolean newValue;
31 protected boolean firstValue;
32 protected int recordCount;
33
34 /**
35  * Creates a new group and gives it a header section containing a selectable
36  * field and an empty footer section.
37  *
38  * @param report a report
39  * @param selectable a selectable object
40  * @return a new group with a header section containing a column field and
41  * an empty footer section
42  */

43 public static Group create(Report report, Selectable selectable) {
44     Group group = new Group(report, selectable);
45
46     // Create new header section to add to group.
47
Section header = new Section(report);
48
49     // Create field for selected selectable. Make it bold.
50
Field f = Field.create(null, report, header, selectable.fieldTypeString(),
51                selectable.getId(), true);
52     f.getFormat().setBold(true);
53
54     // Add field to header section and add section to group.
55
header.addField(f);
56     group.headers().add(header);
57
58     // Add empty footer section.
59
group.footers().add(new Section(report));
60
61     return group;
62 }
63
64 public static String JavaDoc sortOrderIntToString(int order) {
65     return order == SORT_ASCENDING ? "asc" : "desc";
66 }
67
68 public static int sortOrderStringToInt(String JavaDoc order) {
69     if (order == null || order.length() == 0)
70     return SORT_ASCENDING;
71     return "desc".equals(order.toLowerCase())
72     ? SORT_DESCENDING : SORT_ASCENDING;
73 }
74
75 /**
76  * Constructor.
77  *
78  * @param report a report
79  * @param selectable a selectable thingie
80  */

81 public Group(Report report, Selectable selectable) {
82     this.report = report;
83     this.selectable = selectable;
84     sortOrder = SORT_ASCENDING;
85     headers = new SectionArea(SectionArea.GROUP_HEADER);
86     footers = new SectionArea(SectionArea.GROUP_FOOTER);
87 }
88
89 /**
90  * Returns the selectable used by this group.
91  *
92  * @return a selectable
93  */

94 public Selectable getSelectable() { return selectable; }
95
96 /**
97  * Sets the selectable used by this group.
98  *
99  * @param newSelectable the new selectable
100  */

101 public void setSelectable(Selectable newSelectable) { selectable = newSelectable; }
102
103 /**
104  * Reloads reference to selectable.
105  */

106 public void reloadSelectable(DataSource dataSource) {
107     setSelectable(selectable.reloadInstance(dataSource));
108 }
109
110 public String JavaDoc getSelectableName() {
111     return selectable.getDisplayName();
112 }
113
114 public String JavaDoc getSortString(SQLQuery query) {
115     return selectable.getSortString(query);
116 }
117
118 /**
119  * Returns the sort order (either <code>SORT_ASCENDING</code> or
120  * <code>SORT_DESCENDING</code>).
121  *
122  * @return either <code>SORT_ASCENDING</code> or <code>SORT_DESCENDING</code>
123  */

124 public int getSortOrder() { return sortOrder; }
125
126 /**
127  * Sets the sort order.
128  *
129  * @param newSortOrder either <code>SORT_ASCENDING</code> or
130  * <code>SORT_DESCENDING</code>
131  */

132 public void setSortOrder(int newSortOrder) { sortOrder = newSortOrder; }
133
134 /**
135  * Returns the value of this group's selectable. Only valid while
136  * the report is running.
137  *
138  * @return the value of the selectable; undefined when the report
139  * is not running
140  */

141 public Object JavaDoc getValue() { return value; }
142
143 /**
144  * Sets the group value that is returned by <code>getValue</code>. This
145  * method should only be called by the report while it is running.
146  *
147  * @param report the report from which we retrieve our selectable's value
148  */

149 public void setValue(Report report) {
150     Object JavaDoc val = selectable.getValue(report);
151     if (value == null) {
152     value = val;
153     firstValue = true;
154     newValue = true;
155     }
156     else if (value.equals(val)) {
157     newValue = false;
158     firstValue = false;
159     }
160     else {
161     value = val;
162     newValue = true;
163     firstValue = false;
164     }
165 }
166
167 public void updateCounter() {
168     if (newValue)
169     recordCount = 1;
170     else
171     ++recordCount;
172 }
173
174 /**
175  * Returns <code>true</code> when the value of the selectable has
176  * changed.
177  *
178  * @return <code>true</code> when the value of the selectable has
179  * changed
180  */

181 public boolean isNewValue() { return newValue; }
182
183 /**
184  * Returns the number of records in the group so far.
185  *
186  * @return the number of records in the group so far
187  */

188 public int getRecordCount() { return recordCount; }
189
190 /**
191  * Layout engines need to call this method when a group's footer is being
192  * output not because this group has a new value but because some previous
193  * group's value changed and we want to output this group's footer.
194  *
195  * @see jimm.datavision.layout.LayoutEngine#groupFooters
196  */

197 public void forceFooterOutput() { newValue = true; }
198
199 /**
200  * Returns <code>true</code> when this is the first value ever seen
201  * by the group during a report run.
202  *
203  * @return <code>true</code> if this is the first value ever seen
204  */

205 public boolean isFirstValue() { return firstValue; }
206
207 /**
208  * Returns the headers.
209  *
210  * @return the headers section area
211  */

212 public SectionArea headers() { return headers; }
213
214 /**
215  * Returns the footers.
216  *
217  * @return the footers section area
218  */

219 public SectionArea footers() { return footers; }
220
221 /**
222  * Returns <code>true</code> if the specified section is inside this group,
223  * either as a header or a footer.
224  *
225  * @param s a section
226  * @return <code>true</code> if the section is within this group
227  */

228 public boolean contains(Section s) {
229     return headers.contains(s) || footers.contains(s);
230 }
231
232 /**
233  * Called by a report when it starts running, this method prepares the
234  * group for use.
235  */

236 public void reset() {
237     value = null;
238     newValue = firstValue = true;
239     recordCount = 1;
240 }
241
242 /**
243  * Writes this group as an XML tag. Asks each section to write itself
244  * as well.
245  *
246  * @param out a writer that knows how to write XML
247  */

248 public void writeXML(XMLWriter out) {
249     out.startElement("group");
250     out.attr("groupable-id", selectable.getId());
251     out.attr("groupable-type", selectable.fieldTypeString());
252     out.attr("sort-order", sortOrderIntToString(sortOrder));
253
254     ListWriter.writeList(out, headers.sections(), "headers");
255     ListWriter.writeList(out, footers.sections(), "footers");
256
257     out.endElement();
258 }
259
260 }
261
Popular Tags