org.opensha.commons.param.editor.document
Class IntegerPlainDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.PlainDocument
          extended by org.opensha.commons.param.editor.document.IntegerPlainDocument
All Implemented Interfaces:
Serializable, Document

public class IntegerPlainDocument
extends PlainDocument

Title: IntegerPlainDocument

Description: Model ( or data ) associated with an Integer Text Field. The insertString() function is called whenever data is being entered into the text field. This is where the text field is checked to make sure only integer valid charachters are being added.

This is an extention of the Model View Controller (MVC) design pattern that all Java Swing elements are built upon. For example, the Java class JTextField contains a PlainDocument model that actually contains the text of the JTextField. This class simply replaces the JTextField PlainDocument with this document. Then as a user types in text into the textfield, this class instance is consulted to see if they are valid characters the user is typing.

You don't have to know the details on how this class works in order to use it. To make text field that uses this document model simply extend JTextField and overide the method createDefaultModel() by creating an instance of you subclass of Plain Document. protected Document createDefaultModel() { return new IntegerPlainDocument(); }

Note: SWR: This class was implemented with java JDK 1.3. In the new java JDK 1.4 there is a much simpler way to do this. Now you can instantiate a Formatter ( Decimal, Date, etc. ) and simply pass the formatter to a Standard JTextField. No subclasses to make.

Version:
1.0
Author:
Steven W. Rock
See Also:
NumericTextField, NumericPlainDocument, IntegerTextField, Serialized Form

Nested Class Summary
static interface IntegerPlainDocument.InsertErrorListener
          Local interface definition that listeners must implement to be notified when isert fails occur due to invalid chars, etc.
 
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
protected static String C
          Class name for debugging.
protected static boolean D
          If true print out debug statements.
protected  IntegerPlainDocument.InsertErrorListener errorListener
          Listener to be notified of insert errors, typically the text field
protected  int max
          Largest allowed integer value by JVM.
protected  int min
          Smallest allowed integer value by JVM.
protected  ParsePosition parsePos
           
 
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
IntegerPlainDocument()
           
 
Method Summary
 void addInsertErrorListener(IntegerPlainDocument.InsertErrorListener l)
          Adds a listener that is notified when insertString() fails because text is not an integer
 Integer getIntegerValue()
          Helper function that converts the String model data into an Integer, what the model represents
 void insertString(int offset, String str, AttributeSet a)
          Method called to add data to the text field.
 void removeInsertErrorListener(IntegerPlainDocument.InsertErrorListener l)
          Removes a listener that was notified when insertString() fails because text is not an integer
 
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

C

protected static final String C
Class name for debugging.

See Also:
Constant Field Values

D

protected static final boolean D
If true print out debug statements.

See Also:
Constant Field Values

min

protected int min
Smallest allowed integer value by JVM. These can be changed to be more restrictive.


max

protected int max
Largest allowed integer value by JVM. These can be changed to be more restrictive.


parsePos

protected transient ParsePosition parsePos

errorListener

protected IntegerPlainDocument.InsertErrorListener errorListener
Listener to be notified of insert errors, typically the text field

Constructor Detail

IntegerPlainDocument

public IntegerPlainDocument()
Method Detail

insertString

public void insertString(int offset,
                         String str,
                         AttributeSet a)
                  throws BadLocationException
Method called to add data to the text field. Typically used when users type in text, but may be called by back end process if needed. Throws errors if the string is not valid integer characters.

Specified by:
insertString in interface Document
Overrides:
insertString in class PlainDocument
Throws:
BadLocationException

addInsertErrorListener

public void addInsertErrorListener(IntegerPlainDocument.InsertErrorListener l)
Adds a listener that is notified when insertString() fails because text is not an integer


removeInsertErrorListener

public void removeInsertErrorListener(IntegerPlainDocument.InsertErrorListener l)
Removes a listener that was notified when insertString() fails because text is not an integer


getIntegerValue

public Integer getIntegerValue()
                        throws ParseException
Helper function that converts the String model data into an Integer, what the model represents

Throws:
ParseException