|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.text.AbstractDocument
javax.swing.text.PlainDocument
bb.gui.DocumentLimitedLength
public class DocumentLimitedLength
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.
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 java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final long serialVersionUID
private final int numberCharsMax
Contract: is > 0.
Constructor Detail |
---|
public DocumentLimitedLength(int numberCharsMax) throws IllegalArgumentException
numberCharsMax
- specifies the maximum number of chars that this instance will hold; must be > 0
IllegalArgumentException
- if numberCharsMax <= 0public DocumentLimitedLength(AbstractDocument.Content content, int numberCharsMax) throws IllegalArgumentException, IllegalStateException
content
- a AbstractDocument.Content
instance that contains initial text contentnumberCharsMax
- specifies the maximum number of chars that this instance will hold; must be > 0
IllegalArgumentException
- if content == null; numberCharsMax <= 0; content.length() > numberCharsMax
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
Method Detail |
---|
public int getNumberCharsMax() throws IllegalStateException
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
public String getText() throws IllegalStateException, RuntimeException
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 Throwablepublic void insertString(int offset, String s, AttributeSet attributeSet) throws IllegalStateException, BadLocationException
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.
insertString
in interface Document
insertString
in class PlainDocument
IllegalStateException
- if calling thread is not EventQueue
's dispatch thread
BadLocationException
- if offset is not a valid position within the document
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |