001: package jimm.datavision.field;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.gui.FieldWidget;
005: import jimm.datavision.gui.UserColumnWidget;
006: import jimm.datavision.gui.SectionWidget;
007: import java.util.Collection;
008: import java.util.Observer;
009: import java.util.Observable;
010:
011: /**
012: * A user column field represents a user column, which in turn holds some SQL
013: * that is put in the SELECT clause of a query. The value of a user column
014: * field holds a {@link UserColumn} object. (In the XML, the user column
015: * field's value is the id of the user column.)
016: *
017: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018: */
019: public class UserColumnField extends Field implements Observer {
020:
021: protected UserColumn usercol;
022:
023: /**
024: * Constructs a user column field with the specified id in the specified report
025: * section whose {@link UserColumn}'s id is <var>value</var>.
026: *
027: * @param id the new field's id
028: * @param report the report containing this element
029: * @param section the report section in which the field resides
030: * @param value the id of a user column
031: * @param visible show/hide flag
032: */
033: public UserColumnField(Long id, Report report, Section section,
034: Object value, boolean visible) {
035: super (id, report, section, value, visible);
036: usercol = report.findUserColumn(value);
037: usercol.addObserver(this );
038: }
039:
040: protected void finalize() throws Throwable {
041: usercol.deleteObserver(this );
042: super .finalize();
043: }
044:
045: public void update(Observable o, Object arg) {
046: setChanged();
047: notifyObservers(arg);
048: }
049:
050: public FieldWidget makeWidget(SectionWidget sw) {
051: return new UserColumnWidget(sw, this );
052: }
053:
054: /**
055: * Not really used; we drag user columns, not user column fields.
056: */
057: public String dragString() {
058: return typeString() + ":" + usercol.getId();
059: }
060:
061: /**
062: * Returns the user column.
063: *
064: * @return the user column
065: */
066: public UserColumn getUserColumn() {
067: return usercol;
068: }
069:
070: /**
071: * Sets the user column.
072: *
073: * @param newUsercol the new user column
074: */
075: public void setUserColumn(UserColumn newUsercol) {
076: if (usercol != newUsercol) {
077: usercol.deleteObserver(this );
078: usercol = newUsercol;
079: usercol.addObserver(this );
080: setChanged();
081: notifyObservers();
082: }
083: }
084:
085: public String typeString() {
086: return "usercol";
087: }
088:
089: public String designLabel() {
090: return usercol.designLabel();
091: }
092:
093: public String formulaString() {
094: return usercol.formulaString();
095: }
096:
097: public boolean refersTo(Field f) {
098: return usercol.refersTo(f);
099: }
100:
101: public boolean refersTo(Parameter p) {
102: return usercol.refersTo(p);
103: }
104:
105: /**
106: * This override returns <code>true</code> if this user column is in a
107: * detail section. We don't really know that this user column returns
108: * a number, so we'll err on the side of allowing aggregation.
109: *
110: * @return <code>true</code> if this field can be aggregated
111: */
112: public boolean canBeAggregated() {
113: // Section can be null during dragging.
114: return section != null && section.isDetail();
115: }
116:
117: /**
118: * Returns the value of this field. For user column fields, this is the
119: * value generated by evaluating the {@link UserColumn}.
120: *
121: * @return the result of evaluating the usercol
122: */
123: public Object getValue() {
124: return getReport().columnValue(usercol);
125: }
126:
127: /**
128: * Returns a collection of the columns used in the user column. This is used
129: * by the report's query when it is figuring out what columns and tables
130: * are used by the report. Calls {@link UserColumn#columnsUsed}.
131: *
132: * @see jimm.datavision.source.Query#findSelectablesUsed
133: */
134: public Collection columnsUsed() {
135: return usercol.columnsUsed();
136: }
137:
138: }
|