KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > jtools > JTextAreaWriter


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.gui.jtools;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import javax.swing.JTextArea JavaDoc;
31
32 /**
33  * A implementation of the java.io.Writer class which provides writing to a
34  * JTextArea via a stream.
35  *
36  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
37  * @author Anthony Eden
38  */

39
40 public class JTextAreaWriter extends Writer JavaDoc
41 {
42
43   private boolean closed = false;
44   private JTextArea JavaDoc textArea;
45   private StringBuffer JavaDoc buffer;
46
47   /**
48    * Constructor.
49    *
50    * @param textArea The JTextArea to write to.
51    */

52
53   public JTextAreaWriter(JTextArea JavaDoc textArea)
54   {
55     setTextArea(textArea);
56   }
57
58   /**
59    * Set the JTextArea to write to.
60    *
61    * @param textArea The JTextArea
62    */

63
64   public void setTextArea(JTextArea JavaDoc textArea)
65   {
66     if (textArea == null)
67     {
68       throw new IllegalArgumentException JavaDoc("The text area must not be null.");
69     }
70     this.textArea = textArea;
71   }
72
73   /** Close the stream. */
74
75   public void close()
76   {
77     closed = true;
78   }
79
80   /**
81    * Flush the data that is currently in the buffer.
82    *
83    * @throws IOException if fails
84    */

85
86   public void flush() throws IOException JavaDoc
87   {
88     if (closed)
89     {
90       throw new IOException JavaDoc("The stream is closed.");
91     }
92     textArea.append(getBuffer().toString());
93     textArea.setCaretPosition(textArea.getDocument().getLength());
94     buffer = null;
95   }
96
97   /**
98    * Write the given character array to the output stream.
99    *
100    * @param charArray The character array
101    * @throws IOException if fails
102    */

103
104   public void write(char[] charArray) throws IOException JavaDoc
105   {
106     write(charArray, 0, charArray.length);
107   }
108
109   /**
110    * Write the given character array to the output stream beginning from the
111    * given offset and proceeding to until the given length is reached.
112    *
113    * @param charArray The character array
114    * @param offset The start offset
115    * @param length The length to write
116    * @throws IOException if fails
117    */

118
119   public void write(char[] charArray, int offset, int length)
120       throws IOException JavaDoc
121   {
122     if (closed)
123     {
124       throw new IOException JavaDoc("The stream is not open.");
125     }
126     getBuffer().append(charArray, offset, length);
127   }
128
129   /**
130    * Write the given character to the output stream.
131    *
132    * @param c The character
133    * @throws IOException if fails
134    */

135
136   public void write(int c) throws IOException JavaDoc
137   {
138     if (closed)
139     {
140       throw new IOException JavaDoc("The stream is not open.");
141     }
142     getBuffer().append((char) c);
143   }
144
145   /**
146    * Write the given String to the output stream.
147    *
148    * @param string The String
149    * @throws IOException if fails
150    */

151
152   public void write(String JavaDoc string) throws IOException JavaDoc
153   {
154     if (closed)
155     {
156       throw new IOException JavaDoc("The stream is not open.");
157     }
158     getBuffer().append(string);
159   }
160
161   /**
162    * Write the given String to the output stream beginning from the given
163    * offset and proceeding to until the given length is reached.
164    *
165    * @param string The String
166    * @param offset The start offset
167    * @param length The length to write
168    * @throws IOException if fails
169    */

170
171   public void write(String JavaDoc string, int offset, int length) throws IOException JavaDoc
172   {
173     if (closed)
174     {
175       throw new IOException JavaDoc("The stream is not open.");
176     }
177     getBuffer().append(string.substring(offset, length));
178   }
179
180   /**
181    * Get the StringBuffer which holds the data prior to writing via a call to
182    * the <code>flush()</code> method. This method should never return null.
183    *
184    * @return A StringBuffer
185    */

186
187   private StringBuffer JavaDoc getBuffer()
188   {
189     if (buffer == null)
190     {
191       buffer = new StringBuffer JavaDoc();
192     }
193     return buffer;
194   }
195
196 }
197
Popular Tags