All pastes #1927278 Raw Edit

RegexFormatter.java

public java v1 · immutable
#1927278 ·published 2010-08-28 01:31 UTC
rendered paste body
// RegexFormatter.javapackage javaapplication;import java.text.*;import java.util.regex.*;import javax.swing.text.*;public class RegexFormatter extends DefaultFormatter {  private Pattern pattern;  private Matcher matcher;  public RegexFormatter() {    super();  }  public RegexFormatter(String regex) throws PatternSyntaxException {    this();    setPattern(Pattern.compile(regex));  }  public RegexFormatter(Pattern pattern) {    this();    setPattern(pattern);  }  public void setPattern(Pattern pattern) {    this.pattern = pattern;  }  public Pattern getPattern() {    return pattern;  }  protected void setMatcher(Matcher matcher) {    this.matcher = matcher;  }  protected Matcher getMatcher() {    return matcher;  }  @Override  public Object stringToValue(String text) throws ParseException {    Pattern p = getPattern();    if (p != null) {      Matcher m = p.matcher(text);      if (m.matches()) {        setMatcher(m);        return super.stringToValue(text);      }      throw new ParseException("Pattern did not match", 0);    }    return text;  }}