1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaProject; 22 import org.eclipse.jdt.core.IPackageFragment; 23 import org.eclipse.jdt.core.IPackageFragmentRoot; 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.jdt.ui.ISharedImages; 27 import org.eclipse.jdt.ui.JavaUI; 28 import org.eclipse.jdt.ui.actions.FindReferencesInWorkingSetAction; 29 import org.eclipse.jdt.ui.actions.ShowInPackageViewAction; 30 import org.eclipse.jface.action.Action; 31 import org.eclipse.jface.action.IMenuManager; 32 import org.eclipse.jface.action.Separator; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.viewers.LabelProvider; 36 import org.eclipse.jface.viewers.StructuredSelection; 37 import org.eclipse.jface.viewers.TableViewer; 38 import org.eclipse.jface.viewers.Viewer; 39 import org.eclipse.jface.viewers.ViewerComparator; 40 import org.eclipse.jface.window.Window; 41 import org.eclipse.osgi.service.resolver.BundleDescription; 42 import org.eclipse.osgi.service.resolver.BundleSpecification; 43 import org.eclipse.osgi.service.resolver.ExportPackageDescription; 44 import org.eclipse.osgi.service.resolver.State; 45 import org.eclipse.pde.core.IBaseModel; 46 import org.eclipse.pde.core.IModel; 47 import org.eclipse.pde.core.IModelChangedEvent; 48 import org.eclipse.pde.core.IModelChangedListener; 49 import org.eclipse.pde.core.plugin.IPluginImport; 50 import org.eclipse.pde.core.plugin.IPluginModelBase; 51 import org.eclipse.pde.core.plugin.PluginRegistry; 52 import org.eclipse.pde.internal.core.ICoreConstants; 53 import org.eclipse.pde.internal.core.TargetPlatformHelper; 54 import org.eclipse.pde.internal.core.WorkspaceModelManager; 55 import org.eclipse.pde.internal.core.bundle.BundlePluginBase; 56 import org.eclipse.pde.internal.core.ibundle.IBundle; 57 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 58 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 59 import org.eclipse.pde.internal.core.text.bundle.Bundle; 60 import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader; 61 import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject; 62 import org.eclipse.pde.internal.core.text.bundle.ImportPackageHeader; 63 import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject; 64 import org.eclipse.pde.internal.core.text.bundle.PackageObject; 65 import org.eclipse.pde.internal.core.util.PDEJavaHelper; 66 import org.eclipse.pde.internal.ui.PDELabelProvider; 67 import org.eclipse.pde.internal.ui.PDEPlugin; 68 import org.eclipse.pde.internal.ui.PDEUIMessages; 69 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 70 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 71 import org.eclipse.pde.internal.ui.editor.TableSection; 72 import org.eclipse.pde.internal.ui.editor.context.InputContextManager; 73 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 74 import org.eclipse.pde.internal.ui.parts.ConditionalListSelectionDialog; 75 import org.eclipse.pde.internal.ui.parts.TablePart; 76 import org.eclipse.pde.internal.ui.search.dependencies.UnusedDependenciesAction; 77 import org.eclipse.pde.internal.ui.util.SWTUtil; 78 import org.eclipse.search.ui.NewSearchUI; 79 import org.eclipse.swt.SWT; 80 import org.eclipse.swt.custom.BusyIndicator; 81 import org.eclipse.swt.graphics.Image; 82 import org.eclipse.swt.layout.GridData; 83 import org.eclipse.swt.widgets.Composite; 84 import org.eclipse.swt.widgets.Display; 85 import org.eclipse.swt.widgets.Table; 86 import org.eclipse.ui.IViewPart; 87 import org.eclipse.ui.IWorkingSet; 88 import org.eclipse.ui.IWorkingSetManager; 89 import org.eclipse.ui.PartInitException; 90 import org.eclipse.ui.PlatformUI; 91 import org.eclipse.ui.actions.ActionFactory; 92 import org.eclipse.ui.forms.widgets.FormToolkit; 93 import org.eclipse.ui.forms.widgets.Section; 94 import org.osgi.framework.Constants; 95 import org.osgi.framework.Version; 96 97 98 public class ImportPackageSection extends TableSection implements IModelChangedListener { 99 100 private static final int ADD_INDEX = 0; 101 private static final int REMOVE_INDEX = 1; 102 private static final int PROPERTIES_INDEX = 2; 103 104 private ImportPackageHeader fHeader; 105 106 class ImportItemWrapper { 107 Object fUnderlying; 108 109 public ImportItemWrapper(Object underlying) { 110 fUnderlying = underlying; 111 } 112 113 public String toString() { 114 return getName(); 115 } 116 117 public boolean equals(Object obj) { 118 if (obj instanceof ImportItemWrapper) { 119 ImportItemWrapper item = (ImportItemWrapper)obj; 120 return getName().equals(item.getName()); 121 } 122 return false; 123 } 124 125 public String getName() { 126 if (fUnderlying instanceof ExportPackageDescription) 127 return ((ExportPackageDescription)fUnderlying).getName(); 128 if (fUnderlying instanceof IPackageFragment) 129 return ((IPackageFragment)fUnderlying).getElementName(); 130 if (fUnderlying instanceof ExportPackageObject) 131 return ((ExportPackageObject)fUnderlying).getName(); 132 return null; 133 } 134 135 public Version getVersion() { 136 if (fUnderlying instanceof ExportPackageDescription) 137 return ((ExportPackageDescription)fUnderlying).getVersion(); 138 if (fUnderlying instanceof ExportPackageObject) { 139 String version = ((ExportPackageObject)fUnderlying).getVersion(); 140 if (version != null) 141 return new Version(version); 142 } 143 return null; 144 } 145 146 boolean hasVersion() { 147 return hasEPD() && ((ExportPackageDescription)fUnderlying).getVersion() != null; 148 } 149 150 boolean hasEPD() { 151 return fUnderlying instanceof ExportPackageDescription; 152 } 153 } 154 155 class ImportPackageContentProvider extends DefaultTableProvider { 156 public Object [] getElements(Object parent) { 157 if (fHeader == null) { 158 Bundle bundle = (Bundle)getBundle(); 159 fHeader = (ImportPackageHeader)bundle.getManifestHeader(Constants.IMPORT_PACKAGE); 160 } 161 return fHeader == null ? new Object [0] : fHeader.getPackages(); 162 } 163 } 164 165 class ImportPackageDialogLabelProvider extends LabelProvider { 166 public Image getImage(Object element) { 167 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_PACKAGE); 168 } 169 170 public String getText(Object element) { 171 StringBuffer buffer = new StringBuffer (); 172 ImportItemWrapper p = (ImportItemWrapper) element; 173 buffer.append(p.getName()); 174 Version version = p.getVersion(); 175 if (version != null && !Version.emptyVersion.equals(version)) { 176 buffer.append(' '); 182 buffer.append(PDELabelProvider.formatVersion(version.toString())); 183 } 184 return buffer.toString(); 185 } 186 } 187 188 private TableViewer fPackageViewer; 189 190 private Action fAddAction; 191 private Action fGoToAction; 192 private Action fRemoveAction; 193 private Action fPropertiesAction; 194 195 public ImportPackageSection(PDEFormPage page, Composite parent) { 196 super( 197 page, 198 parent, 199 Section.DESCRIPTION, 200 new String [] {PDEUIMessages.ImportPackageSection_add, PDEUIMessages.ImportPackageSection_remove, PDEUIMessages.ImportPackageSection_properties}); 201 } 202 203 private boolean isFragment() { 204 IPluginModelBase model = (IPluginModelBase)getPage().getPDEEditor().getAggregateModel(); 205 return model.isFragmentModel(); 206 } 207 208 214 protected void createClient(Section section, FormToolkit toolkit) { 215 section.setText(PDEUIMessages.ImportPackageSection_required); 216 if (isFragment()) 217 section.setDescription(PDEUIMessages.ImportPackageSection_descFragment); 218 else 219 section.setDescription(PDEUIMessages.ImportPackageSection_desc); 220 221 Composite container = createClientContainer(section, 2, toolkit); 222 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 223 TablePart tablePart = getTablePart(); 224 fPackageViewer = tablePart.getTableViewer(); 225 fPackageViewer.setContentProvider(new ImportPackageContentProvider()); 226 fPackageViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 227 fPackageViewer.setComparator(new ViewerComparator() { 228 public int compare(Viewer viewer, Object e1, Object e2) { 229 String s1 = e1.toString(); 230 String s2 = e2.toString(); 231 if (s1.indexOf(" ") != -1) s1 = s1.substring(0, s1.indexOf(" ")); if (s2.indexOf(" ") != -1) s2 = s2.substring(0, s2.indexOf(" ")); return super.compare(viewer, s1, s2); 236 } 237 }); 238 toolkit.paintBordersFor(container); 239 section.setClient(container); 240 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 241 section.setLayoutData(new GridData(GridData.FILL_BOTH)); 242 makeActions(); 243 244 IBundleModel model = getBundleModel(); 245 fPackageViewer.setInput(model); 246 model.addModelChangedListener(this); 247 updateButtons(); 248 } 249 250 public boolean doGlobalAction(String actionId) { 251 252 if (!isEditable()) { return false; } 253 254 if (actionId.equals(ActionFactory.DELETE.getId())) { 255 handleRemove(); 256 return true; 257 } 258 if (actionId.equals(ActionFactory.CUT.getId())) { 259 handleRemove(); 262 return false; 263 } 264 if (actionId.equals(ActionFactory.PASTE.getId())) { 265 doPaste(); 266 return true; 267 } 268 return false; 269 } 270 271 274 protected boolean canPaste(Object targetObject, Object [] sourceObjects) { 275 for (int i = 0; i < sourceObjects.length; i++) { 277 if ((sourceObjects[i] instanceof ImportPackageObject) == false) { 279 return false; 280 } 281 287 if (fHeader == null) { 290 continue; 291 } 292 ImportPackageObject importPackageObject = 295 (ImportPackageObject)sourceObjects[i]; 296 if (fHeader.hasPackage(importPackageObject.getName())) { 297 return false; 298 } 299 } 300 return true; 301 } 302 303 public void dispose() { 304 IBundleModel model = getBundleModel(); 305 if (model != null) 306 model.removeModelChangedListener(this); 307 super.dispose(); 308 } 309 310 313 protected void doPaste(Object targetObject, Object [] sourceObjects) { 314 IBundleModel model = getBundleModel(); 316 if (model == null) { 318 return; 319 } 320 IBundle bundle = model.getBundle(); 322 for (int i = 0; i < sourceObjects.length; i++) { 324 Object sourceObject = sourceObjects[i]; 325 if (sourceObject instanceof ImportPackageObject) { 326 ImportPackageObject importPackageObject = 327 (ImportPackageObject)sourceObject; 328 importPackageObject.reconnect(model, fHeader, getVersionAttribute()); 332 if (fHeader == null) { 334 bundle.setHeader(getImportedPackageHeader(), ""); } 342 fHeader.addPackage(importPackageObject); 344 } 345 } 346 } 347 348 351 private String getImportedPackageHeader() { 352 return Constants.IMPORT_PACKAGE; 353 } 354 355 protected void selectionChanged(IStructuredSelection sel) { 356 getPage().getPDEEditor().setSelection(sel); 357 updateButtons(); 358 } 359 360 private void updateButtons() { 361 Object [] selected = ((IStructuredSelection)fPackageViewer.getSelection()).toArray(); 362 int size = selected.length; 363 TablePart tablePart = getTablePart(); 364 tablePart.setButtonEnabled(ADD_INDEX, isEditable()); 365 tablePart.setButtonEnabled(REMOVE_INDEX, isEditable() && size > 0); 366 tablePart.setButtonEnabled(PROPERTIES_INDEX, shouldEnableProperties(selected)); 367 } 368 369 protected void handleDoubleClick(IStructuredSelection selection) { 370 handleGoToPackage(selection); 371 } 372 373 protected void buttonSelected(int index) { 374 switch (index) { 375 case ADD_INDEX: 376 handleAdd(); 377 break; 378 case REMOVE_INDEX: 379 handleRemove(); 380 break; 381 case PROPERTIES_INDEX: 382 handleOpenProperties(); 383 } 384 } 385 386 private IPackageFragment getPackageFragment(ISelection sel) { 387 if (sel instanceof IStructuredSelection) { 388 IStructuredSelection selection = (IStructuredSelection) sel; 389 if (selection.size() != 1) 390 return null; 391 392 IBaseModel model = getPage().getModel(); 393 if (!(model instanceof IPluginModelBase)) 394 return null; 395 396 return PDEJavaHelper.getPackageFragment( 397 ((PackageObject)selection.getFirstElement()).getName(), 398 ((IPluginModelBase)model).getPluginBase().getId(), 399 getPage().getPDEEditor().getCommonProject()); 400 } 401 return null; 402 } 403 404 private void handleGoToPackage(ISelection selection) { 405 IPackageFragment frag = getPackageFragment(selection); 406 if (frag != null) 407 try { 408 IViewPart part = PDEPlugin.getActivePage().showView(JavaUI.ID_PACKAGES); 409 ShowInPackageViewAction action = new ShowInPackageViewAction(part.getSite()); 410 action.run(frag); 411 } catch (PartInitException e) { 412 } 413 } 414 415 private void handleOpenProperties() { 416 Object [] selected = ((IStructuredSelection) fPackageViewer.getSelection()).toArray(); 417 ImportPackageObject first = (ImportPackageObject)selected[0]; 418 DependencyPropertiesDialog dialog = new DependencyPropertiesDialog(isEditable(), first); 419 dialog.create(); 420 SWTUtil.setDialogSize(dialog, 400, -1); 421 if (selected.length == 1) 422 dialog.setTitle(((ImportPackageObject)selected[0]).getName()); 423 else 424 dialog.setTitle(PDEUIMessages.ExportPackageSection_props); 425 if (dialog.open() == Window.OK && isEditable()) { 426 String newVersion = dialog.getVersion(); 427 boolean newOptional = dialog.isOptional(); 428 for (int i = 0; i < selected.length; i++) { 429 ImportPackageObject object = (ImportPackageObject)selected[i]; 430 if (!newVersion.equals(object.getVersion())) 431 object.setVersion(newVersion); 432 if (!newOptional == object.isOptional()) 433 object.setOptional(newOptional); 434 } 435 } 436 } 437 438 private void handleRemove() { 439 Object [] removed = ((IStructuredSelection) fPackageViewer.getSelection()).toArray(); 440 for (int i = 0; i < removed.length; i++) { 441 fHeader.removePackage((PackageObject) removed[i]); 442 } 443 } 444 445 private void handleAdd() { 446 final ConditionalListSelectionDialog dialog = new ConditionalListSelectionDialog( 447 PDEPlugin.getActiveWorkbenchShell(), 448 new ImportPackageDialogLabelProvider(), 449 PDEUIMessages.ImportPackageSection_dialogButtonLabel); 450 Runnable runnable = new Runnable () { 451 public void run() { 452 setElements(dialog); 453 dialog.setMultipleSelection(true); 454 dialog.setMessage(PDEUIMessages.ImportPackageSection_exported); 455 dialog.setTitle(PDEUIMessages.ImportPackageSection_selection); 456 dialog.create(); 457 SWTUtil.setDialogSize(dialog, 400, 500); 458 } 459 }; 460 461 BusyIndicator.showWhile(Display.getCurrent(), runnable); 462 if (dialog.open() == Window.OK) { 463 Object [] selected = dialog.getResult(); 464 if (fHeader != null) { 465 for (int i = 0; i < selected.length; i++) { 466 ImportPackageObject impObject = null; 467 if (selected[i] instanceof ImportItemWrapper) 468 selected[i] = ((ImportItemWrapper)selected[i]).fUnderlying; 469 470 if (selected[i] instanceof ExportPackageDescription) 471 impObject = new ImportPackageObject(fHeader, (ExportPackageDescription) selected[i], getVersionAttribute()); 472 else if (selected[i] instanceof IPackageFragment) { 473 IPackageFragment fragment = ((IPackageFragment) selected[i]); 475 impObject = new ImportPackageObject(fHeader, fragment.getElementName(), null, getVersionAttribute()); 476 } else if (selected[i] instanceof ExportPackageObject) { 477 ExportPackageObject epo = (ExportPackageObject)selected[i]; 478 impObject = new ImportPackageObject(fHeader, epo.getName(), epo.getVersion(), getVersionAttribute()); 479 } 480 if (impObject != null) 481 fHeader.addPackage(impObject); 482 } 483 } else { 484 getBundle().setHeader(Constants.IMPORT_PACKAGE, getValue(selected)); 485 } 486 } 487 } 488 489 private String getValue(Object [] objects) { 490 StringBuffer buffer = new StringBuffer (); 491 for (int i = 0; i < objects.length; i++) { 492 if (!(objects[i] instanceof ImportItemWrapper)) 493 continue; 494 Version version = ((ImportItemWrapper)objects[i]).getVersion(); 495 if (buffer.length() > 0) 496 buffer.append("," + getLineDelimiter() + " "); buffer.append(((ImportItemWrapper)objects[i]).getName()); 498 if (version != null && !version.equals(Version.emptyVersion)) { 499 buffer.append(";"); buffer.append(getVersionAttribute()); 501 buffer.append("=\""); buffer.append(version.toString()); 503 buffer.append("\""); } 505 } 506 return buffer.toString(); 507 } 508 509 private void setElements(ConditionalListSelectionDialog dialog) { 510 Set forbidden = getForbiddenIds(); 511 boolean allowJava = "true".equals(getBundle().getHeader(ICoreConstants.ECLIPSE_JREBUNDLE)); 513 ArrayList elements = new ArrayList (); 514 ArrayList conditional = new ArrayList (); 515 IPluginModelBase[] models = PluginRegistry.getActiveModels(); 516 Set names = new HashSet (); 517 518 for (int i = 0; i < models.length; i++) { 519 BundleDescription desc = models[i].getBundleDescription(); 520 String id = desc == null ? null : desc.getSymbolicName(); 521 if (id == null || forbidden.contains(id)) 522 continue; 523 524 ExportPackageDescription[] exported = desc.getExportPackages(); 525 for (int j = 0; j < exported.length; j++) { 526 String name = exported[j].getName(); 527 if (("java".equals(name) || name.startsWith("java.")) && !allowJava) continue; 529 if (names.add(name) && 530 (fHeader == null || !fHeader.hasPackage(name))) 531 elements.add(new ImportItemWrapper(exported[j])); 532 } 533 IPluginModelBase model = (IPluginModelBase)getPage().getPDEEditor().getAggregateModel(); 534 if (model instanceof IBundlePluginModelBase) { 535 IBundleModel bmodel = ((IBundlePluginModelBase)model).getBundleModel(); 536 if (bmodel != null) { 537 ExportPackageHeader header = (ExportPackageHeader)bmodel.getBundle().getManifestHeader(Constants.EXPORT_PACKAGE); 538 if (header != null) { 539 ExportPackageObject[] pkgs = header.getPackages(); 540 for (int j = 0; j < pkgs.length; j++) { 541 String name = pkgs[j].getName(); 542 if (names.add(name) && (fHeader == null || !fHeader.hasPackage(name))) 543 elements.add(new ImportItemWrapper(pkgs[j])); 544 } 545 } 546 } 547 548 } 549 } 550 for (int i = 0; i < models.length; i++) { 551 try { 552 IResource resource = models[i].getUnderlyingResource(); 554 IProject project = resource != null ? resource.getProject() : null; 555 if (project == null 556 || !project.hasNature(JavaCore.NATURE_ID) 557 || WorkspaceModelManager.isBinaryProject(project) 558 || !project.exists(ICoreConstants.MANIFEST_PATH)) 559 continue; 560 IJavaProject jp = JavaCore.create(project); 561 IPackageFragmentRoot[] roots = jp.getPackageFragmentRoots(); 562 for (int j = 0; j < roots.length; j++) { 563 if (roots[j].getKind() == IPackageFragmentRoot.K_SOURCE 564 || (roots[j].getKind() == IPackageFragmentRoot.K_BINARY && !roots[j].isExternal())) { 565 IJavaElement[] children = roots[j].getChildren(); 566 for (int k = 0; k < children.length; k++) { 567 IPackageFragment f = (IPackageFragment) children[k]; 568 String name = f.getElementName(); 569 if (name.equals("")) name = "."; if ((f.hasChildren() || f.getNonJavaResources().length > 0) && 572 names.add(name)) 573 conditional.add(new ImportItemWrapper(f)); 574 } 575 } 576 } 577 } catch (CoreException e) { 578 } 579 } 580 dialog.setElements(elements.toArray()); 581 dialog.setConditionalElements(conditional.toArray()); 582 } 583 584 public void modelChanged(IModelChangedEvent event) { 585 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 586 fHeader = null; 587 markStale(); 588 return; 589 } 590 591 if (Constants.IMPORT_PACKAGE.equals(event.getChangedProperty())) { 592 refresh(); 593 return; 594 } 595 596 Object [] objects = event.getChangedObjects(); 597 for (int i = 0; i < objects.length; i++) { 598 if (objects[i] instanceof ImportPackageObject) { 599 ImportPackageObject object = (ImportPackageObject)objects[i]; 600 switch (event.getChangeType()) { 601 case IModelChangedEvent.INSERT: 602 fPackageViewer.add(object); 603 fPackageViewer.setSelection(new StructuredSelection(object)); 604 fPackageViewer.getTable().setFocus(); 605 break; 606 case IModelChangedEvent.REMOVE: 607 Table table = fPackageViewer.getTable(); 608 int index = table.getSelectionIndex(); 609 fPackageViewer.remove(object); 610 table.setSelection(index < table.getItemCount() ? index : table.getItemCount() -1); 611 break; 612 default: 613 fPackageViewer.refresh(object); 614 } 615 } 616 } 617 } 618 619 public void refresh() { 620 fPackageViewer.refresh(); 621 super.refresh(); 622 } 623 624 private void makeActions() { 625 fAddAction = new Action(PDEUIMessages.RequiresSection_add) { 626 public void run() { 627 handleAdd(); 628 } 629 }; 630 fAddAction.setEnabled(isEditable()); 631 fGoToAction = new Action(PDEUIMessages.ImportPackageSection_goToPackage) { 632 public void run() { 633 handleGoToPackage(fPackageViewer.getSelection()); 634 } 635 }; 636 fRemoveAction = new Action(PDEUIMessages.RequiresSection_delete) { 637 public void run() { 638 handleRemove(); 639 } 640 }; 641 fRemoveAction.setEnabled(isEditable()); 642 643 fPropertiesAction = new Action(PDEUIMessages.ImportPackageSection_propertyAction) { 644 public void run() { 645 handleOpenProperties(); 646 } 647 }; 648 } 649 650 protected void fillContextMenu(IMenuManager manager) { 651 final ISelection selection = fPackageViewer.getSelection(); 652 manager.add(fAddAction); 653 boolean singleSelection = selection instanceof IStructuredSelection && 654 ((IStructuredSelection)selection).size() == 1; 655 if (singleSelection) 656 manager.add(fGoToAction); 657 manager.add(new Separator()); 658 if (!selection.isEmpty()) 659 manager.add(fRemoveAction); 660 getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); 661 662 if (((IModel)getPage().getModel()).getUnderlyingResource()!=null) { 663 manager.add(new Separator()); 664 if (singleSelection){ 665 manager.add(new Action(PDEUIMessages.DependencyExtentSearchResultPage_referencesInPlugin) { 666 public void run() { 667 doReferenceSearch(selection); 668 } 669 }); 670 } 671 manager.add(new UnusedDependenciesAction((IPluginModelBase) getPage().getModel(), false)); 672 } 673 674 if (shouldEnableProperties(((IStructuredSelection)fPackageViewer.getSelection()).toArray())) { 675 manager.add(new Separator()); 676 manager.add(fPropertiesAction); 677 } 678 } 679 680 private void doReferenceSearch(final ISelection sel) { 681 IPackageFragmentRoot[] roots = null; 682 try { 683 roots = getSourceRoots(); 684 } catch (JavaModelException e) { 685 } 686 final IPackageFragment fragment = getPackageFragment(sel); 687 if (fragment != null && roots != null) { 688 IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager(); 689 IWorkingSet set = manager.createWorkingSet("temp", roots); new FindReferencesInWorkingSetAction(getPage().getEditorSite(), new IWorkingSet[] {set}).run(fragment); 691 manager.removeWorkingSet(set); 692 } else if (sel instanceof IStructuredSelection) { 693 IStructuredSelection selection = (IStructuredSelection) sel; 694 PackageObject importObject = (PackageObject)selection.getFirstElement(); 695 NewSearchUI.runQueryInBackground(new BlankQuery(importObject)); 696 } 697 } 698 699 private IPackageFragmentRoot[] getSourceRoots() throws JavaModelException { 700 ArrayList result = new ArrayList (); 701 IProject project = getPage().getPDEEditor().getCommonProject(); 702 if (project == null) { 704 return null; 705 } 706 IPackageFragmentRoot[] roots = JavaCore.create(project).getPackageFragmentRoots(); 707 for (int i = 0; i < roots.length; i++) { 708 if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE 709 || (roots[i].isArchive() && !roots[i].isExternal())) 710 result.add(roots[i]); 711 } 712 return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]); 713 } 714 715 private BundleInputContext getBundleContext() { 716 InputContextManager manager = getPage().getPDEEditor().getContextManager(); 717 return (BundleInputContext) manager.findContext(BundleInputContext.CONTEXT_ID); 718 } 719 720 private IBundleModel getBundleModel() { 721 BundleInputContext context = getBundleContext(); 722 return (context != null) ? (IBundleModel)context.getModel() : null; 723 724 } 725 private String getLineDelimiter() { 726 BundleInputContext inputContext = getBundleContext(); 727 if (inputContext != null) { 728 return inputContext.getLineDelimiter(); 729 } 730 return System.getProperty("line.separator"); } 732 733 private IBundle getBundle() { 734 IBundleModel model = getBundleModel(); 735 return (model != null) ? model.getBundle() : null; 736 } 737 738 private String getVersionAttribute() { 739 return getVersionAttribute(getBundle()); 740 } 741 742 private String getVersionAttribute(IBundle bundle) { 743 int manifestVersion = BundlePluginBase.getBundleManifestVersion(bundle); 744 return (manifestVersion < 2) ? ICoreConstants.PACKAGE_SPECIFICATION_VERSION : Constants.VERSION_ATTRIBUTE; 745 } 746 747 private Set getForbiddenIds() { 748 HashSet set = new HashSet (); 749 IPluginModelBase model = (IPluginModelBase)getPage().getPDEEditor().getAggregateModel(); 750 String id = model.getPluginBase().getId(); 751 if (id != null) 752 set.add(id); 753 IPluginImport[] imports = model.getPluginBase().getImports(); 754 State state = TargetPlatformHelper.getState(); 755 for (int i = 0; i < imports.length; i++) { 756 addDependency(state, imports[i].getId(), set); 757 } 758 return set; 759 } 760 761 private void addDependency(State state, String bundleID, Set set) { 762 if (bundleID == null || !set.add(bundleID)) 763 return; 764 765 BundleDescription desc = state.getBundle(bundleID, null); 766 if (desc == null) 767 return; 768 769 BundleDescription[] fragments = desc.getFragments(); 770 for (int i = 0; i < fragments.length; i++) { 771 addDependency(state, fragments[i].getSymbolicName(), set); 772 } 773 774 BundleSpecification[] specs = desc.getRequiredBundles(); 775 for (int j = 0; j < specs.length; j++) { 776 if (specs[j].isResolved() && specs[j].isExported()) { 777 addDependency(state, specs[j].getName(), set); 778 } 779 } 780 } 781 782 protected boolean createCount() { return true; } 783 784 private boolean shouldEnableProperties(Object [] selected) { 785 if (selected.length == 0) 786 return false; 787 if (selected.length == 1) 788 return true; 789 790 String version = ((ImportPackageObject) selected[0]).getVersion(); 791 boolean optional =((ImportPackageObject) selected[0]).isOptional(); 792 for (int i = 1; i < selected.length; i++) { 793 ImportPackageObject object = (ImportPackageObject) selected[i]; 794 if (version == null) { 795 if (object.getVersion() != null || !(optional == object.isOptional())) { 796 return false; 797 } 798 } else if (!version.equals(object.getVersion()) || !(optional == object.isOptional())) { 799 return false; 800 } 801 } 802 return true; 803 } 804 805 } 806 | Popular Tags |