KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.field;
2 import jimm.datavision.Report;
3 import jimm.datavision.Section;
4 import jimm.datavision.Group;
5 import jimm.util.I18N;
6 import java.util.HashMap JavaDoc;
7
8 /**
9  * A SpecialField represents a special value such as the report name or
10  * current page number. The value of a SpecialField is a string that
11  * identifies which value to display.
12  * <ul>
13  * <li>report.name</li>
14  * <li>report.title</li>
15  * <li>report.author</li>
16  * <li>report.description</li>
17  * <li>report.date</li>
18  * <li>report.row</li>
19  * <li>page.number</li>
20  * <li>group.count</li>
21  * </ul>
22  *
23  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
24  */

25 public class SpecialField extends Field {
26
27 public static final String JavaDoc TYPE_STRING = "special";
28
29 /**
30  * Returns an array of all of the special field names. We read the map
31  * each time because (in theory) the language could have changed and
32  * all the strings could be different.
33  *
34  * @return an array strings containing all of the special field names
35  */

36 public static HashMap JavaDoc specialFieldNames() {
37     HashMap JavaDoc map = new HashMap JavaDoc();
38
39     map.put("report.title", I18N.get("SpecialField.report.title"));
40     map.put("report.name", I18N.get("SpecialField.report.name"));
41     map.put("report.author", I18N.get("SpecialField.report.author"));
42     map.put("report.description", I18N.get("SpecialField.report.description"));
43     map.put("report.date", I18N.get("SpecialField.report.date"));
44     map.put("report.row", I18N.get("SpecialField.report.row"));
45     map.put("page.number", I18N.get("SpecialField.page.number"));
46     map.put("group.count", I18N.get("SpecialField.group.count"));
47
48     return map;
49 }
50
51 /**
52  * Returns the value associated with the given special field name string.
53  *
54  * @param str the special field name string
55  * @param report the report that is running
56  * @return a string
57  */

58 public static Object JavaDoc value(Field f, String JavaDoc str, Report report) {
59     if ("report.title".equals(str))
60     return report.getTitle();
61     if ("report.name".equals(str))
62     return report.getName();
63     if ("report.author".equals(str))
64     return report.getAuthor();
65     if ("report.description".equals(str))
66     return report.getDescription();
67     else if ("report.date".equals(str))
68     return new java.util.Date JavaDoc();
69     else if ("report.row".equals(str))
70     return new Integer JavaDoc(report.rowNumber());
71     else if ("page.number".equals(str))
72     return new Integer JavaDoc(report.pageNumber());
73     else if ("group.count".equals(str))
74     return groupCount(f);
75     else
76     return I18N.get("SpecialField.unknown");
77 }
78
79 /**
80  * Returns the group count for the group in which this field resides. If we
81  * are in the detail section, return the count of the innermost group. Else
82  * If the group is <code>null</code> (for example, we are in the report
83  * footer), returns the report.row value. If <var>f</var> is
84  * <code>null</code>, return 0.
85  *
86  * @param f a field
87  * @return the number of records in the group or the current record number
88  * within the group.
89  */

90 protected static Integer JavaDoc groupCount(Field f) {
91     if (f == null)
92     return new Integer JavaDoc(0);
93
94     Report report = f.getReport();
95     Group group = report.findGroup(f.getSection());
96
97     if (group == null && f.getSection().isDetail())
98     group = report.innermostGroup(); // May be null
99

100     return new Integer JavaDoc(group == null ? report.rowNumber()
101                : group.getRecordCount());
102 }
103
104
105 /**
106  * Constructs a special field with the specified id in the specified report
107  * section whose special value is <i>value</i>.
108  *
109  * @param id the new field's id
110  * @param report the report containing this element
111  * @param section the report section in which the field resides
112  * @param value the magic string
113  * @param visible show/hide flag
114  */

115 public SpecialField(Long JavaDoc id, Report report, Section section, Object JavaDoc value,
116             boolean visible)
117 {
118     super(id, report, section, value, visible);
119 }
120
121 public String JavaDoc dragString() {
122     return typeString() + ":" + value;
123 }
124
125 public String JavaDoc typeString() { return TYPE_STRING; }
126
127 public String JavaDoc designLabel() { return "{" + value + "}"; }
128
129 public String JavaDoc formulaString() { return "{%" + value + "}"; }
130
131 /**
132  * Returns the value of this field.
133  *
134  * @return a string
135  */

136 public Object JavaDoc getValue() {
137     return SpecialField.value(this, (String JavaDoc)value, section.getReport());
138 }
139 }
140
Popular Tags