| 1 19 20 package ca.mcgill.sable.soot.ui; 21 22 23 import java.util.*; 24 import org.eclipse.jface.dialogs.*; 25 import org.eclipse.jface.viewers.*; 26 import org.eclipse.swt.custom.*; 27 import org.eclipse.swt.events.*; 28 import org.eclipse.swt.widgets.*; 29 import org.eclipse.swt.layout.*; 30 import org.eclipse.swt.*; 31 import ca.mcgill.sable.soot.launching.SavedConfigManager; 32 import ca.mcgill.sable.soot.launching.SootSavedConfiguration; 33 import ca.mcgill.sable.soot.testing.*; 34 35 36 public abstract class AbstractOptionsDialog extends TitleAreaDialog implements ISelectionChangedListener { 37 38 private SashForm sashForm; 39 private TreeViewer treeViewer; 40 private Composite pageContainer; 41 private HashMap config; 42 private String configName; 43 private HashMap editMap; 44 private boolean canRun = true; 45 private HashMap radioGroups; 46 private ArrayList enableGroups; 47 private HashMap eclipseDefList; 48 private HashMap defList; 49 private CheckboxTableViewer tableViewer; 50 private Button addButton; 51 private Button removeButton; 52 private String sootMainClass; 53 54 58 public AbstractOptionsDialog(Shell parentShell) { 59 super(parentShell); 60 this.setShellStyle(SWT.RESIZE); 61 } 62 63 public void addToEclipseDefList(String key, Object val) { 64 if (getEclipseDefList() == null) { 65 setEclipseDefList(new HashMap()); 66 } 67 getEclipseDefList().put(key, val); 68 69 addToDefList(key, val); 70 71 } 72 73 public void addToDefList(String key, Object val) { 74 if (getDefList() == null) { 75 setDefList(new HashMap()); 76 } 77 getDefList().put(key, val); 78 79 } 80 81 public boolean isInDefList(String key) { 82 if (getDefList().containsKey(key)) return true; 83 else return false; 84 } 85 86 public boolean getBoolDef(String key) { 87 Boolean temp = (Boolean )getDefList().get(key); 88 return temp.booleanValue(); 89 } 90 91 public String getStringDef(String key) { 92 93 return (String )getDefList().get(key); 94 } 95 96 public String getArrayDef(String key){ 97 String res = ""; 98 if (getDefList().get(key) instanceof ArrayList){ 99 100 ArrayList list = (ArrayList)getDefList().get(key); 101 Iterator it = list.iterator(); 102 while (it.hasNext()){ 103 if (res.equals("")){ 104 res = res + (String )it.next(); 105 } 106 else { 107 res = res + "\r\n" + (String )it.next(); 108 } 109 } 110 } 111 else { 112 res = (String )getDefList().get(key); 113 } 114 return res; 115 } 116 117 protected void configureShell(Shell shell){ 120 super.configureShell(shell); 121 shell.setText(Messages.getString("AbstractOptionsDialog.Soot_Options")); } 123 124 public boolean isEnableButton(String alias){ 125 if (alias.equals("enabled")) return true; 126 return false; 127 } 128 129 130 public void handleWidgetSelected(SelectionEvent e){ 131 132 if (getRadioGroups() != null) { 133 Iterator it = getRadioGroups().keySet().iterator(); 134 while (it.hasNext()){ 135 Integer key = (Integer )it.next(); 136 if (getRadioGroups().get(key) == null) break; 137 ArrayList buttons = (ArrayList)getRadioGroups().get(key); 138 Iterator itButtons = buttons.iterator(); 139 while (itButtons.hasNext()){ 140 if (((BooleanOptionWidget)itButtons.next()).getButton().equals(e.getSource())) { 141 switchButtons(buttons, (Button)e.getSource()); 142 } 143 } 144 } 145 } 146 147 updateAllEnableGroups(); 148 149 } 150 151 public void updateEnableGroup(Button button){ 152 if (getEnableGroups() == null) return; 153 Iterator it = getEnableGroups().iterator(); 154 while (it.hasNext()){ 155 EnableGroup eGroup = (EnableGroup)it.next(); 156 if (eGroup.getLeader().getButton().equals(button)){ 157 eGroup.changeControlState(eGroup.getLeader().getButton().getSelection()); 159 if (eGroup.getControls() != null){ 160 Iterator itCon = eGroup.getControls().iterator(); 161 while (itCon.hasNext()){ 162 Object obj = itCon.next(); 163 if (obj instanceof BooleanOptionWidget){ 164 updateEnableGroup(((BooleanOptionWidget)obj).getButton()); 165 } 166 } 167 } 168 } 169 } 170 } 171 172 public void switchButtons(ArrayList buttons, Button change){ 173 if (change.getSelection()){ 174 Iterator it = buttons.iterator(); 175 while (it.hasNext()){ 176 BooleanOptionWidget nextWidget = (BooleanOptionWidget)it.next(); 177 if (nextWidget.getButton().equals(change)){ 178 nextWidget.getButton().setSelection(true); 179 } 180 else { 181 nextWidget.getButton().setSelection(false); 182 } 183 } 184 } 185 else { 186 Iterator it = buttons.iterator(); 187 while (it.hasNext()){ 188 BooleanOptionWidget defWidget = (BooleanOptionWidget)it.next(); 189 if (defWidget.getData().isDefaultVal()){ 190 defWidget.getButton().setSelection(true); 191 } 192 else { 193 defWidget.getButton().setSelection(false); 194 } 195 } 196 } 197 } 198 199 protected void makeNewEnableGroup(String phaseAlias){ 200 if (getEnableGroups() == null){ 201 setEnableGroups(new ArrayList()); 202 } 203 204 EnableGroup eGroup = new EnableGroup(); 205 eGroup.setPhaseAlias(phaseAlias); 206 207 getEnableGroups().add(eGroup); 208 } 209 210 protected void makeNewEnableGroup(String phaseAlias, String subPhaseAlias){ 211 if (getEnableGroups() == null){ 212 setEnableGroups(new ArrayList()); 213 } 214 215 EnableGroup eGroup = new EnableGroup(); 216 eGroup.setPhaseAlias(phaseAlias); 217 eGroup.setSubPhaseAlias(subPhaseAlias); 218 219 getEnableGroups().add(eGroup); 220 } 221 222 protected void addToEnableGroup(String phaseAlias, ISootOptionWidget widget, String alias){ 223 EnableGroup eGroup = findEnableGroup(phaseAlias); 224 if (widget instanceof BooleanOptionWidget){ 225 if (isEnableButton(alias)){ 227 eGroup.setLeader(((BooleanOptionWidget)widget)); 228 } 229 else { 230 eGroup.addControl(widget); 231 } 232 } 233 else { 234 eGroup.addControl(widget); 235 } 236 } 237 238 private EnableGroup findEnableGroup(String phaseAlias){ 239 Iterator it = getEnableGroups().iterator(); 240 while (it.hasNext()){ 241 EnableGroup next = (EnableGroup)it.next(); 242 if (next.getPhaseAlias().equals(phaseAlias) && 243 (next.getSubPhaseAlias() == null)) return next; 244 } 245 return null; 246 } 247 248 protected void addToEnableGroup(String phaseAlias, String subPhaseAlias, ISootOptionWidget widget, String alias){ 249 EnableGroup eGroup = findEnableGroup(phaseAlias, subPhaseAlias); 250 if (widget instanceof BooleanOptionWidget){ 251 253 if (isEnableButton(alias)){ 254 eGroup.setLeader(((BooleanOptionWidget)widget)); 255 addToEnableGroup(phaseAlias, widget, ""); 256 } 257 else { 258 eGroup.addControl(widget); 259 } 260 } 261 else { 262 eGroup.addControl(widget); 263 } 264 } 265 266 private EnableGroup findEnableGroup(String phaseAlias, String subPhaseAlias){ 267 Iterator it = getEnableGroups().iterator(); 268 while (it.hasNext()){ 269 EnableGroup next = (EnableGroup)it.next(); 270 if (next.getSubPhaseAlias() == null) continue; 271 if (next.getPhaseAlias().equals(phaseAlias) && 272 next.getSubPhaseAlias().equals(subPhaseAlias)) return next; 273 } 274 return null; 275 } 276 277 protected void updateAllEnableGroups(){ 278 if (getEnableGroups() == null) return; 279 Iterator it = getEnableGroups().iterator(); 280 281 while (it.hasNext()){ 282 EnableGroup eGroup = (EnableGroup)it.next(); 283 if (eGroup.isPhaseOptType()){ 284 if (eGroup.getLeader() == null){ 285 continue; 286 } 287 if (eGroup.getLeader().getButton().getSelection() && eGroup.getLeader().getButton().isEnabled()){ 288 eGroup.changeControlState(true); 289 } 290 else { 291 eGroup.changeControlState(false); 292 } 293 } 294 } 295 296 it = getEnableGroups().iterator(); 297 298 while (it.hasNext()){ 299 EnableGroup eGroup = (EnableGroup)it.next(); 300 if (!eGroup.isPhaseOptType()){ 301 if (eGroup.getLeader() == null){ 302 continue; 303 } 304 if (eGroup.getLeader().getButton().getSelection() && eGroup.getLeader().getButton().isEnabled()){ 305 eGroup.changeControlState(true); 306 } 307 else { 308 eGroup.changeControlState(false); 309 } 310 } 311 } 312 } 313 314 private void printEnableGroups(){ 315 if (getEnableGroups() == null) return; 316 Iterator it = getEnableGroups().iterator(); 317 while (it.hasNext()){ 318 EnableGroup eGroup = (EnableGroup)it.next(); 319 System.out.println(eGroup); 320 } 321 } 322 323 324 328 protected Control createDialogArea(Composite parent) { 329 GridData gd; 330 331 Composite dialogComp = (Composite)super.createDialogArea(parent); 332 Composite topComp = new Composite(dialogComp, SWT.NONE); 333 334 gd = new GridData(GridData.FILL_BOTH); 335 topComp.setLayoutData(gd); 336 GridLayout topLayout = new GridLayout(); 337 topComp.setLayout(topLayout); 338 339 341 setTitle(Messages.getString("AbstractOptionsDialog.Soot_Launching_Options")); setMessage(""); 344 setSashForm(new SashForm(topComp, SWT.NONE)); 347 getSashForm().setOrientation(SWT.HORIZONTAL); 348 349 gd = new GridData(GridData.FILL_BOTH); 350 getSashForm().setLayoutData(gd); 351 352 Composite selection = createSelectionArea(getSashForm()); 353 354 setPageContainer(createEditArea(getSashForm())); 355 356 initializePageContainer(); 357 358 Control [] pages = getPageContainer().getChildren(); 360 ((StackLayout)getPageContainer().getLayout()).topControl = pages[0]; 361 getPageContainer().layout(); 362 363 364 try { 365 getSashForm().setWeights(new int[] {30, 70}); 366 } 367 catch(Exception e1) { 368 System.out.println(e1.getMessage()); 369 } 370 371 Label separator = new Label(topComp, SWT.HORIZONTAL | SWT.SEPARATOR); 372 gd = new GridData(GridData.FILL_HORIZONTAL); 373 separator.setLayoutData(gd); 374 375 dialogComp.layout(true); 376 377 return dialogComp; 378 } 379 380 protected void createButtonsForButtonBar(Composite parent) { 383 if (isCanRun()) { 384 createButton(parent, 1, Messages.getString("AbstractOptionsDialog.Run"), true); createButton(parent, 2, Messages.getString("AbstractOptionsDialog.Close"), false); } 387 else { 388 createButton(parent, 0, Messages.getString("AbstractOptionsDialog.Save"), true); createButton(parent, 2, Messages.getString("AbstractOptionsDialog.Close"), false); } 391 } 392 393 394 protected void buttonPressed(int i){ 395 switch(i) { 396 case 0: { 397 handleSaving(); 398 break; 399 } 400 case 1: { 401 okPressed(); 402 break; 403 } 404 case 2: { 405 cancelPressed(); 406 break; 407 } 408 } 409 410 } 411 412 protected abstract HashMap savePressed(); 413 414 private void handleSaving() { 415 416 saveConfigToMap(this.getConfigName()); 417 418 SavedConfigManager scm = new SavedConfigManager(); 419 scm.setEditMap(getEditMap()); 420 scm.handleEdits(); 421 422 super.okPressed(); 423 424 425 } 426 427 428 private void saveConfigToMap(String name) { 429 430 SootSavedConfiguration newConfig = new SootSavedConfiguration(name, savePressed()); 431 432 newConfig.setEclipseDefs(getEclipseDefList()); 433 if (getEditMap() == null) { 434 setEditMap(new HashMap()); 435 } 436 437 getEditMap().put(name, newConfig.toSaveArray()); 438 439 } 440 441 protected abstract void initializePageContainer(); 442 protected abstract SootOption getInitialInput(); 443 444 445 448 private Composite createEditArea(Composite parent) { 449 Composite editArea = new Composite(parent, SWT.NONE); 450 StackLayout layout = new StackLayout(); 451 editArea.setLayout(layout); 452 return editArea; 453 } 454 455 458 private Composite createSelectionArea(Composite parent) { 459 Composite comp = new Composite(parent, SWT.NONE); 460 setSelectionArea(comp); 461 462 GridLayout layout = new GridLayout(); 463 464 layout.numColumns = 3; 465 layout.marginHeight = 0; 466 layout.marginWidth = 5; 467 468 comp.setLayout(layout); 469 470 GridData gd = new GridData(); 471 472 TreeViewer tree = new TreeViewer(comp); 473 gd = new GridData(GridData.FILL_BOTH); 474 gd.horizontalSpan = 3; 475 gd.widthHint = 0; 476 tree.getControl().setLayoutData(gd); 477 478 tree.setContentProvider(new SootOptionsContentProvider()); 479 tree.setLabelProvider(new SootOptionsLabelProvider()); 480 tree.setInput(getInitialInput()); 481 482 483 setTreeViewer(tree); 484 485 tree.addSelectionChangedListener(this); 486 487 tree.getControl().addKeyListener(new KeyAdapter() { 488 public void keyPressed(KeyEvent e) { 489 handleKeyPressed(e); 490 } 491 }); 492 493 return comp; 494 } 495 496 500 public void selectionChanged(SelectionChangedEvent event) { 501 IStructuredSelection selection = (IStructuredSelection)event.getSelection(); 502 if (!selection.isEmpty()) { 503 Object elem = selection.getFirstElement(); 504 if (elem instanceof SootOption) { 505 SootOption sel = (SootOption)elem; 506 Control [] children = getPageContainer().getChildren(); 507 String childTitle = null; 508 for (int i = 0; i < children.length; i++) { 509 510 if( children[i] instanceof Composite) { 511 if (children[i] instanceof Group) { 512 childTitle = (String )((Group)children[i]).getData("id"); 513 514 } 515 if (childTitle.compareTo(sel.getAlias()) == 0) { 516 ((StackLayout)getPageContainer().getLayout()).topControl = children[i]; 517 getPageContainer().layout(); 518 519 } 520 else { 521 children[i].setVisible(false); 522 } 523 } 524 } 525 } 526 } 527 } 528 529 public void addOtherBranches(SootOption root){ 530 SootOption sootMainClassBranch = new SootOption("Soot Main Class", "sootMainClass"); 531 root.addChild(sootMainClassBranch); 532 } 533 534 public void addOtherPages(Composite parent){ 535 Composite mainClassChild = sootMainClassCreate(parent); 536 } 537 538 private Composite sootMainClassCreate(Composite parent) { 539 540 Group editGroupSootMainClass = new Group(parent, SWT.NONE); 541 GridLayout layout = new GridLayout(); 542 editGroupSootMainClass.setLayout(layout); 543 544 editGroupSootMainClass.setText("Soot Main Class Manager"); 545 546 editGroupSootMainClass.setData("id", "sootMainClass"); 547 548 String desc = "Specify main class to run."; 549 if (desc.length() > 0) { 550 Label descLabel = new Label(editGroupSootMainClass, SWT.WRAP); 551 descLabel.setText(desc); 552 } 553 554 String defKey = "sootMainClass"; 555 String defaultString; 556 557 if (isInDefList(defKey)) { 558 defaultString = getStringDef(defKey); 559 } 560 else { 561 defaultString = ""; 562 } 563 setSootMainClassWidget(new StringOptionWidget(editGroupSootMainClass, SWT.NONE, new OptionData("Soot Main Class", "", "","sootMainClass", "\nUses specified main class to run Soot.", defaultString))); 564 565 return editGroupSootMainClass; 566 } 567 568 569 570 private StringOptionWidget sootMainClassWidget; 571 572 private void setPageContainer(Composite comp) { 573 pageContainer = comp; 574 } 575 576 protected Composite getPageContainer() { 577 return pageContainer; 578 } 579 580 584 private SashForm getSashForm() { 585 return sashForm; 586 } 587 588 592 private void setSashForm(SashForm sashForm) { 593 this.sashForm = sashForm; 594 } 595 596 protected void handleKeyPressed(KeyEvent event) { 597 } 598 599 private Composite selectionArea; 600 601 private void setSelectionArea(Composite comp){ 602 selectionArea = comp; 603 } 604 605 private Composite getSelectionArea() { 606 return selectionArea; 607 } 608 609 private void setTreeViewer(TreeViewer tree) { 610 treeViewer = tree; 611 } 612 613 private TreeViewer getTreeViewer() { 614 return treeViewer; 615 } 616 617 618 622 public HashMap getDefList() { 623 return defList; 624 } 625 626 630 public void setDefList(HashMap defList) { 631 this.defList = defList; 632 } 633 634 638 public HashMap getConfig() { 639 return config; 640 } 641 642 646 public void setConfig(HashMap config) { 647 this.config = config; 648 } 649 650 654 public String getConfigName() { 655 return configName; 656 } 657 658 662 public void setConfigName(String configName) { 663 this.configName = configName; 664 } 665 666 670 public HashMap getEclipseDefList() { 671 return eclipseDefList; 672 } 673 674 678 public void setEclipseDefList(HashMap eclipseDefList) { 679 this.eclipseDefList = eclipseDefList; 680 } 681 682 683 687 public HashMap getEditMap() { 688 return editMap; 689 } 690 691 692 696 public void setEditMap(HashMap editMap) { 697 this.editMap = editMap; 698 } 699 700 704 public boolean isCanRun() { 705 return canRun; 706 } 707 708 712 public void setCanRun(boolean canRun) { 713 this.canRun = canRun; 714 } 715 716 719 public HashMap getRadioGroups() { 720 return radioGroups; 721 } 722 723 726 public void setRadioGroups(HashMap map) { 727 radioGroups = map; 728 } 729 730 733 public ArrayList getEnableGroups() { 734 return enableGroups; 735 } 736 737 740 public void setEnableGroups(ArrayList list) { 741 enableGroups = list; 742 } 743 744 747 public Button getAddButton() { 748 return addButton; 749 } 750 751 754 public Button getRemoveButton() { 755 return removeButton; 756 } 757 758 761 public void setAddButton(Button button) { 762 addButton = button; 763 } 764 765 768 public void setRemoveButton(Button button) { 769 removeButton = button; 770 } 771 772 775 public CheckboxTableViewer getTableViewer() { 776 return tableViewer; 777 } 778 779 782 public void setTableViewer(CheckboxTableViewer viewer) { 783 tableViewer = viewer; 784 } 785 786 789 public StringOptionWidget getSootMainClassWidget() { 790 return sootMainClassWidget; 791 } 792 793 796 public void setSootMainClassWidget(StringOptionWidget widget) { 797 sootMainClassWidget = widget; 798 } 799 800 803 public String getSootMainClass() { 804 return sootMainClass; 805 } 806 807 810 public void setSootMainClass(String string) { 811 sootMainClass = string; 812 } 813 814 } 815 | Popular Tags |