Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.swing.JTextArea;
- import org.eclipse.swt.widgets.Text;
- /**
- * A implementation of the java.io.Writer class which facilitates writing to a
- * JTextArea via a stream.
- *
- * <p>
- * <b>Note:</b> There appears to be bug in the Macintosh implementation of the
- * JDK 1.1 where a PrintWriter writing to this class will not include the
- * correct line feeds for display in a JTextArea. There is a simple test of the
- * "java.version" system property which, if it starts with the String "1.1" will
- * cause newlines to be written each time the buffer is flushed.
- * </p>
- *
- * @author Anthony Eden
- */
- public class TextAreaWriter extends PrintStream {
- private boolean closed = false;
- private Text textArea;
- private StringBuffer buffer;
- /**
- * Constructor.
- *
- * @param txtfieldProtokoll
- * The JTextArea to write to.
- */
- public TextAreaWriter(Text txtfieldProtokoll) {
- super(new OutputStream() {
- public void write(int b) {
- this.write((char) b);
- }
- });
- setTextArea(txtfieldProtokoll);
- }
- /**
- * Set the JTextArea to write to.
- *
- * @param txtfieldProtokoll
- * The JTextArea
- */
- public void setTextArea(Text txtfieldProtokoll) {
- if (txtfieldProtokoll == null) {
- throw new IllegalArgumentException(
- "The text area must not be null.");
- }
- this.textArea = txtfieldProtokoll;
- }
- /** Close the stream. */
- public void close() {
- closed = true;
- }
- /**
- * Flush the data that is currently in the buffer.
- *
- * @throws IOException
- */
- public void flush() { // throws IOException{
- // if(closed){
- // throw new IOException("The stream is not open.");
- // }
- // the newline character should not be necessary. The PrintWriter
- // should autmatically put the newline, but it doesn't seem to work
- textArea.append(getBuffer().toString());
- if (System.getProperty("java.version").startsWith("1.1")) {
- textArea.append("\n");
- }
- // textArea.setCaretPosition(textArea.getText().length());
- // textArea.setca
- buffer = null;
- }
- /**
- * Write the given character array to the output stream.
- *
- * @param charArray
- * The character array
- * @throws IOException
- */
- public void write(char[] charArray) throws IOException {
- write(charArray, 0, charArray.length);
- }
- /**
- * Write the given character array to the output stream beginning from the
- * given offset and proceeding to until the given length is reached.
- *
- * @param charArray
- * The character array
- * @param offset
- * The start offset
- * @param length
- * The length to write
- * @throws IOException
- */
- public void write(char[] charArray, int offset, int length)
- throws IOException {
- if (closed) {
- throw new IOException("The stream is not open.");
- }
- getBuffer().append(charArray, offset, length);
- }
- /**
- * Write the given character to the output stream.
- *
- * @param c
- * The character
- * @throws IOException
- */
- public void write(int c) {
- getBuffer().append((char) c);
- }
- /**
- * Write the given String to the output stream.
- *
- * @param string
- * The String
- * @throws IOException
- */
- public void write(byte[] buf, int off, int len) {
- try {
- char[] c = new char[len];
- for (int i = 0; i < len; i++)
- c[i] = (char) buf[i];
- this.write(c, off, len);
- } catch (Exception e) {
- }
- }
- public void write(String string) throws IOException {
- if (closed) {
- throw new IOException("The stream is not open.");
- }
- getBuffer().append(string);
- }
- /**
- * Write the given String to the output stream beginning from the given
- * offset and proceeding to until the given length is reached.
- *
- * @param string
- * The String
- * @param offset
- * The start offset
- * @param length
- * The length to write
- * @throws IOException
- */
- public void write(String string, int offset, int length) throws IOException {
- if (closed) {
- throw new IOException("The stream is not open.");
- }
- getBuffer().append(string.substring(offset, length));
- }
- // protected methods
- /**
- * Get the StringBuffer which holds the data prior to writing via a call to
- * the <code>flush()method. This method should
- never return null.
- *
- * @return A StringBuffer
- */
- protected StringBuffer getBuffer() {
- if (buffer == null) {
- buffer = new StringBuffer();
- }
- return buffer;
- }
- public void print(boolean b) {
- try {
- if (b)
- write("true");
- else
- write("false");
- flush();
- } catch (Exception e) {
- }
- }
- public void print(char c) {
- try {
- this.write(new char[] { c });
- flush();
- } catch (Exception e) {
- }
- }
- public void print(char[] s) {
- try {
- this.write(s);
- flush();
- } catch (Exception e) {
- }
- }
- public void print(double d) {
- try {
- this.write("" + d);
- flush();
- } catch (Exception e) {
- }
- }
- public void print(float f) {
- try {
- this.write("" + f);
- flush();
- } catch (Exception e) {
- }
- }
- public void print(int i) {
- try {
- this.write("" + i);
- flush();
- } catch (Exception e) {
- }
- }
- public void print(long l) {
- try {
- this.write("" + l);
- flush();
- } catch (Exception e) {
- }
- }
- public void print(Object o) {
- try {
- this.write(o.toString());
- flush();
- } catch (Exception e) {
- }
- }
- public void print(String s) {
- try {
- this.write(s);
- flush();
- } catch (Exception e) {
- }
- }
- public void println(boolean b) {
- print(b);
- println();
- }
- public void println(char c) {
- print(c);
- println();
- }
- public void println(char[] s) {
- print(s);
- println();
- }
- public void println(double d) {
- print(d);
- ;
- println();
- }
- public void println(float f) {
- print(f);
- println();
- }
- public void println(int i) {
- print(i);
- println();
- }
- public void println(long l) {
- print(l);
- println();
- }
- public void println(Object o) {
- print(o);
- println();
- }
- public void println(String s) {
- print(s);
- println();
- }
- public void println() {
- try {
- this.write(new char[] { '\n' });
- flush();
- } catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement