KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > cmd > AbstractAggregateCommand


1 package jimm.datavision.gui.cmd;
2 import jimm.datavision.*;
3 import jimm.datavision.field.*;
4 import jimm.datavision.gui.Designer;
5 import jimm.datavision.gui.FieldWidget;
6 import jimm.datavision.gui.SectionWidget;
7
8 /**
9  * A command for adding a aggregate to a field for a particular section.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public class AbstractAggregateCommand extends CommandAdapter {
14
15 protected Report report;
16 protected FieldWidget fieldWidget;
17 protected Group group;
18 protected FieldWidget aggregateWidget;
19 protected String JavaDoc functionName;
20
21 /**
22  * Constructor.
23  *
24  * @param report the report containing the field and the aggregate
25  * @param fw the field widget to which we are adding a aggregate
26  * @param group if <code>null</code>, the aggregate is added to the
27  * report footer; else the aggregate is added to the first section in
28  * the group's footer.
29  * @param commandName the command name
30  */

31 public AbstractAggregateCommand(Report report, FieldWidget fw, Group group,
32                   String JavaDoc functionName, String JavaDoc commandName)
33 {
34     super(commandName);
35     this.report = report;
36     fieldWidget = fw;
37     this.group = group;
38     this.functionName = functionName;
39 }
40
41 public FieldWidget getAggregateWidget() { return aggregateWidget; }
42
43 protected void createAggregate() {
44     // Find the proper footer. Don't worry; report and group footers always
45
// have at least one section.
46
Section s = (group == null) ? report.footers().first()
47       : group.footers().first();
48
49     Field originalField = fieldWidget.getField();
50     Object JavaDoc id = originalField.getId();
51 // if (originalField instanceof AggregateField) // Can't be true
52
// id = ((AggregateField)originalField).getFieldId();
53

54     // Create the aggregate field. Set the group (OK if null).
55
AggregateField aggregate =
56     (AggregateField)Field.create(null, report, s, functionName, id, true);
57     aggregate.setGroup(group);
58         
59     // If we already have a aggregate widget (we already created one, or
60
// this is a delete command and we are re-creating the deleted
61
// aggregate), copy the format and bounds from that. Else, copy the
62
// bounds and format from the field we are aggregating and make the
63
// format bold.
64
Format fmt = null;
65     Rectangle bounds = null;
66     if (aggregateWidget == null) {
67     fmt = originalField.getFormat();
68     fmt = (Format)fmt.clone();
69     fmt.setBold(true);
70
71     bounds = new Rectangle(originalField.getBounds());
72     }
73     else {
74     fmt = aggregateWidget.getField().getFormat();
75     fmt = (Format)fmt.clone();
76
77     bounds = new Rectangle(aggregateWidget.getField().getBounds());
78     }
79     aggregate.setFormat(fmt);
80     aggregate.setBounds(bounds);
81
82     // Add the field to the section
83
s.addField(aggregate);
84
85     // Create the widget and add it to the proper section widget.
86
SectionWidget sectionWidget =
87     Designer.findWindowFor(report).findSectionWidgetFor(s);
88
89     aggregateWidget = new FieldWidget(null, aggregate);
90     sectionWidget.addField(aggregateWidget);
91
92     // For some reason we need to force a repaint of the section. Can't
93
// just call invalidate.
94
sectionWidget.repaint();
95 }
96
97 protected void deleteAggregate() {
98     java.awt.Component JavaDoc parent = aggregateWidget.getComponent().getParent();
99     aggregateWidget.doDelete();
100     parent.repaint();
101 }
102
103 }
104
Popular Tags