1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Vector ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.jface.action.Action; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.action.IMenuManager; 25 import org.eclipse.jface.action.Separator; 26 import org.eclipse.jface.action.ToolBarManager; 27 import org.eclipse.jface.util.IPropertyChangeListener; 28 import org.eclipse.jface.util.PropertyChangeEvent; 29 import org.eclipse.jface.viewers.ISelection; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.viewers.TableViewer; 33 import org.eclipse.jface.window.Window; 34 import org.eclipse.pde.core.IModel; 35 import org.eclipse.pde.core.IModelChangedEvent; 36 import org.eclipse.pde.core.IModelChangedListener; 37 import org.eclipse.pde.core.plugin.IPlugin; 38 import org.eclipse.pde.core.plugin.IPluginBase; 39 import org.eclipse.pde.core.plugin.IPluginImport; 40 import org.eclipse.pde.core.plugin.IPluginModel; 41 import org.eclipse.pde.core.plugin.IPluginModelBase; 42 import org.eclipse.pde.core.plugin.IPluginModelFactory; 43 import org.eclipse.pde.core.plugin.PluginRegistry; 44 import org.eclipse.pde.internal.core.IPluginModelListener; 45 import org.eclipse.pde.internal.core.PDECore; 46 import org.eclipse.pde.internal.core.PluginModelDelta; 47 import org.eclipse.pde.internal.core.bundle.BundlePluginBase; 48 import org.eclipse.pde.internal.core.bundle.BundlePluginModelBase; 49 import org.eclipse.pde.internal.core.plugin.AbstractPluginModelBase; 50 import org.eclipse.pde.internal.core.plugin.ExternalPluginModel; 51 import org.eclipse.pde.internal.core.plugin.ImportObject; 52 import org.eclipse.pde.internal.core.plugin.PluginBase; 53 import org.eclipse.pde.internal.core.text.plugin.PluginBaseNode; 54 import org.eclipse.pde.internal.core.text.plugin.PluginDocumentNodeFactory; 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.TableSection; 59 import org.eclipse.pde.internal.ui.editor.actions.SortAction; 60 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 61 import org.eclipse.pde.internal.ui.parts.TablePart; 62 import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup; 63 import org.eclipse.pde.internal.ui.search.dependencies.UnusedDependenciesAction; 64 import org.eclipse.pde.internal.ui.util.SWTUtil; 65 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog; 66 import org.eclipse.swt.SWT; 67 import org.eclipse.swt.events.DisposeEvent; 68 import org.eclipse.swt.events.DisposeListener; 69 import org.eclipse.swt.graphics.Cursor; 70 import org.eclipse.swt.layout.GridData; 71 import org.eclipse.swt.widgets.Composite; 72 import org.eclipse.swt.widgets.Control; 73 import org.eclipse.swt.widgets.Display; 74 import org.eclipse.swt.widgets.Table; 75 import org.eclipse.swt.widgets.TableItem; 76 import org.eclipse.swt.widgets.ToolBar; 77 import org.eclipse.ui.actions.ActionContext; 78 import org.eclipse.ui.actions.ActionFactory; 79 import org.eclipse.ui.forms.widgets.FormToolkit; 80 import org.eclipse.ui.forms.widgets.Section; 81 82 public class RequiresSection 83 extends TableSection 84 implements IModelChangedListener, IPluginModelListener, IPropertyChangeListener { 85 86 private static final int ADD_INDEX = 0; 87 private static final int REMOVE_INDEX = 1; 88 private static final int UP_INDEX = 2; 89 private static final int DOWN_INDEX = 3; 90 private static final int PROPERTIES_INDEX = 4; 91 92 private TableViewer fImportViewer; 93 private Vector fImports; 94 private Action fOpenAction; 95 private Action fAddAction; 96 private Action fRemoveAction; 97 private Action fPropertiesAction; 98 private Action fSortAction; 99 100 class ImportContentProvider extends DefaultTableProvider { 101 public Object [] getElements(Object parent) { 102 if (fImports == null) 103 createImportObjects(); 104 return fImports.toArray(); 105 } 106 } 107 108 public RequiresSection(DependenciesPage page, Composite parent, String [] labels) { 109 super(page, parent, Section.DESCRIPTION, labels); 110 getSection().setText(PDEUIMessages.RequiresSection_title); 111 boolean fragment = ((IPluginModelBase)getPage().getModel()).isFragmentModel(); 112 if (fragment) 113 getSection().setDescription(PDEUIMessages.RequiresSection_fDesc); 114 else 115 getSection().setDescription(PDEUIMessages.RequiresSection_desc); 116 getTablePart().setEditable(false); 117 } 118 119 public void createClient(Section section, FormToolkit toolkit) { 120 Composite container = createClientContainer(section, 2, toolkit); 121 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 122 TablePart tablePart = getTablePart(); 123 fImportViewer = tablePart.getTableViewer(); 124 125 fImportViewer.setContentProvider(new ImportContentProvider()); 126 fImportViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 127 toolkit.paintBordersFor(container); 128 makeActions(); 129 section.setClient(container); 130 GridData gd = new GridData(GridData.FILL_BOTH); 131 gd.minimumWidth = 250; 132 gd.grabExcessVerticalSpace = true; 133 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 134 section.setLayoutData(gd); 135 section.setText(PDEUIMessages.RequiresSection_title); 136 createSectionToolbar(section, toolkit); 137 initialize(); 138 } 139 140 private void createSectionToolbar(Section section, FormToolkit toolkit) { 141 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); 142 ToolBar toolbar = toolBarManager.createControl(section); 143 final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 144 toolbar.setCursor(handCursor); 145 toolbar.addDisposeListener(new DisposeListener() { 147 public void widgetDisposed(DisposeEvent e) { 148 if ((handCursor != null) && 149 (handCursor.isDisposed() == false)) { 150 handCursor.dispose(); 151 } 152 } 153 }); 154 155 fSortAction = new SortAction(fImportViewer, 157 PDEUIMessages.RequiresSection_sortAlpha, null, null, this); 158 toolBarManager.add(fSortAction); 159 160 toolBarManager.update(true); 161 162 section.setTextClient(toolbar); 163 } 164 165 protected void selectionChanged(IStructuredSelection sel) { 166 getPage().getPDEEditor().setSelection(sel); 167 updateButtons(); 168 } 169 170 private void updateButtons() { 171 Table table = getTablePart().getTableViewer().getTable(); 172 TableItem[] selection = table.getSelection(); 173 boolean hasSelection = selection.length > 0; 174 TablePart tablePart = getTablePart(); 175 tablePart.setButtonEnabled(ADD_INDEX, isEditable()); 176 updateUpDownButtons(); 177 if (isBundle()) 178 tablePart.setButtonEnabled(PROPERTIES_INDEX, selection.length == 1); 179 tablePart.setButtonEnabled(REMOVE_INDEX, isEditable() && hasSelection); 180 } 181 182 private void updateUpDownButtons() { 183 TablePart tablePart = getTablePart(); 184 if (fSortAction.isChecked()) { 185 tablePart.setButtonEnabled(UP_INDEX, false); 186 tablePart.setButtonEnabled(DOWN_INDEX, false); 187 return; 188 } 189 Table table = getTablePart().getTableViewer().getTable(); 190 TableItem[] selection = table.getSelection(); 191 boolean hasSelection = selection.length > 0; 192 boolean canMove = table.getItemCount() > 1 && selection.length == 1; 193 194 tablePart.setButtonEnabled( 195 UP_INDEX, 196 canMove && isEditable() && hasSelection && table.getSelectionIndex() > 0); 197 tablePart.setButtonEnabled( 198 DOWN_INDEX, 199 canMove 200 && hasSelection && isEditable() 201 && table.getSelectionIndex() < table.getItemCount() - 1); 202 } 203 204 protected void handleDoubleClick(IStructuredSelection sel) { 205 handleOpen(sel); 206 } 207 208 protected void buttonSelected(int index) { 209 switch (index) { 210 case ADD_INDEX: 211 handleAdd(); 212 break; 213 case REMOVE_INDEX: 214 handleRemove(); 215 break; 216 case UP_INDEX: 217 handleUp(); 218 break; 219 case DOWN_INDEX: 220 handleDown(); 221 break; 222 case PROPERTIES_INDEX: 223 handleOpenProperties(); 224 break; 225 } 226 } 227 228 private void handleOpenProperties() { 229 Object changeObject = ((IStructuredSelection)fImportViewer.getSelection()).getFirstElement(); 230 IPluginImport importObject = ((ImportObject) changeObject).getImport(); 231 232 DependencyPropertiesDialog dialog = new DependencyPropertiesDialog( 233 isEditable(), 234 importObject); 235 dialog.create(); 236 SWTUtil.setDialogSize(dialog, 400, -1); 237 dialog.setTitle(importObject.getId()); 238 if (dialog.open() == Window.OK && isEditable()) { 239 try { 240 importObject.setOptional(dialog.isOptional()); 241 importObject.setReexported(dialog.isReexported()); 242 importObject.setVersion(dialog.getVersion()); 243 } catch (CoreException e) { 244 PDEPlugin.logException(e); 245 } 246 } 247 } 248 249 public void dispose() { 250 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 251 if (model!=null) 252 model.removeModelChangedListener(this); 253 PDECore.getDefault().getModelManager().removePluginModelListener(this); 254 super.dispose(); 255 } 256 257 260 public boolean doGlobalAction(String actionId) { 261 262 if (!isEditable()) { return false; } 263 264 if (actionId.equals(ActionFactory.DELETE.getId())) { 265 handleRemove(); 266 return true; 267 } 268 if (actionId.equals(ActionFactory.CUT.getId())) { 269 handleRemove(); 272 return false; 273 } 274 if (actionId.equals(ActionFactory.PASTE.getId())) { 275 doPaste(); 276 return true; 277 } 278 return false; 279 } 280 281 284 protected boolean canPaste(Object targetObject, Object [] sourceObjects) { 285 HashSet existingImportsSet = null; 286 for (int i = 0; i < sourceObjects.length; i++) { 289 if ((sourceObjects[i] instanceof ImportObject) == false) { 291 return false; 292 } 293 if (existingImportsSet == null) { 296 existingImportsSet = 297 PluginSelectionDialog.getExistingImports(getModel(), false); 298 } 299 ImportObject importObject = (ImportObject)sourceObjects[i]; 301 if (existingImportsSet.contains(importObject.getImport().getId())) { 302 return false; 303 } 304 } 305 return true; 306 } 307 308 311 protected void doPaste(Object targetObject, Object [] sourceObjects) { 312 IPluginModelBase model = getModel(); 314 IPluginBase pluginBase = model.getPluginBase(); 315 try { 316 for (int i = 0; i < sourceObjects.length; i++) { 318 Object sourceObject = sourceObjects[i]; 319 if (sourceObject instanceof ImportObject) { 320 ImportObject importObject = (ImportObject) sourceObject; 322 importObject.reconnect(model); 329 pluginBase.add(importObject.getImport()); 331 } 332 } 333 } catch (CoreException e) { 334 PDEPlugin.logException(e); 335 } 336 } 337 338 341 private IPluginModelBase getModel() { 342 return (IPluginModelBase)getPage().getModel(); 343 } 344 345 public boolean setFormInput(Object object) { 346 if (object instanceof IPluginImport) { 347 ImportObject iobj = new ImportObject((IPluginImport) object); 348 fImportViewer.setSelection(new StructuredSelection(iobj), true); 349 return true; 350 } 351 return false; 352 } 353 354 protected void fillContextMenu(IMenuManager manager) { 355 ISelection selection = fImportViewer.getSelection(); 356 manager.add(fAddAction); 357 if (!selection.isEmpty()) { 358 manager.add(fOpenAction); 359 } 360 manager.add(new Separator()); 361 getPage().contextMenuAboutToShow(manager); 362 363 if (!selection.isEmpty()) 364 manager.add(fRemoveAction); 365 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 366 manager); 367 manager.add(new Separator()); 368 369 PluginSearchActionGroup actionGroup = new PluginSearchActionGroup(); 370 actionGroup.setContext(new ActionContext(selection)); 371 actionGroup.fillContextMenu(manager); 372 if (((IModel)getPage().getModel()).getUnderlyingResource()!=null) { 373 manager.add(new UnusedDependenciesAction((IPluginModelBase) getPage().getModel(), false)); 374 } 375 if (fPropertiesAction != null && !fImportViewer.getSelection().isEmpty()) { 376 manager.add(new Separator()); 377 manager.add(fPropertiesAction); 378 } 379 } 380 381 private void handleOpen(ISelection sel) { 382 if (sel instanceof IStructuredSelection) { 383 IStructuredSelection ssel = (IStructuredSelection) sel; 384 if (ssel.size() == 1) { 385 Object obj = ssel.getFirstElement(); 386 if (obj instanceof ImportObject) { 387 IPlugin plugin = ((ImportObject) obj).getPlugin(); 388 if (plugin != null) 389 ManifestEditor.open(plugin, false); 390 } 391 } 392 } 393 } 394 395 private void handleRemove() { 396 IStructuredSelection ssel = (IStructuredSelection) fImportViewer.getSelection(); 397 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 398 IPluginBase pluginBase = model.getPluginBase(); 399 IPluginImport[] imports = new IPluginImport[ssel.size()]; 400 int i = 0; 401 for (Iterator iter = ssel.iterator(); iter.hasNext();i++) 402 imports[i] = ((ImportObject) iter.next()).getImport(); 403 404 try { 405 removeImports(pluginBase, imports); 406 } catch (CoreException e) { 407 PDEPlugin.logException(e); 408 } 409 updateButtons(); 410 } 411 412 private void removeImports(IPluginBase base, IPluginImport[] imports) throws CoreException{ 413 if (base instanceof BundlePluginBase) 414 ((BundlePluginBase)base).remove(imports); 415 else if (base instanceof PluginBase) 416 ((PluginBase)base).remove(imports); 417 else if (base instanceof PluginBaseNode) 418 ((PluginBaseNode)base).remove(imports); 419 } 420 421 private void handleAdd() { 422 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 423 PluginSelectionDialog dialog = 424 new PluginSelectionDialog( 425 PDEPlugin.getActiveWorkbenchShell(), 426 getAvailablePlugins(model), 427 true); 428 dialog.create(); 429 if (dialog.open() == Window.OK) { 430 Object [] models = dialog.getResult(); 431 IPluginImport[] imports = new IPluginImport[models.length]; 432 try { 433 for (int i = 0; i < models.length; i++) { 434 IPluginModel candidate = (IPluginModel) models[i]; 435 String pluginId = candidate.getPlugin().getId(); 436 IPluginImport importNode = createImport(model.getPluginFactory(), pluginId); 437 imports[i] = importNode; 438 } 439 addImports(model.getPluginBase(), imports); 440 } catch (CoreException e) { 441 } 442 } 443 } 444 445 private IPluginImport createImport(IPluginModelFactory factory, String id) { 446 if (factory instanceof AbstractPluginModelBase) 447 return ((AbstractPluginModelBase)factory).createImport(id); 448 else if (factory instanceof BundlePluginModelBase) 449 return ((BundlePluginModelBase)factory).createImport(id); 450 else if (factory instanceof PluginDocumentNodeFactory) 451 return ((PluginDocumentNodeFactory)factory).createImport(id); 452 return null; 453 } 454 455 private void addImports(IPluginBase base, IPluginImport[] imports) throws CoreException { 456 if (base instanceof BundlePluginBase) 457 ((BundlePluginBase)base).add(imports); 458 else if (base instanceof PluginBase) 459 ((PluginBase)base).add(imports); 460 else if (base instanceof PluginBaseNode) 461 ((PluginBaseNode)base).add(imports); 462 } 463 464 private void handleUp() { 465 int index = getTablePart().getTableViewer().getTable().getSelectionIndex(); 466 if (index < 1) 467 return; 468 swap(index, index - 1); 469 } 470 471 private void handleDown() { 472 Table table = getTablePart().getTableViewer().getTable(); 473 int index = table.getSelectionIndex(); 474 if (index == table.getItemCount() - 1) 475 return; 476 swap(index, index + 1); 477 } 478 479 public void swap(int index1, int index2) { 480 Table table = getTablePart().getTableViewer().getTable(); 481 IPluginImport dep1 = ((ImportObject)table.getItem(index1).getData()).getImport(); 482 IPluginImport dep2 = ((ImportObject)table.getItem(index2).getData()).getImport(); 483 484 try { 485 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 486 IPluginBase pluginBase = model.getPluginBase(); 487 pluginBase.swap(dep1, dep2); 488 } catch (CoreException e) { 489 PDEPlugin.logException(e); 490 } 491 } 492 493 private IPluginModelBase[] getAvailablePlugins(IPluginModelBase model) { 494 IPluginModelBase[] plugins = PluginRegistry.getActiveModels(false); 495 HashSet existingImports = PluginSelectionDialog.getExistingImports(model, false); 496 ArrayList result = new ArrayList (); 497 for (int i = 0; i < plugins.length; i++) { 498 if (!existingImports.contains(plugins[i].getPluginBase().getId())) { 499 result.add(plugins[i]); 500 } 501 } 502 503 if (!existingImports.contains("system.bundle")) addSystemBundle(result); 505 return (IPluginModelBase[])result.toArray(new IPluginModelBase[result.size()]); 506 } 507 508 private void addSystemBundle(java.util.List list) { 509 try { 510 ExternalPluginModel model = new ExternalPluginModel(); 511 512 IPluginModelBase osgi = PluginRegistry.findModel("org.eclipse.osgi"); if (osgi == null) 515 return; 516 model.setInstallLocation(osgi.getInstallLocation()); 517 518 String pluginInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><plugin id=\"system.bundle\" name=\"System Bundle\"></plugin>"; InputStream is = new BufferedInputStream (new ByteArrayInputStream (pluginInfo.getBytes())); 521 model.load(is, false); 522 523 list.add(model); 524 525 } catch (CoreException e) { 526 } 527 } 528 529 530 public void initialize() { 531 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 532 if (model == null) 533 return; 534 fImportViewer.setInput(model.getPluginBase()); 535 updateButtons(); 536 model.addModelChangedListener(this); 537 PDECore.getDefault().getModelManager().addPluginModelListener(this); 538 fAddAction.setEnabled(model.isEditable()); 539 fRemoveAction.setEnabled(model.isEditable()); 540 } 541 542 private void makeActions() { 543 fAddAction = new Action(PDEUIMessages.RequiresSection_add) { 544 public void run() { 545 handleAdd(); 546 } 547 }; 548 fOpenAction = new Action(PDEUIMessages.RequiresSection_open) { 549 public void run() { 550 handleOpen(fImportViewer.getSelection()); 551 } 552 }; 553 fRemoveAction = new Action(PDEUIMessages.RequiresSection_delete) { 554 public void run() { 555 handleRemove(); 556 } 557 }; 558 if (isBundle()) { 559 fPropertiesAction = new Action(PDEUIMessages.RequiresSection_properties) { 560 public void run() { 561 handleOpenProperties(); 562 } 563 }; 564 } 565 } 566 567 public void refresh() { 568 fImports = null; 569 fImportViewer.refresh(); 570 super.refresh(); 571 } 572 573 public void modelChanged(IModelChangedEvent event) { 574 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 575 markStale(); 576 return; 577 } 578 if (event.getChangedProperty() == IPluginBase.P_IMPORT_ORDER) { 579 refresh(); 580 updateButtons(); 581 return; 582 } 583 584 Object [] changedObjects = event.getChangedObjects(); 585 if (changedObjects[0] instanceof IPluginImport) { 586 ImportObject[] objects = new ImportObject[changedObjects.length]; 587 int index = 0; 588 for (int i = 0; i < changedObjects.length; i++) { 589 Object changeObject = changedObjects[i]; 590 IPluginImport iimport = (IPluginImport) changeObject; 591 if (event.getChangeType() == IModelChangedEvent.INSERT) { 592 ImportObject iobj = new ImportObject(iimport); 593 if (fImports == null) 594 createImportObjects(); 596 else 597 fImports.add(iobj); 598 fImportViewer.add(iobj); 599 } else { 600 ImportObject iobj = findImportObject(iimport); 601 if (iobj != null) { 602 if (event.getChangeType() == IModelChangedEvent.REMOVE) { 603 if (fImports == null) 604 createImportObjects(); 606 else 607 fImports.remove(iobj); 608 Table table = fImportViewer.getTable(); 609 index = table.getSelectionIndex(); 610 fImportViewer.remove(iobj); 611 } else { 612 fImportViewer.update(iobj, null); 613 } 614 } 615 } 616 } 617 if (event.getChangeType() == IModelChangedEvent.INSERT) { 618 fImportViewer.setSelection(new StructuredSelection(objects), true); 619 fImportViewer.getTable().setFocus(); 620 } else if (event.getChangeType() == IModelChangedEvent.REMOVE) { 621 Table table = fImportViewer.getTable(); 622 table.setSelection(index < table.getItemCount() ? index : table.getItemCount() -1); 623 } 624 } else { 625 fImportViewer.update(((IStructuredSelection)fImportViewer.getSelection()).toArray(), null); 626 } 627 } 628 629 public void modelsChanged(PluginModelDelta delta) { 630 fImports = null; 631 final Control control = fImportViewer.getControl(); 632 if (!control.isDisposed()) { 633 control.getDisplay().asyncExec(new Runnable () { 634 public void run() { 635 if (!control.isDisposed()) 636 fImportViewer.refresh(); 637 } 638 }); 639 } 640 } 641 642 private ImportObject findImportObject(IPluginImport iimport) { 643 if (fImports == null) 644 return null; 645 for (int i = 0; i < fImports.size(); i++) { 646 ImportObject iobj = (ImportObject) fImports.get(i); 647 if (iobj.getImport().equals(iimport)) 648 return iobj; 649 } 650 return null; 651 } 652 653 private void createImportObjects() { 654 fImports = new Vector (); 655 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 656 IPluginImport[] iimports = model.getPluginBase().getImports(); 657 for (int i = 0; i < iimports.length; i++) { 658 IPluginImport iimport = iimports[i]; 659 fImports.add(new ImportObject(iimport)); 660 } 661 } 662 663 public void setFocus() { 664 if (fImportViewer != null) 665 fImportViewer.getTable().setFocus(); 666 } 667 668 private boolean isBundle() { 669 return getPage().getPDEEditor().getContextManager().findContext(BundleInputContext.CONTEXT_ID) != null; 670 } 671 672 protected boolean createCount() { return true; } 673 674 public void propertyChange(PropertyChangeEvent event) { 675 if (fSortAction.equals(event.getSource()) && IAction.RESULT.equals(event.getProperty())) { 676 updateUpDownButtons(); 677 } 678 } 679 } 680 | Popular Tags |