00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.console.gui.jtools;
00026
00027 import java.io.IOException;
00028 import java.io.Writer;
00029
00030 import javax.swing.JTextArea;
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 public class JTextAreaWriter extends Writer
00041 {
00042
00043 private boolean closed = false;
00044 private JTextArea textArea;
00045 private StringBuffer buffer;
00046
00047
00048
00049
00050
00051
00052
00053 public JTextAreaWriter(JTextArea textArea)
00054 {
00055 setTextArea(textArea);
00056 }
00057
00058
00059
00060
00061
00062
00063
00064 public void setTextArea(JTextArea textArea)
00065 {
00066 if (textArea == null)
00067 {
00068 throw new IllegalArgumentException("The text area must not be null.");
00069 }
00070 this.textArea = textArea;
00071 }
00072
00073
00074
00075 public void close()
00076 {
00077 closed = true;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 public void flush() throws IOException
00087 {
00088 if (closed)
00089 {
00090 throw new IOException("The stream is closed.");
00091 }
00092 textArea.append(getBuffer().toString());
00093 textArea.setCaretPosition(textArea.getDocument().getLength());
00094 buffer = null;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 public void write(char[] charArray) throws IOException
00105 {
00106 write(charArray, 0, charArray.length);
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 public void write(char[] charArray, int offset, int length)
00120 throws IOException
00121 {
00122 if (closed)
00123 {
00124 throw new IOException("The stream is not open.");
00125 }
00126 getBuffer().append(charArray, offset, length);
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136 public void write(int c) throws IOException
00137 {
00138 if (closed)
00139 {
00140 throw new IOException("The stream is not open.");
00141 }
00142 getBuffer().append((char) c);
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 public void write(String string) throws IOException
00153 {
00154 if (closed)
00155 {
00156 throw new IOException("The stream is not open.");
00157 }
00158 getBuffer().append(string);
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 public void write(String string, int offset, int length) throws IOException
00172 {
00173 if (closed)
00174 {
00175 throw new IOException("The stream is not open.");
00176 }
00177 getBuffer().append(string.substring(offset, length));
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187 private StringBuffer getBuffer()
00188 {
00189 if (buffer == null)
00190 {
00191 buffer = new StringBuffer();
00192 }
00193 return buffer;
00194 }
00195
00196 }