/*
* Copyright (C) 2011 Peransin Nicolas.
* Use is subject to license terms.
*/
package org.mypsycho.swing;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.text.JTextComponent;
/**
* XXX Doc
* <p>Detail ... </p>
* @author Peransin Nicolas
*/
public abstract class JTextStream extends PrintStream {
private static final OutputStream STUB = new ByteArrayOutputStream(0);
public static final int DEFAULT_MAX = 2000*1000;
final protected int maxPrintedChar;
final protected int start = 200;
final protected JTextComponent content;
// private static final Style colorStyle(Color c) {
// Style style = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
// StyleConstants.setForeground(style, c);
// return style;
// }
/**
* No need to close
* @param mess
*/
public JTextStream(JTextComponent c) {
this(c, DEFAULT_MAX);
}
public JTextStream(JTextComponent c, int max) {
super(STUB); // used as lock : need a OuputWriter or a OuputStream
maxPrintedChar = max;
content = c;
}
public JTextComponent getText() {
return content;
}
/**
* Flush the stream.
* @see #checkError()
*/
public void flush() {
super.flush();
content.invalidate();
}
/**
* Close the stream.
* @see #checkError()
*/
public void close() {
// close studio ??
}
/**
* Flush the stream if it's not closed and check its error state.
* Errors are cumulative; once the stream encounters an error, this
* routine will return true on all successive calls.
*
* @return True if the print stream has encountered an error, either on the
* underlying output stream or during a format conversion.
*/
public boolean checkError() { return false; }
/** Indicate that an error has occurred. */
protected void setError() {}
/**
* Write a portion of an array of characters.
* @param buf Array of characters
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*/
public void write(char buf[], int off, int len) {
write(new String(buf, off, len));
}
/**
* Write an array of characters. This method cannot be inherited from the
* Writer class because it must suppress I/O exceptions.
* @param buf Array of characters to be written
*/
public void write(char buf[]) { write(new String(buf)); }
/**
* Write a portion of a string.
* @param s A String
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*/
public void write(String s, int off, int len) {
if ( (off == 0) && (len == s.length()) )
write(s);
else
write(s.substring(off, len));
}
/**
* Write the specified byte to this stream. If the byte is a newline and
* automatic flushing is enabled then the <code>flush</code> method will be
* invoked.
*
* <p> Note that the byte is written as given; to write a character that
* will be translated according to the platform's default character
* encoding, use the <code>print(char)</code> or <code>println(char)</code>
* methods.
*
* @param b The byte to be written
* @see #print(char)
* @see #println(char)
*/
public void write(int b) {
print((char) b);
}
/**
* Write a string. This method cannot be inherited from the Writer class
* because it must suppress I/O exceptions.
* @param s String to be written
*/
public abstract void write(String s);
/* Methods that do terminate lines */
/**
* Terminate the current line by writing the line separator string. The
* line separator string is defined by the system property
* <code>line.separator</code>, and is not necessarily a single newline
* character (<code>'\n'</code>).
*/
public void println() {
write("\n");
}
/* Methods that do not terminate lines */
/**
* Print a boolean value. The string produced by <code>{@link
* java.lang.String#valueOf(boolean)}</code> is translated into bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param b The <code>boolean</code> to be printed
*/
public void print(boolean b) {
write(b ? "true" : "false");
}
/**
* Print a character. The character is translated into one or more bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param c The <code>char</code> to be printed
*/
public void print(char c) {
write(String.valueOf(c));
}
/**
* Print an integer. The string produced by <code>{@link
* java.lang.String#valueOf(int)}</code> is translated into bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param i The <code>int</code> to be printed
* @see java.lang.Integer#toString(int)
*/
public void print(int i) {
write(String.valueOf(i));
}
/**
* Print a long integer. The string produced by <code>{@link
* java.lang.String#valueOf(long)}</code> is translated into bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param l The <code>long</code> to be printed
* @see java.lang.Long#toString(long)
*/
public void print(long l) {
write(String.valueOf(l));
}
/**
* Print a floating-point number. The string produced by <code>{@link
* java.lang.String#valueOf(float)}</code> is translated into bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param f The <code>float</code> to be printed
* @see java.lang.Float#toString(float)
*/
public void print(float f) {
write(String.valueOf(f));
}
/**
* Print a double-precision floating-point number. The string produced by
* <code>{@link java.lang.String#valueOf(double)}</code> is translated into
* bytes according to the platform's default character encoding, and these
* bytes are written in exactly the manner of the <code>{@link
* #write(int)}</code> method.
*
* @param d The <code>double</code> to be printed
* @see java.lang.Double#toString(double)
*/
public void print(double d) {
write(String.valueOf(d));
}
/**
* Print an array of characters. The characters are converted into bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param s The array of chars to be printed
*
* @throws NullPointerException If <code>s</code> is <code>null</code>
*/
public void print(char s[]) {
write(s);
}
/**
* Print a string. If the argument is <code>null</code> then the string
* <code>"null"</code> is printed. Otherwise, the string's characters are
* converted into bytes according to the platform's default character
* encoding, and these bytes are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param s The <code>String</code> to be printed
*/
public void print(String s) {
write(s);
}
/**
* Print an object. The string produced by the <code>{@link
* java.lang.String#valueOf(Object)}</code> method is translated into bytes
* according to the platform's default character encoding, and these bytes
* are written in exactly the manner of the
* <code>{@link #write(int)}</code> method.
*
* @param obj The <code>Object</code> to be printed
* @see java.lang.Object#toString()
*/
public void print(Object obj) {
write(String.valueOf(obj));
}
/* Methods that do terminate lines */
/**
* Print a boolean and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(boolean)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>boolean</code> to be printed
*/
public void println(boolean x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print a character and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(char)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>char</code> to be printed.
*/
public void println(char x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print an integer and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(int)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>int</code> to be printed.
*/
public void println(int x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print a long and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(long)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x a The <code>long</code> to be printed.
*/
public void println(long x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print a float and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(float)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>float</code> to be printed.
*/
public void println(float x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print a double and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(double)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>double</code> to be printed.
*/
public void println(double x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print an array of characters and then terminate the line. This method
* behaves as though it invokes <code>{@link #print(char[])}</code> and
* then <code>{@link #println()}</code>.
*
* @param x an array of chars to print.
*/
public void println(char x[]) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print a String and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>String</code> to be printed.
*/
public void println(String x) {
synchronized (content) {
print(x);
println();
}
}
/**
* Print an Object and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(Object)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>Object</code> to be printed.
*/
public void println(Object x) {
synchronized (content) {
print(x);
println();
}
}
} // endclass StudioStream