bb.gui
Class DocumentLimitedLength

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.PlainDocument
          extended by bb.gui.DocumentLimitedLength
All Implemented Interfaces:
Serializable, Document
Direct Known Subclasses:
TextFieldSecure.DocumentSecure

public class DocumentLimitedLength
extends PlainDocument

Subclass of PlainDocument which limits the number of chars that it will contain.

One use of this class is to serve as the model of a JTextComponent like JTextField when the text needs to be constrained to a maximum length.

Like typical Java GUI code, this class is not multithread safe: it expects to only be called by EventQueue's dispatch thread. This threading limitation is checked in every public method.

Author:
Brent Boyer
See Also:
Java forum posting #1, Java forum posting #2, Serialized Form

Nested Class Summary
static class DocumentLimitedLength.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
 
Field Summary
private  int numberCharsMax
          Maximum number of chars that this instance will hold.
private static long serialVersionUID
           
 
Fields inherited from class javax.swing.text.PlainDocument
lineLimitAttribute, tabSizeAttribute
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
DocumentLimitedLength(AbstractDocument.Content content, int numberCharsMax)
          Constructs a new DocumentLimitedLength instance.
DocumentLimitedLength(int numberCharsMax)
          Constructs a new DocumentLimitedLength instance.
 
Method Summary
 int getNumberCharsMax()
          Returns the maximum number of chars that may be typed in this instance.
 String getText()
          Returns all the text contained in this instance.
 void insertString(int offset, String s, AttributeSet attributeSet)
          First calls super.insertString(offset, s, attributeSet).
 
Methods inherited from class javax.swing.text.PlainDocument
createDefaultRoot, getDefaultRootElement, getParagraphElement, insertUpdate, removeUpdate
 
Methods inherited from class javax.swing.text.AbstractDocument
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

numberCharsMax

private final int numberCharsMax
Maximum number of chars that this instance will hold.

Contract: is > 0.

Constructor Detail

DocumentLimitedLength

public DocumentLimitedLength(int numberCharsMax)
                      throws IllegalArgumentException
Constructs a new DocumentLimitedLength instance.

Parameters:
numberCharsMax - specifies the maximum number of chars that this instance will hold; must be > 0
Throws:
IllegalArgumentException - if numberCharsMax <= 0

DocumentLimitedLength

public DocumentLimitedLength(AbstractDocument.Content content,
                             int numberCharsMax)
                      throws IllegalArgumentException,
                             IllegalStateException
Constructs a new DocumentLimitedLength instance.

Parameters:
content - a AbstractDocument.Content instance that contains initial text content
numberCharsMax - specifies the maximum number of chars that this instance will hold; must be > 0
Throws:
IllegalArgumentException - if content == null; numberCharsMax <= 0; content.length() > numberCharsMax
IllegalStateException - if calling thread is not EventQueue's dispatch thread
Method Detail

getNumberCharsMax

public int getNumberCharsMax()
                      throws IllegalStateException
Returns the maximum number of chars that may be typed in this instance.

Throws:
IllegalStateException - if calling thread is not EventQueue's dispatch thread

getText

public String getText()
               throws IllegalStateException,
                      RuntimeException
Returns all the text contained in this instance.

Throws:
IllegalStateException - if calling thread is not EventQueue's dispatch thread
RuntimeException - (or some subclass) if any error occurs; this may merely wrap some other underlying Throwable

insertString

public void insertString(int offset,
                         String s,
                         AttributeSet attributeSet)
                  throws IllegalStateException,
                         BadLocationException
First calls super.insertString(offset, s, attributeSet). Then checks to see if the new length exceeds numberCharsMax, and if it does, sounds a beep and then truncates the final chars so that the length ends up equaling numberCharsMax.

Specified by:
insertString in interface Document
Overrides:
insertString in class PlainDocument
Throws:
IllegalStateException - if calling thread is not EventQueue's dispatch thread
BadLocationException - if offset is not a valid position within the document