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

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

public class NumericPlainDocument
extends PlainDocument

Title: NumericPlainDocument

Description: Model ( or data) associated with an Numeric 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 numeric 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 NumericPlainDocument(); }

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 create 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, IntegerPlainDocument, IntegerTextField, Serialized Form

Nested Class Summary
static interface NumericPlainDocument.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  char decimalSeparator
          valid char for decimal point
protected static DecimalFormat defaultFormat
          Format of this Decimal.
protected  NumericPlainDocument.InsertErrorListener errorListener
          Listener to be notified of insert errors, typically the text field
protected  DecimalFormat format
          Format of this Decimal.
protected  char groupingSeparator
          valid char for thousands seperator
protected  String negativePrefix
           
protected  int negativePrefixLen
           
protected  String negativeSuffix
           
protected  int negativeSuffixLen
           
protected  ParsePosition parsePos
           
protected  String positivePrefix
           
protected  int positivePrefixLen
           
protected  String positiveSuffix
           
protected  int positiveSuffixLen
           
 
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
NumericPlainDocument()
          Default constructor, sets a null format
NumericPlainDocument(AbstractDocument.Content content, DecimalFormat format)
          Constructor that lets you set the content and the decimal format.
NumericPlainDocument(DecimalFormat format)
          Constructor that lets you set the decimal format.
 
Method Summary
 void addInsertErrorListener(NumericPlainDocument.InsertErrorListener l)
          Adds a listener that is notified when insertString() fails because text is not a decimal number
 Double getDoubleValue()
          Helper function that converts the String representation model data into an Double, i.e.
 DecimalFormat getFormat()
          Returns the Deciaml format.
 Long getLongValue()
          Helper function that converts the String representation model data into an Long.
 Number getNumberValue()
          Helper function that converts the String representation model data into a Number, the superclass of a Double.
 void insertString(int offset, String str, AttributeSet a)
          Method called to add data to the text field.
 void removeInsertErrorListener(NumericPlainDocument.InsertErrorListener l)
          Removes a listener that was notified when insertString() fails because text is not a decimal number
 void setFormat(DecimalFormat fmt)
          Sets the Deciaml format.
 
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

errorListener

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


defaultFormat

protected static DecimalFormat defaultFormat
Format of this Decimal. Determines what are valid non-digit characters such as decimal point.


format

protected DecimalFormat format
Format of this Decimal. Determines what are valid non-digit characters such as decimal point.


decimalSeparator

protected char decimalSeparator
valid char for decimal point


groupingSeparator

protected char groupingSeparator
valid char for thousands seperator


positivePrefix

protected String positivePrefix

positivePrefixLen

protected int positivePrefixLen

positiveSuffix

protected String positiveSuffix

positiveSuffixLen

protected int positiveSuffixLen

negativePrefix

protected String negativePrefix

negativePrefixLen

protected int negativePrefixLen

negativeSuffix

protected String negativeSuffix

negativeSuffixLen

protected int negativeSuffixLen

parsePos

protected transient ParsePosition parsePos
Constructor Detail

NumericPlainDocument

public NumericPlainDocument()
Default constructor, sets a null format


NumericPlainDocument

public NumericPlainDocument(DecimalFormat format)
Constructor that lets you set the decimal format.


NumericPlainDocument

public NumericPlainDocument(AbstractDocument.Content content,
                            DecimalFormat format)
Constructor that lets you set the content and the decimal format.

Method Detail

getFormat

public DecimalFormat getFormat()
Returns the Deciaml format. Decimal strings can have different format, which in effect determines what are the valid characters for thousands seperator char, decimal char, etc.


setFormat

public void setFormat(DecimalFormat fmt)
Sets the Deciaml format. Decimal strings can have different format, which in effect determines what are the valid characters for thousands seperator char, decimal char, etc.


getNumberValue

public Number getNumberValue()
                      throws ParseException
Helper function that converts the String representation model data into a Number, the superclass of a Double.

Throws:
ParseException

getLongValue

public Long getLongValue()
                  throws ParseException
Helper function that converts the String representation model data into an Long.

Throws:
ParseException

getDoubleValue

public Double getDoubleValue()
                      throws ParseException
Helper function that converts the String representation model data into an Double, i.e. what the model represents

Throws:
ParseException

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 decimal characters.

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

addInsertErrorListener

public void addInsertErrorListener(NumericPlainDocument.InsertErrorListener l)
Adds a listener that is notified when insertString() fails because text is not a decimal number


removeInsertErrorListener

public void removeInsertErrorListener(NumericPlainDocument.InsertErrorListener l)
Removes a listener that was notified when insertString() fails because text is not a decimal number