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 | */
|
---|
16 | package org.tp23.gui.widget;
|
---|
17 |
|
---|
18 | import java.io.ByteArrayOutputStream;
|
---|
19 | import java.io.PrintStream;
|
---|
20 |
|
---|
21 | import 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 | */
|
---|
32 | public 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);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | public synchronized void print(float f) {
|
---|
104 | line.append(f);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | public synchronized void print(int i) {
|
---|
109 | line.append(i);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | public synchronized void print(long l) {
|
---|
114 | line.append(l);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | public synchronized void println() {
|
---|
119 | line.append('\n');
|
---|
120 | flush();
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | public synchronized void println(Object x) {
|
---|
125 | if(line.length()>0){
|
---|
126 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
127 | textArea.append("\n");
|
---|
128 | line = new StringBuffer();
|
---|
129 | }
|
---|
130 | else {
|
---|
131 | textArea.append(String.valueOf(x));
|
---|
132 | textArea.append("\n");
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | public synchronized void println(String x) {
|
---|
138 | if(line.length()>0){
|
---|
139 | textArea.append(line.append(x).toString());
|
---|
140 | textArea.append("\n");
|
---|
141 | line = new StringBuffer();
|
---|
142 | }
|
---|
143 | else {
|
---|
144 | textArea.append(x);
|
---|
145 | textArea.append("\n");
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public synchronized void println(boolean x) {
|
---|
150 | if(line.length()>0){
|
---|
151 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
152 | textArea.append("\n");
|
---|
153 | line = new StringBuffer();
|
---|
154 | }
|
---|
155 | else {
|
---|
156 | textArea.append(String.valueOf(x));
|
---|
157 | textArea.append("\n");
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | public synchronized void println(char x) {
|
---|
163 | if(line.length()>0){
|
---|
164 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
165 | textArea.append("\n");
|
---|
166 | line = new StringBuffer();
|
---|
167 | }
|
---|
168 | else {
|
---|
169 | textArea.append(String.valueOf(x));
|
---|
170 | textArea.append("\n");
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | public synchronized void println(char[] x) {
|
---|
176 | if(line.length()>0){
|
---|
177 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
178 | textArea.append("\n");
|
---|
179 | line = new StringBuffer();
|
---|
180 | }
|
---|
181 | else {
|
---|
182 | textArea.append(String.valueOf(x));
|
---|
183 | textArea.append("\n");
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | public synchronized void println(double x) {
|
---|
189 | if(line.length()>0){
|
---|
190 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
191 | textArea.append("\n");
|
---|
192 | line = new StringBuffer();
|
---|
193 | }
|
---|
194 | else {
|
---|
195 | textArea.append(String.valueOf(x));
|
---|
196 | textArea.append("\n");
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | public synchronized void println(float x) {
|
---|
202 | if(line.length()>0){
|
---|
203 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
204 | textArea.append("\n");
|
---|
205 | line = new StringBuffer();
|
---|
206 | }
|
---|
207 | else {
|
---|
208 | textArea.append(String.valueOf(x));
|
---|
209 | textArea.append("\n");
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | public synchronized void println(int x) {
|
---|
215 | if(line.length()>0){
|
---|
216 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
217 | textArea.append("\n");
|
---|
218 | line = new StringBuffer();
|
---|
219 | }
|
---|
220 | else {
|
---|
221 | textArea.append(String.valueOf(x));
|
---|
222 | textArea.append("\n");
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | public synchronized void println(long x) {
|
---|
228 | if(line.length()>0){
|
---|
229 | textArea.append(line.append(String.valueOf(x)).toString());
|
---|
230 | textArea.append("\n");
|
---|
231 | line = new StringBuffer();
|
---|
232 | }
|
---|
233 | else {
|
---|
234 | textArea.append(String.valueOf(x));
|
---|
235 | textArea.append("\n");
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | protected void setError() {
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | }
|
---|