1 import java.io.Writer;
2 import java.io.IOException;
3 import javax.swing.JTextArea;
4
5 /**
6 * A implementation of the java.io.Writer class
7 * which facilitates writing to a JTextArea via a stream.
8 *
9 * <p><b>Note:</b> There appears to be bug in the Macintosh implementation of
10 * the JDK 1.1 where a PrintWriter writing to this class will not include the
11 * correct line feeds for display in a JTextArea. There is a simple test of
12 * the "java.version" system property which, if it starts with the String "1.1"
13 * will cause newlines to be written each time the buffer is flushed.</p>
14 *
15 * @author Anthony Eden
16 */
17 public class JTextAreaWriter extends Writer {
18 private boolean closed = false;
19 private JTextArea textArea;
20 private StringBuffer buffer;
21
22 /**
23 * Constructor.
24 *
25 * @param textArea The JTextArea to write to.
26 */
27 public JTextAreaWriter(JTextArea textArea) {
28 setTextArea(textArea);
29 }
30
31 /**
32 * Set the JTextArea to write to.
33 *
34 * @param textArea The JTextArea
35 */
36 public void setTextArea(JTextArea textArea) {
37 if (textArea == null) {
38 throw new IllegalArgumentException("The text area must not be null.");
39 }
40 this.textArea = textArea;
41 }
42
43 /**
44 * Close the stream.
45 */
46 public void close() {
47 closed = true;
48 }
49
50 /**
51 * Flush the data that is currently in the buffer.
52 *
53 * @throws IOException
54 */
55 public void flush() throws IOException {
56 if (closed) {
57 throw new IOException("The stream is not open.");
58 }
59 // the newline character should not be necessary. The PrintWriter
60 // should autmatically put the newline, but it doesn't seem to work
61 textArea.append(getBuffer().toString());
62 if (System.getProperty("java.version").startsWith("1.1")) {
63 textArea.append("\n");
64 }
65 textArea.setCaretPosition(textArea.getDocument().getLength());
66 buffer = null;
67 }
68
69 /**
70 * Write the given character array to the output stream.
71 *
72 * @param charArray The character array
73 * @throws IOException
74 */
75 public void write(char[] charArray) throws IOException {
76 write(charArray, 0, charArray.length);
77 }
78
79 /**
80 * Write the given character array to the output stream beginning from
81 * the given offset and proceeding to until the given length is reached.
82 *
83 * @param charArray The character array
84 * @param offset The start offset
85 * @param length The length to write
86 * @throws IOException
87 */
88 public void write(char[] charArray, int offset, int length) throws IOException {
89 if (closed) {
90 throw new IOException("The stream is not open.");
91 }
92 getBuffer().append(charArray, offset, length);
93 }
94
95 /**
96 * Write the given character to the output stream.
97 *
98 * @param c The character
99 * @throws IOException
100 */
101 public void write(int c) throws IOException {
102 if (closed) {
103 throw new IOException("The stream is not open.");
104 }
105 getBuffer().append((char) c);
106 }
107
108 /**
109 * Write the given String to the output stream.
110 *
111 * @param string The String
112 * @throws IOException
113 */
114 public void write(String string) throws IOException {
115 if (closed) {
116 throw new IOException("The stream is not open.");
117 }
118 getBuffer().append(string);
119 }
120
121 /**
122 * Write the given String to the output stream beginning
123 * from the given offset
124 * and proceeding to until the given length is reached.
125 *
126 * @param string The String
127 * @param offset The start offset
128 * @param length The length to write
129 * @throws IOException
130 */
131 public void write(String string, int offset, int length) throws IOException {
132 if (closed) {
133 throw new IOException("The stream is not open.");
134 }
135 getBuffer().append(string.substring(offset, length));
136 }
137
138 // protected methods
139
140 /**
141 * Get the StringBuffer which holds the data prior to writing via
142 * a call to the <code>flush() method. This method should
143 * never return null.
144 *
145 * @return A StringBuffer
146 */
147 protected StringBuffer getBuffer() {
148 if (buffer == null) {
149 buffer = new StringBuffer();
150 }
151 return buffer;
152 }
153 }
