01: package kawa;
02:
03: /** A Writer that appends its output to a TextArea.
04: * Based on code from Albert L. Ting" <alt@artisan.com>.
05: */
06:
07: public class TextAreaWriter extends java.io.Writer {
08: java.awt.TextArea area;
09: String str = "";
10:
11: public TextAreaWriter(java.awt.TextArea area) {
12: this .area = area;
13: }
14:
15: public synchronized void write(int x) {
16: str = str + (char) x;
17: if (x == '\n')
18: flush();
19: }
20:
21: public void write(String str) {
22: if (area instanceof MessageArea) {
23: MessageArea msg = (MessageArea) area;
24: msg.write(str);
25: } else
26: area.append(str);
27: }
28:
29: public synchronized void write(char[] data, int off, int len) {
30: flush();
31: write(new String(data, off, len));
32: }
33:
34: public synchronized void flush() {
35: if (!str.equals("")) {
36: write(str);
37: str = "";
38: }
39: }
40:
41: public void close() {
42: flush();
43: }
44: }
|