1 package com.instantbank.common.uiutils;
2
3 import java.awt.Toolkit;
4 import javax.swing.text.PlainDocument;
5 import javax.swing.text.AttributeSet;
6 import javax.swing.text.BadLocationException;
7
8
15 class IntegerDocument extends PlainDocument {
16
17
20 public static final int POSITIVE = 1;
21
22
25 public static final int NEGATIVE = 2;
26
27
30 public static final int ALL = 3;
31
32
35 private int range;
36
37
38
43 public IntegerDocument(int range) {
44 this.range = range;
45 }
46
47
48
56 public void insertString(int offset, String s, AttributeSet attributeSet)
57 throws BadLocationException {
58 try {
59 int val = Integer.parseInt(s);
60 if(range == POSITIVE && val < 0 || range == NEGATIVE && val > 0) {
61 throw new Exception();
62 }
63 }
64 catch(Exception ex) {
65 Toolkit.getDefaultToolkit().beep();
66 return;
67 }
68 super.insertString(offset, s, attributeSet);
69 }
70 }
71