1 19 20 package ca.mcgill.sable.soot.ui; 21 22 import java.util.*; 23 24 import org.eclipse.jface.dialogs.IDialogSettings; 25 import org.eclipse.jface.dialogs.InputDialog; 26 import org.eclipse.jface.dialogs.TitleAreaDialog; 27 import org.eclipse.jface.viewers.ISelectionChangedListener; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 import org.eclipse.jface.viewers.SelectionChangedEvent; 30 import org.eclipse.jface.viewers.TreeViewer; 31 import org.eclipse.swt.custom.SashForm; 32 import org.eclipse.swt.events.KeyAdapter; 33 import org.eclipse.swt.events.KeyEvent; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Button; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.jface.dialogs.*; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.swt.widgets.Shell; 44 import org.eclipse.swt.*; 45 46 import ca.mcgill.sable.soot.SootPlugin; 47 import ca.mcgill.sable.soot.launching.*; 48 import ca.mcgill.sable.soot.ui.PhaseOptionsDialog; 49 50 public class SootConfigManagerDialog extends TitleAreaDialog implements ISelectionChangedListener { 51 52 private SashForm sashForm; 53 private Composite selectionArea; 54 private TreeViewer treeViewer; 55 private String selected; 56 private Composite buttonPanel; 57 private SootConfiguration treeRoot; 58 private HashMap editDefs; 59 private SootLauncher launcher; 60 61 private void addEclipseDefsToDialog(PhaseOptionsDialog dialog) { 62 if (getEclipseDefList() == null) return; 63 Iterator it = getEclipseDefList().keySet().iterator(); 64 while (it.hasNext()) { 65 String key = (String )it.next(); 66 dialog.addToEclipseDefList(key, getEclipseDefList().get(key)); 67 } 68 } 69 70 private void setMainClassInDialog(PhaseOptionsDialog dialog, String mainClass){ 71 dialog.addToEclipseDefList("sootMainClass", mainClass); 72 } 73 74 private HashMap eclipseDefList; 75 76 80 public HashMap getEclipseDefList() { 81 return eclipseDefList; 82 } 83 84 88 public void setEclipseDefList(HashMap eclipseDefList) { 89 this.eclipseDefList = eclipseDefList; 90 } 91 92 public SootConfigManagerDialog(Shell parentShell) { 93 super(parentShell); 94 this.setShellStyle(SWT.RESIZE); 95 } 96 97 protected void configureShell(Shell shell){ 98 super.configureShell(shell); 99 shell.setText(Messages.getString("SootConfigManagerDialog.Manage_Configurations")); } 101 105 protected Control createDialogArea(Composite parent) { 106 GridData gd; 107 108 Composite dialogComp = (Composite)super.createDialogArea(parent); 109 Composite topComp = new Composite(dialogComp, SWT.NONE); 110 111 gd = new GridData(GridData.FILL_BOTH); 112 topComp.setLayoutData(gd); 113 GridLayout topLayout = new GridLayout(); 114 topLayout.numColumns = 2; 115 116 topComp.setLayout(topLayout); 117 118 120 setTitle(Messages.getString("SootConfigManagerDialog.Soot_Configurations_Manager")); setMessage(""); 123 124 Composite selection = createSelectionArea(topComp); 125 gd = new GridData(GridData.FILL_BOTH); 126 gd.horizontalSpan = 1; 127 128 selection.setLayoutData(gd); 129 130 131 Control specialButtons = createSpecialButtonBar(topComp); 132 gd = new GridData(GridData.FILL_BOTH); 133 134 specialButtons.setLayoutData(gd); 135 136 Label separator = new Label(topComp, SWT.HORIZONTAL | SWT.SEPARATOR); 137 gd = new GridData(GridData.FILL_HORIZONTAL); 138 gd.horizontalSpan = 2; 139 separator.setLayoutData(gd); 140 141 142 dialogComp.layout(true); 143 return dialogComp; 144 } 145 146 147 150 private Composite createSelectionArea(Composite parent) { 151 Composite comp = new Composite(parent, SWT.NONE); 152 setSelectionArea(comp); 153 154 GridLayout layout = new GridLayout(); 155 156 layout.numColumns = 1; 157 158 159 comp.setLayout(layout); 160 161 GridData gd = new GridData(); 162 163 TreeViewer tree = new TreeViewer(comp); 164 gd = new GridData(GridData.FILL_BOTH); 165 166 tree.getControl().setLayoutData(gd); 167 168 tree.setContentProvider(new SootConfigContentProvider()); 169 tree.setLabelProvider(new SootConfigLabelProvider()); 170 tree.setInput(getInitialInput()); 171 172 setTreeViewer(tree); 173 174 tree.addSelectionChangedListener(this); 175 176 177 tree.expandAll(); 178 tree.getControl().addKeyListener(new KeyAdapter() { 179 public void keyPressed(KeyEvent e) { 180 handleKeyPressed(e); 181 } 182 }); 183 184 185 return comp; 186 } 187 188 public void selectionChanged(SelectionChangedEvent event) { 189 IStructuredSelection selection = (IStructuredSelection)event.getSelection(); 190 if (!selection.isEmpty()) { 191 Object elem = selection.getFirstElement(); 192 if (elem instanceof SootConfiguration) { 193 SootConfiguration sel = (SootConfiguration)elem; 194 setSelected(sel.getLabel()); 195 } 196 enableButtons(); 197 } 198 } 199 200 private void enableButtons(){ 201 Iterator it = specialButtonList.iterator(); 202 while (it.hasNext()){ 203 ((Button)it.next()).setEnabled(true); 204 } 205 } 206 207 protected void handleKeyPressed(KeyEvent e) { 208 } 209 210 private SootConfiguration getInitialInput() { 211 212 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 213 int numConfig = 0; 214 try { 215 numConfig = settings.getInt(Messages.getString("SootConfigManagerDialog.config_count")); } 217 catch(NumberFormatException e) { 218 } 219 220 SootConfiguration root = new SootConfiguration(""); 222 if (numConfig != 0) { 223 String [] configNames = new String [numConfig]; 224 225 226 for (int i = 0; i < numConfig; i++) { 227 configNames[i] = settings.get(Messages.getString("SootConfigManagerDialog.soot_run_config")+(i+1)); root.addChild(new SootConfiguration(configNames[i])); 229 } 230 231 232 } 233 setTreeRoot(root); 234 235 return root; 236 } 237 238 241 protected Control createSpecialButtonBar(Composite parent) { 242 Composite composite= new Composite(parent, SWT.NULL); 243 GridLayout layout= new GridLayout(); 244 layout.numColumns= 1; 245 246 composite.setLayout(layout); 247 composite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 248 249 250 applyDialogFont(composite); 251 252 composite.setLayout(layout); 253 254 GridData data = 255 new GridData( 256 GridData.VERTICAL_ALIGN_END | GridData.HORIZONTAL_ALIGN_CENTER); 257 composite.setLayoutData(data); 258 259 createSpecialButtonsForButtonBar(composite); 261 262 return composite; 263 } 264 265 ArrayList specialButtonList = new ArrayList(); 266 267 protected Button createSpecialButton( 268 Composite parent, 269 int id, 270 String label, 271 boolean defaultButton, boolean enabled) { 272 273 274 Button button = new Button(parent, SWT.PUSH); 275 button.setText(label); 276 277 button.setData(new Integer (id)); 278 button.addSelectionListener(new SelectionAdapter() { 279 public void widgetSelected(SelectionEvent event) { 280 buttonPressed(((Integer ) event.widget.getData()).intValue()); 281 } 282 }); 283 if (defaultButton) { 284 Shell shell = parent.getShell(); 285 if (shell != null) { 286 shell.setDefaultButton(button); 287 } 288 } 289 button.setFont(parent.getFont()); 290 if (!enabled){ 291 button.setEnabled(false); 292 } 293 294 setButtonLayoutData(button); 295 specialButtonList.add(button); 296 return button; 297 } 298 299 protected void createButtonsForButtonBar(Composite parent){ 300 Button runButton = createButton(parent, 5, Messages.getString("SootConfigManagerDialog.Run"), false); runButton.setEnabled(false); 303 specialButtonList.add(runButton); 304 createButton(parent, 6, Messages.getString("SootConfigManagerDialog.Close"), true); } 306 protected void createSpecialButtonsForButtonBar(Composite parent) { 307 createSpecialButton(parent, 0, Messages.getString("SootConfigManagerDialog.New"), false, true); createSpecialButton(parent, 1, Messages.getString("SootConfigManagerDialog.Edit"), false, false); createSpecialButton(parent, 2, Messages.getString("SootConfigManagerDialog.Delete"), false, false); createSpecialButton(parent, 3, Messages.getString("SootConfigManagerDialog.Rename"), false, false); createSpecialButton(parent, 4, Messages.getString("SootConfigManagerDialog.Clone"), false, false); 313 } 314 315 protected void buttonPressed(int id) { 316 switch (id) { 317 case 0: { 318 newPressed(); 319 break; 320 } 321 case 1: { 322 editPressed(); 323 break; 324 } 325 case 2: { 326 deletePressed(); 327 break; 328 } 329 case 3: { 330 renamePressed(); 331 break; 332 } 333 case 4: { 334 clonePressed(); 335 break; 336 } 337 case 5: { 338 runPressed(); 339 break; 340 } 341 case 6: { 342 cancelPressed(); 343 break; 344 } 345 case 7: { 346 break; 347 } 348 case 8: { 349 break; 350 } 351 352 } 353 } 354 355 private void newPressed() { 358 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 359 360 int config_count = 0; 362 try { 363 config_count = settings.getInt(Messages.getString("SootConfigManagerDialog.config_count")); } 365 catch (NumberFormatException e) { 366 } 367 368 ArrayList currentNames = new ArrayList(); 369 for (int i = 1; i <= config_count; i++) { 370 currentNames.add(settings.get(Messages.getString("SootConfigManagerDialog.soot_run_config")+i)); } 372 373 SootConfigNameInputValidator validator = new SootConfigNameInputValidator(); 376 validator.setAlreadyUsed(currentNames); 377 378 379 InputDialog nameDialog = new InputDialog(this.getShell(), Messages.getString("SootConfigManagerDialog.Saving_Configuration_Name"), Messages.getString("SootConfigManagerDialog.Enter_name_to_save_configuration_with"), "", validator); nameDialog.open(); 382 383 if (nameDialog.getReturnCode() == Dialog.OK) { 384 setEditDefs(null); 385 int returnCode = displayOptions(nameDialog.getValue(), "soot.Main"); 386 388 if (returnCode != Dialog.CANCEL) { 389 getTreeRoot().addChild(new SootConfiguration(nameDialog.getValue())); 390 refreshTree(); 391 392 } 393 394 } 395 else { 396 } 398 } 399 400 private void saveMainClass(String configName, String mainClass){ 402 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 403 settings.put(configName+"_mainClass", mainClass); 404 } 405 406 private String getMainClass(String configName){ 408 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 409 String mainClass = settings.get(configName+"_mainClass"); 410 if ((mainClass == null) || (mainClass.length() == 0)){ 411 return "soot.Main"; 412 } 413 else { 414 return mainClass; 415 } 416 } 417 418 419 private ArrayList stringToList(String string){ 420 StringTokenizer st = new StringTokenizer(string, ","); 421 ArrayList list = new ArrayList(); 422 while (st.hasMoreTokens()){ 423 list.add(st.nextToken()); 424 } 425 return list; 426 } 427 428 private void refreshTree() { 429 getTreeViewer().setInput(getTreeRoot()); 430 getTreeViewer().setExpandedState(getTreeRoot(), true); 431 getTreeViewer().refresh(getTreeRoot(), false); 432 } 433 434 private int displayOptions(String name) { 435 return displayOptions(name, "soot.Main"); 436 } 437 438 private int displayOptions(String name, String mainClass) { 439 440 PhaseOptionsDialog dialog = new PhaseOptionsDialog(getShell()); 441 addEclipseDefsToDialog(dialog); 442 setMainClassInDialog(dialog, mainClass); 443 if (getEditDefs() != null) { 444 Iterator it = getEditDefs().keySet().iterator(); 445 while (it.hasNext()) { 446 Object next = it.next(); 447 String key = (String )next; 448 String val = (String )getEditDefs().get(key); 449 if ((val.equals("true")) || (val.equals("false"))) { dialog.addToDefList(key, new Boolean (val)); 451 } 452 else { 453 dialog.addToDefList(key, val); 454 } 455 } 456 } 457 458 459 dialog.setConfigName(name); 460 dialog.setCanRun(false); 461 dialog.open(); 462 if (dialog.getReturnCode() == Dialog.OK){ 463 saveMainClass(name, dialog.getSootMainClass()); 465 } 466 return dialog.getReturnCode(); 467 469 470 } 471 472 private void editPressed() { 474 if (getSelected() == null) return; 475 476 String result = this.getSelected(); 477 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 478 String [] saveArray = settings.getArray(result); 479 SootSavedConfiguration ssc = new SootSavedConfiguration(result, saveArray); 480 setEditDefs(ssc.toHashMapFromArray()); 481 displayOptions(result, getMainClass(result)); 482 483 484 } 485 486 private void deletePressed() { 488 if (getSelected() == null) return; 489 490 String result = this.getSelected(); 491 492 MessageDialog msgDialog = new MessageDialog(this.getShell(), Messages.getString("SootConfigManagerDialog.Soot_Configuration_Remove_Message"), null, Messages.getString("SootConfigManagerDialog.Are_you_sure_you_want_to_remove_this_configuration"), 0, new String [] {Messages.getString("SootConfigManagerDialog.Yes"), Messages.getString("SootConfigManagerDialog.No")}, 0); msgDialog.open(); 495 if (msgDialog.getReturnCode() == 0) { 496 497 ArrayList toRemove = new ArrayList(); 499 toRemove.add(result); 500 SavedConfigManager scm = new SavedConfigManager(); 501 scm.setDeleteList(toRemove); 502 scm.handleDeletes(); 503 504 getTreeRoot().removeChild(result); 506 refreshTree(); 507 } 508 509 510 } 511 512 private void renamePressed(){ 513 if (getSelected() == null) return; 514 515 String result = this.getSelected(); 516 517 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 518 519 int config_count = 0; 521 int oldNameCount = 0; 522 try { 523 config_count = settings.getInt(Messages.getString("SootConfigManagerDialog.config_count")); } 525 catch (NumberFormatException e) { 526 } 527 528 ArrayList currentNames = new ArrayList(); 529 for (int i = 1; i <= config_count; i++) { 530 currentNames.add(settings.get(Messages.getString("SootConfigManagerDialog.soot_run_config")+i)); if (((String )currentNames.get(i-1)).equals(result)){ 532 oldNameCount = i; 533 } 534 } 535 536 537 SootConfigNameInputValidator validator = new SootConfigNameInputValidator(); 539 validator.setAlreadyUsed(currentNames); 540 541 InputDialog nameDialog = new InputDialog(this.getShell(), Messages.getString("SootConfigManagerDialog.Rename_Saved_Configuration"), Messages.getString("SootConfigManagerDialog.Enter_new_name"), "", validator); nameDialog.open(); 543 if (nameDialog.getReturnCode() == Dialog.OK){ 544 settings.put(Messages.getString("SootConfigManagerDialog.soot_run_config")+oldNameCount, nameDialog.getValue()); settings.put(nameDialog.getValue(), settings.getArray(result)); 546 getTreeRoot().renameChild(result, nameDialog.getValue()); 547 saveMainClass(nameDialog.getValue(), settings.get(result+"_mainClass")); 548 } 549 refreshTree(); 550 } 551 552 553 554 private void clonePressed(){ 555 if (getSelected() == null) return; 556 557 String result = this.getSelected(); 558 559 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 560 561 int config_count = 0; 563 try { 564 config_count = settings.getInt(Messages.getString("SootConfigManagerDialog.config_count")); } 566 catch (NumberFormatException e) { 567 } 568 ArrayList currentNames = new ArrayList(); 569 for (int i = 1; i <= config_count; i++) { 570 currentNames.add(settings.get(Messages.getString("SootConfigManagerDialog.soot_run_config")+i)); 572 } 573 574 575 SootConfigNameInputValidator validator = new SootConfigNameInputValidator(); 577 validator.setAlreadyUsed(currentNames); 578 579 InputDialog nameDialog = new InputDialog(this.getShell(), Messages.getString("SootConfigManagerDialog.Clone_Saved_Configuration"), Messages.getString("SootConfigManagerDialog.Enter_new_name"), result, validator); nameDialog.open(); 581 if (nameDialog.getReturnCode() == Dialog.OK){ 582 config_count++; 583 settings.put(Messages.getString("SootConfigManagerDialog.soot_run_config")+config_count, nameDialog.getValue()); settings.put(nameDialog.getValue(), settings.getArray(result)); 585 settings.put(Messages.getString("SootConfigManagerDialog.config_count"), config_count); getTreeRoot().addChild(new SootConfiguration(nameDialog.getValue())); 587 saveMainClass(nameDialog.getValue(), settings.get(result+"_mainClass")); 588 } 589 refreshTree(); 590 } 591 592 private void runPressed() { 594 super.okPressed(); 595 if (getSelected() == null) return; 596 597 IDialogSettings settings = SootPlugin.getDefault().getDialogSettings(); 598 String mainClass = settings.get(getSelected()+"_mainClass"); 599 600 if (getLauncher() instanceof SootConfigProjectLauncher) { 601 ((SootConfigProjectLauncher)getLauncher()).launch(getSelected(), mainClass); 602 } 603 else if (getLauncher() instanceof SootConfigJavaProjectLauncher){ 604 ((SootConfigJavaProjectLauncher)getLauncher()).launch(getSelected(), mainClass); 605 } 606 else if (getLauncher() instanceof SootConfigFileLauncher) { 607 ((SootConfigFileLauncher)getLauncher()).launch(getSelected(), mainClass); 608 } 609 else if (getLauncher() instanceof SootConfigFromJavaFileLauncher){ 610 ((SootConfigFromJavaFileLauncher)getLauncher()).launch(getSelected(), mainClass); 611 } 612 613 614 } 615 616 private void importPressed(){ 617 618 } 619 620 624 public SashForm getSashForm() { 625 return sashForm; 626 } 627 628 632 public void setSashForm(SashForm sashForm) { 633 this.sashForm = sashForm; 634 } 635 636 640 public Composite getSelectionArea() { 641 return selectionArea; 642 } 643 644 648 public TreeViewer getTreeViewer() { 649 return treeViewer; 650 } 651 652 656 public void setSelectionArea(Composite selectionArea) { 657 this.selectionArea = selectionArea; 658 } 659 660 664 public void setTreeViewer(TreeViewer treeViewer) { 665 this.treeViewer = treeViewer; 666 } 667 668 672 public String getSelected() { 673 return selected; 674 } 675 676 680 public void setSelected(String selected) { 681 this.selected = selected; 682 } 683 684 688 public Composite getButtonPanel() { 689 return buttonPanel; 690 } 691 692 696 public void setButtonPanel(Composite buttonPanel) { 697 this.buttonPanel = buttonPanel; 698 } 699 700 701 705 public SootConfiguration getTreeRoot() { 706 return treeRoot; 707 } 708 709 713 public void setTreeRoot(SootConfiguration treeRoot) { 714 this.treeRoot = treeRoot; 715 } 716 717 721 public HashMap getEditDefs() { 722 return editDefs; 723 } 724 725 729 public void setEditDefs(HashMap editDefs) { 730 this.editDefs = editDefs; 731 } 732 733 737 public SootLauncher getLauncher() { 738 return launcher; 739 } 740 741 745 public void setLauncher(SootLauncher launcher) { 746 this.launcher = launcher; 747 } 748 749 } 750 | Popular Tags |