001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2002 Elmar Grom
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.panels;
023:
024: import java.awt.Toolkit;
025:
026: import javax.swing.JTextField;
027: import javax.swing.text.AttributeSet;
028: import javax.swing.text.BadLocationException;
029: import javax.swing.text.Document;
030: import javax.swing.text.PlainDocument;
031:
032: /*---------------------------------------------------------------------------*/
033: /**
034: * One line synopsis. <BR>
035: * <BR>
036: * Enter detailed class description here.
037: *
038: * @see UserInputPanel
039: *
040: * @version 0.0.1 / 10/20/02
041: * @author Elmar Grom
042: */
043: /*---------------------------------------------------------------------------*/
044: public class RuleTextField extends JTextField {
045:
046: /**
047: *
048: */
049: private static final long serialVersionUID = 3976731454594365493L;
050:
051: /** Used to specify numeric input only */
052: public static final int N = 1;
053:
054: /** Used to specify hexadecimal input only */
055: public static final int H = 2;
056:
057: /** Used to specify alphabetic input only */
058: public static final int A = 3;
059:
060: /** Used to specify open input (no restrictions) */
061: public static final int O = 4;
062:
063: /** Used to specify alpha-numeric input only */
064: public static final int AN = 5;
065:
066: private int columns;
067:
068: private int editLength;
069:
070: private boolean unlimitedEdit;
071:
072: private Toolkit toolkit;
073:
074: public RuleTextField(int digits, int editLength, int type,
075: boolean unlimitedEdit, Toolkit toolkit) {
076: super (digits + 1);
077:
078: setColumns(digits);
079: this .toolkit = toolkit;
080: this .editLength = editLength;
081: this .unlimitedEdit = unlimitedEdit;
082: Rule rule = new Rule();
083: rule.setRuleType(type, editLength, unlimitedEdit);
084: setDocument(rule);
085: }
086:
087: protected Document createDefaultModel() {
088: Rule rule = new Rule();
089: return (rule);
090: }
091:
092: public int getColumns() {
093: return (columns);
094: }
095:
096: public int getEditLength() {
097: return (editLength);
098: }
099:
100: public boolean unlimitedEdit() {
101: return (unlimitedEdit);
102: }
103:
104: public void setColumns(int columns) {
105: super .setColumns(columns + 1);
106: this .columns = columns;
107: }
108:
109: // --------------------------------------------------------------------------
110: //
111: // --------------------------------------------------------------------------
112:
113: class Rule extends PlainDocument {
114:
115: /**
116: *
117: */
118: private static final long serialVersionUID = 3258134643651063862L;
119:
120: private int editLength;
121:
122: private int type;
123:
124: private boolean unlimitedEdit;
125:
126: public void setRuleType(int type, int editLength,
127: boolean unlimitedEdit) {
128: this .type = type;
129: this .editLength = editLength;
130: this .unlimitedEdit = unlimitedEdit;
131: }
132:
133: public void insertString(int offs, String str, AttributeSet a)
134: throws BadLocationException {
135: // --------------------------------------------------
136: // don't process if we get a null reference
137: // --------------------------------------------------
138: if (str == null) {
139: return;
140: }
141:
142: // --------------------------------------------------
143: // Compute the total length the string would become
144: // if the insert request were be honored. If this
145: // size is within the specified limits, apply further
146: // rules, otherwise give an error signal and return.
147: // --------------------------------------------------
148: int totalSize = getLength() + str.length();
149:
150: if ((totalSize <= editLength) || (unlimitedEdit)) {
151: boolean error = false;
152:
153: // test for numeric type
154: if (type == N) {
155: for (int i = 0; i < str.length(); i++) {
156: if (!Character.isDigit(str.charAt(i))) {
157: error = true;
158: }
159: }
160: }
161: // test for hex type
162: else if (type == H) {
163: for (int i = 0; i < str.length(); i++) {
164: char focusChar = Character.toUpperCase(str
165: .charAt(i));
166: if (!Character.isDigit(focusChar)
167: && (focusChar != 'A')
168: && (focusChar != 'B')
169: && (focusChar != 'C')
170: && (focusChar != 'D')
171: && (focusChar != 'E')
172: && (focusChar != 'F')) {
173: error = true;
174: }
175: }
176: }
177: // test for alpha type
178: else if (type == A) {
179: for (int i = 0; i < str.length(); i++) {
180: if (!Character.isLetter(str.charAt(i))) {
181: error = true;
182: }
183: }
184: }
185: // test for alpha-numeric type
186: else if (type == AN) {
187: for (int i = 0; i < str.length(); i++) {
188: if (!Character.isLetterOrDigit(str.charAt(i))) {
189: error = true;
190: }
191: }
192: }
193: // test for 'open' -> no limiting rule at all
194: else if (type == O) {
195: // let it slide...
196: } else {
197: System.out.println("type = " + type);
198: }
199:
200: // ------------------------------------------------
201: // if we had no error when applying the rules, we
202: // are ready to insert the string, otherwise give
203: // an error signal.
204: // ------------------------------------------------
205: if (!error) {
206: super .insertString(offs, str, a);
207: } else {
208: toolkit.beep();
209: }
210: } else {
211: toolkit.beep();
212: }
213: }
214: }
215: }
216: /*---------------------------------------------------------------------------*/
|