1 14 package org.compiere.grid.ed; 15 16 import javax.swing.text.*; 17 import javax.swing.event.*; 18 import java.text.*; 19 20 import org.compiere.util.*; 21 22 47 public final class MDocString extends PlainDocument implements CaretListener 48 { 49 55 public MDocString(String VFormat, int fieldLength, JTextComponent tc) 56 { 57 super(); 58 m_fieldLength = fieldLength; 59 setFormat (VFormat); 60 m_tc = tc; 61 if (tc != null) 62 m_tc.addCaretListener(this); 63 } 65 private String m_VFormat; private String m_mask; private int m_fieldLength; 68 private int m_maxLength; 69 private JTextComponent m_tc; 70 private int m_lastDot = 0; private static final char SPACE = ' '; 73 private static final char SPACE_IND = '_'; 74 75 79 public final void setFormat (String VFormat) 80 { 81 m_VFormat = VFormat; 84 if (m_VFormat == null) 85 m_VFormat = ""; 86 m_mask = m_VFormat; 87 m_maxLength = m_fieldLength; 89 if (m_VFormat.length() == 0) 90 return; 91 if (m_maxLength > m_VFormat.length()) 92 m_maxLength = m_VFormat.length(); 93 94 m_mask = m_mask.replace('c', SPACE); m_mask = m_mask.replace('C', SPACE); m_mask = m_mask.replace('a', SPACE); m_mask = m_mask.replace('A', SPACE); m_mask = m_mask.replace('l', SPACE); m_mask = m_mask.replace('L', SPACE); m_mask = m_mask.replace('o', SPACE); m_mask = m_mask.replace('O', SPACE); m_mask = m_mask.replace('0', SPACE); m_mask = m_mask.replace('9', SPACE); 106 if (m_tc == null || m_tc.getCaret() instanceof VOvrCaret) 108 return; 109 else 110 m_tc.setCaret(new VOvrCaret()); 111 } 113 120 public void insertString(int offset, String string, AttributeSet attr) 121 throws BadLocationException 122 { 123 if (offset >= m_maxLength) 125 return; 126 if (m_VFormat.length() == 0 || string.length() != 1) 128 { 129 super.insertString(offset, string, attr); 130 return; 131 } 132 133 138 String text = getText(); 139 140 if (m_VFormat.length() != text.length()) 142 { 143 char[] result = m_mask.toCharArray(); 144 for (int i = 0; i < result.length; i++) 145 { 146 if (result[i] == SPACE && text.length() > i) 147 result[i] = text.charAt(i); 148 else if (result[i] == SPACE_IND) 149 result[i] = SPACE; 150 } 151 super.remove(0, text.length()); 153 super.insertString(0, new String (result), attr); 154 m_tc.setCaretPosition(offset); 155 text = getText(); 156 } 157 158 if (offset+1 < text.length() && m_mask.charAt(offset+1) != SPACE) 160 if (offset+2 < getText().length()) 161 m_tc.setCaretPosition(offset+2); 162 163 if (m_mask.charAt(offset) != SPACE) 165 { 166 if (offset+1 == m_mask.length()) 167 return; 168 offset++; 169 m_tc.setCaretPosition(offset+1); 170 } 171 172 char c = string.charAt(0); 174 char cmd = m_VFormat.charAt(offset); 175 Log.trace(Log.l6_Database, "char=" + c + ", cmd=" + cmd); 176 switch (cmd) 177 { 178 case 'c': if (Character.isLetter(c) || Character.isDigit(c) || Character.isSpaceChar(c)) 180 ; 181 else 182 return; 183 break; 184 case 'C': if (Character.isLetter(c) || Character.isDigit(c) || Character.isSpaceChar(c)) 187 string = string.toUpperCase(); 188 else 189 return; 190 break; 191 case 'a': if (Character.isLetter(c) || Character.isDigit(c)) 194 ; 195 else 196 return; 197 break; 198 case 'A': if (Character.isLetter(c) || Character.isDigit(c)) 201 string = string.toUpperCase(); 202 else 203 return; 204 break; 205 case 'l': if (!Character.isLetter(c)) 208 return; 209 break; 210 case 'L': if (!Character.isLetter(c)) 213 return; 214 string = string.toUpperCase(); 215 break; 216 case 'o': if (Character.isLetter(c) || Character.isSpaceChar(c)) 219 ; 220 else 221 return; 222 break; 223 case 'O': if (Character.isLetter(c) || Character.isSpaceChar(c)) 226 string = string.toUpperCase(); 227 else 228 return; 229 break; 230 case '9': if (Character.isDigit(c) || Character.isSpaceChar(c)) 233 ; 234 else 235 return; 236 break; 237 case '0': if (!Character.isDigit(c)) 240 return; 241 break; 242 case SPACE: break; 245 default: return; 248 } 250 super.remove(offset, 1); super.insertString(offset, string, attr); 252 } 254 260 public void remove (int offset, int length) 261 throws BadLocationException 262 { 263 if (m_VFormat.length() == 0 || length != 1) 265 { 266 super.remove(offset, length); 267 return; 268 } 269 270 272 if (offset == 0) 274 { 275 if (length == m_mask.length()) 277 super.remove(offset, length); 278 return; 279 } 280 281 if (offset-1 >= 0 && m_mask.charAt(offset-1) != SPACE) 283 { 284 if (offset-2 >= 0) 285 m_tc.setCaretPosition(offset-2); 286 else 287 return; 288 } 289 else 290 m_tc.setCaretPosition(offset-1); 291 } 293 297 private String getText() 298 { 299 String str = ""; 300 try 301 { 302 str = getContent().getString(0, getContent().length()-1); } 304 catch (Exception e) 305 { 306 str = ""; 307 } 308 return str; 309 } 311 312 313 317 public void caretUpdate(CaretEvent e) 318 { 319 if (m_VFormat.length() == 0 321 || m_VFormat.equals(m_mask)) 323 return; 324 if (e.getDot() != e.getMark()) 326 { 327 m_lastDot = e.getDot(); 328 return; 329 } 330 333 if (e.getDot()+1 > m_mask.length() || m_mask.charAt(e.getDot()) == SPACE) 335 { 336 m_lastDot = e.getDot(); 337 return; 338 } 339 340 int newDot = -1; 342 if (m_lastDot > e.getDot()) newDot = e.getDot() - 1; 344 else newDot = e.getDot() + 1; 346 if (e.getDot() == 0) newDot = 1; 349 else if (e.getDot() == m_mask.length()-1) newDot = e.getDot() - 1; 351 354 m_lastDot = e.getDot(); 355 if (newDot >= 0 && newDot < getText().length()) 356 m_tc.setCaretPosition(newDot); 357 } 359 } | Popular Tags |