document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.text.ParseException;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import java.util.regex.PatternSyntaxException;
  5. import javax.swing.text.DefaultFormatter;
  6.  
  7. /**
  8. *
  9. * @author homisinho
  10. */
  11. public class RegexFormatter extends DefaultFormatter {
  12. private Pattern pattern;
  13. private Matcher matcher;
  14.  
  15. public RegexFormatter() {
  16. super();
  17. }
  18.  
  19. public RegexFormatter(String pattern) throws PatternSyntaxException {
  20. this();
  21. setPattern(Pattern.compile(pattern));
  22. }
  23.  
  24. public RegexFormatter(Pattern pattern) {
  25. this();
  26. setPattern(pattern);
  27. }
  28.  
  29. public void setPattern(Pattern pattern) {
  30. this.pattern = pattern;
  31. }
  32.  
  33. public Pattern getPattern() {
  34. return pattern;
  35. }
  36.  
  37. protected void setMatcher(Matcher matcher) {
  38. this.matcher = matcher;
  39. }
  40.  
  41. protected Matcher getMatcher() {
  42. return matcher;
  43. }
  44.  
  45. @Override
  46. public Object stringToValue(String text) throws ParseException {
  47. Pattern pattern = getPattern();
  48.  
  49. if (pattern != null) {
  50. Matcher matcher = pattern.matcher(text);
  51.  
  52. if (matcher.matches()) {
  53. setMatcher(matcher);
  54. return super.stringToValue(text);
  55. }
  56. throw new ParseException("Pattern did not match", 0);
  57. }
  58. return text;
  59. }
  60.  
  61. }
');