1 11 package org.eclipse.ui.externaltools.internal.ui; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.resources.ICommand; 22 import org.eclipse.core.resources.IFolder; 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IProjectDescription; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.IWorkspace; 27 import org.eclipse.core.resources.IWorkspaceDescription; 28 import org.eclipse.core.resources.IncrementalProjectBuilder; 29 import org.eclipse.core.resources.ResourcesPlugin; 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IAdaptable; 32 import org.eclipse.core.runtime.IExtension; 33 import org.eclipse.core.runtime.IProgressMonitor; 34 import org.eclipse.core.runtime.IStatus; 35 import org.eclipse.core.runtime.Platform; 36 import org.eclipse.core.runtime.Status; 37 import org.eclipse.debug.core.DebugPlugin; 38 import org.eclipse.debug.core.ILaunchConfiguration; 39 import org.eclipse.debug.core.ILaunchConfigurationListener; 40 import org.eclipse.debug.core.ILaunchConfigurationType; 41 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 42 import org.eclipse.debug.core.ILaunchManager; 43 import org.eclipse.debug.ui.DebugUITools; 44 import org.eclipse.jface.dialogs.ErrorDialog; 45 import org.eclipse.jface.dialogs.IDialogConstants; 46 import org.eclipse.jface.dialogs.MessageDialog; 47 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 48 import org.eclipse.jface.operation.IRunnableWithProgress; 49 import org.eclipse.jface.preference.IPreferenceStore; 50 import org.eclipse.jface.viewers.CheckStateChangedEvent; 51 import org.eclipse.jface.viewers.CheckboxTableViewer; 52 import org.eclipse.jface.viewers.ICheckStateListener; 53 import org.eclipse.jface.viewers.ILabelProvider; 54 import org.eclipse.jface.viewers.IStructuredSelection; 55 import org.eclipse.jface.window.Window; 56 import org.eclipse.osgi.util.NLS; 57 import org.eclipse.swt.SWT; 58 import org.eclipse.swt.events.SelectionAdapter; 59 import org.eclipse.swt.events.SelectionEvent; 60 import org.eclipse.swt.events.SelectionListener; 61 import org.eclipse.swt.graphics.Font; 62 import org.eclipse.swt.layout.GridData; 63 import org.eclipse.swt.layout.GridLayout; 64 import org.eclipse.swt.widgets.Button; 65 import org.eclipse.swt.widgets.Composite; 66 import org.eclipse.swt.widgets.Control; 67 import org.eclipse.swt.widgets.Display; 68 import org.eclipse.swt.widgets.Event; 69 import org.eclipse.swt.widgets.Label; 70 import org.eclipse.swt.widgets.Listener; 71 import org.eclipse.swt.widgets.Shell; 72 import org.eclipse.swt.widgets.Table; 73 import org.eclipse.swt.widgets.TableItem; 74 import org.eclipse.ui.PlatformUI; 75 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 76 import org.eclipse.ui.dialogs.PropertyPage; 77 import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsMainTab; 78 import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil; 79 import org.eclipse.ui.externaltools.internal.launchConfigurations.IgnoreWhiteSpaceComparator; 80 import org.eclipse.ui.externaltools.internal.model.BuilderUtils; 81 import org.eclipse.ui.externaltools.internal.model.ExternalToolBuilder; 82 import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin; 83 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; 84 import org.eclipse.ui.externaltools.internal.model.IExternalToolsHelpContextIds; 85 import org.eclipse.ui.externaltools.internal.model.IPreferenceConstants; 86 import org.eclipse.ui.progress.IProgressService; 87 88 91 public final class BuilderPropertyPage extends PropertyPage implements ICheckStateListener { 92 93 private static final String COMMAND_ENABLED= "CommandEnabled"; 96 private Button upButton, downButton, newButton, importButton, editButton, removeButton; 97 98 private boolean userHasMadeChanges= false; 99 100 private List configsToBeDeleted= null; 101 private List commandsToBeDeleted= null; 102 103 private CheckboxTableViewer viewer= null; 104 105 private ILabelProvider labelProvider= new BuilderLabelProvider(); 106 107 111 public class ErrorConfig { 112 private ICommand command; 113 public ErrorConfig(ICommand command) { 114 this.command= command; 115 } 116 public ICommand getCommand() { 117 return command; 118 } 119 } 120 121 125 private List newConfigList= new ArrayList (); 126 127 private SelectionListener buttonListener= new SelectionAdapter() { 128 public void widgetSelected(SelectionEvent e) { 129 handleButtonPressed((Button) e.widget); 130 } 131 }; 132 133 143 private ILaunchConfigurationListener configurationListener= new ILaunchConfigurationListener() { 144 149 public void launchConfigurationAdded(final ILaunchConfiguration configuration) { 150 ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager(); 151 final ILaunchConfiguration oldConfig= manager.getMovedFrom(configuration); 152 if (oldConfig == null) { 153 return; 154 } 155 if (newConfigList.remove(oldConfig)) { 157 newConfigList.add(configuration); 158 } 159 160 Display.getDefault().asyncExec(new Runnable () { 161 public void run() { 162 TableItem[] items= viewer.getTable().getItems(); 163 for (int i = 0; i < items.length; i++) { 164 TableItem item = items[i]; 165 Object data= item.getData(); 166 if (data == oldConfig) { 167 item.setData(configuration); 169 viewer.update(configuration, null); 170 break; 171 } 172 } 173 } 174 }); 175 } 176 public void launchConfigurationChanged(ILaunchConfiguration configuration) { 177 } 178 public void launchConfigurationRemoved(ILaunchConfiguration configuration) { 179 } 180 }; 181 182 185 public BuilderPropertyPage() { 186 super(); 187 noDefaultAndApplyButton(); 188 } 189 190 193 private void addBuildersToTable() { 194 IProject project = getInputProject(); 195 if (project == null) { 196 return; 197 } 198 ICommand[] commands= null; 200 try { 201 commands = project.getDescription().getBuildSpec(); 202 } catch (CoreException e) { 203 handleException(e); 204 return; 205 } 206 207 boolean projectNeedsMigration= false; 208 for (int i = 0; i < commands.length; i++) { 209 String [] version= new String [] {""}; ILaunchConfiguration config = BuilderUtils.configFromBuildCommandArgs(project, commands[i].getArguments(), version); 211 if (BuilderUtils.VERSION_2_1.equals(version[0])) { 212 projectNeedsMigration= true; 215 } 216 Object element= null; 217 if (config != null) { 218 if (!config.isWorkingCopy() && !config.exists()) { 219 Shell shell= getShell(); 220 if (shell == null) { 221 return; 222 } 223 IStatus status = new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, 0, NLS.bind(ExternalToolsUIMessages.BuilderPropertyPage_Exists, new String []{config.getLocation().toOSString()}), null); 224 ErrorDialog.openError(getShell(), ExternalToolsUIMessages.BuilderPropertyPage_errorTitle, 225 NLS.bind(ExternalToolsUIMessages.BuilderPropertyPage_External_Tool_Builder__0__Not_Added_2, new String []{config.getName()}), 226 status); 227 userHasMadeChanges= true; 228 } else { 229 element= config; 230 } 231 } else { 232 String builderID = commands[i].getBuilderName(); 233 if (builderID.equals(ExternalToolBuilder.ID) && commands[i].getArguments().get(BuilderUtils.LAUNCH_CONFIG_HANDLE) != null) { 234 element= new ErrorConfig(commands[i]); 236 } else { 237 element= commands[i]; 238 } 239 } 240 if (element != null) { 241 viewer.add(element); 242 viewer.setChecked(element, isEnabled(element)); 243 } 244 } 245 if (projectNeedsMigration) { 246 IPreferenceStore store= ExternalToolsPlugin.getDefault().getPreferenceStore(); 247 boolean prompt= store.getBoolean(IPreferenceConstants.PROMPT_FOR_PROJECT_MIGRATION); 248 boolean proceed= true; 249 if (prompt) { 250 Shell shell= getShell(); 251 if (shell == null) { 252 return; 253 } 254 MessageDialogWithToggle dialog= MessageDialogWithToggle.openYesNoQuestion(shell, ExternalToolsUIMessages.BuilderPropertyPage_0, ExternalToolsUIMessages.BuilderPropertyPage_1, ExternalToolsUIMessages.BuilderPropertyPage_2, false, null, null); 255 proceed= dialog.getReturnCode() == IDialogConstants.YES_ID; 256 store.setValue(IPreferenceConstants.PROMPT_FOR_PROJECT_MIGRATION, !dialog.getToggleState()); 257 } 258 if (!proceed) { 259 viewer.getTable().setEnabled(false); 261 downButton.setEnabled(false); 262 editButton.setEnabled(false); 263 importButton.setEnabled(false); 264 newButton.setEnabled(false); 265 removeButton.setEnabled(false); 266 } 267 } 268 } 269 270 273 private Button createButton(Composite parent, String label) { 274 Button button = new Button(parent, SWT.PUSH); 275 GridData data = new GridData(); 276 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 277 button.setLayoutData(data); 278 button.setFont(parent.getFont()); 279 button.setText(label); 280 button.setEnabled(false); 281 button.addSelectionListener(buttonListener); 282 return button; 283 } 284 285 288 protected Control createContents(Composite parent) { 289 290 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IExternalToolsHelpContextIds.EXTERNAL_TOOLS_BUILDER_PROPERTY_PAGE); 291 292 Font font = parent.getFont(); 293 294 Composite topLevel = new Composite(parent, SWT.NONE); 295 GridLayout layout = new GridLayout(); 296 layout.marginHeight = 0; 297 layout.marginWidth = 0; 298 topLevel.setLayout(layout); 299 topLevel.setLayoutData(new GridData(GridData.FILL_BOTH)); 300 301 Label description = new Label(topLevel, SWT.WRAP); 302 description.setText(ExternalToolsUIMessages.BuilderPropertyPage_description); 303 description.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 304 description.setFont(font); 305 306 Composite tableAndButtons = new Composite(topLevel, SWT.NONE); 307 tableAndButtons.setLayoutData(new GridData(GridData.FILL_BOTH)); 308 layout = new GridLayout(); 309 layout.marginHeight = 0; 310 layout.marginWidth = 0; 311 layout.numColumns = 2; 312 tableAndButtons.setLayout(layout); 313 314 viewer= CheckboxTableViewer.newCheckList(tableAndButtons, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); 316 viewer.setLabelProvider(labelProvider); 317 viewer.addCheckStateListener(this); 318 Table builderTable= viewer.getTable(); 319 builderTable.setLayoutData(new GridData(GridData.FILL_BOTH)); 320 builderTable.setFont(font); 321 builderTable.addSelectionListener(new SelectionAdapter() { 322 public void widgetSelected(SelectionEvent e) { 323 handleTableSelectionChanged(); 324 } 325 }); 326 327 builderTable.addListener(SWT.MouseDoubleClick, new Listener() { 328 public void handleEvent(Event event) { 329 handleEditButtonPressed(); 330 } 331 }); 332 333 Composite buttonArea = new Composite(tableAndButtons, SWT.NONE); 335 layout = new GridLayout(); 336 layout.marginHeight = 0; 337 layout.marginWidth = 0; 338 buttonArea.setLayout(layout); 339 buttonArea.setFont(font); 340 buttonArea.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 341 newButton = createButton(buttonArea, ExternalToolsUIMessages.BuilderPropertyPage_newButton); 342 importButton = createButton(buttonArea, ExternalToolsUIMessages.BuilderPropertyPage__Import____3); 343 editButton = createButton(buttonArea, ExternalToolsUIMessages.BuilderPropertyPage_editButton); 344 removeButton = createButton(buttonArea, ExternalToolsUIMessages.BuilderPropertyPage_removeButton); 345 new Label(buttonArea, SWT.LEFT); 346 upButton = createButton(buttonArea, ExternalToolsUIMessages.BuilderPropertyPage_upButton); 347 downButton = createButton(buttonArea, ExternalToolsUIMessages.BuilderPropertyPage_downButton); 348 349 newButton.setEnabled(true); 350 importButton.setEnabled(true); 351 352 addBuildersToTable(); 354 355 return topLevel; 356 } 357 358 361 private void setAutobuild(boolean newState) throws CoreException { 362 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 363 IWorkspaceDescription wsDescription= workspace.getDescription(); 364 boolean oldState= wsDescription.isAutoBuilding(); 365 if (oldState != newState) { 366 wsDescription.setAutoBuilding(newState); 367 workspace.setDescription(wsDescription); 368 } 369 } 370 371 375 private IProject getInputProject() { 376 IAdaptable element = getElement(); 377 if (element instanceof IProject) { 378 return (IProject) element; 379 } 380 Object resource = element.getAdapter(IResource.class); 381 if (resource instanceof IProject) { 382 return (IProject) resource; 383 } 384 return null; 385 } 386 387 390 private void handleButtonPressed(Button button) { 391 if (button == newButton) { 392 handleNewButtonPressed(); 393 } else if (button == importButton) { 394 handleImportButtonPressed(); 395 } else if (button == editButton) { 396 handleEditButtonPressed(); 397 } else if (button == removeButton) { 398 handleRemoveButtonPressed(); 399 } else if (button == upButton) { 400 moveSelectionUp(); 401 } else if (button == downButton) { 402 moveSelectionDown(); 403 } 404 if (getControl().isDisposed()) { 405 return; 406 } 407 handleTableSelectionChanged(); 408 viewer.getTable().setFocus(); 409 } 410 411 414 public void checkStateChanged(CheckStateChangedEvent event) { 415 Object element= event.getElement(); 416 boolean checked = event.getChecked(); 417 if (element instanceof ILaunchConfiguration) { 418 enableLaunchConfiguration((ILaunchConfiguration) element, checked); 419 } else if (element instanceof ICommand) { 420 Shell shell= getShell(); 421 if (shell == null) { 422 return; 423 } 424 if (checked) { 425 enableCommand((ICommand)element, checked); 426 } else if (MessageDialog.openConfirm(shell, ExternalToolsUIMessages.BuilderPropertyPage_6, ExternalToolsUIMessages.BuilderPropertyPage_7)) { 427 enableCommand((ICommand)element, checked); 428 } else { 429 viewer.removeCheckStateListener(this); 430 viewer.setChecked(element, true); 431 viewer.addCheckStateListener(this); 432 } 433 } 434 } 435 436 private void enableLaunchConfiguration(ILaunchConfiguration configuration, boolean enable) { 437 ILaunchConfigurationWorkingCopy workingCopy= null; 438 try { 439 if (configuration instanceof ILaunchConfigurationWorkingCopy) { 440 workingCopy = (ILaunchConfigurationWorkingCopy) configuration; 441 } else { 442 TableItem[] items= viewer.getTable().getItems(); 444 for (int i = 0; i < items.length; i++) { 445 TableItem item = items[i]; 446 if (item.getData() == configuration) { 447 workingCopy = configuration.getWorkingCopy(); 448 item.setData(workingCopy); 449 } 450 } 451 } 452 if (workingCopy != null) { 453 workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILDER_ENABLED, enable); 454 } 455 } catch (CoreException e) { 456 return; 457 } 458 userHasMadeChanges= true; 459 } 460 461 private void enableCommand(ICommand command, boolean enable) { 462 Map args= command.getArguments(); 463 if (args == null) { 464 args= new HashMap (1); 465 } 466 args.put(COMMAND_ENABLED, Boolean.valueOf(enable)); 467 command.setArguments(args); 468 userHasMadeChanges= true; 469 } 470 471 475 private void handleImportButtonPressed() { 476 ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager(); 477 List toolTypes= getConfigurationTypes(IExternalToolConstants.ID_EXTERNAL_TOOLS_LAUNCH_CATEGORY); 478 List configurations= new ArrayList (); 479 Iterator iter= toolTypes.iterator(); 480 while (iter.hasNext()) { 481 try { 482 ILaunchConfiguration[] configs= manager.getLaunchConfigurations((ILaunchConfigurationType) iter.next()); 483 for (int i = 0; i < configs.length; i++) { 484 ILaunchConfiguration launchConfiguration = configs[i]; 485 if (!DebugUITools.isPrivate(launchConfiguration)) { 486 configurations.add(launchConfiguration); 487 } 488 } 489 } catch (CoreException e) { 490 } 491 } 492 Shell shell= getShell(); 493 if (shell == null) { 494 return; 495 } 496 ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new BuilderLabelProvider()); 497 dialog.setTitle(ExternalToolsUIMessages.BuilderPropertyPage_4); 498 dialog.setMessage(ExternalToolsUIMessages.BuilderPropertyPage_5); 499 dialog.setElements(configurations.toArray()); 500 if (dialog.open() == Window.CANCEL) { 501 return; 502 } 503 Object results[]= dialog.getResult(); 504 if (results.length == 0) { return; 506 } 507 ILaunchConfiguration config= (ILaunchConfiguration) results[0]; 508 ILaunchConfiguration newConfig= null; 509 boolean wasAutobuilding= ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding(); 510 try { 511 setAutobuild(false); 512 newConfig= BuilderUtils.duplicateConfiguration(getInputProject(), config); 513 } catch (CoreException e) { 514 handleException(e); 515 } finally { 516 try { 517 setAutobuild(wasAutobuilding); 518 } catch (CoreException e) { 519 handleException(e); 520 } 521 } 522 if (newConfig != null) { 523 userHasMadeChanges= true; 524 viewer.add(newConfig); 525 viewer.setChecked(newConfig, isEnabled(newConfig)); 526 newConfigList.add(newConfig); 527 } 528 } 529 530 533 private void handleRemoveButtonPressed() { 534 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); 535 if (selection != null) { 536 int numSelected= selection.size(); 537 userHasMadeChanges= true; 538 Iterator iterator= selection.iterator(); 539 while (iterator.hasNext()) { 540 Object item= iterator.next(); 541 if (item instanceof ILaunchConfiguration) { 542 if (configsToBeDeleted == null) { 543 configsToBeDeleted= new ArrayList (numSelected); 544 } 545 configsToBeDeleted.add(item); 546 } else if (item instanceof ICommand) { 547 if (commandsToBeDeleted == null) { 548 commandsToBeDeleted= new ArrayList (numSelected); 549 } 550 commandsToBeDeleted.add(item); 551 } 552 viewer.remove(item); 553 } 554 } 555 } 556 557 561 private void handleNewButtonPressed() { 562 ILaunchConfigurationType type = promptForConfigurationType(); 563 if (type == null) { 564 return; 565 } 566 boolean wasAutobuilding= ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding(); 567 try { 568 ILaunchConfigurationWorkingCopy workingCopy = null; 569 String name= DebugPlugin.getDefault().getLaunchManager().generateUniqueLaunchConfigurationNameFrom(ExternalToolsUIMessages.BuilderPropertyPage_New_Builder_7); 570 workingCopy = type.newInstance(BuilderUtils.getBuilderFolder(getInputProject(), true), name); 571 572 StringBuffer buffer= new StringBuffer (IExternalToolConstants.BUILD_TYPE_FULL); 573 buffer.append(','); 574 buffer.append(IExternalToolConstants.BUILD_TYPE_INCREMENTAL); 575 buffer.append(','); 576 workingCopy.setAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, buffer.toString()); 577 workingCopy.setAttribute(ExternalToolsMainTab.FIRST_EDIT, true); 578 ILaunchConfiguration config = null; 579 setAutobuild(false); 580 config = workingCopy.doSave(); 581 newConfigList.add(config); 584 int code= editConfiguration(config); 585 if (code == Window.CANCEL) { 586 newConfigList.remove(config); 588 config.delete(); 589 } else { 590 userHasMadeChanges= true; 591 config= (ILaunchConfiguration)newConfigList.get(newConfigList.size() - 1); 594 viewer.add(config); 595 viewer.setChecked(config, isEnabled(config)); 596 } 597 } catch (CoreException e) { 598 handleException(e); 599 } finally { 600 try { 601 setAutobuild(wasAutobuilding); 602 } catch (CoreException e) { 603 handleException(e); 604 } 605 } 606 } 607 608 612 private int editConfiguration(ILaunchConfiguration config) { 613 ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager(); 614 manager.addLaunchConfigurationListener(configurationListener); 615 Shell shell= getShell(); 616 if (shell == null) { 617 return Window.CANCEL; 618 } 619 int code= DebugUITools.openLaunchConfigurationPropertiesDialog(shell, config, IExternalToolConstants.ID_EXTERNAL_TOOLS_BUILDER_LAUNCH_GROUP); 620 manager.removeLaunchConfigurationListener(configurationListener); 621 return code; 622 } 623 624 632 private ILaunchConfigurationType promptForConfigurationType() { 633 List externalToolTypes= getConfigurationTypes(IExternalToolConstants.ID_EXTERNAL_TOOLS_BUILDER_LAUNCH_CATEGORY); 634 Shell shell= getShell(); 635 if (shell == null) { 636 return null; 637 } 638 ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new BuilderLabelProvider()); 639 dialog.setElements(externalToolTypes.toArray()); 640 dialog.setMultipleSelection(false); 641 dialog.setTitle(ExternalToolsUIMessages.BuilderPropertyPage_Choose_configuration_type_8); 642 dialog.setMessage(ExternalToolsUIMessages.BuilderPropertyPage_Choose_an_external_tool_type_to_create_9); 643 dialog.open(); 644 Object result[] = dialog.getResult(); 645 if (result == null || result.length == 0) { 646 return null; 647 } 648 return (ILaunchConfigurationType) result[0]; 649 } 650 651 654 private List getConfigurationTypes(String category) { 655 ILaunchConfigurationType types[] = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes(); 656 List externalToolTypes = new ArrayList (); 657 for (int i = 0; i < types.length; i++) { 658 ILaunchConfigurationType configurationType = types[i]; 659 if (category.equals(configurationType.getCategory())) { 660 externalToolTypes.add(configurationType); 661 } 662 } 663 return externalToolTypes; 664 } 665 666 670 private void handleEditButtonPressed() { 671 TableItem[] items= viewer.getTable().getSelection(); 672 if (items.length == 0) { 673 return; 674 } 675 TableItem selection= items[0]; 676 if (selection != null) { 677 Object data = selection.getData(); 678 if (data instanceof ILaunchConfiguration) { 679 ILaunchConfiguration config= (ILaunchConfiguration) data; 680 if (BuilderUtils.isUnmigratedConfig(config)) { 681 if (!shouldProceedWithMigration()) { 682 return; 683 } 684 try { 685 config= BuilderUtils.migrateBuilderConfiguration(getInputProject(), (ILaunchConfigurationWorkingCopy) config); 686 } catch (CoreException e) { 687 handleException(e); 688 return; 689 } 690 selection.setData(config); 692 } 693 userHasMadeChanges= true; 694 boolean wasAutobuilding= ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding(); 695 try { 696 setAutobuild(false); 697 editConfiguration(config); 698 } catch (CoreException e) { 699 handleException(e); 700 } finally { 701 try { 702 setAutobuild(wasAutobuilding); 703 } catch (CoreException e) { 704 handleException(e); 705 } 706 } 707 } else if (data instanceof ICommand) { 708 ICommand command= (ICommand) data; 709 if (command.isConfigurable()) { 710 if (editCommand(command)) { 711 userHasMadeChanges= true; 712 } 713 } 714 } 715 } 716 } 717 718 private boolean editCommand(ICommand data) { 719 EditCommandDialog dialog= new EditCommandDialog(getShell(), data); 720 return Window.OK == dialog.open(); 721 } 722 723 730 private boolean shouldProceedWithMigration() { 731 if (!ExternalToolsPlugin.getDefault().getPreferenceStore().getBoolean(IPreferenceConstants.PROMPT_FOR_TOOL_MIGRATION)) { 732 return true; 734 } 735 Shell shell= getShell(); 736 if (shell == null) { 737 return false; 738 } 739 MessageDialogWithToggle dialog= MessageDialogWithToggle.openYesNoQuestion(getShell(), 741 ExternalToolsUIMessages.BuilderPropertyPage_Migrate_project_builder_10, 742 ExternalToolsUIMessages.BuilderPropertyPage_Not_Support, 743 ExternalToolsUIMessages.BuilderPropertyPage_Prompt, 744 false, 745 ExternalToolsPlugin.getDefault().getPreferenceStore(), 746 IPreferenceConstants.PROMPT_FOR_TOOL_MIGRATION); 747 return dialog.getReturnCode() == IDialogConstants.YES_ID; 748 } 749 750 753 private void handleException(Exception e) { 754 final IStatus[] status= new IStatus[1]; 755 if (e instanceof CoreException) { 756 status[0] = ((CoreException) e).getStatus(); 757 } else { 758 status[0] = new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, 0, ExternalToolsUIMessages.BuilderPropertyPage_statusMessage, e); 759 } 760 Display.getDefault().asyncExec(new Runnable () { 761 public void run() { 762 Shell shell= getShell(); 763 if (shell != null) { 764 ErrorDialog.openError(shell, ExternalToolsUIMessages.BuilderPropertyPage_errorTitle, 765 ExternalToolsUIMessages.BuilderPropertyPage_errorMessage, 766 status[0]); 767 } 768 } 769 }); 770 } 771 772 776 private void handleTableSelectionChanged() { 777 newButton.setEnabled(true); 778 Table builderTable= viewer.getTable(); 779 TableItem[] items = builderTable.getSelection(); 780 boolean validSelection= items != null && items.length > 0; 781 boolean enableEdit= validSelection; 782 boolean enableRemove= validSelection; 783 boolean enableUp= validSelection; 784 boolean enableDown= validSelection; 785 if (validSelection) { 786 if (items.length > 1) { 787 enableEdit= false; 788 } 789 int indices[]= builderTable.getSelectionIndices(); 790 int max = builderTable.getItemCount(); 791 enableUp= indices[0] != 0; 792 enableDown= indices[indices.length - 1] < max - 1; 793 for (int i = 0; i < items.length; i++) { 794 TableItem item = items[i]; 795 Object data= item.getData(); 796 if (data instanceof ILaunchConfiguration) { 797 ILaunchConfiguration config= (ILaunchConfiguration)data; 798 String builderName= null; 799 try { 800 builderName = config.getAttribute(IExternalToolConstants.ATTR_DISABLED_BUILDER, (String )null); 801 } catch (CoreException e) { 802 } 803 if (builderName != null) { 804 IExtension ext= Platform.getExtensionRegistry().getExtension(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_BUILDERS, builderName); 806 enableEdit= false; 807 enableRemove= ext == null; 808 } 809 } else { 810 if (data instanceof ErrorConfig) { 811 enableEdit= false; 812 continue; 813 } 814 ICommand command= (ICommand) data; 815 enableEdit= command.isConfigurable(); 816 IExtension ext= Platform.getExtensionRegistry().getExtension(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_BUILDERS, command.getBuilderName()); 817 enableRemove= ext == null; 818 break; 819 } 820 } 821 } 822 editButton.setEnabled(enableEdit); 823 removeButton.setEnabled(enableRemove); 824 upButton.setEnabled(enableUp); 825 downButton.setEnabled(enableDown); 826 } 827 828 835 private boolean isEnabled(Object element) { 836 if (element instanceof ICommand) { 837 Boolean enabled= (Boolean )((ICommand) element).getArguments().get(COMMAND_ENABLED); 838 if (enabled != null) { 839 return enabled.booleanValue(); 840 } 841 } else if (element instanceof ILaunchConfiguration) { 842 try { 843 return ExternalToolsUtil.isBuilderEnabled((ILaunchConfiguration) element); 844 } catch (CoreException e) { 845 } 846 } else if (element instanceof ErrorConfig) { 847 return false; 848 } 849 return true; 850 } 851 852 855 private void move(TableItem item, int index) { 856 userHasMadeChanges= true; 857 Object data = item.getData(); 858 item.dispose(); 859 viewer.insert(data, index); 860 viewer.setChecked(data, isEnabled(data)); 861 } 862 863 866 private void moveSelectionDown() { 867 Table builderTable= viewer.getTable(); 868 int indices[]= builderTable.getSelectionIndices(); 869 if (indices.length < 1) { 870 return; 871 } 872 int newSelection[]= new int[indices.length]; 873 int max= builderTable.getItemCount() - 1; 874 for (int i = indices.length - 1; i >= 0; i--) { 875 int index= indices[i]; 876 if (index < max) { 877 move (builderTable.getItem(index), index + 1); 878 newSelection[i]= index + 1; 879 } 880 } 881 builderTable.setSelection(newSelection); 882 } 883 884 887 private void moveSelectionUp() { 888 Table builderTable= viewer.getTable(); 889 int indices[]= builderTable.getSelectionIndices(); 890 int newSelection[]= new int[indices.length]; 891 for (int i = 0; i < indices.length; i++) { 892 int index= indices[i]; 893 if (index > 0) { 894 move (builderTable.getItem(index), index - 1); 895 newSelection[i]= index - 1; 896 } 897 } 898 builderTable.setSelection(newSelection); 899 } 900 901 904 public boolean performOk() { 905 if (!userHasMadeChanges) { 906 return super.performOk(); 907 } 908 userHasMadeChanges= false; 909 Table builderTable= viewer.getTable(); 910 int numCommands = builderTable.getItemCount(); 911 final Object [] itemData= new Object [numCommands]; 912 for (int i = 0; i < numCommands; i++) { 913 itemData[i]= builderTable.getItem(i).getData(); 914 } 915 IRunnableWithProgress runnable= new IRunnableWithProgress() { 916 public void run(IProgressMonitor monitor) 917 throws InvocationTargetException , InterruptedException { 918 doPerformOk(monitor, itemData); 919 if (monitor.isCanceled()) { 920 throw new InterruptedException (); 921 } 922 } 923 }; 924 925 IProgressService service= PlatformUI.getWorkbench().getProgressService(); 926 try { 927 service.busyCursorWhile(runnable); 928 } catch (InvocationTargetException e) { 929 return false; 930 } catch (InterruptedException e) { 931 return false; 932 } 933 return super.performOk(); 934 } 935 936 private void doPerformOk(IProgressMonitor monitor, Object [] itemData) { 937 if (monitor.isCanceled()) { 938 return; 939 } 940 941 IProject project = getInputProject(); 942 int numCommands = itemData.length; 944 monitor.beginTask(ExternalToolsUIMessages.BuilderPropertyPage_3, numCommands + 1); 945 List possibleCommands= new ArrayList (numCommands); 946 for (int i = 0; i < numCommands; i++) { 947 Object data = itemData[i]; 948 if (data instanceof ICommand) { 949 if (commandsToBeDeleted != null && commandsToBeDeleted.contains(data)) { 950 data= null; 952 } 953 ICommand command= (ICommand)data; 954 Map args= command.getArguments(); 955 Boolean enabled= (Boolean )args.get(COMMAND_ENABLED); 956 if (enabled != null && enabled.equals(Boolean.FALSE)) { 957 ILaunchConfiguration config= disableCommand(command); 958 if (config != null) { 959 data= BuilderUtils.commandFromLaunchConfig(project,config); 960 } 961 } else { 962 args.remove(COMMAND_ENABLED); 963 command.setArguments(args); 964 } 965 } else if (data instanceof ILaunchConfiguration) { 966 ILaunchConfiguration config= (ILaunchConfiguration) data; 967 String disabledBuilderName; 968 try { 969 disabledBuilderName = config.getAttribute(IExternalToolConstants.ATTR_DISABLED_BUILDER, (String )null); 970 if (disabledBuilderName != null && ExternalToolsUtil.isBuilderEnabled(config)) { 971 possibleCommands.add(translateBackToCommand(config, project)); 972 continue; 973 } 974 } catch (CoreException e1) { 975 } 976 977 if (!BuilderUtils.isUnmigratedConfig(config) && (config instanceof ILaunchConfigurationWorkingCopy)) { 978 ILaunchConfigurationWorkingCopy workingCopy= ((ILaunchConfigurationWorkingCopy) config); 979 if (workingCopy.isDirty()) { 981 try { 982 workingCopy.doSave(); 983 } catch (CoreException e) { 984 Shell shell= getShell(); 985 if (shell != null) { 986 MessageDialog.openError(shell, ExternalToolsUIMessages.BuilderPropertyPage_39, NLS.bind(ExternalToolsUIMessages.BuilderPropertyPage_40, new String [] {workingCopy.getName()})); 987 } 988 } 989 } 990 } 991 data= BuilderUtils.commandFromLaunchConfig(project, config); 992 } else if (data instanceof ErrorConfig) { 993 data= ((ErrorConfig) data).getCommand(); 994 } 995 if (data != null) { 996 possibleCommands.add(data); 997 } 998 monitor.worked(1); 999 } 1000 ICommand[] commands= new ICommand[possibleCommands.size()]; 1001 possibleCommands.toArray(commands); 1002 if (checkCommandsForChange(commands)) { 1003 try { 1005 IProjectDescription desc = project.getDescription(); 1006 desc.setBuildSpec(commands); 1007 project.setDescription(desc, IResource.FORCE, monitor); 1008 } catch (CoreException e) { 1009 handleException(e); 1010 performCancel(); 1011 } 1012 } 1013 1014 if (configsToBeDeleted != null) { 1015 deleteConfigurations(); 1016 } 1017 monitor.done(); 1018 } 1019 1020 private void checkBuilderFolder() { 1021 try { 1022 IFolder builderFolder= BuilderUtils.getBuilderFolder(getInputProject(), false); 1023 if (builderFolder != null && builderFolder.exists() && builderFolder.members().length == 0) { 1024 builderFolder.delete(true, false, null); 1026 } 1027 } catch (CoreException e) { 1028 handleException(e); 1029 } 1030 } 1031 1032 1037 private ICommand translateBackToCommand(ILaunchConfiguration config, IProject project) { 1038 try { 1039 ICommand newCommand = project.getDescription().newCommand(); 1040 String builderName= config.getAttribute(IExternalToolConstants.ATTR_DISABLED_BUILDER, (String )null); 1041 Map args= config.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, new HashMap (0)); 1042 1043 newCommand.setBuilderName(builderName); 1044 newCommand.setArguments(args); 1045 if (configsToBeDeleted == null) { 1046 configsToBeDeleted= new ArrayList (); 1047 } 1048 configsToBeDeleted.add(config); 1049 return newCommand; 1050 } catch (CoreException exception) { 1051 Shell shell= getShell(); 1052 if (shell != null) { 1053 MessageDialog.openError(shell, ExternalToolsUIMessages.BuilderPropertyPage_13, ExternalToolsUIMessages.BuilderPropertyPage_error); 1054 } 1055 return null; 1056 } 1057 } 1058 1059 1063 private ILaunchConfiguration disableCommand(ICommand command) { 1064 Map arguments= command.getArguments(); 1065 if (arguments != null) { 1066 arguments.remove(COMMAND_ENABLED); 1067 } 1068 List externalToolTypes= getConfigurationTypes(IExternalToolConstants.ID_EXTERNAL_TOOLS_BUILDER_LAUNCH_CATEGORY); 1069 if (externalToolTypes.size() == 0) { 1070 return null; 1071 } 1072 ILaunchConfigurationType type= (ILaunchConfigurationType)externalToolTypes.get(0); 1073 if (type == null) { 1074 return null; 1075 } 1076 boolean wasAutobuilding= ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding(); 1077 try { 1078 ILaunchConfigurationWorkingCopy workingCopy = null; 1079 String builderName = command.getBuilderName(); 1080 String name= DebugPlugin.getDefault().getLaunchManager().generateUniqueLaunchConfigurationNameFrom(builderName); 1081 workingCopy = type.newInstance(BuilderUtils.getBuilderFolder(getInputProject(), true), name); 1082 1083 workingCopy.setAttribute(IExternalToolConstants.ATTR_DISABLED_BUILDER, builderName); 1084 if (arguments != null) { 1085 workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments); 1086 } 1087 workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILDER_ENABLED, false); 1088 ILaunchConfiguration config = null; 1089 setAutobuild(false); 1090 config = workingCopy.doSave(); 1091 return config; 1092 } catch (CoreException e) { 1093 handleException(e); 1094 } finally { 1095 try { 1096 setAutobuild(wasAutobuilding); 1097 } catch (CoreException e) { 1098 handleException(e); 1099 } 1100 } 1101 return null; 1102 } 1103 1104 private void deleteConfigurations() { 1105 boolean wasAutobuilding= ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding(); 1106 try { 1107 setAutobuild(false); 1108 1109 Iterator itr= configsToBeDeleted.iterator(); 1110 while (itr.hasNext()) { 1111 ILaunchConfiguration element = (ILaunchConfiguration) itr.next(); 1112 element.delete(); 1113 } 1114 1115 checkBuilderFolder(); 1116 } catch (CoreException e) { 1117 handleException(e); 1118 } finally { 1119 try { 1120 setAutobuild(wasAutobuilding); 1121 } catch (CoreException e) { 1122 handleException(e); 1123 } 1124 } 1125 } 1126 1127 1130 private boolean checkCommandsForChange(ICommand[] newCommands) { 1131 try { 1132 ICommand[] oldCommands = getInputProject().getDescription().getBuildSpec(); 1133 if (oldCommands.length != newCommands.length) { 1134 return true; 1135 } 1136 IgnoreWhiteSpaceComparator comparator= new IgnoreWhiteSpaceComparator(); 1137 for (int i = 0; i < oldCommands.length; i++) { 1138 ICommand oldCommand = oldCommands[i]; 1139 ICommand newCommand= newCommands[i]; 1140 String oldName= oldCommand.getBuilderName(); 1141 String newName= newCommand.getBuilderName(); 1142 if (oldName == null && newName != null) { 1143 return true; 1144 } 1145 1146 if(oldName != null && !oldName.equals(newName)) { 1147 return true; 1148 } 1149 Map oldArgs= oldCommand.getArguments(); 1150 Map newArgs= newCommand.getArguments(); 1151 if (oldArgs == null && newArgs != null) { 1152 return true; 1153 } 1154 if (oldArgs == null && newArgs == null) { 1155 continue; 1156 } 1157 if(oldArgs.size() != newArgs.size()) { 1158 return true; 1159 } 1160 Iterator keySet= oldArgs.keySet().iterator(); 1161 while (keySet.hasNext()) { 1162 Object key = keySet.next(); 1163 Object oldArg= oldArgs.get(key); 1164 Object newArg= newArgs.get(key); 1165 if (oldArg instanceof String && newArg instanceof String ) { 1166 if (comparator.compare(oldArg, newArg) != 0) { 1167 return true; 1168 } 1169 } else if (!oldArg.equals(newArg)){ 1170 return true; 1171 } 1172 } 1173 1174 if (oldCommand.isBuilding(IncrementalProjectBuilder.AUTO_BUILD) != newCommand.isBuilding(IncrementalProjectBuilder.AUTO_BUILD) 1175 || oldCommand.isBuilding(IncrementalProjectBuilder.CLEAN_BUILD) != newCommand.isBuilding(IncrementalProjectBuilder.CLEAN_BUILD) 1176 || oldCommand.isBuilding(IncrementalProjectBuilder.INCREMENTAL_BUILD) != newCommand.isBuilding(IncrementalProjectBuilder.INCREMENTAL_BUILD) 1177 || oldCommand.isBuilding(IncrementalProjectBuilder.FULL_BUILD) != newCommand.isBuilding(IncrementalProjectBuilder.FULL_BUILD)) { 1178 return true; 1179 } 1180 } 1181 } catch (CoreException ce) { 1182 return true; 1183 } 1184 return false; 1185 } 1186 1187 1190 public boolean performCancel() { 1191 Iterator iter= newConfigList.iterator(); 1192 while (iter.hasNext()) { 1193 try { 1194 ((ILaunchConfiguration) iter.next()).delete(); 1195 } catch (CoreException e) { 1196 handleException(e); 1197 } 1198 } 1199 1200 checkBuilderFolder(); 1201 1202 Table builderTable= viewer.getTable(); 1204 int numCommands = builderTable.getItemCount(); 1205 for (int i = 0; i < numCommands; i++) { 1206 Object data = builderTable.getItem(i).getData(); 1207 if (data instanceof ICommand) { 1208 ICommand command= (ICommand)data; 1209 Map args= command.getArguments(); 1210 args.remove(COMMAND_ENABLED); 1211 command.setArguments(args); 1212 } 1213 } 1214 return super.performCancel(); 1215 } 1216 1217 public Shell getShell() { 1218 if (getControl().isDisposed()) { 1219 return null; 1220 } 1221 return super.getShell(); 1222 } 1223} 1224 | Popular Tags |