01: package jimm.datavision.field;
02:
03: import jimm.datavision.Report;
04: import jimm.datavision.Section;
05: import jimm.datavision.gui.FieldWidget;
06: import jimm.datavision.gui.TextFieldWidget;
07: import jimm.datavision.gui.SectionWidget;
08: import jimm.util.XMLWriter;
09:
10: /**
11: * A text field represents static text. The value of a text field holds the
12: * text.
13: *
14: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
15: */
16: public class TextField extends Field {
17:
18: /**
19: * Constructs a text field with the specified id in the specified report
20: * section whose text value is <i>value</i>.
21: *
22: * @param id the new field's id
23: * @param report the report containing this element
24: * @param section the report section in which the field resides
25: * @param value the text string
26: * @param visible show/hide flag
27: */
28: public TextField(Long id, Report report, Section section,
29: Object value, boolean visible) {
30: super (id, report, section, value, visible);
31: }
32:
33: public void setValue(Object newValue) {
34: String newString = (newValue == null) ? null : newValue
35: .toString();
36: if (value != newString
37: && (value == null || !value.equals(newString))) {
38: value = newString;
39: setChanged();
40: notifyObservers();
41: }
42: }
43:
44: public FieldWidget makeWidget(SectionWidget sw) {
45: return new TextFieldWidget(sw, this );
46: }
47:
48: public String dragString() {
49: return typeString() + ":" + value;
50: }
51:
52: public String typeString() {
53: return "text";
54: }
55:
56: public String designLabel() {
57: return value.toString();
58: }
59:
60: /**
61: * Returns a string representing the field as it appears in a formula.
62: * We need to escape quotes in the string.
63: *
64: * @return a string useful in a formula
65: */
66: public String formulaString() {
67: String str = value.toString();
68:
69: int pos = str.indexOf('"');
70: int len = str.length();
71: if (pos == -1 || len == 0)
72: return "\"" + str + "\"";
73:
74: StringBuffer buf = new StringBuffer("\"");
75: for (int i = 0; i < len; ++i) {
76: char c = str.charAt(i);
77: if (c == '"')
78: buf.append("\\\"");
79: else
80: buf.append(c);
81: }
82: buf.append("\"");
83: return buf.toString();
84: }
85:
86: public void writeXML(XMLWriter out) {
87: out.startElement("field");
88: out.attr("id", id);
89: out.attr("type", typeString());
90: out.cdataElement("text", value.toString());
91: writeFieldGuts(out);
92: out.endElement();
93: }
94:
95: }
|