KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > MessageArea


1 package kawa;
2
3 import java.io.*;
4 import java.awt.*;
5 import java.awt.event.*;
6
7 /** Simple TextArea that always scrolls to the bottom. Also creates an
8  * out and err PrintWriter so that you can redirect stdout/stderr to
9  * these streams, using the System.setOut/setErr methods.
10  *
11  * @author Albert Ting
12  */

13 public class MessageArea extends TextArea
14   implements KeyListener, TextListener {
15   private kawa.TextAreaWriter out_stream;
16   private PrintWriter out;
17   private PrintWriter err;
18   gnu.text.QueueReader in;
19
20   public int outputMark = 0;
21   public int endMark = -1;
22   int length = 0;
23
24   /**
25    * simple TextArea that always scrolls to the bottom. Also creates an
26    * out and err PrintWriter so that you can redirect stdout/stderr to
27    * these streams, using the System.setOut/setErr methods.
28    */

29   public MessageArea(gnu.text.QueueReader in) {
30     super();
31
32     this.in = in;
33
34     out_stream = new kawa.TextAreaWriter(this);
35     kawa.TextAreaWriter err_stream = new kawa.TextAreaWriter(this);
36     out = new PrintWriter(out_stream);
37     err = new PrintWriter(err_stream);
38
39     addKeyListener(this);
40     addTextListener(this);
41   }
42
43   void enter ()
44   {
45     int pos = getCaretPosition();
46     String JavaDoc str = getText();
47     int len = str.length();
48     if (len != length) {
49       System.err.println("(actual) len:"+len + " (saved) length:"+length);
50     }
51     int lineAfter = str.indexOf('\n', pos);
52     endMark = -1;
53     if (lineAfter < 0)
54       lineAfter = len;
55     int lineBefore = pos == 0 ? 0 : 1 + str.lastIndexOf('\n', pos-1);
56     if (pos >= outputMark || outputMark <= lineAfter) {
57       lineAfter = str.indexOf('\n', outputMark);
58       if (lineAfter < 0) {
59         lineAfter = len;
60         append("\n");
61       }
62       else
63         endMark = len;
64       str = str.substring(outputMark, lineAfter);
65       str = str + '\n';
66       outputMark = lineAfter+1;
67     }
68     else {
69       str = str.substring(lineBefore, lineAfter+1);
70       out_stream.write(str);
71     }
72
73     setCaretPosition(outputMark);
74
75     if (in != null) {
76       in.append(str);
77     }
78   }
79
80   public void keyPressed(KeyEvent e) {
81     int code = e.getKeyCode();
82     if (code == KeyEvent.VK_ENTER)
83       {
84     enter();
85     e.consume();
86       }
87   }
88   public void keyReleased(KeyEvent e) {
89   }
90
91   public void keyTyped(KeyEvent e) {
92   }
93
94   public synchronized void write (String JavaDoc str) {
95     boolean moveCaret = getCaretPosition() == outputMark;
96     insert(str, outputMark);
97     int len = str.length();
98     outputMark += len;
99     if (moveCaret)
100       setCaretPosition(outputMark);
101     if (endMark >= 0)
102       endMark += len;
103   }
104
105   /** Delete old text, prior to line containing outputMark. */
106
107   public synchronized void deleteOldText () {
108     String JavaDoc str = getText();
109     int lineBefore = (outputMark <= 0 ? 0
110               : (str.lastIndexOf('\n', outputMark-1)) + 1);
111     setCaretPosition(outputMark);
112     replaceRange("", 0, lineBefore);
113   }
114
115   public synchronized void textValueChanged (TextEvent e) {
116     int pos = getCaretPosition();
117     String JavaDoc text = getText();
118     int delta = text.length() - length;
119     length += delta;
120     if (pos < outputMark)
121       outputMark += delta;
122     else if (pos - delta < outputMark)
123       outputMark = pos;
124     if (endMark >= 0)
125       {
126     if (pos < endMark)
127       endMark += delta;
128     else if (pos - delta < endMark)
129       endMark = pos;
130       }
131   }
132
133   public PrintWriter getStdout() {
134     return out;
135   }
136   public PrintWriter getStderr() {
137     return err;
138   }
139
140 }
141
Popular Tags