Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

JTextAreaWriter.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
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  * A implementation of the java.io.Writer class which provides writing to a
00034  * JTextArea via a stream.
00035  * 
00036  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00037  * @author Anthony Eden
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    * Constructor.
00049    * 
00050    * @param textArea The JTextArea to write to.
00051    */
00052 
00053   public JTextAreaWriter(JTextArea textArea)
00054   {
00055     setTextArea(textArea);
00056   }
00057 
00058   /**
00059    * Set the JTextArea to write to.
00060    * 
00061    * @param textArea The JTextArea
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   /** Close the stream. */
00074 
00075   public void close()
00076   {
00077     closed = true;
00078   }
00079 
00080   /**
00081    * Flush the data that is currently in the buffer.
00082    * 
00083    * @throws IOException if fails
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    * Write the given character array to the output stream.
00099    * 
00100    * @param charArray The character array
00101    * @throws IOException if fails
00102    */
00103 
00104   public void write(char[] charArray) throws IOException
00105   {
00106     write(charArray, 0, charArray.length);
00107   }
00108 
00109   /**
00110    * Write the given character array to the output stream beginning from the
00111    * given offset and proceeding to until the given length is reached.
00112    * 
00113    * @param charArray The character array
00114    * @param offset The start offset
00115    * @param length The length to write
00116    * @throws IOException if fails
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    * Write the given character to the output stream.
00131    * 
00132    * @param c The character
00133    * @throws IOException if fails
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    * Write the given String to the output stream.
00147    * 
00148    * @param string The String
00149    * @throws IOException if fails
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    * Write the given String to the output stream beginning from the given
00163    * offset and proceeding to until the given length is reached.
00164    * 
00165    * @param string The String
00166    * @param offset The start offset
00167    * @param length The length to write
00168    * @throws IOException if fails
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    * Get the StringBuffer which holds the data prior to writing via a call to
00182    * the <code>flush()</code> method. This method should never return null.
00183    * 
00184    * @return A StringBuffer
00185    */
00186 
00187   private StringBuffer getBuffer()
00188   {
00189     if (buffer == null)
00190     {
00191       buffer = new StringBuffer();
00192     }
00193     return buffer;
00194   }
00195 
00196 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1