1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import javax.swing.border.LineBorder ; 5 import javax.swing.border.TitledBorder ; 6 import java.awt.*; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.ActionListener ; 9 import java.io.*; 10 import java.util.Vector ; 11 12 41 42 43 public class CBAutoGUI extends JDialog implements ActionListener 44 { 45 String commandName; Vector menuItems; Vector commonItems; 49 final JEditorPane output = new JEditorPane(); JTabbedPane tabPane; CBPanel commonOptions; PrimaryOption common; 54 static String lastDirectory = ""; 56 boolean guiInitialised = false; 57 58 boolean debug = true; 59 60 static boolean standAlone = false; 61 62 65 66 public static void main(String args[]) 67 { 68 standAlone = true; 69 70 String fileName = "keytool.txt"; 71 if (args.length >= 1) 72 fileName = args[0]; 73 74 CBAutoGUI MrGui = new CBAutoGUI(null, fileName); 75 MrGui.show(null); 76 } 77 78 89 90 public CBAutoGUI(Frame owner, String fileName) 91 { 92 super(owner); 93 94 menuItems = new Vector (8); 95 commonItems = new Vector (8); 96 common = new PrimaryOption("common options"); 97 98 processFile(fileName); 99 100 setSize(650, 550); 101 } 102 103 108 109 static Object getNamedObject(Vector v, String s) 110 { 111 s = s.trim(); 112 for (int i = 0; i < v.size(); i++) 113 { 114 if (v.get(i).toString().trim().equalsIgnoreCase(s)) 115 return v.get(i); 116 } 117 return null; 118 } 119 120 121 127 128 public void setOption(String optionName) 129 { 130 if (optionName == null || optionName.length() == 0) 131 { 132 if (tabPane.getTabCount() > 0) 133 optionName = tabPane.getTitleAt(0).trim(); 134 else 135 { 136 error("no tabs set! - nothing to display"); 137 return; 138 } 139 } 140 141 optionName = optionName.trim(); 142 143 if (optionName.length() == 0) return; 144 if (optionName.charAt(0) == '-') 145 optionName = optionName.substring(1); 146 if (optionName.length() == 0) return; 147 148 for (int i = 0; i < tabPane.getTabCount(); i++) 149 { 150 if (optionName.equalsIgnoreCase(tabPane.getTitleAt(i).trim())) 151 { 152 tabPane.setSelectedIndex(i); 153 return; 154 } 155 } 156 } 157 158 168 169 public void setDefaultValue(String primaryOptionName, String secondaryOptionName, String defaultValue) 170 { 171 173 primaryOptionName = formatOptionName(primaryOptionName); 174 secondaryOptionName = formatOptionName(secondaryOptionName); 175 176 PrimaryOption primary = (PrimaryOption) getNamedObject(menuItems, primaryOptionName); 177 if (primary == null) 178 primary = common; 179 180 SecondaryOption secondary = primary.get(secondaryOptionName); 181 182 if (secondary != null) 183 secondary.setDefaultValue(defaultValue); 184 } 185 186 192 193 public void show(String option) 194 { 195 if (debug) System.out.println("showing GUI with option : " + ((option == null) ? " (no tab set) " : option)); 196 197 if (guiInitialised == false) 198 initialiseGUI(); 199 else 200 clearPasswords(); 201 202 setOption(option); 203 setVisible(true); 204 } 205 206 212 213 public void initialiseGUI() 214 { 215 if (debug) System.out.println("initialising GUI"); 216 217 setTitle("GUI interface to " + commandName); 218 219 CBPanel display = new CBPanel(); 220 221 addTabbedPanes(display); 222 223 addCommonOptions(display); 224 225 226 JPanel buttons = new JPanel(); 227 JButton OK, Cancel, Help; 228 229 display.makeWide(); 230 display.add(new JLabel("")); display.makeLight(); 232 display.add(OK = new JButton("Execute")); 233 display.add(Cancel = new JButton("Cancel")); 234 display.makeWide(); 236 display.addLine(new JLabel("")); display.makeHeavy(); 238 239 OK.addActionListener(this); 240 Cancel.addActionListener(this); 241 243 output.setBorder(new TitledBorder (new LineBorder (Color.black, 2), "output")); 245 output.setPreferredSize(new Dimension(400, 150)); 246 247 display.addLine(new JScrollPane(output)); 248 249 getContentPane().add(display); 250 setVisible(true); 251 guiInitialised = true; 252 } 253 254 259 260 public void addTabbedPanes(CBPanel display) 261 { 262 if (menuItems.size() == 0) return; 264 tabPane = new JTabbedPane(); 265 266 for (int i = 0; i < menuItems.size(); i++) 267 { 268 PrimaryOption primary = (PrimaryOption) menuItems.get(i); 269 CBPanel panel = new CBPanel(); 270 panel.makeLight(); 271 tabPane.addTab(primary.toString(), panel); 272 273 boolean newLine = false; 274 for (int j = 0; j < primary.size(); j++) 275 { 276 SecondaryOption secondary = primary.get(j); 277 if (secondary.isLabel()) 278 { 279 addSecondaryToPanel(panel, secondary, newLine); 280 newLine = false; 281 } 282 else 283 { 284 addSecondaryToPanel(panel, secondary, newLine); 285 newLine = !newLine; 286 } 287 } 288 290 panel.newLine(); 291 panel.add(new JLabel(" ")); panel.makeHeavy(); 293 panel.addWide(new JLabel(" "), 5); } 295 display.addLine(tabPane); 296 } 297 298 305 306 public void addCommonOptions(CBPanel display) 307 { 308 if (commonItems.size() == 0) return; 310 commonOptions = new CBPanel(); 311 for (int i = 0; i < commonItems.size(); i++) 312 { 313 SecondaryOption secondary = (SecondaryOption) commonItems.get(i); 314 addSecondaryToPanel(commonOptions, secondary, ((i % 2) != 0)); 315 } 316 318 commonOptions.newLine(); 319 commonOptions.add(new JLabel(" ")); commonOptions.makeHeavy(); 321 commonOptions.addWide(new JLabel(" "), 5); 323 display.addLine(commonOptions); 324 } 325 326 void clearPasswords() 327 { 328 for (int i = 0; i < tabPane.getTabCount(); i++) 329 { 330 Component c = tabPane.getComponent(i); 331 clearPasswords(c); 332 } 333 clearPasswords(commonOptions); 334 } 335 336 void clearPasswords(Component c) 337 { 338 if (c instanceof Container) 339 { 340 Container con = (Container) c; 341 for (int i = 0; i < con.getComponentCount(); i++) 342 { 343 Component comp = con.getComponent(i); 344 if (comp instanceof JPasswordField) 345 { 346 ((JPasswordField) comp).setText(""); 347 } 348 } 349 } 350 } 351 352 void addSecondaryToPanel(CBPanel panel, SecondaryOption secondary, boolean newLine) 353 { 354 if (secondary.isHidden()) return; 356 JComponent comp1 = null; 357 JComponent comp2 = null; 358 CBFileChooserButton fileChooser = null; 359 360 String defaultValue = secondary.getDefaultValue(); 362 if (secondary.isLabel()) 363 { 364 JLabel label = new JLabel(secondary.toString()); 365 Font current = label.getFont(); 366 label.setFont(new Font(current.getName(), Font.BOLD, current.getSize() + 2)); 367 panel.addLine(label); 368 373 newLine = true; return; 375 } 376 else if (secondary.isPassword()) 377 { 378 panel.add(comp1 = new JLabel(secondary.toString())); 379 panel.addGreedyWide(comp2 = new JPasswordField(), 2); 380 } 381 else if (secondary.isCheckBox()) 382 { 383 panel.add(comp1 = new JLabel(secondary.toString())); 384 panel.add(comp2 = new JCheckBox()); 385 if ("".equals(defaultValue) == false) 386 ((JCheckBox) comp2).setSelected(true); 387 panel.add(new JLabel("")); 388 } 389 else if (secondary.isFile()) 390 { 391 panel.add(comp1 = new JLabel(secondary.toString())); 392 panel.addGreedyWide(comp2 = new JTextField(defaultValue, 30)); 393 panel.add(fileChooser = new CBFileChooserButton((JTextField) comp2, this, "File")); 394 if (defaultValue.length() > 0) 395 { 396 fileChooser.setLocalDirectoryUse(true); 397 fileChooser.setStartingDirectory(defaultValue); 398 } 399 } 400 else 401 { 402 panel.add(comp1 = new JLabel(secondary.toString())); 403 panel.addGreedyWide(comp2 = new JTextField(defaultValue, 40), 2); 404 } 405 406 if (comp1 != null) comp1.setToolTipText(secondary.getTip()); 407 409 if (newLine) 410 panel.newLine(); 411 } 412 413 414 void processFile(String fileName) 415 { 416 if (debug) System.out.println("processing file '" + fileName + "'"); 417 418 PrimaryOption currentItem = null; 419 try 420 { 421 BufferedReader readText = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); 422 423 String line = ""; int count = 0; 425 while ((line = readText.readLine()) != null) 426 { 427 line = line.trim(); 428 if (line.length() == 0 || line.charAt(0) == '#') 429 { 430 } 432 else if (commandName == null) { 434 if (debug) System.out.println("read commandName as '" + line + "'"); 435 commandName = line; 436 } 437 else if (line.toLowerCase().startsWith("primary: ")) 438 { 439 line = line.substring(9).trim(); 440 if (debug) System.out.println("read primary option as '" + line + "'"); 441 menuItems.add((currentItem = new PrimaryOption(line))); 442 } 443 else if (line.toLowerCase().startsWith("common: ")) 444 { 445 line = line.substring(8).trim(); 446 if (debug) System.out.println("read common option as '" + line + "'"); 447 SecondaryOption op = getSecondaryOption(line); 448 common.add(op); 449 commonItems.add(op); 450 } 451 else if (line.toLowerCase().startsWith("secondary: ") && currentItem != null) 452 { 453 line = line.substring(11).trim(); 454 if (debug) System.out.println("read secondary option as '" + line + "'"); 455 currentItem.add(getSecondaryOption(line)); 456 } 457 else 458 error("WARNING: ignoring line '" + line + "'"); 459 } 460 } 461 catch (Exception e) 462 { 463 error("unable to open file:\n" + e.toString()); 464 e.printStackTrace(); 465 } 466 } 467 468 SecondaryOption getSecondaryOption(String line) 469 { 470 int pos = line.indexOf(' '); 471 if (pos < 0) pos = line.length(); 472 473 String name = line.substring(0, pos); 474 475 boolean isPwd = false; 476 boolean hasArg = true; 477 boolean isLabel = false; 478 boolean isHidden = false; 479 boolean isFile = false; 480 481 String tooltip = null; 482 String defaultValue = null; 483 484 if (line.charAt(0) == '\"') 485 { 486 isLabel = true; 487 name = line.substring(1, line.length() - 1); } 489 else if (pos != line.length()) 490 { 491 String lowC = line.substring(pos).toLowerCase(); 492 493 if (lowC.indexOf("tip=") != -1) { 496 pos = lowC.indexOf("tip="); 497 pos = line.indexOf("\"", pos); 498 int pos2 = line.indexOf("\"", pos + 1); 499 if (pos2 == -1) pos2 = line.length(); tooltip = line.substring(pos + 1, pos2); 501 lowC = line.substring(0, pos).toLowerCase() + line.substring(pos2); } 503 if (lowC.indexOf("default=") != -1) { 505 pos = lowC.indexOf("default="); 506 pos = line.indexOf("\"", pos); 507 int pos2 = line.indexOf("\"", pos + 1); 508 if (pos2 == -1) pos2 = line.length(); defaultValue = line.substring(pos + 1, pos2); 510 lowC = line.substring(0, pos).toLowerCase() + line.substring(pos2); } 512 if (lowC.indexOf("nostring") != -1) hasArg = false; 513 if (lowC.indexOf("ispassword") != -1) isPwd = true; 514 if (lowC.indexOf("hidden") != -1) isHidden = true; 515 if (lowC.indexOf("file") != -1) isFile = true; 516 } 517 518 return (new SecondaryOption(name, isPwd, hasArg, isLabel, isHidden, isFile, tooltip, defaultValue)); 519 } 520 521 522 525 526 public void error(String msg) 527 { 528 System.err.println(msg); 529 } 530 531 public void actionPerformed(ActionEvent e) 532 { 533 String cmd = e.getActionCommand().trim(); 534 if (cmd.equalsIgnoreCase("Execute")) 535 { 536 execute(); 537 } 538 else if (cmd.equalsIgnoreCase("Cancel")) 539 { 540 cancel(); 541 } 542 else if (cmd.equalsIgnoreCase("Help")) 543 { 544 help(); 545 } 546 else 547 error("Unknown command in OpenConWin\n" + cmd); 548 } 549 550 public void execute() 551 { 552 execute(2000); } 554 555 public void execute(int millisecTimeout) 556 { 557 final int timeout = millisecTimeout; 558 559 CBPanel current = (CBPanel) tabPane.getSelectedComponent(); 560 Vector args = new Vector (); 561 String title = tabPane.getTitleAt(tabPane.getSelectedIndex()); 563 PrimaryOption command = (PrimaryOption) getNamedObject(menuItems, title); 564 args.add(command.toCommandString()); 565 566 Component[] comps = current.getComponents(); 567 568 setArguments(args, comps, command); 569 570 comps = commonOptions.getComponents(); 571 572 setArguments(args, comps, common); 573 574 String commandString = commandName; 575 576 if (commandString.startsWith("%JAVA%")) 577 { 578 commandString = System.getProperty("java.home") + System.getProperty("file.separator") + 579 "bin" + System.getProperty("file.separator") + commandString.substring(6); 580 } 581 582 final String finalName = commandString + " " + command.toCommandString(); 583 584 for (int i = 0; i < args.size(); i++) 585 commandString += " " + args.get(i).toString(); 586 587 final String finalCommand = commandString; 588 589 output.setText("running command:\n " + finalName); 590 591 final Thread worker = new Thread ("Execute " + finalName) { 593 public void run() 594 { 595 try 596 { 597 Runtime runMe = Runtime.getRuntime(); 598 Process goBoy = runMe.exec(finalCommand); 599 output.read(goBoy.getInputStream(), "text"); 601 output.setText(output.getText() + "\n\nCommand " + finalName + " Executed.\n"); 602 } 603 catch (IOException e) 604 { 605 output.setText(output.getText() + "\n\nIOException reading command Output\n" + e.toString()); 606 } 607 } 608 }; 609 610 612 Thread waiter = new Thread ("Execute Watcher for " + finalCommand) 613 { 614 public void run() 615 { 616 try 617 { 618 (Thread.currentThread()).sleep(timeout); 619 620 if (worker != null && worker.isAlive()) 621 { 622 worker.interrupt(); 623 try 624 { 625 (Thread.currentThread()).sleep(10); 626 } catch (InterruptedException e) 628 { 629 } 630 631 638 output.setText(output.getText() + "\n\nError - unable to complete command " + finalName + "\n - request timed out in " + timeout + " milliseconds.\n"); 639 } 640 } 641 catch (InterruptedException intEx) 642 { 643 } 644 } 645 }; 646 647 worker.start(); 648 waiter.start(); 649 } 650 651 void setArguments(Vector args, Component[] comps, PrimaryOption command) 652 { 653 Component oldC = comps[0]; 654 655 for (int i = 1; i < comps.length; i++) 656 { 657 Component newC = comps[i]; 658 if (oldC instanceof JLabel) 659 { 660 String newArg = null; 661 662 if (newC instanceof JPasswordField) 663 { 664 JPasswordField temp = (JPasswordField) newC; 665 char[] pass = temp.getPassword(); 666 if (pass.length > 0) 667 { 668 String hack = new String (pass); if (hack.trim().length() > 0) newArg = hack.trim(); 671 } 672 } 673 else if (newC instanceof JTextField) 674 { 675 JTextField temp = (JTextField) newC; 676 if (temp.getText().trim().length() > 0) 677 newArg = temp.getText().trim(); 678 679 } 680 else if (newC instanceof JCheckBox) 681 { 682 JCheckBox temp = (JCheckBox) newC; 683 if (temp.isSelected()) 684 newArg = ""; } 686 687 if (newArg != null) 688 { 689 String secondaryOptionName = ((JLabel) oldC).getText().trim(); 690 SecondaryOption opt = command.get(secondaryOptionName); 691 if (opt == null) 692 error("Error - unknown option " + secondaryOptionName); 693 else 694 { 695 args.add(opt.toCommandString()); 696 args.add(newArg); 697 } 698 } 699 } 700 oldC = newC; 701 } 702 703 Vector hiddenOps = command.getHiddenOptions(); 704 for (int i = 0; i < hiddenOps.size(); i++) 705 { 706 SecondaryOption opt = (SecondaryOption) hiddenOps.get(i); 707 args.add(opt.toCommandString()); 708 } 709 } 710 711 public void cancel() 712 { 713 setVisible(false); 714 dispose(); 715 if (standAlone) System.exit(0); 716 } 717 718 public void help() 720 { 721 722 } 723 724 public static String formatOptionName(String name) 725 { 726 if (name == null || name.length() == 0) 727 return ""; 728 else 729 return (" " + ((name.charAt(0) == '-') ? name.substring(1) : name)); 730 } 731 732 735 abstract class Option 736 { 737 String name; String tip = ""; 740 public Option(String optionString) 741 { 742 name = optionString; 743 } 744 745 747 public String toCommandString() 748 { 749 return name; 750 } 751 752 public String getName() 753 { 754 return name; 755 } 756 757 public void setTip(String t) 758 { 759 if (t != null) tip = t; 760 } 761 762 public String getTip() 763 { 764 return tip; 765 } 766 767 public String toString() 768 { 769 return formatOptionName(name); 770 } 771 } 772 773 777 778 class PrimaryOption extends Option 779 { 780 Vector options; 781 782 public PrimaryOption(String optionName) 783 { 784 super(optionName); 785 options = new Vector (8); 786 } 787 788 public void add(SecondaryOption option) 789 { 790 options.add(option); 791 } 792 793 public int size() 794 { 795 return options.size(); 796 } 797 798 public SecondaryOption get(int i) 799 { 800 return (SecondaryOption) options.get(i); 801 } 802 803 807 public SecondaryOption get(String s) 808 { 809 SecondaryOption op = (SecondaryOption) CBAutoGUI.getNamedObject(options, s); 810 if (op == null && s.charAt(0) != '-') 811 op = (SecondaryOption) CBAutoGUI.getNamedObject(options, "-" + s); 812 return op; 813 } 814 815 public Vector getHiddenOptions() 816 { 817 Vector ret = new Vector (); 818 for (int i = 0; i < options.size(); i++) 819 { 820 SecondaryOption test = (SecondaryOption) options.get(i); 821 if (test.isHidden()) 822 ret.add(test); 823 } 824 return ret; 825 } 826 827 public String toCommandString() 828 { 829 return name; 830 } 831 832 } 833 834 839 840 class SecondaryOption extends Option 841 { 842 boolean password; 843 boolean label; 844 boolean hasArgument; 845 boolean hidden; 846 boolean file; 847 String defaultValue = ""; 848 849 861 public SecondaryOption(String optionString, boolean pwd, boolean arg, boolean lbl, boolean hide, boolean isFile, String tooltip, String defVal) 862 { 863 super(optionString); 864 password = pwd; 865 hasArgument = arg; 866 label = lbl; 867 hidden = hide; 868 file = isFile; 869 if (tooltip != null) 870 tip = tooltip; 871 if (defVal != null) 872 defaultValue = defVal; 873 } 874 875 public void setDefaultValue(String s) 877 { 878 if (defaultValue != null) defaultValue = s; 879 } 880 881 public String getDefaultValue() 882 { 883 return (defaultValue == null) ? "" : defaultValue; 884 } 885 886 public String toCommandString() 887 { 888 return name; 889 } 890 891 public boolean isPassword() 892 { 893 return password; 894 } 895 896 public boolean isLabel() 897 { 898 return label; 899 } 900 901 public boolean isCheckBox() 902 { 903 return !hasArgument; 904 } 905 906 public boolean isHidden() 907 { 908 return hidden; 909 } 910 911 public boolean isFile() 912 { 913 return file; 914 } 915 916 } 917 } | Popular Tags |