1 18 19 package com.izforge.izpack.panels; 20 21 import java.awt.Color ; 22 import java.awt.Font ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.File ; 26 import java.io.InputStream ; 27 import java.text.MessageFormat ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.StringTokenizer ; 33 import java.util.Vector ; 34 35 import javax.swing.BorderFactory ; 36 import javax.swing.ButtonGroup ; 37 import javax.swing.JButton ; 38 import javax.swing.JCheckBox ; 39 import javax.swing.JComboBox ; 40 import javax.swing.JComponent ; 41 import javax.swing.JFileChooser ; 42 import javax.swing.JLabel ; 43 import javax.swing.JOptionPane ; 44 import javax.swing.JPanel ; 45 import javax.swing.JPasswordField ; 46 import javax.swing.JRadioButton ; 47 import javax.swing.JTextField ; 48 49 import net.n3.nanoxml.NonValidator; 50 import net.n3.nanoxml.StdXMLBuilder; 51 import net.n3.nanoxml.StdXMLParser; 52 import net.n3.nanoxml.StdXMLReader; 53 import net.n3.nanoxml.XMLElement; 54 55 import com.izforge.izpack.LocaleDatabase; 56 import com.izforge.izpack.Pack; 57 import com.izforge.izpack.gui.ButtonFactory; 58 import com.izforge.izpack.gui.TwoColumnConstraints; 59 import com.izforge.izpack.gui.TwoColumnLayout; 60 import com.izforge.izpack.installer.InstallData; 61 import com.izforge.izpack.installer.InstallerFrame; 62 import com.izforge.izpack.installer.IzPanel; 63 import com.izforge.izpack.installer.ResourceManager; 64 import com.izforge.izpack.rules.RulesEngine; 65 import com.izforge.izpack.util.MultiLineLabel; 66 import com.izforge.izpack.util.OsConstraint; 67 import com.izforge.izpack.util.OsVersion; 68 import com.izforge.izpack.util.VariableSubstitutor; 69 70 71 80 81 112 public class UserInputPanel extends IzPanel 113 { 114 115 119 132 135 private static final long serialVersionUID = 3257850965439886129L; 136 137 private static final int POS_DISPLAYED = 0; 138 139 private static final int POS_TYPE = 1; 140 141 private static final int POS_VARIABLE = 2; 142 143 private static final int POS_CONSTRAINTS = 3; 144 145 private static final int POS_FIELD = 4; 146 147 private static final int POS_PACKS = 5; 148 149 private static final int POS_OS = 6; 150 151 private static final int POS_TRUE = 7; 152 153 private static final int POS_FALSE = 8; 154 155 private static final int POS_MESSAGE = 9; 156 157 private static final int POS_GROUP = 10; 158 159 160 private static final String SPEC_FILE_NAME = "userInputSpec.xml"; 161 162 private static final String LANG_FILE_NAME = "userInputLang.xml"; 163 164 165 private static final String NODE_ID = "panel"; 166 167 private static final String FIELD_NODE_ID = "field"; 168 169 private static final String INSTANCE_IDENTIFIER = "order"; 170 171 private static final String TYPE = "type"; 172 173 private static final String DESCRIPTION = "description"; 174 175 private static final String VARIABLE = "variable"; 176 177 private static final String TEXT = "txt"; 178 179 private static final String KEY = "id"; 180 181 private static final String SPEC = "spec"; 182 183 private static final String SET = "set"; 184 185 private static final String TRUE = "true"; 186 187 private static final String FALSE = "false"; 188 189 private static final String ALIGNMENT = "align"; 190 191 private static final String LEFT = "left"; 192 193 private static final String CENTER = "center"; 194 195 private static final String RIGHT = "right"; 196 197 private static final String TOP = "top"; 198 199 private static final String ITALICS = "italic"; 200 201 private static final String BOLD = "bold"; 202 203 private static final String SIZE = "size"; 204 205 private static final String VALIDATOR = "validator"; 206 207 private static final String PROCESSOR = "processor"; 208 209 private static final String CLASS = "class"; 210 211 private static final String FIELD_LABEL = "label"; 212 213 private static final String TITLE_FIELD = "title"; 214 215 private static final String TEXT_FIELD = "text"; 216 217 private static final String TEXT_SIZE = "size"; 218 219 private static final String STATIC_TEXT = "staticText"; 220 221 private static final String COMBO_FIELD = "combo"; 222 223 private static final String COMBO_CHOICE = "choice"; 224 225 private static final String COMBO_VALUE = "value"; 226 227 private static final String RADIO_FIELD = "radio"; 228 229 private static final String RADIO_CHOICE = "choice"; 230 231 private static final String RADIO_VALUE = "value"; 232 233 private static final String SPACE_FIELD = "space"; 234 235 private static final String DIVIDER_FIELD = "divider"; 236 237 private static final String CHECK_FIELD = "check"; 238 239 private static final String RULE_FIELD = "rule"; 240 241 private static final String RULE_LAYOUT = "layout"; 242 243 private static final String RULE_SEPARATOR = "separator"; 244 245 private static final String RULE_RESULT_FORMAT = "resultFormat"; 246 247 private static final String RULE_PLAIN_STRING = "plainString"; 248 249 private static final String RULE_DISPLAY_FORMAT = "displayFormat"; 250 251 private static final String RULE_SPECIAL_SEPARATOR = "specialSeparator"; 252 253 private static final String RULE_ENCRYPTED = "processed"; 254 255 private static final String RULE_PARAM_NAME = "name"; 256 257 private static final String RULE_PARAM_VALUE = "value"; 258 259 private static final String RULE_PARAM = "param"; 260 261 private static final String PWD_FIELD = "password"; 262 263 private static final String PWD_INPUT = "pwd"; 264 265 private static final String PWD_SIZE = "size"; 266 267 private static final String SEARCH_FIELD = "search"; 268 269 private static final String SEARCH_BUTTON_FIELD = "autodetect"; 271 272 private static final String SEARCH_CHOICE = "choice"; 273 274 private static final String SEARCH_FILENAME = "filename"; 275 276 private static final String SEARCH_RESULT = "result"; 277 278 private static final String SEARCH_VALUE = "value"; 279 280 private static final String SEARCH_TYPE = "type"; 281 282 private static final String SEARCH_FILE = "file"; 283 284 private static final String SEARCH_DIRECTORY = "directory"; 285 286 private static final String SEARCH_PARENTDIR = "parentdir"; 287 288 private static final String SEARCH_CHECKFILENAME = "checkfilename"; 289 290 private static final String SELECTEDPACKS = "createForPack"; 292 private static final String UNSELECTEDPACKS = "createForUnselectedPack"; 294 protected static final String ATTRIBUTE_CONDITIONID_NAME = "conditionid"; 295 296 protected static final String VARIABLE_NODE = "variable"; 297 298 protected static final String ATTRIBUTE_VARIABLE_NAME = "name"; 299 300 protected static final String ATTRIBUTE_VARIABLE_VALUE = "value"; 301 302 304 private static final String NAME = "name"; 305 306 private static final String OS = "os"; 307 308 private static final String FAMILY = "family"; 309 310 private static int instanceCount = 0; 314 315 protected int instanceNumber = 0; 316 317 323 private boolean packsDefined = false; 324 325 private InstallerFrame parentFrame; 326 327 328 private XMLElement spec; 329 330 private boolean haveSpec = false; 331 332 333 private Vector uiElements = new Vector (); 334 335 336 private Vector buttonGroups = new Vector (); 337 338 339 private Vector passwordGroups = new Vector (); 340 341 345 private Vector passwordGroupsRead = new Vector (); 346 347 348 private Vector searchFields = new Vector (); 349 350 351 private Vector entries = new Vector (); 352 353 private LocaleDatabase langpack = null; 354 355 356 361 367 373 374 public UserInputPanel(InstallerFrame parent, InstallData installData) 375 { 376 super(parent, installData); 377 instanceNumber = instanceCount++; 378 this.parentFrame = parent; 379 } 380 381 protected void init() 382 { 383 super.removeAll(); 384 uiElements.clear(); 385 386 TwoColumnLayout layout = new TwoColumnLayout(10, 5, 30, 25, TwoColumnLayout.LEFT); 389 setLayout(layout); 390 391 try 395 { 396 398 String resource = LANG_FILE_NAME + "_" + idata.localeISO3; 399 this.langpack = new LocaleDatabase(ResourceManager.getInstance().getInputStream( 400 resource)); 401 } 402 catch (Throwable exception) 403 {} 404 405 try 409 { 410 readSpec(); 411 } 412 catch (Throwable exception) 413 { 414 exception.printStackTrace(); 416 } 417 418 if (!haveSpec) 419 { 420 return; 424 } 425 426 updateVariables(); 428 429 Vector fields = spec.getChildrenNamed(FIELD_NODE_ID); 435 436 for (int i = 0; i < fields.size(); i++) 437 { 438 XMLElement field = (XMLElement) fields.elementAt(i); 439 String attribute = field.getAttribute(TYPE); 440 String conditionid = field.getAttribute(ATTRIBUTE_CONDITIONID_NAME); 441 if (conditionid != null) 442 { 443 if (!this.parent.getRules().isConditionTrue(conditionid, idata.getVariables())) 445 { 446 continue; 447 } 448 } 449 if (attribute != null) 450 { 451 if (attribute.equals(RULE_FIELD)) 452 { 453 addRuleField(field); 454 } 455 else if (attribute.equals(TEXT_FIELD)) 456 { 457 addTextField(field); 458 } 459 else if (attribute.equals(COMBO_FIELD)) 460 { 461 addComboBox(field); 462 } 463 else if (attribute.equals(RADIO_FIELD)) 464 { 465 addRadioButton(field); 466 } 467 else if (attribute.equals(PWD_FIELD)) 468 { 469 addPasswordField(field); 470 } 471 else if (attribute.equals(SPACE_FIELD)) 472 { 473 addSpace(field); 474 } 475 else if (attribute.equals(DIVIDER_FIELD)) 476 { 477 addDivider(field); 478 } 479 else if (attribute.equals(CHECK_FIELD)) 480 { 481 addCheckBox(field); 482 } 483 else if (attribute.equals(STATIC_TEXT)) 484 { 485 addText(field); 486 } 487 else if (attribute.equals(TITLE_FIELD)) 488 { 489 addTitle(field); 490 } 491 else if (attribute.equals(SEARCH_FIELD)) 492 { 493 addSearch(field); 494 } 495 } 496 } 497 } 498 499 protected void updateUIElements() 500 { 501 boolean updated = false; 502 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 503 504 for (int i = 0; i < uiElements.size(); i++) 505 { 506 Object [] element = (Object []) uiElements.get(i); 507 if (element[POS_VARIABLE] == null) 508 { 509 continue; 510 } 511 String value = idata.getVariable((String ) element[POS_VARIABLE]); 512 513 if (RADIO_FIELD.equals(element[POS_TYPE])) 514 { 515 JRadioButton choice = (JRadioButton ) element[POS_FIELD]; 517 if (value == null) 518 { 519 continue; 520 } 521 if (value.equals(element[POS_TRUE])) 522 { 523 choice.setSelected(true); 524 } 525 else 526 { 527 choice.setSelected(false); 528 } 529 element[POS_FIELD] = choice; 530 } 531 else if (TEXT_FIELD.equals(element[POS_TYPE])) 532 { 533 JTextField textf = (JTextField ) element[POS_FIELD]; 535 536 if (value == null) 538 { 539 value = textf.getText(); 540 } 541 textf.setText(vs.substitute(value, null)); 542 element[POS_FIELD] = textf; 543 } 544 else if (CHECK_FIELD.equals(element[POS_TYPE])) 545 { 546 } 548 else if (SEARCH_FIELD.equals(element[POS_TYPE])) 549 { 550 } 552 else if (RULE_FIELD.equals(element[POS_TYPE])) 553 { 554 555 RuleInputField rulef = (RuleInputField) element[POS_FIELD]; 556 if (value == null) 558 { 559 value = rulef.getText(); 560 } 561 } 562 uiElements.set(i, element); 564 updated = true; 565 } 566 if (updated) 567 { 568 super.invalidate(); 570 } 572 } 573 574 575 582 583 public boolean isValidated() 584 { 585 return (readInput()); 586 } 587 588 589 592 593 public void panelActivate() 594 { 595 this.init(); 596 597 if (spec == null) 598 { 599 emitError("User input specification could not be found.", 601 "The specification for the user input panel could not be found. Please contact the packager."); 602 parentFrame.skipPanel(); 603 } 604 updateUIElements(); 606 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 607 Vector forUnselectedPacks = spec.getChildrenNamed(UNSELECTEDPACKS); 608 Vector forOs = spec.getChildrenNamed(OS); 609 610 if (!itemRequiredFor(forPacks) || !itemRequiredForUnselected(forUnselectedPacks) 611 || !itemRequiredForOs(forOs)) 612 { 613 parentFrame.skipPanel(); 614 return; 615 } 616 if (!haveSpec) 617 { 618 parentFrame.skipPanel(); 619 return; 620 } 621 626 buildUI(); 627 validate(); 629 if (packsDefined) 630 { 631 parentFrame.lockPrevButton(); 632 } 633 } 634 635 636 642 643 public void makeXMLData(XMLElement panelRoot) 644 { 645 Map entryMap = new HashMap (); 646 647 for (int i = 0; i < entries.size(); i++) 648 { 649 TextValuePair pair = (TextValuePair) entries.elementAt(i); 650 entryMap.put(pair.toString(), pair.getValue()); 651 } 652 653 new UserInputPanelAutomationHelper(entryMap).makeXMLData(idata, panelRoot); 654 } 655 656 657 660 661 private void buildUI() 662 { 663 Object [] uiElement; 664 665 for (int i = 0; i < uiElements.size(); i++) 666 { 667 uiElement = (Object []) uiElements.elementAt(i); 668 669 if (itemRequiredFor((Vector ) uiElement[POS_PACKS]) 670 && itemRequiredForOs((Vector ) uiElement[POS_OS])) 671 { 672 try 673 { 674 if (uiElement[POS_DISPLAYED] == null 675 || "false".equals(uiElement[POS_DISPLAYED].toString())) 676 { 677 add((JComponent ) uiElement[POS_FIELD], uiElement[POS_CONSTRAINTS]); 678 } 679 680 uiElement[POS_DISPLAYED] = Boolean.valueOf(true); 681 uiElements.remove(i); 682 uiElements.add(i, uiElement); 683 } 684 catch (Throwable exception) 685 { 686 System.out.println("Internal format error in field: " 687 + uiElement[POS_TYPE].toString()); } 689 } 690 else 691 { 692 try 693 { 694 if (uiElement[POS_DISPLAYED] != null 695 && "true".equals(uiElement[POS_DISPLAYED].toString())) 696 { 697 remove((JComponent ) uiElement[POS_FIELD]); 698 } 699 } 700 catch (Throwable exception) 701 { 702 System.out.println("Internal format error in field: " 703 + uiElement[POS_TYPE].toString()); } 705 uiElement[POS_DISPLAYED] = Boolean.valueOf(false); 706 uiElements.remove(i); 707 uiElements.add(i, uiElement); 708 } 709 } 710 } 711 712 713 718 719 private boolean readInput() 720 { 721 boolean success; 722 String fieldType = null; 723 Object [] field = null; 724 725 passwordGroupsRead.clear(); 726 for (int i = 0; i < uiElements.size(); i++) 731 { 732 field = (Object []) uiElements.elementAt(i); 733 734 if ((field != null) && (((Boolean ) field[POS_DISPLAYED]).booleanValue())) 735 { 736 fieldType = (String ) (field[POS_TYPE]); 737 738 if (fieldType.equals(RULE_FIELD)) 740 { 741 success = readRuleField(field); 742 if (!success) { return (false); } 743 } 744 745 if (fieldType.equals(PWD_FIELD)) 747 { 748 success = readPasswordField(field); 749 if (!success) { return (false); } 750 } 751 752 else if (fieldType.equals(TEXT_FIELD)) 754 { 755 success = readTextField(field); 756 if (!success) { return (false); } 757 } 758 759 else if (fieldType.equals(COMBO_FIELD)) 761 { 762 success = readComboBox(field); 763 if (!success) { return (false); } 764 } 765 766 else if (fieldType.equals(RADIO_FIELD)) 768 { 769 success = readRadioButton(field); 770 if (!success) { return (false); } 771 } 772 773 else if (fieldType.equals(CHECK_FIELD)) 775 { 776 success = readCheckBox(field); 777 if (!success) { return (false); } 778 } 779 else if (fieldType.equals(SEARCH_FIELD)) 780 { 781 success = readSearch(field); 782 if (!success) { return (false); } 783 } 784 } 785 } 786 787 return (true); 788 } 789 790 791 796 797 private void readSpec() throws Exception 798 { 799 InputStream input = null; 800 XMLElement data; 801 Vector specElements; 802 String attribute; 803 String instance = Integer.toString(instanceNumber); 804 805 try 806 { 807 input = parentFrame.getResource(SPEC_FILE_NAME); 808 } 809 catch (Exception exception) 810 { 811 haveSpec = false; 812 return; 813 } 814 if (input == null) 815 { 816 haveSpec = false; 817 return; 818 } 819 820 StdXMLParser parser = new StdXMLParser(); 822 parser.setBuilder(new StdXMLBuilder()); 823 parser.setValidator(new NonValidator()); 824 parser.setReader(new StdXMLReader(input)); 825 826 data = (XMLElement) parser.parse(); 828 829 if (data.hasChildren()) 831 { 832 specElements = data.getChildrenNamed(NODE_ID); 833 for (int i = 0; i < specElements.size(); i++) 834 { 835 data = (XMLElement) specElements.elementAt(i); 836 attribute = data.getAttribute(INSTANCE_IDENTIFIER); 837 838 if (instance.equals(attribute)) 839 { 840 spec = data; 842 input.close(); 844 haveSpec = true; 845 return; 846 } 847 } 848 849 haveSpec = false; 850 return; 851 } 852 853 haveSpec = false; 854 } 855 856 857 864 865 private void addTitle(XMLElement spec) 866 { 867 String title = getText(spec); 868 boolean italic = getBoolean(spec, ITALICS, false); 869 boolean bold = getBoolean(spec, BOLD, false); 870 float multiplier = getFloat(spec, SIZE, 2.0f); 871 int justify = getAlignment(spec); 872 873 if (title != null) 874 { 875 JLabel label = new JLabel (title); 876 Font font = label.getFont(); 877 float size = font.getSize(); 878 int style = 0; 879 880 if (bold) 881 { 882 style += Font.BOLD; 883 } 884 if (italic) 885 { 886 style += Font.ITALIC; 887 } 888 889 font = font.deriveFont(style, (size * multiplier)); 890 label.setFont(font); 891 label.setAlignmentX(0); 892 893 TwoColumnConstraints constraints = new TwoColumnConstraints(); 894 constraints.align = justify; 895 constraints.position = TwoColumnConstraints.NORTH; 896 897 add(label, constraints); 898 } 899 } 900 901 902 907 908 private void addRuleField(XMLElement spec) 909 { 910 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 911 Vector forOs = spec.getChildrenNamed(OS); 912 XMLElement element = spec.getFirstChildNamed(SPEC); 913 String variable = spec.getAttribute(VARIABLE); 914 RuleInputField field = null; 915 JLabel label; 916 String layout; 917 String set; 918 String separator; 919 String format; 920 String validator = null; 921 String message = null; 922 boolean hasParams = false; 923 String paramName = null; 924 String paramValue = null; 925 HashMap validateParamMap = null; 926 Vector validateParams = null; 927 String processor = null; 928 int resultFormat = RuleInputField.DISPLAY_FORMAT; 929 930 if (element != null) 934 { 935 label = new JLabel (getText(element)); 936 layout = element.getAttribute(RULE_LAYOUT); 937 set = element.getAttribute(SET); 938 939 946 separator = element.getAttribute(RULE_SEPARATOR); 947 format = element.getAttribute(RULE_RESULT_FORMAT); 948 949 if (format != null) 950 { 951 if (format.equals(RULE_PLAIN_STRING)) 952 { 953 resultFormat = RuleInputField.PLAIN_STRING; 954 } 955 else if (format.equals(RULE_DISPLAY_FORMAT)) 956 { 957 resultFormat = RuleInputField.DISPLAY_FORMAT; 958 } 959 else if (format.equals(RULE_SPECIAL_SEPARATOR)) 960 { 961 resultFormat = RuleInputField.SPECIAL_SEPARATOR; 962 } 963 else if (format.equals(RULE_ENCRYPTED)) 964 { 965 resultFormat = RuleInputField.ENCRYPTED; 966 } 967 } 968 } 969 else 974 { 975 return; 976 } 977 978 element = spec.getFirstChildNamed(DESCRIPTION); 983 addDescription(element, forPacks, forOs); 984 985 element = spec.getFirstChildNamed(VALIDATOR); 989 if (element != null) 990 { 991 validator = element.getAttribute(CLASS); 992 message = getText(element); 993 validateParams = element.getChildrenNamed(RULE_PARAM); 998 if (validateParams != null && validateParams.size() > 0) 999 { 1000 hasParams = true; 1001 1002 if (validateParamMap == null) validateParamMap = new HashMap (); 1003 1004 for (Iterator it = validateParams.iterator(); it.hasNext();) 1005 { 1006 element = (XMLElement) it.next(); 1007 paramName = element.getAttribute(RULE_PARAM_NAME); 1008 paramValue = element.getAttribute(RULE_PARAM_VALUE); 1009 validateParamMap.put(paramName, paramValue); 1010 } 1011 1012 } 1013 1014 } 1015 1016 element = spec.getFirstChildNamed(PROCESSOR); 1017 if (element != null) 1018 { 1019 processor = element.getAttribute(CLASS); 1020 } 1021 1022 if (hasParams) 1028 { 1029 field = new RuleInputField(layout, set, separator, validator, validateParamMap, 1030 processor, resultFormat, getToolkit(), idata); 1031 } 1032 else 1033 { 1034 field = new RuleInputField(layout, set, separator, validator, processor, resultFormat, 1035 getToolkit(), idata); 1036 1037 } 1038 TwoColumnConstraints constraints = new TwoColumnConstraints(); 1039 constraints.position = TwoColumnConstraints.WEST; 1040 1041 uiElements 1042 .add(new Object [] { null, FIELD_LABEL, null, constraints, label, forPacks, forOs}); 1043 1044 TwoColumnConstraints constraints2 = new TwoColumnConstraints(); 1045 constraints2.position = TwoColumnConstraints.EAST; 1046 1047 uiElements.add(new Object [] { null, RULE_FIELD, variable, constraints2, field, forPacks, 1048 forOs, null, null, message}); 1049 } 1050 1051 1052 1061 1062 private boolean readRuleField(Object [] field) 1063 { 1064 RuleInputField ruleField = null; 1065 String variable = null; 1066 1067 try 1068 { 1069 ruleField = (RuleInputField) field[POS_FIELD]; 1070 variable = (String ) field[POS_VARIABLE]; 1071 } 1072 catch (Throwable exception) 1073 { 1074 return (true); 1075 } 1076 if ((variable == null) || (ruleField == null)) { return (true); } 1077 1078 boolean success = ruleField.validateContents(); 1079 if (!success) 1080 { 1081 String message = ""; 1082 try 1083 { 1084 message = langpack.getString((String ) field[POS_MESSAGE]); 1085 if ("".equals(message)) 1086 { 1087 message = (String ) field[POS_MESSAGE]; 1088 } 1089 } 1090 catch (Throwable t) 1091 { 1092 message = (String ) field[POS_MESSAGE]; 1093 } 1094 JOptionPane.showMessageDialog(parentFrame, message, parentFrame.langpack 1095 .getString("UserInputPanel.error.caption"), JOptionPane.WARNING_MESSAGE); 1096 return (false); 1097 } 1098 1099 idata.setVariable(variable, ruleField.getText()); 1100 entries.add(new TextValuePair(variable, ruleField.getText())); 1101 return (true); 1102 } 1103 1104 1105 1110 1111 private void addTextField(XMLElement spec) 1112 { 1113 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 1114 Vector forOs = spec.getChildrenNamed(OS); 1115 XMLElement element = spec.getFirstChildNamed(SPEC); 1116 JLabel label; 1117 String set; 1118 int size; 1119 1120 String variable = spec.getAttribute(VARIABLE); 1121 if ((variable == null) || (variable.length() == 0)) { return; } 1122 1123 if (element != null) 1127 { 1128 label = new JLabel (getText(element)); 1129 set = element.getAttribute(SET); 1130 if (set == null) 1131 { 1132 set = idata.getVariable(variable); 1133 if (set == null) 1134 { 1135 set = ""; 1136 } 1137 } 1138 else 1139 { 1140 if (set != null && !"".equals(set)) 1141 { 1142 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1143 set = vs.substitute(set, null); 1144 } 1145 } 1146 1147 try 1148 { 1149 size = Integer.parseInt(element.getAttribute(TEXT_SIZE)); 1150 } 1151 catch (Throwable exception) 1152 { 1153 size = 1; 1154 } 1155 } 1156 else 1161 { 1162 return; 1163 } 1164 1165 element = spec.getFirstChildNamed(DESCRIPTION); 1170 addDescription(element, forPacks, forOs); 1171 1172 JTextField field = new JTextField (set, size); 1176 field.setCaretPosition(0); 1177 1178 TwoColumnConstraints constraints = new TwoColumnConstraints(); 1179 constraints.position = TwoColumnConstraints.WEST; 1180 1181 uiElements 1182 .add(new Object [] { null, FIELD_LABEL, null, constraints, label, forPacks, forOs}); 1183 1184 TwoColumnConstraints constraints2 = new TwoColumnConstraints(); 1185 constraints2.position = TwoColumnConstraints.EAST; 1186 1187 uiElements.add(new Object [] { null, TEXT_FIELD, variable, constraints2, field, forPacks, 1188 forOs}); 1189 } 1190 1191 1192 1201 1202 private boolean readTextField(Object [] field) 1203 { 1204 JTextField textField = null; 1205 String variable = null; 1206 String value = null; 1207 1208 try 1209 { 1210 textField = (JTextField ) field[POS_FIELD]; 1211 variable = (String ) field[POS_VARIABLE]; 1212 value = textField.getText(); 1213 } 1214 catch (Throwable exception) 1215 { 1216 return (true); 1217 } 1218 if ((variable == null) || (value == null)) { return (true); } 1219 1220 idata.setVariable(variable, value); 1221 entries.add(new TextValuePair(variable, value)); 1222 return (true); 1223 } 1224 1225 1226 1250 1251 private void addComboBox(XMLElement spec) 1252 { 1253 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 1254 Vector forOs = spec.getChildrenNamed(OS); 1255 XMLElement element = spec.getFirstChildNamed(SPEC); 1256 String variable = spec.getAttribute(VARIABLE); 1257 TextValuePair listItem = null; 1258 JComboBox field = new JComboBox (); 1259 JLabel label; 1260 1261 if (element != null) 1265 { 1266 label = new JLabel (getText(element)); 1267 1268 Vector choices = element.getChildrenNamed(COMBO_CHOICE); 1269 1270 if (choices == null) { return; } 1271 1272 for (int i = 0; i < choices.size(); i++) 1273 { 1274 String processorClass = ((XMLElement) choices.elementAt(i)) 1275 .getAttribute("processor"); 1276 1277 if (processorClass != null && !"".equals(processorClass)) 1278 { 1279 String choiceValues = ""; 1280 try 1281 { 1282 choiceValues = ((Processor) Class.forName(processorClass).newInstance()) 1283 .process(null); 1284 } 1285 catch (Throwable t) 1286 { 1287 t.printStackTrace(); 1288 } 1289 String set = ((XMLElement) choices.elementAt(i)).getAttribute(SET); 1290 if (set == null) 1291 { 1292 set = ""; 1293 } 1294 if (set != null && !"".equals(set)) 1295 { 1296 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1297 set = vs.substitute(set, null); 1298 } 1299 1300 StringTokenizer tokenizer = new StringTokenizer (choiceValues, ":"); 1301 int counter = 0; 1302 while (tokenizer.hasMoreTokens()) 1303 { 1304 String token = tokenizer.nextToken(); 1305 listItem = new TextValuePair(token, token); 1306 field.addItem(listItem); 1307 if (set.equals(token)) 1308 { 1309 field.setSelectedIndex(field.getItemCount() - 1); 1310 } 1311 counter++; 1312 } 1313 } 1314 else 1315 { 1316 listItem = new TextValuePair(getText((XMLElement) choices.elementAt(i)), 1317 ((XMLElement) choices.elementAt(i)).getAttribute(COMBO_VALUE)); 1318 field.addItem(listItem); 1319 String set = ((XMLElement) choices.elementAt(i)).getAttribute(SET); 1320 if (set != null) 1321 { 1322 if (set != null && !"".equals(set)) 1323 { 1324 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1325 set = vs.substitute(set, null); 1326 } 1327 if (set.equals(TRUE)) 1328 { 1329 field.setSelectedIndex(i); 1330 } 1331 } 1332 } 1333 1334 } 1335 } 1336 else 1341 { 1342 return; 1343 } 1344 1345 element = spec.getFirstChildNamed(DESCRIPTION); 1350 addDescription(element, forPacks, forOs); 1351 1352 TwoColumnConstraints constraints = new TwoColumnConstraints(); 1353 constraints.position = TwoColumnConstraints.WEST; 1354 1355 uiElements 1356 .add(new Object [] { null, FIELD_LABEL, null, constraints, label, forPacks, forOs}); 1357 1358 TwoColumnConstraints constraints2 = new TwoColumnConstraints(); 1359 constraints2.position = TwoColumnConstraints.EAST; 1360 1361 uiElements.add(new Object [] { null, COMBO_FIELD, variable, constraints2, field, forPacks, 1362 forOs}); 1363 } 1364 1365 1366 1375 1376 private boolean readComboBox(Object [] field) 1377 { 1378 String variable; 1379 String value; 1380 JComboBox comboBox; 1381 1382 try 1383 { 1384 variable = (String ) field[POS_VARIABLE]; 1385 comboBox = (JComboBox ) field[POS_FIELD]; 1386 value = ((TextValuePair) comboBox.getSelectedItem()).getValue(); 1387 } 1388 catch (Throwable exception) 1389 { 1390 return true; 1391 } 1392 if ((variable == null) || (value == null)) { return true; } 1393 1394 idata.setVariable(variable, value); 1395 entries.add(new TextValuePair(variable, value)); 1396 return true; 1397 } 1398 1399 1400 1426 1427 private void addRadioButton(XMLElement spec) 1428 { 1429 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 1430 Vector forOs = spec.getChildrenNamed(OS); 1431 String variable = spec.getAttribute(VARIABLE); 1432 String value = null; 1433 1434 XMLElement element = null; 1435 ButtonGroup group = new ButtonGroup (); 1436 1437 TwoColumnConstraints constraints = new TwoColumnConstraints(); 1438 constraints.position = TwoColumnConstraints.BOTH; 1439 constraints.indent = true; 1440 constraints.stretch = true; 1441 1442 element = spec.getFirstChildNamed(DESCRIPTION); 1447 addDescription(element, forPacks, forOs); 1448 1449 element = spec.getFirstChildNamed(SPEC); 1453 1454 if (element != null) 1455 { 1456 Vector choices = element.getChildrenNamed(RADIO_CHOICE); 1457 1458 if (choices == null) { return; } 1459 1460 for (int i = 0; i < choices.size(); i++) 1464 { 1465 JRadioButton choice = new JRadioButton (); 1466 choice.setText(getText((XMLElement) choices.elementAt(i))); 1467 value = (((XMLElement) choices.elementAt(i)).getAttribute(RADIO_VALUE)); 1468 1469 group.add(choice); 1470 1471 String set = ((XMLElement) choices.elementAt(i)).getAttribute(SET); 1472 if (set != null) 1473 { 1474 if (set != null && !"".equals(set)) 1475 { 1476 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1477 set = vs.substitute(set, null); 1478 } 1479 if (set.equals(TRUE)) 1480 { 1481 choice.setSelected(true); 1482 } 1483 } 1484 1485 buttonGroups.add(group); 1486 uiElements.add(new Object [] { null, RADIO_FIELD, variable, constraints, choice, 1487 forPacks, forOs, value, null, null, group}); 1488 } 1489 } 1490 } 1491 1492 1493 1502 1503 private boolean readRadioButton(Object [] field) 1504 { 1505 String variable = null; 1506 String value = null; 1507 JRadioButton button = null; 1508 1509 try 1510 { 1511 button = (JRadioButton ) field[POS_FIELD]; 1512 1513 if (!button.isSelected()) { return (true); } 1514 1515 variable = (String ) field[POS_VARIABLE]; 1516 value = (String ) field[POS_TRUE]; 1517 } 1518 catch (Throwable exception) 1519 { 1520 return (true); 1521 } 1522 1523 idata.setVariable(variable, value); 1524 entries.add(new TextValuePair(variable, value)); 1525 return (true); 1526 } 1527 1528 1529 1554 1555 private void addPasswordField(XMLElement spec) 1556 { 1557 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 1558 Vector forOs = spec.getChildrenNamed(OS); 1559 String variable = spec.getAttribute(VARIABLE); 1560 String validator = null; 1561 String message = null; 1562 String processor = null; 1563 XMLElement element = null; 1564 PasswordGroup group = null; 1565 int size = 0; 1566 1567 element = spec.getFirstChildNamed(DESCRIPTION); 1572 addDescription(element, forPacks, forOs); 1573 1574 element = spec.getFirstChildNamed(VALIDATOR); 1578 if (element != null) 1579 { 1580 validator = element.getAttribute(CLASS); 1581 message = getText(element); 1582 } 1583 1584 element = spec.getFirstChildNamed(PROCESSOR); 1585 if (element != null) 1586 { 1587 processor = element.getAttribute(CLASS); 1588 } 1589 1590 group = new PasswordGroup(validator, processor); 1591 1592 element = spec.getFirstChildNamed(SPEC); 1596 1597 if (element != null) 1598 { 1599 Vector inputs = element.getChildrenNamed(PWD_INPUT); 1600 1601 if (inputs == null) { return; } 1602 1603 XMLElement fieldSpec; 1607 for (int i = 0; i < inputs.size(); i++) 1608 { 1609 fieldSpec = (XMLElement) inputs.elementAt(i); 1610 String set = fieldSpec.getAttribute(SET); 1611 if (set != null && !"".equals(set)) 1612 { 1613 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1614 set = vs.substitute(set, null); 1615 } 1616 JLabel label = new JLabel (getText(fieldSpec)); 1617 try 1618 { 1619 size = Integer.parseInt(fieldSpec.getAttribute(PWD_SIZE)); 1620 } 1621 catch (Throwable exception) 1622 { 1623 size = 1; 1624 } 1625 1626 JPasswordField field = new JPasswordField (set, size); 1630 field.setCaretPosition(0); 1631 1632 TwoColumnConstraints constraints = new TwoColumnConstraints(); 1633 constraints.position = TwoColumnConstraints.WEST; 1634 1635 uiElements.add(new Object [] { null, FIELD_LABEL, null, constraints, label, 1636 forPacks, forOs}); 1637 1638 TwoColumnConstraints constraints2 = new TwoColumnConstraints(); 1639 constraints2.position = TwoColumnConstraints.EAST; 1640 1641 uiElements.add(new Object [] { null, PWD_FIELD, variable, constraints2, field, 1642 forPacks, forOs, null, null, message, group}); 1643 group.addField(field); 1644 } 1645 } 1646 1647 passwordGroups.add(group); 1648 } 1649 1650 1651 1660 1661 private boolean readPasswordField(Object [] field) 1662 { 1663 PasswordGroup group = null; 1664 String variable = null; 1665 String message = null; 1666 1667 try 1668 { 1669 group = (PasswordGroup) field[POS_GROUP]; 1670 variable = (String ) field[POS_VARIABLE]; 1671 message = (String ) field[POS_MESSAGE]; 1672 } 1673 catch (Throwable exception) 1674 { 1675 return (true); 1676 } 1677 if ((variable == null) || (passwordGroupsRead.contains(group))) { return (true); } 1678 passwordGroups.add(group); 1679 1680 boolean success = group.validateContents(); 1681 1682 if (!success) 1683 { 1684 JOptionPane.showMessageDialog(parentFrame, message, parentFrame.langpack 1685 .getString("UserInputPanel.error.caption"), JOptionPane.WARNING_MESSAGE); 1686 return (false); 1687 } 1688 1689 idata.setVariable(variable, group.getPassword()); 1690 entries.add(new TextValuePair(variable, group.getPassword())); 1691 return (true); 1692 } 1693 1694 1695 1700 1701 private void addCheckBox(XMLElement spec) 1702 { 1703 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 1704 Vector forOs = spec.getChildrenNamed(OS); 1705 String label = ""; 1706 String set = null; 1707 String trueValue = null; 1708 String falseValue = null; 1709 String variable = spec.getAttribute(VARIABLE); 1710 XMLElement detail = spec.getFirstChildNamed(SPEC); 1711 1712 if (variable == null) { return; } 1713 1714 if (detail != null) 1715 { 1716 label = getText(detail); 1717 set = detail.getAttribute(SET); 1718 trueValue = detail.getAttribute(TRUE); 1719 falseValue = detail.getAttribute(FALSE); 1720 } 1721 1722 JCheckBox checkbox = new JCheckBox (label); 1723 1724 if (set != null) 1725 { 1726 if (set != null && !"".equals(set)) 1727 { 1728 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1729 set = vs.substitute(set, null); 1730 } 1731 if (set.equals(FALSE)) 1732 { 1733 checkbox.setSelected(false); 1734 } 1735 if (set.equals(TRUE)) 1736 { 1737 checkbox.setSelected(true); 1738 } 1739 } 1740 1741 XMLElement element = spec.getFirstChildNamed(DESCRIPTION); 1746 addDescription(element, forPacks, forOs); 1747 1748 TwoColumnConstraints constraints = new TwoColumnConstraints(); 1749 constraints.position = TwoColumnConstraints.BOTH; 1750 constraints.stretch = true; 1751 constraints.indent = true; 1752 1753 uiElements.add(new Object [] { null, CHECK_FIELD, variable, constraints, checkbox, forPacks, 1754 forOs, trueValue, falseValue}); 1755 } 1756 1757 1758 1767 1768 private boolean readCheckBox(Object [] field) 1769 { 1770 String variable = null; 1771 String trueValue = null; 1772 String falseValue = null; 1773 JCheckBox box = null; 1774 1775 try 1776 { 1777 box = (JCheckBox ) field[POS_FIELD]; 1778 variable = (String ) field[POS_VARIABLE]; 1779 trueValue = (String ) field[POS_TRUE]; 1780 if (trueValue == null) 1781 { 1782 trueValue = ""; 1783 } 1784 1785 falseValue = (String ) field[POS_FALSE]; 1786 if (falseValue == null) 1787 { 1788 falseValue = ""; 1789 } 1790 } 1791 catch (Throwable exception) 1792 { 1793 return (true); 1794 } 1795 1796 if (box.isSelected()) 1797 { 1798 idata.setVariable(variable, trueValue); 1799 entries.add(new TextValuePair(variable, trueValue)); 1800 } 1801 else 1802 { 1803 idata.setVariable(variable, falseValue); 1804 entries.add(new TextValuePair(variable, falseValue)); 1805 } 1806 1807 return (true); 1808 } 1809 1810 1811 1834 1835 private void addSearch(XMLElement spec) 1836 { 1837 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 1838 Vector forOs = spec.getChildrenNamed(OS); 1839 XMLElement element = spec.getFirstChildNamed(SPEC); 1840 String variable = spec.getAttribute(VARIABLE); 1841 String filename = null; 1842 String check_filename = null; 1843 int search_type = 0; 1844 int result_type = 0; 1845 JComboBox combobox = new JComboBox (); 1846 JLabel label = null; 1847 1848 1850 combobox.setEditable(true); 1852 1853 if (element != null) 1857 { 1858 label = new JLabel (getText(element)); 1859 1860 search_type = SearchField.TYPE_FILE; 1862 1863 String search_type_str = element.getAttribute(SEARCH_TYPE); 1864 1865 if (search_type_str != null) 1866 { 1867 if (search_type_str.equals(SEARCH_FILE)) 1868 { 1869 search_type = SearchField.TYPE_FILE; 1870 } 1871 else if (search_type_str.equals(SEARCH_DIRECTORY)) 1872 { 1873 search_type = SearchField.TYPE_DIRECTORY; 1874 } 1875 } 1876 1877 String result_type_str = element.getAttribute(SEARCH_RESULT); 1879 1880 if (result_type_str == null) 1881 { 1882 return; 1883 } 1884 else if (result_type_str.equals(SEARCH_FILE)) 1885 { 1886 result_type = SearchField.RESULT_FILE; 1887 } 1888 else if (result_type_str.equals(SEARCH_DIRECTORY)) 1889 { 1890 result_type = SearchField.RESULT_DIRECTORY; 1891 } 1892 else if (result_type_str.equals(SEARCH_PARENTDIR)) 1893 { 1894 result_type = SearchField.RESULT_PARENTDIR; 1895 } 1896 else 1897 { 1898 return; 1899 } 1900 1901 filename = element.getAttribute(SEARCH_FILENAME); 1903 1904 check_filename = element.getAttribute(SEARCH_CHECKFILENAME); 1905 1906 Vector choices = element.getChildrenNamed(SEARCH_CHOICE); 1907 1908 if (choices == null) { return; } 1909 1910 for (int i = 0; i < choices.size(); i++) 1911 { 1912 XMLElement choice_el = (XMLElement) choices.elementAt(i); 1913 1914 if (!OsConstraint.oneMatchesCurrentSystem(choice_el)) continue; 1915 1916 String value = choice_el.getAttribute(SEARCH_VALUE); 1917 1918 combobox.addItem(value); 1919 1920 String set = ((XMLElement) choices.elementAt(i)).getAttribute(SET); 1921 if (set != null) 1922 { 1923 if (set != null && !"".equals(set)) 1924 { 1925 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 1926 set = vs.substitute(set, null); 1927 } 1928 if (set.equals(TRUE)) 1929 { 1930 combobox.setSelectedIndex(i); 1931 } 1932 } 1933 } 1934 } 1935 else 1940 { 1941 return; 1942 } 1943 1944 element = spec.getFirstChildNamed(DESCRIPTION); 1949 addDescription(element, forPacks, forOs); 1950 1951 TwoColumnConstraints westconstraint1 = new TwoColumnConstraints(); 1952 westconstraint1.position = TwoColumnConstraints.WEST; 1953 1954 uiElements.add(new Object [] { null, FIELD_LABEL, null, westconstraint1, label, forPacks, 1955 forOs}); 1956 1957 TwoColumnConstraints eastconstraint1 = new TwoColumnConstraints(); 1958 eastconstraint1.position = TwoColumnConstraints.EAST; 1959 1960 StringBuffer tooltiptext = new StringBuffer (); 1961 1962 if ((filename != null) && (filename.length() > 0)) 1963 { 1964 tooltiptext.append(MessageFormat.format(parentFrame.langpack 1965 .getString("UserInputPanel.search.location"), new String [] { filename})); 1966 } 1967 1968 boolean showAutodetect = (check_filename != null) && (check_filename.length() > 0); 1969 if (showAutodetect) 1970 { 1971 tooltiptext.append(MessageFormat.format(parentFrame.langpack 1972 .getString("UserInputPanel.search.location.checkedfile"), 1973 new String [] { check_filename})); 1974 } 1975 1976 if (tooltiptext.length() > 0) combobox.setToolTipText(tooltiptext.toString()); 1977 1978 uiElements.add(new Object [] { null, SEARCH_FIELD, variable, eastconstraint1, combobox, 1979 forPacks, forOs}); 1980 1981 JPanel buttonPanel = new JPanel (); 1982 buttonPanel.setLayout(new com.izforge.izpack.gui.FlowLayout( 1983 com.izforge.izpack.gui.FlowLayout.LEADING)); 1984 1985 JButton autodetectButton = ButtonFactory.createButton(parentFrame.langpack 1986 .getString("UserInputPanel.search.autodetect"), idata.buttonsHColor); 1987 autodetectButton.setVisible(showAutodetect); 1988 1989 autodetectButton.setToolTipText(parentFrame.langpack 1990 .getString("UserInputPanel.search.autodetect.tooltip")); 1991 1992 buttonPanel.add(autodetectButton); 1993 1994 JButton browseButton = ButtonFactory.createButton(parentFrame.langpack 1995 .getString("UserInputPanel.search.browse"), idata.buttonsHColor); 1996 1997 buttonPanel.add(browseButton); 1998 1999 TwoColumnConstraints eastonlyconstraint = new TwoColumnConstraints(); 2000 eastonlyconstraint.position = TwoColumnConstraints.EASTONLY; 2001 2002 uiElements.add(new Object [] { null, SEARCH_BUTTON_FIELD, null, eastonlyconstraint, 2003 buttonPanel, forPacks, forOs}); 2004 2005 searchFields.add(new SearchField(filename, check_filename, parentFrame, combobox, 2006 autodetectButton, browseButton, search_type, result_type)); 2007 } 2008 2009 2010 2019 2020 private boolean readSearch(Object [] field) 2021 { 2022 String variable = null; 2023 String value = null; 2024 JComboBox comboBox = null; 2025 2026 try 2027 { 2028 variable = (String ) field[POS_VARIABLE]; 2029 comboBox = (JComboBox ) field[POS_FIELD]; 2030 for (int i = 0; i < this.searchFields.size(); ++i) 2031 { 2032 SearchField sf = (SearchField) this.searchFields.elementAt(i); 2033 if (sf.belongsTo(comboBox)) 2034 { 2035 value = sf.getResult(); 2036 break; 2037 } 2038 } 2039 } 2040 catch (Throwable exception) 2041 { 2042 return (true); 2043 } 2044 if ((variable == null) || (value == null)) { return (true); } 2045 2046 idata.setVariable(variable, value); 2047 entries.add(new TextValuePair(variable, value)); 2048 return (true); 2049 } 2050 2051 2052 2057 2058 private void addText(XMLElement spec) 2059 { 2060 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 2061 Vector forOs = spec.getChildrenNamed(OS); 2062 2063 addDescription(spec, forPacks, forOs); 2064 } 2065 2066 2067 2073 2074 private void addSpace(XMLElement spec) 2075 { 2076 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 2077 Vector forOs = spec.getChildrenNamed(OS); 2078 JPanel panel = new JPanel (); 2079 2080 TwoColumnConstraints constraints = new TwoColumnConstraints(); 2081 constraints.position = TwoColumnConstraints.BOTH; 2082 constraints.stretch = true; 2083 2084 uiElements 2085 .add(new Object [] { null, SPACE_FIELD, null, constraints, panel, forPacks, forOs}); 2086 } 2087 2088 2089 2094 2095 private void addDivider(XMLElement spec) 2096 { 2097 Vector forPacks = spec.getChildrenNamed(SELECTEDPACKS); 2098 Vector forOs = spec.getChildrenNamed(OS); 2099 JPanel panel = new JPanel (); 2100 String alignment = spec.getAttribute(ALIGNMENT); 2101 2102 if (alignment != null) 2103 { 2104 if (alignment.equals(TOP)) 2105 { 2106 panel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.gray)); 2107 } 2108 else 2109 { 2110 panel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.gray)); 2111 } 2112 } 2113 else 2114 { 2115 panel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.gray)); 2116 } 2117 2118 TwoColumnConstraints constraints = new TwoColumnConstraints(); 2119 constraints.position = TwoColumnConstraints.BOTH; 2120 constraints.stretch = true; 2121 2122 uiElements.add(new Object [] { null, DIVIDER_FIELD, null, constraints, panel, forPacks, 2123 forOs}); 2124 } 2125 2126 2127 2132 2133 private void addDescription(XMLElement spec, Vector forPacks, Vector forOs) 2134 { 2135 String description; 2136 TwoColumnConstraints constraints = new TwoColumnConstraints(); 2137 constraints.position = TwoColumnConstraints.BOTH; 2138 constraints.stretch = true; 2139 2140 if (spec != null) 2141 { 2142 description = getText(spec); 2143 2144 if (description != null) 2146 { 2147 String alignment = spec.getAttribute(ALIGNMENT); 2148 int justify = MultiLineLabel.LEFT; 2149 2150 if (alignment != null) 2151 { 2152 if (alignment.equals(LEFT)) 2153 { 2154 justify = MultiLineLabel.LEFT; 2155 } 2156 else if (alignment.equals(CENTER)) 2157 { 2158 justify = MultiLineLabel.CENTER; 2159 } 2160 else if (alignment.equals(RIGHT)) 2161 { 2162 justify = MultiLineLabel.RIGHT; 2163 } 2164 } 2165 2166 javax.swing.JTextPane label = new javax.swing.JTextPane (); 2167 2168 label.setEditable(false); 2170 2171 if(description.startsWith("<html>") && description.endsWith("</html>")) 2174 label.setContentType("text/html"); 2175 label.setText(description); 2176 2177 label.setBackground(javax.swing.UIManager.getColor("label.backgroud")); 2179 label.setMargin(new java.awt.Insets (3, 0, 3, 0)); 2180 2181 uiElements.add(new Object [] { null, DESCRIPTION, null, constraints, label, 2182 forPacks, forOs}); 2183 } 2184 } 2185 } 2186 2187 2188 2204 2205 private boolean getBoolean(XMLElement element, String attribute, boolean defaultValue) 2206 { 2207 boolean result = defaultValue; 2208 2209 if ((attribute != null) && (attribute.length() > 0)) 2210 { 2211 String value = element.getAttribute(attribute); 2212 2213 if (value != null) 2214 { 2215 if (value.equals(TRUE)) 2216 { 2217 result = true; 2218 } 2219 else if (value.equals(FALSE)) 2220 { 2221 result = false; 2222 } 2223 } 2224 } 2225 2226 return (result); 2227 } 2228 2229 2230 2241 2242 2259 2270 2271 private float getFloat(XMLElement element, String attribute, float defaultValue) 2272 { 2273 float result = defaultValue; 2274 2275 if ((attribute != null) && (attribute.length() > 0)) 2276 { 2277 try 2278 { 2279 result = Float.parseFloat(element.getAttribute(attribute)); 2280 } 2281 catch (Throwable exception) 2282 {} 2283 } 2284 2285 return (result); 2286 } 2287 2288 2289 2299 2300 private String getText(XMLElement element) 2301 { 2302 if (element == null) { return (null); } 2303 2304 String key = element.getAttribute(KEY); 2305 String text = null; 2306 2307 if ((key != null) && (langpack != null)) 2308 { 2309 try 2310 { 2311 text = langpack.getString(key); 2312 } 2313 catch (Throwable exception) 2314 { 2315 text = null; 2316 } 2317 } 2318 2319 if (text == null) 2324 { 2325 text = element.getAttribute(TEXT); 2326 } 2327 2328 return (text); 2329 } 2330 2331 2332 2345 2346 private int getAlignment(XMLElement element) 2347 { 2348 int result = TwoColumnConstraints.LEFT; 2349 2350 String value = element.getAttribute(ALIGNMENT); 2351 2352 if (value != null) 2353 { 2354 if (value.equals(LEFT)) 2355 { 2356 result = TwoColumnConstraints.LEFT; 2357 } 2358 else if (value.equals(CENTER)) 2359 { 2360 result = TwoColumnConstraints.CENTER; 2361 } 2362 else if (value.equals(RIGHT)) 2363 { 2364 result = TwoColumnConstraints.RIGHT; 2365 } 2366 } 2367 2368 return (result); 2369 } 2370 2371 2383 public boolean itemRequiredForOs(Vector os) 2384 { 2385 if (os.size() == 0) { return true; } 2386 2387 for (int i = 0; i < os.size(); i++) 2388 { 2389 String family = ((XMLElement) os.elementAt(i)).getAttribute(FAMILY); 2390 boolean match = false; 2391 2392 if ("windows".equals(family)) 2393 { 2394 match = OsVersion.IS_WINDOWS; 2395 } 2396 else if ("mac".equals(family)) 2397 { 2398 match = OsVersion.IS_OSX; 2399 } 2400 else if ("unix".equals(family)) 2401 { 2402 match = OsVersion.IS_UNIX; 2403 } 2404 if (match) { return true; } 2405 } 2406 return false; 2407 } 2408 2409 2410 2424 2425 2432 private boolean itemRequiredFor(Vector packs) 2433 { 2434 2435 String selected; 2436 String required; 2437 2438 if (packs.size() == 0) { return (true); } 2439 2440 2450 for (int i = 0; i < idata.selectedPacks.size(); i++) 2455 { 2456 selected = ((Pack) idata.selectedPacks.get(i)).name; 2457 2458 for (int k = 0; k < packs.size(); k++) 2459 { 2460 required = (String ) ((XMLElement) packs.elementAt(k)).getAttribute(NAME, ""); 2461 if (selected.equals(required)) { return (true); } 2462 } 2463 } 2464 2465 return (false); 2466 } 2467 2468 2469 2483 2484 2491 private boolean itemRequiredForUnselected(Vector packs) 2492 { 2493 2494 String selected; 2495 String required; 2496 2497 if (packs.size() == 0) { return (true); } 2498 2499 for (int i = 0; i < idata.selectedPacks.size(); i++) 2504 { 2505 selected = ((Pack) idata.selectedPacks.get(i)).name; 2506 2507 for (int k = 0; k < packs.size(); k++) 2508 { 2509 required = (String ) ((XMLElement) packs.elementAt(k)).getAttribute(NAME, ""); 2510 if (selected.equals(required)) { return (false); } 2511 } 2512 } 2513 2514 return (true); 2515 } 2516 2517 2523 protected Vector getUiElements() 2524 { 2525 return uiElements; 2526 } 2527 2528 2532 2533 2536 2537 private static class TextValuePair 2538 { 2539 2540 private String text = ""; 2541 2542 private String value = ""; 2543 2544 2545 2551 2552 public TextValuePair(String text, String value) 2553 { 2554 this.text = text; 2555 this.value = value; 2556 } 2557 2558 2559 2564 2565 public void setText(String text) 2566 { 2567 this.text = text; 2568 } 2569 2570 2571 2576 2577 public void setValue(String value) 2578 { 2579 this.value = value; 2580 } 2581 2582 2583 2588 2589 public String toString() 2590 { 2591 return (text); 2592 } 2593 2594 2595 2600 2601 public String getValue() 2602 { 2603 return (value); 2604 } 2605 } 2606 2607 2608 2614 2615 2616 private class SearchField implements ActionListener 2617 { 2618 2619 2620 public static final int TYPE_DIRECTORY = 1; 2621 2622 2623 public static final int TYPE_FILE = 2; 2624 2625 2626 public static final int RESULT_DIRECTORY = 1; 2627 2628 2629 public static final int RESULT_FILE = 2; 2630 2631 2632 public static final int RESULT_PARENTDIR = 3; 2633 2634 private String filename = null; 2635 2636 private String checkFilename = null; 2637 2638 private JButton autodetectButton = null; 2639 2640 private JButton browseButton = null; 2641 2642 private JComboBox pathComboBox = null; 2643 2644 private int searchType = TYPE_DIRECTORY; 2645 2646 private int resultType = RESULT_DIRECTORY; 2647 2648 private InstallerFrame parent = null; 2649 2650 2651 2668 2669 public SearchField(String filename, String checkFilename, InstallerFrame parent, 2670 JComboBox combobox, JButton autobutton, JButton browsebutton, int search_type, 2671 int result_type) 2672 { 2673 this.filename = filename; 2674 this.checkFilename = checkFilename; 2675 this.parent = parent; 2676 this.autodetectButton = autobutton; 2677 this.browseButton = browsebutton; 2678 this.pathComboBox = combobox; 2679 this.searchType = search_type; 2680 this.resultType = result_type; 2681 2682 this.autodetectButton.addActionListener(this); 2683 this.browseButton.addActionListener(this); 2684 2685 autodetect(); 2686 } 2687 2688 2692 public boolean belongsTo(JComboBox combobox) 2693 { 2694 return (this.pathComboBox == combobox); 2695 } 2696 2697 2698 private boolean pathMatches(String path) 2699 { 2700 if (path != null) 2701 { 2704 File file = null; 2705 2706 if ((this.filename == null) || (this.searchType == TYPE_DIRECTORY)) 2707 { 2708 file = new File (path); 2709 } 2710 else 2711 { 2712 file = new File (path, this.filename); 2713 } 2714 2715 if (file.exists()) 2716 { 2717 2718 if (((this.searchType == TYPE_DIRECTORY) && (file.isDirectory())) 2719 || ((this.searchType == TYPE_FILE) && (file.isFile()))) 2720 { 2721 if (this.checkFilename == null) return true; 2723 2724 file = new File (file, this.checkFilename); 2725 2726 return file.exists(); 2727 } 2728 2729 } 2730 2731 } return false; 2734 } 2735 2736 2737 public boolean autodetect() 2738 { 2739 Vector items = new Vector (); 2740 2741 2744 String selected = (String ) this.pathComboBox.getSelectedItem(); 2745 if (selected == null) { return false; } 2746 boolean found = false; 2747 for (int x = 0; x < this.pathComboBox.getItemCount(); x++) 2748 { 2749 if (((String ) this.pathComboBox.getItemAt(x)).equals(selected)) 2750 { 2751 found = true; 2752 } 2753 } 2754 if (!found) 2755 { 2756 this.pathComboBox.addItem(this.pathComboBox.getSelectedItem()); 2758 } 2759 2760 for (int i = 0; i < this.pathComboBox.getItemCount(); ++i) 2765 { 2766 String path = (String ) this.pathComboBox.getItemAt(i); 2767 2768 if (path.endsWith("*")) 2769 { 2770 path = path.substring(0, path.length() - 1); 2771 File dir = new File (path); 2772 2773 if (dir.isDirectory()) 2774 { 2775 File [] subdirs = dir.listFiles(); 2776 for (int x = 0; x < subdirs.length; x++) 2777 { 2778 String search = subdirs[x].getAbsolutePath(); 2779 if (this.pathMatches(search)) 2780 { 2781 items.add(search); 2782 } 2783 } 2784 } 2785 } 2786 else 2787 { 2788 if (this.pathMatches(path)) 2789 { 2790 items.add(path); 2791 } 2792 } 2793 } 2794 items = new Vector (new HashSet (items)); 2796 2797 this.pathComboBox.removeAllItems(); 2800 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 2801 for (int i = 0; i < items.size(); i++) 2802 { 2803 this.pathComboBox.addItem(vs.substitute((String ) items.get(i), "plain")); 2804 } 2805 2806 for (int i = 0; i < this.pathComboBox.getItemCount(); ++i) 2808 { 2809 String path = (String ) this.pathComboBox.getItemAt(i); 2810 2811 if (this.pathMatches(path)) 2812 { 2813 this.pathComboBox.setSelectedIndex(i); 2814 return true; 2815 } 2816 2817 } 2818 2819 return this.pathMatches((String ) this.pathComboBox.getSelectedItem()); 2821 2822 } 2823 2824 2825 2830 2831 public void actionPerformed(ActionEvent event) 2832 { 2833 2835 if (event.getSource() == this.autodetectButton) 2836 { 2837 if (!autodetect()) 2838 JOptionPane.showMessageDialog(parent, parent.langpack 2839 .getString("UserInputPanel.search.autodetect.failed.message"), 2840 parent.langpack 2841 .getString("UserInputPanel.search.autodetect.failed.caption"), 2842 JOptionPane.WARNING_MESSAGE); 2843 } 2844 else if (event.getSource() == this.browseButton) 2845 { 2846 JFileChooser chooser = new JFileChooser (); 2847 2848 if (this.resultType != TYPE_FILE) 2849 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 2850 2851 int result = chooser.showOpenDialog(this.parent); 2852 2853 if (result == JFileChooser.APPROVE_OPTION) 2854 { 2855 File f = chooser.getSelectedFile(); 2856 2857 this.pathComboBox.setSelectedItem(f.getAbsolutePath()); 2858 2859 if (this.resultType != TYPE_FILE && !this.pathMatches(f.getAbsolutePath())) 2861 { 2862 JOptionPane.showMessageDialog(parent, parent.langpack 2863 .getString("UserInputPanel.search.wrongselection.message"), 2864 parent.langpack 2865 .getString("UserInputPanel.search.wrongselection.caption"), 2866 JOptionPane.WARNING_MESSAGE); 2867 } 2868 } 2869 2870 } 2871 2872 } 2874 2875 2876 2884 2885 public String getResult() 2886 { 2887 String item = (String ) this.pathComboBox.getSelectedItem(); 2888 if (item != null) item = item.trim(); 2889 String path = item; 2890 2891 File f = new File (item); 2892 2893 if (!f.isDirectory()) 2894 { 2895 path = f.getParent(); 2896 } 2897 2898 if (this.resultType == RESULT_DIRECTORY) 2900 { 2901 return path; 2902 } 2903 else if (this.resultType == RESULT_FILE) 2904 { 2905 if (this.filename != null) 2906 { 2907 return path + File.separatorChar + this.filename; 2908 } 2909 else 2910 { 2911 return item; 2912 } 2913 } 2914 else if (this.resultType == RESULT_PARENTDIR) 2915 { 2916 File dir = new File (path); 2917 return dir.getParent(); 2918 } 2919 2920 return null; 2921 } 2922 2923 } 2925 protected void updateVariables() 2926 { 2927 2930 Vector variables = spec.getChildrenNamed(VARIABLE_NODE); 2931 RulesEngine rules = parent.getRules(); 2932 2933 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 2934 for (int i = 0; i < variables.size(); i++) 2935 { 2936 XMLElement variable = (XMLElement) variables.elementAt(i); 2937 String vname = variable.getAttribute(ATTRIBUTE_VARIABLE_NAME); 2938 String vvalue = variable.getAttribute(ATTRIBUTE_VARIABLE_VALUE); 2939 String conditionid = variable.getAttribute(ATTRIBUTE_CONDITIONID_NAME); 2940 if (conditionid != null) 2941 { 2942 if (!rules.isConditionTrue(conditionid, idata.getVariables())) 2944 { 2945 continue; 2946 } 2947 } 2948 if (OsConstraint.oneMatchesCurrentSystem(variable)) 2950 { 2951 if (vname == null) 2952 { 2953 continue; 2954 } 2955 else 2956 { 2957 if (vvalue != null) 2959 { 2960 vvalue = vs.substitute(vvalue, null); 2962 idata.setVariable(vname, ""); 2964 vvalue = vs.substitute(vvalue, null); 2965 } 2966 idata.setVariable(vname, vvalue); 2968 2969 } 2971 } 2972 } 2973 } 2974 2975} 2977 2978 | Popular Tags |