KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.field;
2 import jimm.datavision.Report;
3 import jimm.datavision.Section;
4 import jimm.datavision.gui.FieldWidget;
5 import jimm.datavision.gui.TextFieldWidget;
6 import jimm.datavision.gui.SectionWidget;
7 import jimm.util.XMLWriter;
8
9 /**
10  * A text field represents static text. The value of a text field holds the
11  * text.
12  *
13  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
14  */

15 public class TextField extends Field {
16
17 /**
18  * Constructs a text field with the specified id in the specified report
19  * section whose text value is <i>value</i>.
20  *
21  * @param id the new field's id
22  * @param report the report containing this element
23  * @param section the report section in which the field resides
24  * @param value the text string
25  * @param visible show/hide flag
26  */

27 public TextField(Long JavaDoc id, Report report, Section section, Object JavaDoc value,
28          boolean visible)
29 {
30     super(id, report, section, value, visible);
31 }
32
33 public void setValue(Object JavaDoc newValue) {
34     String JavaDoc newString = (newValue == null) ? null : newValue.toString();
35     if (value != newString && (value == null || !value.equals(newString))) {
36     value = newString;
37     setChanged();
38     notifyObservers();
39     }
40 }
41
42 public FieldWidget makeWidget(SectionWidget sw) {
43     return new TextFieldWidget(sw, this);
44 }
45
46 public String JavaDoc dragString() {
47     return typeString() + ":" + value;
48 }
49
50 public String JavaDoc typeString() { return "text"; }
51
52 public String JavaDoc designLabel() { return value.toString(); }
53
54 /**
55  * Returns a string representing the field as it appears in a formula.
56  * We need to escape quotes in the string.
57  *
58  * @return a string useful in a formula
59  */

60 public String JavaDoc formulaString() {
61     String JavaDoc str = value.toString();
62
63     int pos = str.indexOf('"');
64     int len = str.length();
65     if (pos == -1 || len == 0)
66     return "\"" + str + "\"";
67
68     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("\"");
69     for (int i = 0; i < len; ++i) {
70     char c = str.charAt(i);
71     if (c == '"')
72         buf.append("\\\"");
73     else
74         buf.append(c);
75     }
76     buf.append("\"");
77     return buf.toString();
78 }
79
80 public void writeXML(XMLWriter out) {
81     out.startElement("field");
82     out.attr("id", id);
83     out.attr("type", typeString());
84     out.cdataElement("text", value.toString());
85     writeFieldGuts(out);
86     out.endElement();
87 }
88
89 }
90
Popular Tags