org.lyra.Widgets
Class RegexConstrainedDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.PlainDocument
          extended by org.lyra.Widgets.RegexConstrainedDocument
All Implemented Interfaces:
java.io.Serializable, javax.swing.text.Document

public class RegexConstrainedDocument
extends javax.swing.text.PlainDocument

A method for preventing unwanted information from making its way into past the initial user interface. This will permit ports to remain numeric, domains to match a specific structure and so forth.

An example for the use of this document constraint is as follows: RegexConstrainedDocument doc = new RegexConstrainedDocument("^[\\w-. ]{0,45}$"); JTextField fieldName = new JTextField(doc, "", 45); As shown above a new constraint document will be created enforcing with length of the string allowed as well as the characters supported. In this case [A-Z],[a-z],[0-9],[space],[.],[-] are the characters that will be allowed for this filter.

Version:
1.0.5, %Revision, 174%, %LastChangedDate, 27/10/12 3:33PM%
Author:
Martin Foster
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
 
Field Summary
private  java.util.regex.Matcher myMatch
           
private  java.util.regex.Pattern myPattern
           
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
RegexConstrainedDocument()
          Most basic constructor for the class.
RegexConstrainedDocument(javax.swing.text.AbstractDocument.Content c)
          Constructor which can provide a document to create the initial data set.
RegexConstrainedDocument(javax.swing.text.AbstractDocument.Content c, java.lang.String p)
          Constructor which can provide a document to create the initial data set for the document.
RegexConstrainedDocument(java.lang.String p)
          Constructor to create an empty document with an associated pattern.
 
Method Summary
 java.util.regex.Pattern getPattern()
          Returns the current pattern.
 void insertString(int offs, java.lang.String s, javax.swing.text.AttributeSet a)
           
 void setPatternByString(java.lang.String p)
          Method used to inject a regular expression into the Constrained Document which will also confirm that the document currently matches that data set.
 
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

myPattern

private java.util.regex.Pattern myPattern

myMatch

private java.util.regex.Matcher myMatch
Constructor Detail

RegexConstrainedDocument

public RegexConstrainedDocument()
Most basic constructor for the class. Using this element will result in a working document but will not permit changes due to the fact that no Regular Expression has been attached.


RegexConstrainedDocument

public RegexConstrainedDocument(javax.swing.text.AbstractDocument.Content c)
Constructor which can provide a document to create the initial data set. Like the most basic constructor Regular Expressions will not be enabled.

Parameters:
c - Content of an existing document to make use of here.

RegexConstrainedDocument

public RegexConstrainedDocument(javax.swing.text.AbstractDocument.Content c,
                                java.lang.String p)
Constructor which can provide a document to create the initial data set for the document. Will also initialise the Regular Expression meaning the the document may be blanked if the verification fails.

Parameters:
c - Content of an existing document to make use of here.
p - String containing regular expression pattern.

RegexConstrainedDocument

public RegexConstrainedDocument(java.lang.String p)
Constructor to create an empty document with an associated pattern. If the pattern is too strict you may not be able to proceed since it will never match.

Parameters:
p - String containing regular expression pattern.
Method Detail

setPatternByString

public void setPatternByString(java.lang.String p)
Method used to inject a regular expression into the Constrained Document which will also confirm that the document currently matches that data set. Otherwise it will blank the document.

Parameters:
p - String containing regular expression pattern.

getPattern

public java.util.regex.Pattern getPattern()
Returns the current pattern. This could realistically be blanked since it is possible to create a document without specifying a document.

Returns:
String containing pattern

insertString

public void insertString(int offs,
                         java.lang.String s,
                         javax.swing.text.AttributeSet a)
                  throws javax.swing.text.BadLocationException
Specified by:
insertString in interface javax.swing.text.Document
Overrides:
insertString in class javax.swing.text.PlainDocument
Throws:
javax.swing.text.BadLocationException