1 24 25 package org.objectweb.cjdbc.console.gui.jtools; 26 27 import java.io.IOException ; 28 import java.io.Writer ; 29 30 import javax.swing.JTextArea ; 31 32 39 40 public class JTextAreaWriter extends Writer 41 { 42 43 private boolean closed = false; 44 private JTextArea textArea; 45 private StringBuffer buffer; 46 47 52 53 public JTextAreaWriter(JTextArea textArea) 54 { 55 setTextArea(textArea); 56 } 57 58 63 64 public void setTextArea(JTextArea textArea) 65 { 66 if (textArea == null) 67 { 68 throw new IllegalArgumentException ("The text area must not be null."); 69 } 70 this.textArea = textArea; 71 } 72 73 74 75 public void close() 76 { 77 closed = true; 78 } 79 80 85 86 public void flush() throws IOException 87 { 88 if (closed) 89 { 90 throw new IOException ("The stream is closed."); 91 } 92 textArea.append(getBuffer().toString()); 93 textArea.setCaretPosition(textArea.getDocument().getLength()); 94 buffer = null; 95 } 96 97 103 104 public void write(char[] charArray) throws IOException 105 { 106 write(charArray, 0, charArray.length); 107 } 108 109 118 119 public void write(char[] charArray, int offset, int length) 120 throws IOException 121 { 122 if (closed) 123 { 124 throw new IOException ("The stream is not open."); 125 } 126 getBuffer().append(charArray, offset, length); 127 } 128 129 135 136 public void write(int c) throws IOException 137 { 138 if (closed) 139 { 140 throw new IOException ("The stream is not open."); 141 } 142 getBuffer().append((char) c); 143 } 144 145 151 152 public void write(String string) throws IOException 153 { 154 if (closed) 155 { 156 throw new IOException ("The stream is not open."); 157 } 158 getBuffer().append(string); 159 } 160 161 170 171 public void write(String string, int offset, int length) throws IOException 172 { 173 if (closed) 174 { 175 throw new IOException ("The stream is not open."); 176 } 177 getBuffer().append(string.substring(offset, length)); 178 } 179 180 186 187 private StringBuffer getBuffer() 188 { 189 if (buffer == null) 190 { 191 buffer = new StringBuffer (); 192 } 193 return buffer; 194 } 195 196 } 197 | Popular Tags |