KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > field > FormulaField


1 package jimm.datavision.field;
2 import jimm.datavision.*;
3 import jimm.datavision.gui.FieldWidget;
4 import jimm.datavision.gui.FormulaWidget;
5 import jimm.datavision.gui.SectionWidget;
6 import java.util.Collection JavaDoc;
7 import java.util.Observer JavaDoc;
8 import java.util.Observable JavaDoc;
9
10 /**
11  * A formula field represents a formula calculated on-the-fly. The value of a
12  * formula field holds a {@link Formula} object. (In the XML, the formula
13  * field's value is the id of the formula.)
14  *
15  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
16  */

17 public class FormulaField extends Field implements Observer JavaDoc {
18
19 protected Formula formula;
20
21 /**
22  * Constructs a formula field with the specified id in the specified report
23  * section whose {@link Formula}'s id is <var>value</var>.
24  *
25  * @param id the new field's id
26  * @param report the report containing this element
27  * @param section the report section in which the field resides
28  * @param value the id of a formula
29  * @param visible show/hide flag
30  */

31 public FormulaField(Long JavaDoc id, Report report, Section section, Object JavaDoc value,
32             boolean visible)
33 {
34     super(id, report, section, value, visible);
35     formula = report.findFormula(value);
36     formula.addObserver(this);
37 }
38
39 protected void finalize() throws Throwable JavaDoc {
40     formula.deleteObserver(this);
41     super.finalize();
42 }
43
44 public void update(Observable JavaDoc o, Object JavaDoc arg) {
45     setChanged();
46     notifyObservers(arg);
47 }
48
49 public FieldWidget makeWidget(SectionWidget sw) {
50     return new FormulaWidget(sw, this);
51 }
52
53 /**
54  * Not really used; we drag formulas, not formula fields.
55  */

56 public String JavaDoc dragString() {
57     return typeString() + ":" + formula.getId();
58 }
59
60 /**
61  * Returns the formula.
62  *
63  * @return the formula
64  */

65 public Formula getFormula() { return formula; }
66
67 /**
68  * Sets the formula.
69  *
70  * @param newFormula the new formula
71  */

72 public void setFormula(Formula newFormula) {
73     if (formula != newFormula) {
74     formula.deleteObserver(this);
75     formula = newFormula;
76     formula.addObserver(this);
77     setChanged();
78     notifyObservers();
79     }
80 }
81
82 public String JavaDoc typeString() { return "formula"; }
83
84 public String JavaDoc designLabel() { return formula.designLabel(); }
85
86 public String JavaDoc formulaString() { return formula.formulaString(); }
87
88 public boolean refersTo(Field f) {
89     return formula.refersTo(f);
90 }
91
92 public boolean refersTo(Formula f) {
93     return f == formula || formula.refersTo(f);
94 }
95
96 public boolean refersTo(UserColumn uc) {
97     return formula.refersTo(uc);
98 }
99
100 public boolean refersTo(Parameter p) {
101     return formula.refersTo(p);
102 }
103
104 /**
105  * This override returns <code>true</code> if this formula is in a detail
106  * section. We don't really know that this formula returns a number, so
107  * we'll err on the side of allowing aggregation.
108  *
109  * @return <code>true</code> if this field can be aggregated
110  */

111 public boolean canBeAggregated() {
112     // Section can be null during dragging.
113
return section != null && section.isDetail();
114 }
115
116 /**
117  * Returns the value of this field. For formula fields, this is the
118  * value generated by evaluating the {@link Formula}.
119  *
120  * @return the result of evaluating the formula
121  */

122 public Object JavaDoc getValue() { return formula.eval(this); }
123
124 /**
125  * Returns a collection of the columns used in the formula. This is used
126  * by the report's query when it is figuring out what columns and tables
127  * are used by the report. Calls {@link Formula#columnsUsed}.
128  *
129  * @return a possibly empty collection of database columns
130  * @see jimm.datavision.source.Query#findSelectablesUsed
131  */

132 public Collection JavaDoc columnsUsed() {
133     return formula.columnsUsed();
134 }
135
136 /**
137  * Returns a collection of the user columns used in the formula. This is
138  * used by the report's query when it is figuring out what columns, tables,
139  * and user columns are used by the report. Calls
140  * {@link Formula#userColumnsUsed}.
141  *
142  * @return a possibly empty collection of user columns
143  * @see jimm.datavision.source.Query#findSelectablesUsed
144  */

145 public Collection JavaDoc userColumnsUsed() {
146     return formula.userColumnsUsed();
147 }
148
149 }
150
Popular Tags