001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.awt.Toolkit;
021:
022: import javax.swing.JTextField;
023: import javax.swing.text.AttributeSet;
024: import javax.swing.text.BadLocationException;
025: import javax.swing.text.PlainDocument;
026:
027: /**
028: * Document for a restricted set of characters. You can set the following
029: * properties:
030: * <ol>
031: * <li>a set of explicitely allowed characters ({@link #setAllowString})
032: * <li>a set of characters explicitely not allowed ({@link #setDenyString})
033: * <li>the maximum length of characters ({@link #setMaxLength})
034: * </ol>
035: * <br>
036: * These can be easily extended by overloading {@link #inputOK(String)}.<br>
037: * <br>
038: *
039: * @see RestrictedTextField
040: */
041: public class RestrictedDocument extends PlainDocument {
042:
043: /**
044: * Constant for all english characters. Value:
045: * <code>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<code>
046: */
047: public static final String ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
048:
049: /** Constant for german umlaut. Value: <code>öäüßÖÄÜ</code> */
050: public static final String UMLAUT = "öäüßÖÄÜ";
051:
052: /** Constant for all digits. Value: <code>0123456789</code> */
053: public static final String NUM = "0123456789";
054:
055: /** Constant for float special characters. Value: <code>-+.,</code> */
056: public static final String FLOAT = "-+.,";
057:
058: /** Constant for allowed characters in usernames and passwords */
059: public static final String PRINCIPALS = ALPHA + NUM + "-+.,_";
060:
061: public static final String XMLATTRIBUTEVALUE = ALPHA + NUM
062: + "-+.,_:;'/?[]{}\\|=()*^%$#@!~&";
063:
064: /** Explicitely allowed characters. <code>null</code> means, all allowed. */
065: private String allowString = null;
066:
067: /**
068: * Explicitely forbidden characters. <code>null</code> means, none
069: * forbidden.
070: */
071: private String denyString = null;
072:
073: /** How many characters are allowed. <code>-1</code> means unlimited. */
074: private int maxNumberOfCharacters = -1;
075:
076: /**
077: * Sets the set of explicitely allowed characters in text field.
078: */
079: public void setAllowString(String allowString) {
080: this .allowString = allowString;
081: }
082:
083: /**
084: * Gets the set of explicitely allowed characters in text field.
085: */
086: public String getAllowString() {
087: return allowString;
088: }
089:
090: /**
091: * Sets the set of characters explicitely not allowed in text field.
092: */
093: public void setDenyString(String denyString) {
094: this .denyString = denyString;
095: }
096:
097: /**
098: * Gets the set of characters explicitely not allowed in text field.
099: */
100: public String getDenyString() {
101: return denyString;
102: }
103:
104: /**
105: * Sets the the maximum length of characters allowed in text field.
106: *
107: * @param maxLength
108: * the maximum length or <code>-1</code> if unlimited
109: */
110: public void setMaxLength(int max) {
111: maxNumberOfCharacters = max;
112: }
113:
114: /**
115: * Gets the the maximum length of characters allowed in text field.
116: *
117: * @return the maximum length or <code>-1</code> if unlimited
118: */
119: public int getMaxLength() {
120: return maxNumberOfCharacters;
121: }
122:
123: /**
124: * Overloads {@link PlainDocument#insertString}, used by {@link JTextField}
125: * to check whole string.
126: */
127: public void insertString(int offset, String string,
128: AttributeSet attributes) throws BadLocationException {
129: if (string == null) {
130: return;
131: } else {
132: String newValue;
133: int length = getLength();
134: if (length == 0) {
135: newValue = string;
136: } else {
137: String currentContent = getText(0, length);
138: StringBuffer currentBuffer = new StringBuffer(
139: currentContent);
140: currentBuffer.insert(offset, string);
141: newValue = currentBuffer.toString();
142: }
143: if (inputOK(newValue)) {
144: super .insertString(offset, string, attributes);
145: } else {
146: Toolkit.getDefaultToolkit().beep();
147: }
148: }
149: }
150:
151: /**
152: * Checks if the given string fits all constraints. To extend it, overload
153: * it in subclass, e.g.<br>
154: * <br>
155: * <code>
156: * protected boolean inputOK(String proposedValue) {<br>
157: * return (myNewCheck(proposedValue) && super.inputOK(proposedValue));<br>
158: * </code>
159: */
160: protected boolean inputOK(String proposedValue) {
161: return (denyOK(proposedValue) && allowOK(proposedValue) && maxLengthOk(proposedValue));
162: }
163:
164: private boolean allowOK(String proposedValue) {
165: // null means all allowed
166: if (allowString == null) {
167: return true;
168: }
169: for (int i = 0; i < proposedValue.length(); i++) {
170: if (allowString.indexOf(proposedValue.charAt(i)) == -1) {
171: return false;
172: }
173: }
174: return true;
175: }
176:
177: private boolean denyOK(String proposedValue) {
178: // null means none forbidden
179: if (denyString == null) {
180: return true;
181: }
182: for (int i = 0; i < proposedValue.length(); i++) {
183: if (denyString.indexOf(proposedValue.charAt(i)) != -1) {
184: return false;
185: }
186: }
187: // if no denied string is found, this check succeeds
188: return true;
189: }
190:
191: private boolean maxLengthOk(String proposedValue) {
192: return ((maxNumberOfCharacters == -1) || (proposedValue
193: .length() <= maxNumberOfCharacters));
194: }
195: }
|