001: package jimm.datavision.field;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.Section;
005: import jimm.datavision.Group;
006: import jimm.util.I18N;
007: import java.util.HashMap;
008:
009: /**
010: * A SpecialField represents a special value such as the report name or
011: * current page number. The value of a SpecialField is a string that
012: * identifies which value to display.
013: * <ul>
014: * <li>report.name</li>
015: * <li>report.title</li>
016: * <li>report.author</li>
017: * <li>report.description</li>
018: * <li>report.date</li>
019: * <li>report.row</li>
020: * <li>page.number</li>
021: * <li>group.count</li>
022: * </ul>
023: *
024: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
025: */
026: public class SpecialField extends Field {
027:
028: public static final String TYPE_STRING = "special";
029:
030: /**
031: * Returns an array of all of the special field names. We read the map
032: * each time because (in theory) the language could have changed and
033: * all the strings could be different.
034: *
035: * @return an array strings containing all of the special field names
036: */
037: public static HashMap specialFieldNames() {
038: HashMap map = new HashMap();
039:
040: map.put("report.title", I18N.get("SpecialField.report.title"));
041: map.put("report.name", I18N.get("SpecialField.report.name"));
042: map
043: .put("report.author", I18N
044: .get("SpecialField.report.author"));
045: map.put("report.description", I18N
046: .get("SpecialField.report.description"));
047: map.put("report.date", I18N.get("SpecialField.report.date"));
048: map.put("report.row", I18N.get("SpecialField.report.row"));
049: map.put("page.number", I18N.get("SpecialField.page.number"));
050: map.put("group.count", I18N.get("SpecialField.group.count"));
051:
052: return map;
053: }
054:
055: /**
056: * Returns the value associated with the given special field name string.
057: *
058: * @param str the special field name string
059: * @param report the report that is running
060: * @return a string
061: */
062: public static Object value(Field f, String str, Report report) {
063: if ("report.title".equals(str))
064: return report.getTitle();
065: if ("report.name".equals(str))
066: return report.getName();
067: if ("report.author".equals(str))
068: return report.getAuthor();
069: if ("report.description".equals(str))
070: return report.getDescription();
071: else if ("report.date".equals(str))
072: return new java.util.Date();
073: else if ("report.row".equals(str))
074: return new Integer(report.rowNumber());
075: else if ("page.number".equals(str))
076: return new Integer(report.pageNumber());
077: else if ("group.count".equals(str))
078: return groupCount(f);
079: else
080: return I18N.get("SpecialField.unknown");
081: }
082:
083: /**
084: * Returns the group count for the group in which this field resides. If we
085: * are in the detail section, return the count of the innermost group. Else
086: * If the group is <code>null</code> (for example, we are in the report
087: * footer), returns the report.row value. If <var>f</var> is
088: * <code>null</code>, return 0.
089: *
090: * @param f a field
091: * @return the number of records in the group or the current record number
092: * within the group.
093: */
094: protected static Integer groupCount(Field f) {
095: if (f == null)
096: return new Integer(0);
097:
098: Report report = f.getReport();
099: Group group = report.findGroup(f.getSection());
100:
101: if (group == null && f.getSection().isDetail())
102: group = report.innermostGroup(); // May be null
103:
104: return new Integer(group == null ? report.rowNumber() : group
105: .getRecordCount());
106: }
107:
108: /**
109: * Constructs a special field with the specified id in the specified report
110: * section whose special value is <i>value</i>.
111: *
112: * @param id the new field's id
113: * @param report the report containing this element
114: * @param section the report section in which the field resides
115: * @param value the magic string
116: * @param visible show/hide flag
117: */
118: public SpecialField(Long id, Report report, Section section,
119: Object value, boolean visible) {
120: super (id, report, section, value, visible);
121: }
122:
123: public String dragString() {
124: return typeString() + ":" + value;
125: }
126:
127: public String typeString() {
128: return TYPE_STRING;
129: }
130:
131: public String designLabel() {
132: return "{" + value + "}";
133: }
134:
135: public String formulaString() {
136: return "{%" + value + "}";
137: }
138:
139: /**
140: * Returns the value of this field.
141: *
142: * @return a string
143: */
144: public Object getValue() {
145: return SpecialField.value(this , (String) value, section
146: .getReport());
147: }
148: }
|