/* * Copyright (c) 2006 Thomas Weise * * E-Mail : tweise@gmx.de * Creation Date : 2006-05-29 14:16:30 * Original Filename: org.sfc.gui.text.DocumentWriter.java * Version : 1.0.0 * Last modification: 2006-05-29 * by: Thomas Weise * * License : GNU LESSER GENERAL PUBLIC LICENSE * Version 2.1, February 1999 * You should have received a copy of this license along * with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307, USA or download the license under * http://www.gnu.org/copyleft/lesser.html. * * Warranty : This software is provided "as is" without any * warranty; without even the implied warranty of * merchantability or fitness for a particular purpose. * See the Gnu Lesser General Public License for more * details. */ package org.sfc.gui.text; import javax.swing.text.Document; import javax.swing.text.JTextComponent; import org.sfc.io.ReferenceCountedWriter; import org.sfc.xml.XML; /** * This class provides a java writer interface for swing text documents. * * @author Thomas Weise */ public final class DocumentWriter extends ReferenceCountedWriter { /** * The internal document. */ private final Document m_document ; /** * A length restriction for the underlying document. */ private int m_max_len ; /** * Create a new document writer. * @param p_document The internal document. * @param p_max_len A length restriction for the underlying document. */ public DocumentWriter (final Document p_document, final int p_max_len) { super(); this.m_document = p_document; this.m_max_len = ((p_max_len > 0) ? p_max_len : Integer.MAX_VALUE); } /** * Create a new document writer. * @param p_text_component The text component to write to. * @param p_max_len A length restriction for the underlying * document. */ public DocumentWriter (final JTextComponent p_text_component, final int p_max_len) { this(p_text_component.getDocument(), p_max_len); } /** * Create a new document writer. * @param p_document The internal document. */ public DocumentWriter (final Document p_document) { this(p_document, -1); } /** * Create a new document writer. * @param p_text_component The text component to write to. */ public DocumentWriter (final JTextComponent p_text_component) { this(p_text_component, -1); } /** * does nothing */ @Override public final void flush() { // } /** * Write a portion of an array of characters. * * @param p_cbuf Array of characters * @param p_off Offset from which to start writing characters * @param p_len Number of characters to write */ @Override public final void write(final char p_cbuf[], int p_off, int p_len) { int l_i, l_l; Document l_d; l_i = (p_len - this.m_max_len); if(l_i > 0) { p_off += l_i; p_len -= l_i; } l_d = this.m_document; l_l = l_d.getLength(); l_i = (l_l + p_len - this.m_max_len); try { if(l_i > 0) { l_l -= l_i; l_d.remove(0, l_i); } l_d.insertString(l_l, String.valueOf(p_cbuf, p_off, p_len), null); } catch(Throwable l_t) { // } } /** * Return the name of the character encoding being used by this stream. * *
If the encoding has an historical name then that name is returned; * otherwise the encoding's canonical name is returned. * *
If this instance was created with the constructor then the returned * name, being unique for the encoding, may differ from the name passed to * the constructor. This method may return null if the stream has * been closed.
* * @return The historical name of this encoding, or possibly *null
if the stream has been closed
*/
@Override
public final String get_encoding ()
{
return XML.PREFERED_ENCODING;
}
}