01: /*
02: JSmooth: a VM wrapper toolkit for Windows
03: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
04:
05: This program is free software; you can redistribute it and/or modify
06: it under the terms of the GNU General Public License as published by
07: the Free Software Foundation; either version 2 of the License, or
08: (at your option) any later version.
09:
10: This program is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public License
16: along with this program; if not, write to the Free Software
17: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18:
19: */
20:
21: package net.charabia.jsmoothgen.application.gui.util;
22:
23: import java.util.*;
24: import javax.swing.text.*;
25: import java.text.*;
26: import java.util.regex.*;
27:
28: public class RegExDocument extends PlainDocument {
29: private java.util.regex.Pattern m_pattern;
30:
31: public RegExDocument(String pattern) {
32: m_pattern = java.util.regex.Pattern.compile(pattern);
33: }
34:
35: public void insertString(int offset, String string,
36: AttributeSet attributes) throws BadLocationException {
37: if (string == null)
38: return;
39:
40: String result;
41: int length = getLength();
42: if (length == 0) {
43: result = string;
44: } else {
45: String currentContent = getText(0, length);
46: StringBuffer currentBuffer = new StringBuffer(
47: currentContent);
48: currentBuffer.insert(offset, string);
49: result = currentBuffer.toString();
50: }
51:
52: java.util.regex.Matcher m = m_pattern.matcher(result);
53:
54: if (m.matches()) {
55: super.insertString(offset, string, attributes);
56: }
57:
58: java.awt.Toolkit.getDefaultToolkit().beep();
59: }
60:
61: }
|