1 21 22 package com.izforge.izpack.panels; 23 24 import java.awt.Toolkit ; 25 import java.awt.event.FocusEvent ; 26 import java.awt.event.FocusListener ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.KeyListener ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 33 import javax.swing.JComponent ; 34 import javax.swing.JLabel ; 35 import javax.swing.JTextField ; 36 import javax.swing.event.CaretEvent ; 37 import javax.swing.event.CaretListener ; 38 39 import org.apache.regexp.RE; 40 41 import com.izforge.izpack.installer.InstallData; 42 import com.izforge.izpack.util.Debug; 43 import com.izforge.izpack.util.VariableSubstitutor; 44 45 46 80 81 public class RuleInputField extends JComponent implements KeyListener , FocusListener , 82 CaretListener , ProcessingClient 83 { 84 85 88 private static final long serialVersionUID = 3832616275124958257L; 89 90 94 public static final int PLAIN_STRING = 1; 95 96 102 public static final int DISPLAY_FORMAT = 2; 103 104 109 public static final int SPECIAL_SEPARATOR = 3; 110 111 116 public static final int ENCRYPTED = 4; 117 118 119 private static int DEFAULT = DISPLAY_FORMAT; 120 121 private Vector items = new Vector (); 122 123 127 private Vector inputFields = new Vector (); 128 129 private boolean hasParams = false; 130 131 private Map validatorParams; 132 133 private RuleTextField activeField; 134 135 private boolean backstep = false; 136 137 private Toolkit toolkit; 138 139 private String separator; 140 141 private int resultFormat = DEFAULT; 142 143 private InstallData idata = null; 144 145 148 private Validator validationService; 149 150 153 private Processor encryptionService; 154 155 158 public boolean hasParams() 159 { 160 return hasParams; 161 } 162 163 164 192 193 public RuleInputField(String format, String preset, String separator, String validator, 194 Map validatorParams, String processor, int resultFormat, Toolkit toolkit, 195 InstallData idata) 196 { 197 this(format, preset, separator, validator, processor, resultFormat, toolkit, idata); 198 this.validatorParams = validatorParams; 199 this.hasParams = true; 200 } 201 202 203 229 230 public RuleInputField(String format, String preset, String separator, String validator, 231 String processor, int resultFormat, Toolkit toolkit, InstallData idata) 232 { 233 this.toolkit = toolkit; 234 this.separator = separator; 235 this.resultFormat = resultFormat; 236 this.idata = idata; 237 238 com.izforge.izpack.gui.FlowLayout layout = new com.izforge.izpack.gui.FlowLayout(); 239 layout.setAlignment(com.izforge.izpack.gui.FlowLayout.LEFT); 240 setLayout(layout); 241 242 try 246 { 247 if (validator != null) 248 { 249 validationService = (Validator) Class.forName(validator).newInstance(); 250 } 251 } 252 catch (Throwable exception) 253 { 254 validationService = null; 255 Debug.trace(exception); 256 } 257 258 try 262 { 263 if (processor != null) 264 { 265 encryptionService = (Processor) Class.forName(processor).newInstance(); 266 } 267 } 268 catch (Throwable exception) 269 { 270 encryptionService = null; 271 Debug.trace(exception); 272 } 273 274 createItems(format); 278 279 if ((preset != null) && (preset.length() > 0)) 280 { 281 setFields(preset); 282 } 283 284 activeField = (RuleTextField) inputFields.elementAt(0); 288 activeField.grabFocus(); 289 } 290 291 292 297 298 public int getNumFields() 299 { 300 return (inputFields.size()); 301 } 302 303 304 313 314 public String getFieldContents(int index) throws IndexOutOfBoundsException 315 { 316 if ((index < 0) || (index > (inputFields.size() - 1))) { throw (new IndexOutOfBoundsException ()); } 317 318 return (((JTextField ) inputFields.elementAt(index)).getText()); 319 } 320 321 322 328 public Map getValidatorParams() 329 { 330 return validatorParams; 331 } 332 333 334 339 340 public String getText() 341 { 342 Object item; 343 StringBuffer buffer = new StringBuffer (); 344 int size = inputFields.size(); 345 346 if (resultFormat == ENCRYPTED) 353 { 354 if (encryptionService != null) 355 { 356 buffer.append(encryptionService.process(this)); 357 } 358 else 359 { 360 resultFormat = DEFAULT; 361 } 362 } 363 364 else if (resultFormat == PLAIN_STRING) 369 { 370 for (int i = 0; i < inputFields.size(); i++) 371 { 372 buffer.append(((JTextField ) inputFields.elementAt(i)).getText()); 373 } 374 } 375 376 else if (resultFormat == DISPLAY_FORMAT) 381 { 382 for (int i = 0; i < items.size(); i++) 383 { 384 item = items.elementAt(i); 385 if (item instanceof JTextField ) 386 { 387 buffer.append(((JTextField ) item).getText()); 388 } 389 else 390 { 391 buffer.append((String ) item); 392 } 393 } 394 } 395 396 else if (resultFormat == SPECIAL_SEPARATOR) 401 { 402 for (int i = 0; i < size; i++) 403 { 404 buffer.append(((JTextField ) inputFields.elementAt(i)).getText()); 405 406 if (i < (size - 1)) 407 { 408 buffer.append(separator); 409 } 410 } 411 } 412 413 return (buffer.toString()); 414 } 415 416 417 424 425 440 private void createItems(String format) 441 { 442 Object item; 443 JTextField field; 444 String token; 445 FieldSpec spec; 446 StringTokenizer tokenizer = new StringTokenizer (format); 447 448 while (tokenizer.hasMoreTokens()) 449 { 450 token = tokenizer.nextToken(); 451 try 452 { 453 spec = new FieldSpec(token); 454 field = new RuleTextField(spec.getColumns(), spec.getEditLength(), spec.getType(), 455 spec.getUnlimitedEdit(), toolkit); 456 457 if (items.size() > 0) 462 { 463 item = items.lastElement(); 464 465 if (item instanceof JTextField ) 466 { 467 items.add(" "); 468 } 469 } 470 471 items.add(field); 472 inputFields.add(field); 473 field.addFocusListener(this); 474 field.addKeyListener(this); 475 field.addCaretListener(this); 476 } 477 catch (Throwable exception) 485 { 486 if (items.size() == 0) 487 { 488 items.add(token); 489 } 490 else 491 { 492 item = items.lastElement(); 493 494 if (item instanceof String ) 501 { 502 items.setElementAt(item + " " + token, (items.size() - 1)); 503 } 504 else 505 { 506 items.add(token); 507 } 508 } 509 } 510 } 511 512 for (int i = 0; i < items.size(); i++) 516 { 517 item = items.elementAt(i); 518 519 if (item instanceof String ) 520 { 521 add(new JLabel ((String ) item)); 522 } 523 else 524 { 525 add((JTextField ) item); 526 } 527 } 528 } 529 530 531 539 540 private void setFields(String data) 541 { 542 StringTokenizer tokenizer = new StringTokenizer (data); 543 String token; 544 String indexString; 545 int index; 546 boolean process = false; 547 String [] vals = null; 548 int i = 0; 549 550 vals = new String [tokenizer.countTokens()]; 551 while (tokenizer.hasMoreTokens()) 552 { 553 token = tokenizer.nextToken(); 554 indexString = token.substring(0, token.indexOf(':')); 555 556 try 557 { 558 index = Integer.parseInt(indexString); 559 if (index < inputFields.size()) 560 { 561 String val = token.substring((token.indexOf(':') + 1), token.length()); 562 String className = ""; 563 if (val.indexOf(":") > -1) 564 { 565 className = val.substring(val.indexOf(":") + 1); 566 val = val.substring(0, val.indexOf(":")); 567 } 568 569 if (!"".equals(className) && !process) 570 { 571 process = true; 572 } 573 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 574 val = vs.substitute(val, null); 575 vals[i] = val; 576 i++; 577 ((JTextField ) inputFields.elementAt(index)).setText(val); 578 } 579 } 580 catch (Throwable exception) 581 { 582 exception.printStackTrace(); 583 } 584 } 585 586 if (process) 587 { 588 tokenizer = new StringTokenizer (data); 589 while (tokenizer.hasMoreTokens()) 590 { 591 token = tokenizer.nextToken(); 592 indexString = token.substring(0, token.indexOf(':')); 593 594 try 595 { 596 index = Integer.parseInt(indexString); 597 if (index < inputFields.size()) 598 { 599 String val = token.substring((token.indexOf(':') + 1), token.length()); 600 String className = ""; 601 String presult = ""; 602 if (val.indexOf(":") > -1) 603 { 604 className = val.substring(val.indexOf(":") + 1); 605 val = val.substring(0, val.indexOf(":")); 606 } 607 608 if (!"".equals(className)) 609 { 610 Processor p = (Processor) Class.forName(className).newInstance(); 611 presult = p.process(this); 612 } 613 String [] td = new RE("\\*").split(presult); 614 ((JTextField ) inputFields.elementAt(index)).setText(td[index]); 615 } 616 } 617 catch (Throwable exception) 618 {} 619 } 620 } 621 } 622 623 624 631 632 public boolean validateContents() 633 { 634 if (validationService != null) 635 { 636 return (validationService.validate(this)); 637 } 638 else 639 { 640 return (true); 641 } 642 } 643 644 647 648 649 655 656 public void keyTyped(KeyEvent event) 657 { 658 } 659 660 661 670 671 public void keyPressed(KeyEvent event) 672 { 673 if (event.getKeyCode() == KeyEvent.VK_BACK_SPACE) 674 { 675 int caretPosition = activeField.getCaretPosition(); 676 677 if (caretPosition == 0) 678 { 679 int activeIndex = inputFields.indexOf(activeField); 680 681 if (activeIndex > 0) 682 { 683 activeIndex--; 684 backstep = true; 685 activeField = (RuleTextField) inputFields.elementAt(activeIndex); 686 activeField.grabFocus(); 687 } 688 } 689 } 690 } 691 692 693 698 699 public void keyReleased(KeyEvent event) 700 { 701 } 702 703 706 707 708 713 714 720 public void focusGained(FocusEvent event) 721 { 722 activeField = (RuleTextField) event.getSource(); 723 724 if (backstep) 725 { 726 activeField.setCaretPosition(activeField.getText().length()); 727 backstep = false; 728 } 729 else 730 { 731 activeField.selectAll(); 732 } 733 } 734 735 736 742 743 public void focusLost(FocusEvent event) 744 { 745 } 746 747 750 751 752 757 758 public void caretUpdate(CaretEvent event) 759 { 760 if (activeField != null) 761 { 762 String text = activeField.getText(); 763 int fieldSize = activeField.getEditLength(); 764 int caretPosition = activeField.getCaretPosition(); 765 int selection = activeField.getSelectionEnd() - activeField.getSelectionStart(); 766 767 if ((!inputFields.lastElement().equals(activeField)) && (!activeField.unlimitedEdit())) 768 { 769 if ((text.length() == fieldSize) && (selection == 0) 770 && (caretPosition == fieldSize) && !backstep) 771 { 772 activeField.transferFocus(); 773 } 774 } 775 } 776 } 777 778 private static class FieldSpec 782 { 783 784 private int MIN_TOKENS = 2; 785 786 private int MAX_TOKENS = 3; 787 788 private int type; 789 790 private int columns; 791 792 private int editLength; 793 794 private boolean unlimitedEdit = false; 795 796 public FieldSpec(String spec) throws Exception 797 { 798 StringTokenizer tokenizer = new StringTokenizer (spec, ":"); 799 800 if ((tokenizer.countTokens() >= MIN_TOKENS) && (tokenizer.countTokens() <= MAX_TOKENS)) 801 { 802 String token = tokenizer.nextToken().toUpperCase(); 803 if ("N".equals(token)) 808 { 809 type = RuleTextField.N; 810 } 811 else if ("H".equals(token)) 812 { 813 type = RuleTextField.H; 814 } 815 else if ("A".equals(token)) 816 { 817 type = RuleTextField.A; 818 } 819 else if ("O".equals(token)) 820 { 821 type = RuleTextField.O; 822 } 823 else if ("AN".equals(token)) 824 { 825 type = RuleTextField.AN; 826 } 827 else 828 { 829 throw (new Exception ()); 830 } 831 832 try 837 { 838 token = tokenizer.nextToken(); 839 columns = Integer.parseInt(token); 840 } 841 catch (Throwable exception) 842 { 843 throw (new Exception ()); 844 } 845 846 try 853 { 854 token = tokenizer.nextToken().toUpperCase(); 855 editLength = Integer.parseInt(token); 856 } 857 catch (Throwable exception) 858 { 859 if ("U".equals(token)) 860 { 861 unlimitedEdit = true; 862 } 863 else 864 { 865 throw (new Exception ()); 866 } 867 } 868 869 } 870 else 871 { 872 throw (new Exception ()); 873 } 874 } 875 876 public int getColumns() 877 { 878 return (columns); 879 } 880 881 public int getEditLength() 882 { 883 return (editLength); 884 } 885 886 public int getType() 887 { 888 return (type); 889 } 890 891 public boolean getUnlimitedEdit() 892 { 893 return (unlimitedEdit); 894 } 895 896 } 897 899 } 900 901 | Popular Tags |