1 11 12 package org.eclipse.pde.internal.ui.editor.plugin; 13 14 import java.util.HashMap ; 15 import java.util.Vector ; 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.IJavaProject; 21 import org.eclipse.jdt.core.IPackageFragment; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.ui.ISharedImages; 24 import org.eclipse.jdt.ui.JavaElementLabelProvider; 25 import org.eclipse.jdt.ui.JavaUI; 26 import org.eclipse.jdt.ui.actions.FindReferencesAction; 27 import org.eclipse.jdt.ui.actions.ShowInPackageViewAction; 28 import org.eclipse.jface.action.Action; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.action.Separator; 31 import org.eclipse.jface.viewers.ILabelProvider; 32 import org.eclipse.jface.viewers.ISelection; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.LabelProvider; 35 import org.eclipse.jface.viewers.StructuredSelection; 36 import org.eclipse.jface.viewers.TableViewer; 37 import org.eclipse.jface.viewers.Viewer; 38 import org.eclipse.jface.viewers.ViewerComparator; 39 import org.eclipse.jface.window.Window; 40 import org.eclipse.osgi.service.resolver.ExportPackageDescription; 41 import org.eclipse.pde.core.IBaseModel; 42 import org.eclipse.pde.core.IModelChangedEvent; 43 import org.eclipse.pde.core.IModelChangedListener; 44 import org.eclipse.pde.core.plugin.IPluginModelBase; 45 import org.eclipse.pde.internal.core.ICoreConstants; 46 import org.eclipse.pde.internal.core.bundle.BundlePluginBase; 47 import org.eclipse.pde.internal.core.ibundle.IBundle; 48 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 49 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 50 import org.eclipse.pde.internal.core.text.bundle.Bundle; 51 import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader; 52 import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject; 53 import org.eclipse.pde.internal.core.text.bundle.PackageObject; 54 import org.eclipse.pde.internal.core.util.PDEJavaHelper; 55 import org.eclipse.pde.internal.ui.PDEPlugin; 56 import org.eclipse.pde.internal.ui.PDEUIMessages; 57 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 58 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 59 import org.eclipse.pde.internal.ui.editor.TableSection; 60 import org.eclipse.pde.internal.ui.editor.context.InputContextManager; 61 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 62 import org.eclipse.pde.internal.ui.parts.TablePart; 63 import org.eclipse.pde.internal.ui.search.dependencies.CalculateUsesAction; 64 import org.eclipse.pde.internal.ui.util.SWTUtil; 65 import org.eclipse.search.ui.NewSearchUI; 66 import org.eclipse.swt.SWT; 67 import org.eclipse.swt.graphics.Image; 68 import org.eclipse.swt.layout.GridData; 69 import org.eclipse.swt.widgets.Composite; 70 import org.eclipse.swt.widgets.Table; 71 import org.eclipse.ui.IViewPart; 72 import org.eclipse.ui.PartInitException; 73 import org.eclipse.ui.actions.ActionFactory; 74 import org.eclipse.ui.forms.widgets.FormToolkit; 75 import org.eclipse.ui.forms.widgets.Section; 76 import org.osgi.framework.Constants; 77 import org.osgi.framework.Version; 78 79 public class ExportPackageSection extends TableSection implements IModelChangedListener { 80 81 private static final int ADD_INDEX = 0; 82 private static final int REMOVE_INDEX = 1; 83 private static final int PROPERTIES_INDEX = 2; 84 private static final int CALCULATE_USE_INDEX = 3; 85 86 class ExportPackageContentProvider extends DefaultTableProvider { 87 public Object [] getElements(Object parent) { 88 if (fHeader == null) { 89 Bundle bundle = (Bundle)getBundle(); 90 fHeader = (ExportPackageHeader)bundle.getManifestHeader(getExportedPackageHeader()); 91 } 92 return fHeader == null ? new Object [0] : fHeader.getPackages(); 93 } 94 } 95 96 class ExportPackageDialogLabelProvider extends LabelProvider { 97 public Image getImage(Object element) { 98 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_PACKAGE); 99 } 100 101 public String getText(Object element) { 102 ExportPackageDescription p = (ExportPackageDescription) element; 103 StringBuffer buffer = new StringBuffer (p.getName()); 104 String version = p.getVersion().toString(); 105 if (!version.equals(Version.emptyVersion.toString())) { 106 buffer.append(PDEUIMessages.ExportPackageSection_0); 107 buffer.append(version); 108 buffer.append(")"); } 110 return buffer.toString(); 111 } 112 } 113 114 private TableViewer fPackageViewer; 115 116 private Action fAddAction; 117 private Action fGoToAction; 118 private Action fRemoveAction; 119 private Action fPropertiesAction; 120 private ExportPackageHeader fHeader; 121 122 public ExportPackageSection(PDEFormPage page, Composite parent) { 123 super( 124 page, 125 parent, 126 Section.DESCRIPTION, 127 new String [] {PDEUIMessages.ExportPackageSection_add, PDEUIMessages.ExportPackageSection_remove, 128 PDEUIMessages.ExportPackageSection_properties, PDEUIMessages.ExportPackageSection_uses}); 129 } 130 131 private boolean isFragment() { 132 IPluginModelBase model = (IPluginModelBase)getPage().getPDEEditor().getAggregateModel(); 133 return model.isFragmentModel(); 134 } 135 136 142 protected void createClient(Section section, FormToolkit toolkit) { 143 section.setText(PDEUIMessages.ExportPackageSection_title); 144 if (isFragment()) 145 section.setDescription(PDEUIMessages.ExportPackageSection_descFragment); 146 else 147 section.setDescription(PDEUIMessages.ExportPackageSection_desc); 148 149 Composite container = createClientContainer(section, 2, toolkit); 150 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 151 TablePart tablePart = getTablePart(); 152 fPackageViewer = tablePart.getTableViewer(); 153 fPackageViewer.setContentProvider(new ExportPackageContentProvider()); 154 fPackageViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 155 fPackageViewer.setComparator(new ViewerComparator() { 156 public int compare(Viewer viewer, Object e1, Object e2) { 157 String s1 = e1.toString(); 158 String s2 = e2.toString(); 159 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); 164 } 165 }); 166 toolkit.paintBordersFor(container); 167 section.setClient(container); 168 GridData gd = new GridData(GridData.FILL_BOTH); 169 if (((ManifestEditor)getPage().getEditor()).isEquinox()) { 170 gd.verticalSpan = 2; 171 gd.minimumWidth = 300; 172 } 173 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 174 section.setLayoutData(gd); 175 makeActions(); 176 177 IBundleModel model = getBundleModel(); 178 fPackageViewer.setInput(model); 179 model.addModelChangedListener(this); 180 updateButtons(); 181 } 182 183 public boolean doGlobalAction(String actionId) { 184 185 if (!isEditable()) { return false; } 186 187 if (actionId.equals(ActionFactory.DELETE.getId())) { 188 handleRemove(); 189 return true; 190 } 191 if (actionId.equals(ActionFactory.CUT.getId())) { 192 handleRemove(); 195 return false; 196 } 197 if (actionId.equals(ActionFactory.PASTE.getId())) { 198 doPaste(); 199 return true; 200 } 201 return false; 202 } 203 204 public void dispose() { 205 IBundleModel model = getBundleModel(); 206 if (model != null) 207 model.removeModelChangedListener(this); 208 super.dispose(); 209 } 210 211 214 protected boolean canPaste(Object targetObject, Object [] sourceObjects) { 215 HashMap currentPackageFragments = null; 216 for (int i = 0; i < sourceObjects.length; i++) { 220 if ((sourceObjects[i] instanceof ExportPackageObject) == false) { 222 return false; 223 } 224 if (currentPackageFragments == null) { 227 currentPackageFragments = createCurrentExportPackageMap(); 228 } 229 ExportPackageObject exportPackageObject = 232 (ExportPackageObject)sourceObjects[i]; 233 if (currentPackageFragments.containsKey(exportPackageObject.getName()) == false) { 234 return false; 235 } 236 } 237 return true; 238 } 239 240 243 private HashMap createCurrentExportPackageMap() { 244 HashMap packageFragments = new HashMap (0); 246 IPluginModelBase model = getModel(); 248 if (model == null) { 250 return packageFragments; 251 } 252 IResource resource = model.getUnderlyingResource(); 254 if (resource == null) { 256 return packageFragments; 257 } 258 IProject project = resource.getProject(); 260 if (project == null) { 262 return packageFragments; 263 } 264 try { 266 if (project.hasNature(JavaCore.NATURE_ID) == false) { 267 return packageFragments; 268 } 269 } catch (CoreException e) { 270 return packageFragments; 271 } 272 IJavaProject javaProject = JavaCore.create(project); 274 if (javaProject == null) { 276 return packageFragments; 277 } 278 Vector currentExportPackages = null; 280 if (fHeader == null) { 281 currentExportPackages = new Vector (); 282 } else { 283 currentExportPackages = fHeader.getPackageNames(); 284 } 285 return PDEJavaHelper.getPackageFragmentsHash( 291 javaProject, currentExportPackages, allowJavaPackages()); 292 } 293 294 297 private IPluginModelBase getModel() { 298 return (IPluginModelBase) getPage().getModel(); 299 } 300 301 304 private boolean allowJavaPackages() { 305 return "true".equals(getBundle().getHeader(ICoreConstants.ECLIPSE_JREBUNDLE)); } 307 308 311 protected void doPaste(Object targetObject, Object [] sourceObjects) { 312 IBundleModel model = getBundleModel(); 314 if (model == null) { 316 return; 317 } 318 IBundle bundle = model.getBundle(); 320 for (int i = 0; i < sourceObjects.length; i++) { 322 Object sourceObject = sourceObjects[i]; 323 if (sourceObject instanceof ExportPackageObject) { 324 ExportPackageObject exportPackageObject = 325 (ExportPackageObject)sourceObject; 326 exportPackageObject.reconnect(model, fHeader, getVersionAttribute()); 330 if (fHeader == null) { 332 bundle.setHeader(getExportedPackageHeader(), ""); } 340 fHeader.addPackage(exportPackageObject); 342 } 343 } 344 } 345 346 protected void selectionChanged(IStructuredSelection sel) { 347 getPage().getPDEEditor().setSelection(sel); 348 updateButtons(); 349 } 350 351 private void updateButtons() { 352 Object [] selected = ((IStructuredSelection)fPackageViewer.getSelection()).toArray(); 353 354 TablePart tablePart = getTablePart(); 355 tablePart.setButtonEnabled(ADD_INDEX, isEditable()); 356 tablePart.setButtonEnabled(REMOVE_INDEX, isEditable() && selected.length > 0); 357 tablePart.setButtonEnabled(PROPERTIES_INDEX, shouldEnableProperties(selected)); 358 tablePart.setButtonEnabled(CALCULATE_USE_INDEX, isEditable() && fPackageViewer.getTable().getItemCount() > 0); 359 360 } 361 362 private boolean shouldEnableProperties(Object [] selected) { 363 if (selected.length == 0) 364 return false; 365 if (selected.length == 1) 366 return true; 367 368 String version = ((ExportPackageObject) selected[0]).getVersion(); 369 for (int i = 1; i < selected.length; i++) { 370 ExportPackageObject object = (ExportPackageObject) selected[i]; 371 if (version == null) { 372 if (object.getVersion() != null) { 373 return false; 374 } 375 } else if (!version.equals(object.getVersion())) { 376 return false; 377 } 378 } 379 return true; 380 } 381 382 protected void handleDoubleClick(IStructuredSelection selection) { 383 handleGoToPackage(selection); 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 protected void buttonSelected(int index) { 416 switch (index) { 417 case ADD_INDEX: 418 handleAdd(); 419 break; 420 case REMOVE_INDEX: 421 handleRemove(); 422 break; 423 case PROPERTIES_INDEX: 424 handleOpenProperties(); 425 break; 426 case CALCULATE_USE_INDEX: 427 calculateUses(); 428 } 429 } 430 431 private void handleOpenProperties() { 432 Object [] selected = ((IStructuredSelection) fPackageViewer.getSelection()).toArray(); 433 ExportPackageObject first = (ExportPackageObject)selected[0]; 434 DependencyPropertiesDialog dialog = new DependencyPropertiesDialog(isEditable(), first); 435 dialog.create(); 436 SWTUtil.setDialogSize(dialog, 400, -1); 437 if (selected.length == 1) 438 dialog.setTitle(((ExportPackageObject)selected[0]).getName()); 439 else 440 dialog.setTitle(PDEUIMessages.ExportPackageSection_props); 441 if (dialog.open() == Window.OK && isEditable()) { 442 String newVersion = dialog.getVersion(); 443 for (int i = 0; i < selected.length; i++) { 444 ExportPackageObject object = (ExportPackageObject)selected[i]; 445 if (!newVersion.equals(object.getVersion())) 446 object.setVersion(newVersion); 447 } 448 } 449 } 450 451 private void handleRemove() { 452 Object [] removed = ((IStructuredSelection) fPackageViewer.getSelection()).toArray(); 453 for (int i = 0; i < removed.length; i++) { 454 fHeader.removePackage((PackageObject) removed[i]); 455 } 456 } 457 458 private void handleAdd() { 459 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 460 IProject project = model.getUnderlyingResource().getProject(); 461 try { 462 if (project.hasNature(JavaCore.NATURE_ID)) { 463 ILabelProvider labelProvider = new JavaElementLabelProvider(); 464 PackageSelectionDialog dialog = new PackageSelectionDialog( 465 PDEPlugin.getActiveWorkbenchShell(), 466 labelProvider, 467 JavaCore.create(project), 468 fHeader == null ? new Vector () : fHeader.getPackageNames(), 469 "true".equals(getBundle().getHeader(ICoreConstants.ECLIPSE_JREBUNDLE))); if (dialog.open() == Window.OK) { 471 Object [] selected = dialog.getResult(); 472 if (fHeader != null) { 473 for (int i = 0; i < selected.length; i++) { 474 IPackageFragment candidate = (IPackageFragment) selected[i]; 475 fHeader.addPackage(new ExportPackageObject(fHeader, candidate, getVersionAttribute())); 476 } 477 } else { 478 getBundle().setHeader(getExportedPackageHeader(), getValue(selected)); 479 if (selected.length > 0) 481 getTablePart().setButtonEnabled(CALCULATE_USE_INDEX, true); 482 } 483 } 484 labelProvider.dispose(); 485 } 486 } catch (CoreException e) { 487 } 488 } 489 490 private String getValue(Object [] objects) { 491 StringBuffer buffer = new StringBuffer (); 492 for (int i = 0; i < objects.length; i++) { 493 IPackageFragment fragment = (IPackageFragment)objects[i]; 494 if (buffer.length() > 0) 495 buffer.append("," + getLineDelimiter() + " "); buffer.append(fragment.getElementName()); 497 } 498 return buffer.toString(); 499 } 500 501 public void modelChanged(IModelChangedEvent event) { 502 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 503 fHeader = null; 504 markStale(); 505 return; 506 } 507 508 if (getExportedPackageHeader().equals(event.getChangedProperty())) { 509 refresh(); 510 return; 511 } 512 513 Object [] objects = event.getChangedObjects(); 514 for (int i = 0; i < objects.length; i++) { 515 if (objects[i] instanceof ExportPackageObject) { 516 ExportPackageObject object = (ExportPackageObject)objects[i]; 517 switch (event.getChangeType()) { 518 case IModelChangedEvent.INSERT: 519 fPackageViewer.add(object); 520 fPackageViewer.setSelection(new StructuredSelection(object)); 521 fPackageViewer.getTable().setFocus(); 522 break; 523 case IModelChangedEvent.REMOVE: 524 Table table = fPackageViewer.getTable(); 525 int index = table.getSelectionIndex(); 526 fPackageViewer.remove(object); 527 table.setSelection(index < table.getItemCount() ? index : table.getItemCount() -1); 528 break; 529 default: 530 fPackageViewer.refresh(object); 531 } 532 } 533 } 534 } 535 536 public void refresh() { 537 fPackageViewer.refresh(); 538 super.refresh(); 539 } 540 541 private void makeActions() { 542 fAddAction = new Action(PDEUIMessages.RequiresSection_add) { 543 public void run() { 544 handleAdd(); 545 } 546 }; 547 fAddAction.setEnabled(isEditable()); 548 fGoToAction = new Action(PDEUIMessages.ImportPackageSection_goToPackage) { 549 public void run() { 550 handleGoToPackage(fPackageViewer.getSelection()); 551 } 552 }; 553 fRemoveAction = new Action(PDEUIMessages.RequiresSection_delete) { 554 public void run() { 555 handleRemove(); 556 } 557 }; 558 fRemoveAction.setEnabled(isEditable()); 559 560 fPropertiesAction = new Action(PDEUIMessages.ExportPackageSection_propertyAction) { 561 public void run() { 562 handleOpenProperties(); 563 } 564 }; 565 } 566 567 protected void fillContextMenu(IMenuManager manager) { 568 ISelection selection = fPackageViewer.getSelection(); 569 manager.add(fAddAction); 570 boolean singleSelection = selection instanceof IStructuredSelection && 571 ((IStructuredSelection)selection).size() == 1; 572 if (singleSelection) 573 manager.add(fGoToAction); 574 manager.add(new Separator()); 575 if (!selection.isEmpty()) 576 manager.add(fRemoveAction); 577 getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); 578 if (singleSelection) 579 manager.add(new Action(PDEUIMessages.ExportPackageSection_findReferences) { 580 public void run() { 581 doSearch(fPackageViewer.getSelection()); 582 } 583 }); 584 if (shouldEnableProperties(((IStructuredSelection)fPackageViewer.getSelection()).toArray())) { 585 manager.add(new Separator()); 586 manager.add(fPropertiesAction); 587 } 588 } 589 590 private void doSearch(ISelection sel) { 591 IPackageFragment frag = getPackageFragment(sel); 592 if (frag != null) { 593 FindReferencesAction action = new FindReferencesAction(getPage().getEditorSite()); 594 action.run(frag); 595 } else if (sel instanceof IStructuredSelection) { 596 IStructuredSelection selection = (IStructuredSelection) sel; 597 PackageObject exportObject = (PackageObject)selection.getFirstElement(); 598 NewSearchUI.runQueryInBackground(new BlankQuery(exportObject)); 599 } 600 } 601 602 private BundleInputContext getBundleContext() { 603 InputContextManager manager = getPage().getPDEEditor().getContextManager(); 604 return (BundleInputContext) manager.findContext(BundleInputContext.CONTEXT_ID); 605 } 606 607 private IBundleModel getBundleModel() { 608 BundleInputContext context = getBundleContext(); 609 return (context != null) ? (IBundleModel)context.getModel() : null; 610 611 } 612 613 private String getLineDelimiter() { 614 BundleInputContext inputContext = getBundleContext(); 615 if (inputContext != null) { 616 return inputContext.getLineDelimiter(); 617 } 618 return System.getProperty("line.separator"); } 620 621 private IBundle getBundle() { 622 IBundleModel model = getBundleModel(); 623 return (model != null) ? model.getBundle() : null; 624 } 625 626 private String getVersionAttribute() { 627 int manifestVersion = BundlePluginBase.getBundleManifestVersion(getBundle()); 628 return (manifestVersion < 2) ? ICoreConstants.PACKAGE_SPECIFICATION_VERSION : Constants.VERSION_ATTRIBUTE; 629 } 630 631 public String getExportedPackageHeader() { 632 int manifestVersion = BundlePluginBase.getBundleManifestVersion(getBundle()); 633 return (manifestVersion < 2) ? ICoreConstants.PROVIDE_PACKAGE : Constants.EXPORT_PACKAGE; 634 } 635 636 protected boolean createCount() { return true; } 637 638 private void calculateUses() { 639 final IProject proj = getPage().getPDEEditor().getCommonProject(); 640 Action action = new CalculateUsesAction(proj, (IBundlePluginModelBase)getPage().getModel()); 641 action.run(); 642 } 643 } 644 | Popular Tags |