1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.gui.widget;
17
18import java.io.ByteArrayOutputStream;
19import java.io.PrintStream;
20
21import javax.swing.JTextArea;
22
23/**
24 * <p>This subclass of PrintStream is designed to be compatible with System.out and System.err but it provides
25* its own functionality and does not borrow from PrintStream. All methods are overridden.  The calls to each method
26* behave as expected with the exception of flush() which appears to insert a new line for each call. flush() sends
27* the current line to the JTextArea with a call to JTextArea.append(String) which adds a
28* line in the buffer.</p>
29 * @author Paul Hinds
30 * @version 1.0
31 */
32public class JTextAreaPrintStream extends PrintStream{
33
34    private JTextArea textArea;
35    private StringBuffer line;
36
37    public JTextAreaPrintStream(JTextArea textArea) {
38        super(new ByteArrayOutputStream(0));// wasted all methods are overridden
39        this.textArea = textArea;
40        line = new StringBuffer();
41    }
42
43    /**
44     * The stream can not be closed
45     */
46    public void close(){
47    }
48
49    public synchronized void flush(){
50        if(line.length()>0){
51            textArea.append(line.toString());
52            line = new StringBuffer();
53        }
54    }
55
56    public synchronized void write(byte[] b){
57        line.append(new String(b));
58    }
59
60    public synchronized void write(byte[] b, int off, int len){
61        line.append(new String(b,off,len));
62    }
63
64
65    public synchronized void write(int c){
66        line.append(c);
67    }
68
69    public synchronized boolean checkError() {
70        return false;
71    }
72
73    public synchronized void print(Object obj) {
74        line.append(obj);
75    }
76
77
78    public synchronized void print(String s) {
79        line.append(s);
80    }
81
82
83    public synchronized void print(boolean b) {
84        line.append(b);
85    }
86
87
88    public synchronized void print(char c) {
89        line.append(c);
90    }
91
92
93    public synchronized void print(char[] s) {
94        line.append(s);
95    }
96
97
98    public synchronized void print(double d) {
99        line.append(d);
00    }
01
02
03    public synchronized void print(float f) {
04        line.append(f);
05    }
06
07
08    public synchronized void print(int i) {
09        line.append(i);
10    }
11
12
13    public synchronized void print(long l) {
14        line.append(l);
15    }
16
17
18    public synchronized void println() {
19        line.append('\n');
20        flush();
21    }
22
23
24    public synchronized void println(Object x) {
25        if(line.length()>0){
26            textArea.append(line.append(String.valueOf(x)).toString());
27            textArea.append("\n");
28            line = new StringBuffer();
29        }
30        else {
31            textArea.append(String.valueOf(x));
32            textArea.append("\n");
33        }
34    }
35
36
37    public synchronized void println(String x) {
38        if(line.length()>0){
39            textArea.append(line.append(x).toString());
40            textArea.append("\n");
41            line = new StringBuffer();
42        }
43        else {
44            textArea.append(x);
45            textArea.append("\n");
46        }
47    }
48
49    public synchronized void println(boolean x) {
50        if(line.length()>0){
51            textArea.append(line.append(String.valueOf(x)).toString());
52            textArea.append("\n");
53            line = new StringBuffer();
54        }
55        else {
56            textArea.append(String.valueOf(x));
57            textArea.append("\n");
58        }
59    }
60
61
62    public synchronized void println(char x) {
63        if(line.length()>0){
64            textArea.append(line.append(String.valueOf(x)).toString());
65            textArea.append("\n");
66            line = new StringBuffer();
67        }
68        else {
69            textArea.append(String.valueOf(x));
70            textArea.append("\n");
71        }
72    }
73
74
75    public synchronized void println(char[] x) {
76        if(line.length()>0){
77            textArea.append(line.append(String.valueOf(x)).toString());
78            textArea.append("\n");
79            line = new StringBuffer();
80        }
81        else {
82            textArea.append(String.valueOf(x));
83            textArea.append("\n");
84        }
85    }
86
87
88    public synchronized void println(double x) {
89        if(line.length()>0){
90            textArea.append(line.append(String.valueOf(x)).toString());
91            textArea.append("\n");
92            line = new StringBuffer();
93        }
94        else {
95            textArea.append(String.valueOf(x));
96            textArea.append("\n");
97        }
98    }
99
00
01    public synchronized void println(float x) {
02        if(line.length()>0){
03            textArea.append(line.append(String.valueOf(x)).toString());
04            textArea.append("\n");
05            line = new StringBuffer();
06        }
07        else {
08            textArea.append(String.valueOf(x));
09            textArea.append("\n");
10        }
11    }
12
13
14    public synchronized void println(int x) {
15        if(line.length()>0){
16            textArea.append(line.append(String.valueOf(x)).toString());
17            textArea.append("\n");
18            line = new StringBuffer();
19        }
20        else {
21            textArea.append(String.valueOf(x));
22            textArea.append("\n");
23        }
24    }
25
26
27    public synchronized void println(long x) {
28        if(line.length()>0){
29            textArea.append(line.append(String.valueOf(x)).toString());
30            textArea.append("\n");
31            line = new StringBuffer();
32        }
33        else {
34            textArea.append(String.valueOf(x));
35            textArea.append("\n");
36        }
37    }
38
39
40    protected void setError() {
41    }
42
43
44}
45