Source for org.jfree.ui.LengthLimitingDocument

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ---------------------------
  28:  * LengthLimitingDocument.java
  29:  * ---------------------------
  30:  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: LengthLimitingDocument.java,v 1.3 2005/10/18 13:18:34 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 22-Jan-2003 : Initial version
  40:  * 05-Feb-2003 : Documentation
  41:  * 
  42:  */
  43: 
  44: package org.jfree.ui;
  45: 
  46: import javax.swing.text.AttributeSet;
  47: import javax.swing.text.BadLocationException;
  48: import javax.swing.text.PlainDocument;
  49: 
  50: /**
  51:  * This Document restricts the size of the contained plain text to the given number of
  52:  * characters.
  53:  *
  54:  * @author Thomas Morgner
  55:  */
  56: public class LengthLimitingDocument extends PlainDocument {
  57: 
  58:     /** The maximum length. */
  59:     private int maxlen;
  60: 
  61:     /**
  62:      * Creates a new LengthLimitingDocument, with no limitation.
  63:      */
  64:     public LengthLimitingDocument() {
  65:         this(-1);
  66:     }
  67: 
  68:     /**
  69:      * Creates a new LengthLimitingDocument with the given limitation. No more than
  70:      * maxlen characters can be added to the document. If maxlen is negative, then
  71:      * no length check is performed.
  72:      *
  73:      * @param maxlen the maximum number of elements in this document
  74:      */
  75:     public LengthLimitingDocument(final int maxlen) {
  76:         super();
  77:         this.maxlen = maxlen;
  78:     }
  79: 
  80:     /**
  81:      * Sets the maximum number of characters for this document. Existing characters
  82:      * are not removed.
  83:      *
  84:      * @param maxlen the maximum number of characters in this document.
  85:      */
  86:     public void setMaxLength(final int maxlen) {
  87:         this.maxlen = maxlen;
  88:     }
  89: 
  90:     /**
  91:      * Returns the defined maximum number characters for this document.
  92:      * @return the maximum number of characters
  93:      */
  94:     public int getMaxLength() {
  95:         return this.maxlen;
  96:     }
  97: 
  98:     /**
  99:      * Inserts the string into the document. If the length of the document would
 100:      * violate the maximum characters restriction, then the string is cut down so
 101:      * that
 102:      * @param offs the offset, where the string should be inserted into the document
 103:      * @param str the string that should be inserted
 104:      * @param a the attribute set assigned for the document
 105:      * @throws javax.swing.text.BadLocationException if the offset is not correct
 106:      */
 107:     public void insertString(final int offs, final String str, final AttributeSet a)
 108:         throws BadLocationException {
 109:         if (str == null) {
 110:             return;
 111:         }
 112: 
 113:         if (this.maxlen < 0) {
 114:             super.insertString(offs, str, a);
 115:         }
 116: 
 117:         final char[] numeric = str.toCharArray();
 118:         final StringBuffer b = new StringBuffer();
 119:         b.append(numeric, 0, Math.min(this.maxlen, numeric.length));
 120:         super.insertString(offs, b.toString(), a);
 121:     }
 122:     
 123: }