FileDocCategorySizeDatePackage
RegexConstrainedDocument.javaAPI DocExample1957Mon Jan 09 11:02:00 GMT 2006None

RegexConstrainedDocument

public class RegexConstrainedDocument extends PlainDocument

Fields Summary
Pattern
pattern
Matcher
matcher
Constructors Summary
public RegexConstrainedDocument()

 super(); 
public RegexConstrainedDocument(AbstractDocument$Content c)

 super(c); 
public RegexConstrainedDocument(AbstractDocument$Content c, String p)

        super (c);
        setPatternByString (p);
    
public RegexConstrainedDocument(String p)

        super();
        setPatternByString (p);
    
Methods Summary
public java.util.regex.PatterngetPattern()

 return pattern; 
public voidinsertString(int offs, java.lang.String s, javax.swing.text.AttributeSet a)

        // consider whether this insert will match
        String proposedInsert =
            getText (0, offs) +
            s +
            getText (offs, getLength() - offs);
        System.out.println ("proposing to change to: " +
                            proposedInsert);
        if (matcher != null) {
            matcher.reset (proposedInsert);
            System.out.println ("matcher reset");
            if (! matcher.matches()) {
                System.out.println ("insert doesn't match");
                return;
            }
        }
        super.insertString (offs, s, a);
    
public voidsetPatternByString(java.lang.String p)

        Pattern pattern = Pattern.compile (p);
        // check the document against the new pattern
        // and removes the content if it no longer matches
        try {
            matcher = pattern.matcher (getText(0, getLength()));
            System.out.println ("matcher reset to " + 
                                getText (0, getLength()));
            if (! matcher.matches()) {
                System.out.println ("does not match");
                remove (0, getLength());
            }
        } catch (BadLocationException ble) {
            ble.printStackTrace(); // impossible?
        }