1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Locale ; 17 import java.util.Set ; 18 import java.util.TreeSet ; 19 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IAdaptable; 24 import org.eclipse.core.runtime.NullProgressMonitor; 25 import org.eclipse.core.runtime.Preferences; 26 import org.eclipse.debug.core.ILaunchConfiguration; 27 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 28 import org.eclipse.jdt.core.IJavaProject; 29 import org.eclipse.jface.dialogs.MessageDialog; 30 import org.eclipse.jface.viewers.CheckStateChangedEvent; 31 import org.eclipse.jface.viewers.CheckboxTreeViewer; 32 import org.eclipse.jface.viewers.ICheckStateListener; 33 import org.eclipse.jface.viewers.ILabelProvider; 34 import org.eclipse.jface.viewers.ITreeContentProvider; 35 import org.eclipse.jface.window.Window; 36 import org.eclipse.osgi.util.NLS; 37 import org.eclipse.pde.core.plugin.IPluginModelBase; 38 import org.eclipse.pde.core.plugin.ModelEntry; 39 import org.eclipse.pde.core.plugin.PluginRegistry; 40 import org.eclipse.pde.internal.core.DependencyManager; 41 import org.eclipse.pde.internal.core.ICoreConstants; 42 import org.eclipse.pde.internal.core.PDECore; 43 import org.eclipse.pde.internal.ui.PDEPlugin; 44 import org.eclipse.pde.internal.ui.PDEPluginImages; 45 import org.eclipse.pde.internal.ui.PDEUIMessages; 46 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 47 import org.eclipse.pde.internal.ui.elements.NamedElement; 48 import org.eclipse.pde.internal.ui.util.PersistablePluginObject; 49 import org.eclipse.pde.internal.ui.util.SWTUtil; 50 import org.eclipse.pde.internal.ui.wizards.ListUtil; 51 import org.eclipse.pde.ui.launcher.AbstractLauncherTab; 52 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 53 import org.eclipse.swt.SWT; 54 import org.eclipse.swt.SWTException; 55 import org.eclipse.swt.events.SelectionAdapter; 56 import org.eclipse.swt.events.SelectionEvent; 57 import org.eclipse.swt.graphics.Image; 58 import org.eclipse.swt.layout.GridData; 59 import org.eclipse.swt.layout.GridLayout; 60 import org.eclipse.swt.widgets.Button; 61 import org.eclipse.swt.widgets.Composite; 62 import org.eclipse.swt.widgets.Control; 63 import org.eclipse.swt.widgets.Label; 64 import org.eclipse.swt.widgets.Shell; 65 import org.eclipse.ui.IWorkingSet; 66 import org.eclipse.ui.IWorkingSetManager; 67 import org.eclipse.ui.PlatformUI; 68 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog; 69 70 public abstract class AbstractPluginBlock { 71 72 protected AbstractLauncherTab fTab; 73 74 protected CheckboxTreeViewer fPluginTreeViewer; 75 protected NamedElement fWorkspacePlugins; 76 protected NamedElement fExternalPlugins; 77 protected IPluginModelBase[] fExternalModels; 78 protected IPluginModelBase[] fWorkspaceModels; 79 protected int fNumExternalChecked; 80 protected int fNumWorkspaceChecked; 81 82 private Button fIncludeOptionalButton; 83 protected Button fAddWorkspaceButton; 84 private Button fAutoValidate; 85 86 87 private Button fSelectAllButton; 88 private Button fDeselectButton; 89 private Button fWorkingSetButton; 90 private Button fAddRequiredButton; 91 private Button fDefaultsButton; 92 93 private Listener fListener = new Listener(); 94 95 private Label fCounter; 96 97 private LaunchValidationOperation fOperation; 98 private PluginStatusDialog fDialog; 99 100 private Button fValidateButton; 101 102 class Listener extends SelectionAdapter { 103 public void widgetSelected(SelectionEvent e) { 104 Object source = e.getSource(); 105 if (source == fSelectAllButton) { 106 toggleGroups(true); 107 } else if (source == fDeselectButton) { 108 toggleGroups(false); 109 } else if (source == fWorkingSetButton) { 110 handleWorkingSets(); 111 } else if (source == fAddRequiredButton) { 112 computeSubset(); 113 } else if (source == fDefaultsButton) { 114 handleRestoreDefaults(); 115 } else if (source == fValidateButton) { 116 handleValidate(); 117 } 118 fTab.updateLaunchConfigurationDialog(); 119 } 120 } 121 122 class PluginContentProvider extends DefaultContentProvider implements 123 ITreeContentProvider { 124 public boolean hasChildren(Object parent) { 125 return !(parent instanceof IPluginModelBase); 126 } 127 128 public Object [] getChildren(Object parent) { 129 if (parent == fExternalPlugins) 130 return fExternalModels; 131 if (parent == fWorkspacePlugins) 132 return fWorkspaceModels; 133 return new Object [0]; 134 } 135 136 public Object getParent(Object child) { 137 if (child instanceof IPluginModelBase) { 138 IResource resource = ((IPluginModelBase)child).getUnderlyingResource(); 139 return resource == null ? fExternalPlugins : fWorkspacePlugins; 140 } 141 return null; 142 } 143 144 public Object [] getElements(Object input) { 145 ArrayList list = new ArrayList (); 146 if (fWorkspaceModels.length > 0) 147 list.add(fWorkspacePlugins); 148 if (fExternalModels.length > 0) 149 list.add(fExternalPlugins); 150 return list.toArray(); 151 } 152 } 153 154 public AbstractPluginBlock(AbstractLauncherTab tab) { 155 fTab = tab; 156 PDEPlugin.getDefault().getLabelProvider().connect(this); 157 fExternalModels = getExternalModels(); fWorkspaceModels = PluginRegistry.getWorkspaceModels(); 159 } 160 161 protected IPluginModelBase[] getExternalModels() { 162 Preferences pref = PDECore.getDefault().getPluginPreferences(); 163 String saved = pref.getString(ICoreConstants.CHECKED_PLUGINS); 164 if (saved.equals(ICoreConstants.VALUE_SAVED_NONE)) 165 return new IPluginModelBase[0]; 166 167 IPluginModelBase[] models = PluginRegistry.getExternalModels(); 168 if (saved.equals(ICoreConstants.VALUE_SAVED_ALL)) 169 return models; 170 171 ArrayList list = new ArrayList (models.length); 172 for (int i = 0; i < models.length; i++) { 173 if (models[i].isEnabled()) { 174 list.add(models[i]); 175 } 176 } 177 return (IPluginModelBase[])list.toArray(new IPluginModelBase[list.size()]); 178 } 179 180 protected void updateCounter() { 181 if (fCounter != null) { 182 int checked = fNumExternalChecked + fNumWorkspaceChecked; 183 int total = fWorkspaceModels.length + fExternalModels.length; 184 fCounter.setText(NLS.bind(PDEUIMessages.AbstractPluginBlock_counter, new Integer (checked), new Integer (total))); 185 } 186 } 187 188 public void createControl(Composite parent, int span, int indent) { 189 createPluginViewer(parent, span - 1, indent); 190 createButtonContainer(parent); 191 fIncludeOptionalButton = createButton(parent, 192 span, 193 indent, 194 NLS.bind(PDEUIMessages.AdvancedLauncherTab_includeOptional, 195 fTab.getName().toLowerCase(Locale.ENGLISH))); 196 fAddWorkspaceButton = createButton(parent, 197 span, 198 indent, 199 NLS.bind(PDEUIMessages.AdvancedLauncherTab_addNew, 200 fTab.getName().toLowerCase(Locale.ENGLISH))); 201 202 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 203 gd.horizontalSpan = span; 204 Label label = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); 205 label.setLayoutData(gd); 206 207 fAutoValidate = createButton(parent, 208 span - 1, 209 indent, 210 NLS.bind(PDEUIMessages.PluginsTabToolBar_auto_validate, fTab.getName().replaceAll("&", "").toLowerCase(Locale.ENGLISH))); 212 fValidateButton = new Button(parent, SWT.PUSH); 213 fValidateButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); 214 fValidateButton.setText(NLS.bind(PDEUIMessages.PluginsTabToolBar_validate, fTab.getName().replaceAll("&", ""))); SWTUtil.setButtonDimensionHint(fValidateButton); 216 fValidateButton.addSelectionListener(fListener); 217 } 218 219 private Button createButton(Composite parent, int span, int indent, String text) { 220 Button button = new Button(parent, SWT.CHECK); 221 button.setText(text); 222 223 GridData gd = new GridData(); 224 gd.horizontalSpan = span; 225 gd.horizontalIndent = indent; 226 button.setLayoutData(gd); 227 button.addSelectionListener(fListener); 228 229 return button; 230 } 231 232 protected ILabelProvider getLabelProvider() { 233 return PDEPlugin.getDefault().getLabelProvider(); 234 } 235 236 protected void createPluginViewer(Composite composite, int span, int indent) { 237 fPluginTreeViewer = new CheckboxTreeViewer(composite, getTreeViewerStyle()); 238 fPluginTreeViewer.setContentProvider(new PluginContentProvider()); 239 fPluginTreeViewer.setLabelProvider(getLabelProvider()); 240 fPluginTreeViewer.setAutoExpandLevel(2); 241 fPluginTreeViewer.addCheckStateListener(new ICheckStateListener() { 242 public void checkStateChanged(final CheckStateChangedEvent event) { 243 Object element = event.getElement(); 244 if (element instanceof IPluginModelBase) { 245 handleCheckStateChanged(event); 246 } else { 247 handleGroupStateChanged(element, event.getChecked()); 248 } 249 fTab.updateLaunchConfigurationDialog(); 250 } 251 }); 252 fPluginTreeViewer.setComparator(new ListUtil.PluginComparator() { 253 public int category(Object obj) { 254 if (obj == fWorkspacePlugins) 255 return -1; 256 return 0; 257 } 258 }); 259 260 GridData gd = new GridData(GridData.FILL_BOTH); 261 gd.horizontalSpan = span; 262 gd.horizontalIndent = indent; 263 fPluginTreeViewer.getTree().setLayoutData(gd); 264 265 Image siteImage = 266 PDEPlugin.getDefault().getLabelProvider().get( 267 PDEPluginImages.DESC_SITE_OBJ); 268 269 fWorkspacePlugins = 270 new NamedElement( 271 PDEUIMessages.AdvancedLauncherTab_workspacePlugins, 272 siteImage); 273 fExternalPlugins = 274 new NamedElement( 275 PDEUIMessages.PluginsTab_target, 276 siteImage); 277 } 278 279 private void createButtonContainer(Composite parent) { 280 Composite composite = new Composite(parent, SWT.NONE); 281 GridLayout layout = new GridLayout(); 282 layout.marginHeight = layout.marginWidth = 0; 283 composite.setLayout(layout); 284 composite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 285 286 new Label(composite, SWT.NONE); 287 fSelectAllButton = createButton(composite, PDEUIMessages.AdvancedLauncherTab_selectAll); 288 fDeselectButton = createButton(composite, PDEUIMessages.AdvancedLauncherTab_deselectAll); 289 fWorkingSetButton = createButton(composite, PDEUIMessages.AdvancedLauncherTab_workingSet); 290 fAddRequiredButton = createButton(composite, NLS.bind(PDEUIMessages.AdvancedLauncherTab_subset, fTab.getName())); 291 fDefaultsButton = createButton(composite, PDEUIMessages.AdvancedLauncherTab_defaults); 292 293 fCounter = new Label(composite, SWT.NONE); 294 fCounter.setLayoutData(new GridData(GridData.FILL_BOTH|GridData.VERTICAL_ALIGN_END)); 295 updateCounter(); 296 } 297 298 protected int getTreeViewerStyle() { 299 return SWT.BORDER; 300 } 301 302 private Button createButton(Composite composite, String text) { 303 Button button = new Button(composite, SWT.PUSH); 304 button.setText(text); 305 button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 306 SWTUtil.setButtonDimensionHint(button); 307 button.addSelectionListener(fListener); 308 return button; 309 } 310 311 protected void handleCheckStateChanged(CheckStateChangedEvent event) { 312 IPluginModelBase model = (IPluginModelBase)event.getElement(); 313 if (model.getUnderlyingResource() == null) { 314 if (event.getChecked()) { 315 fNumExternalChecked += 1; 316 } else { 317 fNumExternalChecked -= 1; 318 } 319 } else { 320 if (event.getChecked()) { 321 fNumWorkspaceChecked += 1; 322 } else { 323 fNumWorkspaceChecked -= 1; 324 } 325 } 326 adjustGroupState(); 327 } 328 329 protected void handleGroupStateChanged(Object group, boolean checked) { 330 fPluginTreeViewer.setSubtreeChecked(group, checked); 331 fPluginTreeViewer.setGrayed(group, false); 332 333 if (group == fWorkspacePlugins) 334 fNumWorkspaceChecked = checked ? fWorkspaceModels.length : 0; 335 else if (group == fExternalPlugins) 336 fNumExternalChecked = checked ? fExternalModels.length : 0; 337 338 } 339 340 protected void toggleGroups(boolean select) { 341 handleGroupStateChanged(fWorkspacePlugins, select); 342 handleGroupStateChanged(fExternalPlugins, select); 343 } 344 345 private void handleWorkingSets() { 346 IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); 347 IWorkingSetSelectionDialog dialog = workingSetManager.createWorkingSetSelectionDialog(getShell(), true); 348 if (dialog.open() == Window.OK) { 349 String [] ids = getPluginIDs(dialog.getSelection()); 350 for (int i = 0; i < ids.length; i++) { 351 IPluginModelBase model = PluginRegistry.findModel(ids[i]); 352 if (model != null) { 353 if (!fPluginTreeViewer.getChecked(model)) { 354 setChecked(model, true); 355 if (model.getUnderlyingResource() == null) 356 fNumExternalChecked += 1; 357 else 358 fNumWorkspaceChecked += 1; 359 } 360 } 361 } 362 adjustGroupState(); 363 } 364 } 365 366 protected void setChecked(IPluginModelBase model, boolean checked) { 367 fPluginTreeViewer.setChecked(model, checked); 368 } 369 370 private String [] getPluginIDs(IWorkingSet[] workingSets) { 371 HashSet set = new HashSet (); 372 for (int i = 0; i < workingSets.length; i++) { 373 IAdaptable[] elements = workingSets[i].getElements(); 374 for (int j = 0; j < elements.length; j++) { 375 Object element = elements[j]; 376 if (element instanceof PersistablePluginObject) { 377 set.add(((PersistablePluginObject)element).getPluginID()); 378 } else { 379 if (element instanceof IJavaProject) 380 element = ((IJavaProject)element).getProject(); 381 if (element instanceof IProject) { 382 IPluginModelBase model = PluginRegistry.findModel((IProject)element); 383 if (model != null) 384 set.add(model.getPluginBase().getId()); 385 } 386 } 387 } 388 } 389 return (String [])set.toArray(new String [set.size()]); 390 } 391 392 public void initializeFrom(ILaunchConfiguration config) throws CoreException { 393 fIncludeOptionalButton.setSelection(config.getAttribute(IPDELauncherConstants.INCLUDE_OPTIONAL, true)); 394 fAddWorkspaceButton.setSelection(config.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true)); 395 fAutoValidate.setSelection(config.getAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, false)); 396 if (fPluginTreeViewer.getInput() == null) { 397 fPluginTreeViewer.setUseHashlookup(true); 398 fPluginTreeViewer.setInput(PDEPlugin.getDefault()); 399 fPluginTreeViewer.reveal(fWorkspacePlugins); 400 } 401 } 402 403 protected void computeSubset() { 404 Object [] checked = fPluginTreeViewer.getCheckedElements(); 405 ArrayList toCheck = new ArrayList (checked.length); 406 for (int i = 0; i < checked.length; i++) 407 if (checked[i] instanceof IPluginModelBase) 408 toCheck.add(checked[i]); 409 410 Set additionalIds = DependencyManager.getDependencies(checked, fIncludeOptionalButton.getSelection()); 411 412 Iterator it = additionalIds.iterator(); 413 while (it.hasNext()) { 414 String id = (String )it.next(); 415 if (findPlugin(id) == null) { 416 ModelEntry entry = PluginRegistry.findEntry(id); 417 if (entry != null) { 418 IPluginModelBase model = entry.getModel(); 419 if (model != null) 420 toCheck.add(model); 421 } 422 } 423 } 424 425 checked = toCheck.toArray(); 426 setCheckedElements(checked); 427 fNumExternalChecked = 0; 428 fNumWorkspaceChecked = 0; 429 for (int i = 0; i < checked.length; i++) { 430 if (((IPluginModelBase)checked[i]).getUnderlyingResource() != null) 431 fNumWorkspaceChecked += 1; 432 else 433 fNumExternalChecked += 1; 434 } 435 adjustGroupState(); 436 } 437 438 protected void setCheckedElements(Object [] checked) { 439 fPluginTreeViewer.setCheckedElements(checked); 440 } 441 442 protected IPluginModelBase findPlugin(String id) { 443 ModelEntry entry = PluginRegistry.findEntry(id); 444 if (entry != null) { 445 IPluginModelBase model = entry.getModel(); 446 if (fPluginTreeViewer.getChecked(model)) 447 return model; 448 449 IPluginModelBase[] models = entry.getWorkspaceModels(); 450 for (int i = 0; i < models.length; i++) { 451 if (fPluginTreeViewer.getChecked(models[i])) 452 return models[i]; 453 } 454 455 models = entry.getExternalModels(); 456 for (int i = 0; i < models.length; i++) { 457 if (fPluginTreeViewer.getChecked(models[i])) 458 return models[i]; 459 } 460 return null; 461 } 462 return null; 463 } 464 465 protected void adjustGroupState() { 466 fPluginTreeViewer.setChecked(fExternalPlugins, fNumExternalChecked > 0); 467 fPluginTreeViewer.setGrayed( 468 fExternalPlugins, 469 fNumExternalChecked > 0 && fNumExternalChecked < fExternalModels.length); 470 fPluginTreeViewer.setChecked(fWorkspacePlugins, fNumWorkspaceChecked > 0); 471 fPluginTreeViewer.setGrayed( 472 fWorkspacePlugins, 473 fNumWorkspaceChecked > 0 && fNumWorkspaceChecked < fWorkspaceModels.length); 474 } 475 476 public void performApply(ILaunchConfigurationWorkingCopy config) { 477 config.setAttribute(IPDELauncherConstants.INCLUDE_OPTIONAL, fIncludeOptionalButton.getSelection()); 478 config.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, fAddWorkspaceButton.getSelection()); 479 config.setAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, fAutoValidate.getSelection()); 480 savePluginState(config); 481 updateCounter(); 482 } 483 484 protected abstract void savePluginState(ILaunchConfigurationWorkingCopy config); 485 486 public void setDefaults(ILaunchConfigurationWorkingCopy config) { 487 config.setAttribute(IPDELauncherConstants.INCLUDE_OPTIONAL, true); 488 config.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true); 489 config.setAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, false); 490 } 491 492 public void enableViewer(boolean enable) { 493 fPluginTreeViewer.getTree().setEnabled(enable); 494 fAddRequiredButton.setEnabled(enable); 495 fDefaultsButton.setEnabled(enable); 496 fWorkingSetButton.setEnabled(enable); 497 fSelectAllButton.setEnabled(enable); 498 fDeselectButton.setEnabled(enable); 499 fIncludeOptionalButton.setEnabled(enable); 500 fAddWorkspaceButton.setEnabled(enable); 501 fCounter.setEnabled(enable); 502 } 503 504 public void dispose() { 505 PDEPlugin.getDefault().getLabelProvider().disconnect(this); 506 } 507 508 protected boolean isEnabled() { 509 return fPluginTreeViewer.getTree().isEnabled(); 510 } 511 512 protected void handleRestoreDefaults() { 513 TreeSet wtable = new TreeSet (); 514 fNumWorkspaceChecked = 0; 515 fNumExternalChecked = 0; 516 517 for (int i = 0; i < fWorkspaceModels.length; i++) { 518 IPluginModelBase model = fWorkspaceModels[i]; 519 fNumWorkspaceChecked += 1; 520 String id = model.getPluginBase().getId(); 521 if (id != null) 522 wtable.add(model.getPluginBase().getId()); 523 } 524 fPluginTreeViewer.setSubtreeChecked(fWorkspacePlugins, true); 525 526 fNumExternalChecked = 0; 527 for (int i = 0; i < fExternalModels.length; i++) { 528 IPluginModelBase model = fExternalModels[i]; 529 boolean masked = wtable.contains(model.getPluginBase().getId()); 530 if (!masked && model.isEnabled()) { 531 fPluginTreeViewer.setChecked(model, true); 532 fNumExternalChecked += 1; 533 } 534 } 535 adjustGroupState(); 536 } 537 538 protected Shell getShell() { 539 try { 541 Control c = fTab.getControl(); 542 if (!c.isDisposed()) 543 return c.getShell(); 544 } catch (SWTException e) { 545 } 546 return PDEPlugin.getActiveWorkbenchShell(); 547 } 548 549 public void handleValidate() { 550 if (fOperation == null) 551 fOperation = createValidationOperation(); 552 try { 553 fOperation.run(new NullProgressMonitor()); 554 } catch (CoreException e) { 555 PDEPlugin.log(e); 556 } 557 if (fDialog == null) { 558 if (fOperation.hasErrors()) { 559 fDialog = new PluginStatusDialog(getShell(), SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE | SWT.RESIZE); 560 fDialog.setInput(fOperation.getInput()); 561 fDialog.open(); 562 fDialog = null; 563 } else if (fOperation.isEmpty()) { 564 MessageDialog.openInformation(PDEPlugin.getActiveWorkbenchShell(), 565 PDEUIMessages.PluginStatusDialog_pluginValidation, 566 NLS.bind(PDEUIMessages.AbstractLauncherToolbar_noSelection, fTab.getName().toLowerCase(Locale.ENGLISH))); 567 } else { 568 MessageDialog.openInformation(PDEPlugin.getActiveWorkbenchShell(), 569 PDEUIMessages.PluginStatusDialog_pluginValidation, 570 PDEUIMessages.AbstractLauncherToolbar_noProblems); 571 } 572 } else { 573 fDialog.refresh(fOperation.getInput()); 574 } 575 } 576 577 protected abstract LaunchValidationOperation createValidationOperation(); 578 579 } 580 | Popular Tags |