001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.gui.widget;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.PrintStream;
020:
021: import javax.swing.JTextArea;
022:
023: /**
024: * <p>This subclass of PrintStream is designed to be compatible with System.out and System.err but it provides
025: * its own functionality and does not borrow from PrintStream. All methods are overridden. The calls to each method
026: * behave as expected with the exception of flush() which appears to insert a new line for each call. flush() sends
027: * the current line to the JTextArea with a call to JTextArea.append(String) which adds a
028: * line in the buffer.</p>
029: * @author Paul Hinds
030: * @version 1.0
031: */
032: public class JTextAreaPrintStream extends PrintStream {
033:
034: private JTextArea textArea;
035: private StringBuffer line;
036:
037: public JTextAreaPrintStream(JTextArea textArea) {
038: super (new ByteArrayOutputStream(0));// wasted all methods are overridden
039: this .textArea = textArea;
040: line = new StringBuffer();
041: }
042:
043: /**
044: * The stream can not be closed
045: */
046: public void close() {
047: }
048:
049: public synchronized void flush() {
050: if (line.length() > 0) {
051: textArea.append(line.toString());
052: line = new StringBuffer();
053: }
054: }
055:
056: public synchronized void write(byte[] b) {
057: line.append(new String(b));
058: }
059:
060: public synchronized void write(byte[] b, int off, int len) {
061: line.append(new String(b, off, len));
062: }
063:
064: public synchronized void write(int c) {
065: line.append(c);
066: }
067:
068: public synchronized boolean checkError() {
069: return false;
070: }
071:
072: public synchronized void print(Object obj) {
073: line.append(obj);
074: }
075:
076: public synchronized void print(String s) {
077: line.append(s);
078: }
079:
080: public synchronized void print(boolean b) {
081: line.append(b);
082: }
083:
084: public synchronized void print(char c) {
085: line.append(c);
086: }
087:
088: public synchronized void print(char[] s) {
089: line.append(s);
090: }
091:
092: public synchronized void print(double d) {
093: line.append(d);
094: }
095:
096: public synchronized void print(float f) {
097: line.append(f);
098: }
099:
100: public synchronized void print(int i) {
101: line.append(i);
102: }
103:
104: public synchronized void print(long l) {
105: line.append(l);
106: }
107:
108: public synchronized void println() {
109: line.append('\n');
110: flush();
111: }
112:
113: public synchronized void println(Object x) {
114: if (line.length() > 0) {
115: textArea.append(line.append(String.valueOf(x)).toString());
116: textArea.append("\n");
117: line = new StringBuffer();
118: } else {
119: textArea.append(String.valueOf(x));
120: textArea.append("\n");
121: }
122: }
123:
124: public synchronized void println(String x) {
125: if (line.length() > 0) {
126: textArea.append(line.append(x).toString());
127: textArea.append("\n");
128: line = new StringBuffer();
129: } else {
130: textArea.append(x);
131: textArea.append("\n");
132: }
133: }
134:
135: public synchronized void println(boolean x) {
136: if (line.length() > 0) {
137: textArea.append(line.append(String.valueOf(x)).toString());
138: textArea.append("\n");
139: line = new StringBuffer();
140: } else {
141: textArea.append(String.valueOf(x));
142: textArea.append("\n");
143: }
144: }
145:
146: public synchronized void println(char x) {
147: if (line.length() > 0) {
148: textArea.append(line.append(String.valueOf(x)).toString());
149: textArea.append("\n");
150: line = new StringBuffer();
151: } else {
152: textArea.append(String.valueOf(x));
153: textArea.append("\n");
154: }
155: }
156:
157: public synchronized void println(char[] x) {
158: if (line.length() > 0) {
159: textArea.append(line.append(String.valueOf(x)).toString());
160: textArea.append("\n");
161: line = new StringBuffer();
162: } else {
163: textArea.append(String.valueOf(x));
164: textArea.append("\n");
165: }
166: }
167:
168: public synchronized void println(double x) {
169: if (line.length() > 0) {
170: textArea.append(line.append(String.valueOf(x)).toString());
171: textArea.append("\n");
172: line = new StringBuffer();
173: } else {
174: textArea.append(String.valueOf(x));
175: textArea.append("\n");
176: }
177: }
178:
179: public synchronized void println(float x) {
180: if (line.length() > 0) {
181: textArea.append(line.append(String.valueOf(x)).toString());
182: textArea.append("\n");
183: line = new StringBuffer();
184: } else {
185: textArea.append(String.valueOf(x));
186: textArea.append("\n");
187: }
188: }
189:
190: public synchronized void println(int x) {
191: if (line.length() > 0) {
192: textArea.append(line.append(String.valueOf(x)).toString());
193: textArea.append("\n");
194: line = new StringBuffer();
195: } else {
196: textArea.append(String.valueOf(x));
197: textArea.append("\n");
198: }
199: }
200:
201: public synchronized void println(long x) {
202: if (line.length() > 0) {
203: textArea.append(line.append(String.valueOf(x)).toString());
204: textArea.append("\n");
205: line = new StringBuffer();
206: } else {
207: textArea.append(String.valueOf(x));
208: textArea.append("\n");
209: }
210: }
211:
212: protected void setError() {
213: }
214:
215: }
|