001: package jimm.datavision.field;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.Section;
005: import jimm.datavision.Parameter;
006: import jimm.datavision.gui.FieldWidget;
007: import jimm.datavision.gui.ParameterWidget;
008: import jimm.datavision.gui.SectionWidget;
009: import java.util.Observer;
010: import java.util.Observable;
011:
012: /**
013: * A parameter field represents a parameter. The value of a parameter field
014: * holds a {@link Parameter} object. In the XML, the parameter field's value
015: * is the id of the parameter.
016: *
017: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018: */
019: public class ParameterField extends Field implements Observer {
020:
021: protected Parameter parameter;
022:
023: /**
024: * Constructs a parameter field with the specified id in the specified report
025: * section whose {@link Parameter}'s id is <i>value</i>.
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 parameter
031: * @param visible show/hide flag
032: */
033: public ParameterField(Long id, Report report, Section section,
034: Object value, boolean visible) {
035: super (id, report, section, value, visible);
036: parameter = report.findParameter(value);
037: parameter.addObserver(this );
038: }
039:
040: protected void finalize() throws Throwable {
041: parameter.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 ParameterWidget(sw, this );
052: }
053:
054: /**
055: * Not really used; we drag parameters, not parameter fields.
056: */
057: public String dragString() {
058: return typeString() + ":" + parameter.getId();
059: }
060:
061: /**
062: * Returns the parameter.
063: *
064: * @return the parameter
065: */
066: public Parameter getParameter() {
067: return parameter;
068: }
069:
070: /**
071: * Sets the parameter.
072: *
073: * @param newParameter the new parameter
074: */
075: public void setParameter(Parameter newParameter) {
076: if (parameter != newParameter) {
077: parameter.deleteObserver(this );
078: parameter = newParameter;
079: parameter.addObserver(this );
080: setChanged();
081: notifyObservers();
082: }
083: }
084:
085: public String typeString() {
086: return "parameter";
087: }
088:
089: public String designLabel() {
090: return parameter.designLabel();
091: }
092:
093: public String formulaString() {
094: return parameter.formulaString();
095: }
096:
097: public boolean refersTo(Parameter p) {
098: return parameter == p;
099: }
100:
101: /**
102: * This override returns <code>true</code> if this parameter is in a detail
103: * section and returns a number.
104: *
105: * @return <code>true</code> if this field can be aggregated
106: */
107: public boolean canBeAggregated() {
108: // Section can be null during dragging.
109: return section != null && section.isDetail()
110: && getParameter().getType() == Parameter.TYPE_NUMERIC;
111: }
112:
113: /**
114: * Returns the value of this field. For parameter fields, this is the
115: * value generated by evaluating the {@link Parameter}.
116: *
117: * @return the result of evaluating the parameter
118: */
119: public Object getValue() {
120: return parameter.getValue();
121: }
122:
123: }
|