001/* 002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 003 * 004 * http://www.izforge.com/izpack/ 005 * http://developer.berlios.de/projects/izpack/ 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 022package com.izforge.izpack.panels; 023 024import java.awt.Toolkit; 025 026import javax.swing.JTextField; 027import javax.swing.text.AttributeSet; 028import javax.swing.text.BadLocationException; 029import javax.swing.text.Document; 030import 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/*---------------------------------------------------------------------------*/ 044public class RuleTextField extends JTextField 045{ 046 047 /** 048 * 049 */ 050 private static final long serialVersionUID = 3976731454594365493L; 051 052 /** Used to specify numeric input only */ 053 public static final int N = 1; 054 055 /** Used to specify hexadecimal input only */ 056 public static final int H = 2; 057 058 /** Used to specify alphabetic input only */ 059 public static final int A = 3; 060 061 /** Used to specify open input (no restrictions) */ 062 public static final int O = 4; 063 064 /** Used to specify alpha-numeric input only */ 065 public static final int AN = 5; 066 067 private int columns; 068 069 private int editLength; 070 071 private boolean unlimitedEdit; 072 073 private Toolkit toolkit; 074 075 private Rule rule; 076 077 public RuleTextField(int digits, int editLength, int type, boolean unlimitedEdit, 078 Toolkit toolkit) 079 { 080 super(digits + 1); 081 082 columns = digits; 083 this.toolkit = toolkit; 084 this.editLength = editLength; 085 this.unlimitedEdit = unlimitedEdit; 086 rule = new Rule(); 087 rule.setRuleType(type, editLength, unlimitedEdit); 088 setDocument(rule); 089 } 090 091 protected Document createDefaultModel() 092 { 093 rule = new Rule(); 094 return (rule); 095 } 096 097 public int getColumns() 098 { 099 return (columns); 100 } 101 102 public int getEditLength() 103 { 104 return (editLength); 105 } 106 107 public boolean unlimitedEdit() 108 { 109 return (unlimitedEdit); 110 } 111 112 public void setColumns(int columns) 113 { 114 super.setColumns(columns + 1); 115 this.columns = columns; 116 } 117 118 // -------------------------------------------------------------------------- 119 // 120 // -------------------------------------------------------------------------- 121 122 class Rule extends PlainDocument 123 { 124 125 /** 126 * 127 */ 128 private static final long serialVersionUID = 3258134643651063862L; 129 130 private int editLength; 131 132 private int type; 133 134 private boolean unlimitedEdit; 135 136 public void setRuleType(int type, int editLength, boolean unlimitedEdit) 137 { 138 this.type = type; 139 this.editLength = editLength; 140 this.unlimitedEdit = unlimitedEdit; 141 } 142 143 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException 144 { 145 // -------------------------------------------------- 146 // don't process if we get a null reference 147 // -------------------------------------------------- 148 if (str == null) { return; } 149 150 // -------------------------------------------------- 151 // Compute the total length the string would become 152 // if the insert request were be honored. If this 153 // size is within the specified limits, apply further 154 // rules, otherwise give an error signal and return. 155 // -------------------------------------------------- 156 int totalSize = getLength() + str.length(); 157 158 if ((totalSize <= editLength) || (unlimitedEdit)) 159 { 160 boolean error = false; 161 162 // test for numeric type 163 if (type == N) 164 { 165 for (int i = 0; i < str.length(); i++) 166 { 167 if (!Character.isDigit(str.charAt(i))) 168 { 169 error = true; 170 } 171 } 172 } 173 // test for hex type 174 else if (type == H) 175 { 176 for (int i = 0; i < str.length(); i++) 177 { 178 char focusChar = Character.toUpperCase(str.charAt(i)); 179 if (!Character.isDigit(focusChar) && (focusChar != 'A') 180 && (focusChar != 'B') && (focusChar != 'C') && (focusChar != 'D') 181 && (focusChar != 'E') && (focusChar != 'F')) 182 { 183 error = true; 184 } 185 } 186 } 187 // test for alpha type 188 else if (type == A) 189 { 190 for (int i = 0; i < str.length(); i++) 191 { 192 if (!Character.isLetter(str.charAt(i))) 193 { 194 error = true; 195 } 196 } 197 } 198 // test for alpha-numeric type 199 else if (type == AN) 200 { 201 for (int i = 0; i < str.length(); i++) 202 { 203 if (!Character.isLetterOrDigit(str.charAt(i))) 204 { 205 error = true; 206 } 207 } 208 } 209 // test for 'open' -> no limiting rule at all 210 else if (type == O) 211 { 212 // let it slide... 213 } 214 else 215 { 216 System.out.println("type = " + type); 217 } 218 219 // ------------------------------------------------ 220 // if we had no error when applying the rules, we 221 // are ready to insert the string, otherwise give 222 // an error signal. 223 // ------------------------------------------------ 224 if (!error) 225 { 226 super.insertString(offs, str, a); 227 } 228 else 229 { 230 toolkit.beep(); 231 } 232 } 233 else 234 { 235 toolkit.beep(); 236 } 237 } 238 } 239} 240/*---------------------------------------------------------------------------*/