1 21 22 package com.izforge.izpack.panels; 23 24 import java.awt.Toolkit ; 25 26 import javax.swing.JTextField ; 27 import javax.swing.text.AttributeSet ; 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.Document ; 30 import javax.swing.text.PlainDocument ; 31 32 33 43 44 public class RuleTextField extends JTextField 45 { 46 47 50 private static final long serialVersionUID = 3976731454594365493L; 51 52 53 public static final int N = 1; 54 55 56 public static final int H = 2; 57 58 59 public static final int A = 3; 60 61 62 public static final int O = 4; 63 64 65 public static final int AN = 5; 66 67 private int columns; 68 69 private int editLength; 70 71 private boolean unlimitedEdit; 72 73 private Toolkit toolkit; 74 75 public RuleTextField(int digits, int editLength, int type, boolean unlimitedEdit, 76 Toolkit toolkit) 77 { 78 super(digits + 1); 79 80 setColumns(digits); 81 this.toolkit = toolkit; 82 this.editLength = editLength; 83 this.unlimitedEdit = unlimitedEdit; 84 Rule rule = new Rule(); 85 rule.setRuleType(type, editLength, unlimitedEdit); 86 setDocument(rule); 87 } 88 89 protected Document createDefaultModel() 90 { 91 Rule rule = new Rule(); 92 return (rule); 93 } 94 95 public int getColumns() 96 { 97 return (columns); 98 } 99 100 public int getEditLength() 101 { 102 return (editLength); 103 } 104 105 public boolean unlimitedEdit() 106 { 107 return (unlimitedEdit); 108 } 109 110 public void setColumns(int columns) 111 { 112 super.setColumns(columns + 1); 113 this.columns = columns; 114 } 115 116 120 class Rule extends PlainDocument 121 { 122 123 126 private static final long serialVersionUID = 3258134643651063862L; 127 128 private int editLength; 129 130 private int type; 131 132 private boolean unlimitedEdit; 133 134 public void setRuleType(int type, int editLength, boolean unlimitedEdit) 135 { 136 this.type = type; 137 this.editLength = editLength; 138 this.unlimitedEdit = unlimitedEdit; 139 } 140 141 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException 142 { 143 if (str == null) { return; } 147 148 int totalSize = getLength() + str.length(); 155 156 if ((totalSize <= editLength) || (unlimitedEdit)) 157 { 158 boolean error = false; 159 160 if (type == N) 162 { 163 for (int i = 0; i < str.length(); i++) 164 { 165 if (!Character.isDigit(str.charAt(i))) 166 { 167 error = true; 168 } 169 } 170 } 171 else if (type == H) 173 { 174 for (int i = 0; i < str.length(); i++) 175 { 176 char focusChar = Character.toUpperCase(str.charAt(i)); 177 if (!Character.isDigit(focusChar) && (focusChar != 'A') 178 && (focusChar != 'B') && (focusChar != 'C') && (focusChar != 'D') 179 && (focusChar != 'E') && (focusChar != 'F')) 180 { 181 error = true; 182 } 183 } 184 } 185 else if (type == A) 187 { 188 for (int i = 0; i < str.length(); i++) 189 { 190 if (!Character.isLetter(str.charAt(i))) 191 { 192 error = true; 193 } 194 } 195 } 196 else if (type == AN) 198 { 199 for (int i = 0; i < str.length(); i++) 200 { 201 if (!Character.isLetterOrDigit(str.charAt(i))) 202 { 203 error = true; 204 } 205 } 206 } 207 else if (type == O) 209 { 210 } 212 else 213 { 214 System.out.println("type = " + type); 215 } 216 217 if (!error) 223 { 224 super.insertString(offs, str, a); 225 } 226 else 227 { 228 toolkit.beep(); 229 } 230 } 231 else 232 { 233 toolkit.beep(); 234 } 235 } 236 } 237 } 238 239 | Popular Tags |