1 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 56 public class LengthLimitingDocument extends PlainDocument { 57 58 59 private int maxlen; 60 61 64 public LengthLimitingDocument() { 65 this(-1); 66 } 67 68 75 public LengthLimitingDocument(final int maxlen) { 76 super(); 77 this.maxlen = maxlen; 78 } 79 80 86 public void setMaxLength(final int maxlen) { 87 this.maxlen = maxlen; 88 } 89 90 94 public int getMaxLength() { 95 return this.maxlen; 96 } 97 98 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 } 124 | Popular Tags |