1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import java.util.ArrayList ; 13 import java.util.HashSet ; 14 import java.util.Hashtable ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.action.IMenuManager; 24 import org.eclipse.jface.action.MenuManager; 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.ITreeContentProvider; 32 import org.eclipse.jface.viewers.LabelProvider; 33 import org.eclipse.jface.viewers.StructuredSelection; 34 import org.eclipse.jface.viewers.StructuredViewer; 35 import org.eclipse.jface.viewers.TreeViewer; 36 import org.eclipse.jface.wizard.WizardDialog; 37 import org.eclipse.pde.core.IModelChangedEvent; 38 import org.eclipse.pde.core.IModelChangedListener; 39 import org.eclipse.pde.core.plugin.IExtensions; 40 import org.eclipse.pde.core.plugin.IPluginAttribute; 41 import org.eclipse.pde.core.plugin.IPluginBase; 42 import org.eclipse.pde.core.plugin.IPluginElement; 43 import org.eclipse.pde.core.plugin.IPluginExtension; 44 import org.eclipse.pde.core.plugin.IPluginModelBase; 45 import org.eclipse.pde.core.plugin.IPluginObject; 46 import org.eclipse.pde.core.plugin.IPluginParent; 47 import org.eclipse.pde.core.plugin.ISharedExtensionsModel; 48 import org.eclipse.pde.internal.core.PDECore; 49 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 50 import org.eclipse.pde.internal.core.ischema.ISchema; 51 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType; 52 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 53 import org.eclipse.pde.internal.core.schema.SchemaRegistry; 54 import org.eclipse.pde.internal.core.text.IDocumentNode; 55 import org.eclipse.pde.internal.core.text.plugin.IDocumentElement; 56 import org.eclipse.pde.internal.core.text.plugin.IDocumentExtension; 57 import org.eclipse.pde.internal.ui.PDELabelProvider; 58 import org.eclipse.pde.internal.ui.PDEPlugin; 59 import org.eclipse.pde.internal.ui.PDEPluginImages; 60 import org.eclipse.pde.internal.ui.PDEUIMessages; 61 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 62 import org.eclipse.pde.internal.ui.editor.TreeSection; 63 import org.eclipse.pde.internal.ui.editor.actions.CollapseAction; 64 import org.eclipse.pde.internal.ui.editor.actions.SortAction; 65 import org.eclipse.pde.internal.ui.editor.contentassist.XMLElementProposalComputer; 66 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 67 import org.eclipse.pde.internal.ui.parts.TreePart; 68 import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup; 69 import org.eclipse.pde.internal.ui.util.SWTUtil; 70 import org.eclipse.pde.internal.ui.util.SharedLabelProvider; 71 import org.eclipse.pde.internal.ui.wizards.extension.ExtensionEditorWizard; 72 import org.eclipse.pde.internal.ui.wizards.extension.NewExtensionWizard; 73 import org.eclipse.pde.ui.IExtensionEditorWizard; 74 import org.eclipse.swt.SWT; 75 import org.eclipse.swt.custom.BusyIndicator; 76 import org.eclipse.swt.events.DisposeEvent; 77 import org.eclipse.swt.events.DisposeListener; 78 import org.eclipse.swt.graphics.Cursor; 79 import org.eclipse.swt.graphics.Image; 80 import org.eclipse.swt.internal.BidiUtil; 81 import org.eclipse.swt.widgets.Composite; 82 import org.eclipse.swt.widgets.Display; 83 import org.eclipse.swt.widgets.ToolBar; 84 import org.eclipse.swt.widgets.Tree; 85 import org.eclipse.swt.widgets.TreeItem; 86 import org.eclipse.ui.actions.ActionContext; 87 import org.eclipse.ui.actions.ActionFactory; 88 import org.eclipse.ui.dialogs.PatternFilter; 89 import org.eclipse.ui.forms.widgets.FormToolkit; 90 import org.eclipse.ui.forms.widgets.Section; 91 92 public class ExtensionsSection extends TreeSection implements IModelChangedListener, IPropertyChangeListener { 93 private TreeViewer fExtensionTree; 94 private Image fExtensionImage; 95 private Image fGenericElementImage; 96 private FormFilteredTree fFilteredTree; 97 private SchemaRegistry fSchemaRegistry; 98 private Action fNewExtensionAction; 99 private Hashtable fEditorWizards; 100 private SortAction fSortAction; 101 private CollapseAction fCollapseAction; 102 103 private static final String [] COMMON_LABEL_PROPERTIES = { 104 "label", "name", "id"}; 108 class ExtensionContentProvider extends DefaultContentProvider 109 implements 110 ITreeContentProvider { 111 public Object [] getChildren(Object parent) { 112 Object [] children = null; 113 if (parent instanceof IPluginBase) 114 children = ((IPluginBase) parent).getExtensions(); 115 else if (parent instanceof IPluginExtension) { 116 children = ((IPluginExtension) parent).getChildren(); 117 } else if (parent instanceof IPluginElement) { 118 children = ((IPluginElement) parent).getChildren(); 119 } 120 if (children == null) 121 children = new Object [0]; 122 return children; 123 } 124 public boolean hasChildren(Object parent) { 125 return getChildren(parent).length > 0; 126 } 127 public Object getParent(Object child) { 128 if (child instanceof IPluginExtension) { 129 return ((IPluginModelBase)getPage().getModel()).getPluginBase(); 130 } 131 if (child instanceof IPluginObject) 132 return ((IPluginObject) child).getParent(); 133 return null; 134 } 135 public Object [] getElements(Object parent) { 136 return getChildren(parent); 137 } 138 } 139 class ExtensionLabelProvider extends LabelProvider { 140 public String getText(Object obj) { 141 return resolveObjectName(obj); 142 } 143 public Image getImage(Object obj) { 144 return resolveObjectImage(obj); 145 } 146 } 147 public ExtensionsSection(PDEFormPage page, Composite parent) { 148 super(page, parent, Section.DESCRIPTION, new String []{ 149 PDEUIMessages.ManifestEditor_DetailExtension_new, 150 PDEUIMessages.ManifestEditor_DetailExtension_edit, 151 null, 152 PDEUIMessages.ManifestEditor_DetailExtension_up, 153 PDEUIMessages.ManifestEditor_DetailExtension_down}); 154 fHandleDefaultButton = false; 155 } 156 private static void addItemsForExtensionWithSchema(MenuManager menu, 157 IPluginExtension extension, IPluginParent parent) { 158 ISchema schema = getSchema(extension); 159 String tagName = (parent == extension ? "extension" : parent.getName()); ISchemaElement elementInfo = schema.findElement(tagName); 161 162 if ((elementInfo != null) && 163 (elementInfo.getType() instanceof ISchemaComplexType) && 164 (parent instanceof IDocumentNode)) { 165 HashSet elementSet = XMLElementProposalComputer 169 .computeElementProposal(elementInfo, (IDocumentNode)parent); 170 Iterator iterator = elementSet.iterator(); 172 while (iterator.hasNext()) { 173 Action action = new NewElementAction((ISchemaElement) iterator 174 .next(), parent); 175 menu.add(action); 176 } 177 } 178 } 179 180 184 private static ISchema getSchema(IPluginParent parent) { 185 if (parent instanceof IPluginExtension) { 186 return getSchema((IPluginExtension)parent); 187 } else if (parent instanceof IPluginElement) { 188 return getSchema((IPluginElement)parent); 189 } else { 190 return null; 191 } 192 } 193 194 private static ISchema getSchema(IPluginExtension extension) { 195 String point = extension.getPoint(); 196 SchemaRegistry registry = PDECore.getDefault().getSchemaRegistry(); 197 return registry.getSchema(point); 198 } 199 200 204 static ISchemaElement getSchemaElement(IPluginElement element) { 205 ISchema schema = getSchema(element); 206 if (schema != null) { 207 return schema.findElement(element.getName()); 208 } 209 return null; 210 } 211 212 216 private static ISchema getSchema(IPluginElement element) { 217 IPluginObject parent = element.getParent(); 218 while (parent != null && !(parent instanceof IPluginExtension)) { 219 parent = parent.getParent(); 220 } 221 if (parent != null) { 222 return getSchema((IPluginExtension) parent); 223 } 224 return null; 225 } 226 227 public void createClient(Section section, FormToolkit toolkit) { 228 initializeImages(); 229 Composite container = createClientContainer(section, 2, toolkit); 230 TreePart treePart = getTreePart(); 231 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 232 fExtensionTree = treePart.getTreeViewer(); 233 fExtensionTree.setContentProvider(new ExtensionContentProvider()); 234 fExtensionTree.setLabelProvider(new ExtensionLabelProvider()); 235 toolkit.paintBordersFor(container); 236 section.setClient(container); 237 section.setDescription(PDEUIMessages.ExtensionsSection_sectionDescExtensionsMaster); 238 section.setText(PDEUIMessages.ManifestEditor_DetailExtension_title); 240 initialize((IPluginModelBase) getPage().getModel()); 241 createSectionToolbar(section, toolkit); 242 fFilteredTree.createUIListenerEntryFilter(this); 244 } 245 246 250 private void createSectionToolbar(Section section, FormToolkit toolkit) { 251 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); 252 ToolBar toolbar = toolBarManager.createControl(section); 253 final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 254 toolbar.setCursor(handCursor); 255 toolbar.addDisposeListener(new DisposeListener() { 257 public void widgetDisposed(DisposeEvent e) { 258 if ((handCursor != null) && 259 (handCursor.isDisposed() == false)) { 260 handCursor.dispose(); 261 } 262 } 263 }); 264 fSortAction = new SortAction(fExtensionTree, 266 PDEUIMessages.ExtensionsPage_sortAlpha, null, null, this); 267 toolBarManager.add(fSortAction); 268 fCollapseAction = new CollapseAction(fExtensionTree, 270 PDEUIMessages.ExtensionsPage_collapseAll); 271 toolBarManager.add(fCollapseAction); 272 273 toolBarManager.update(true); 274 275 section.setTextClient(toolbar); 276 } 277 278 protected void selectionChanged(IStructuredSelection selection) { 279 getPage().getPDEEditor().setSelection(selection); 280 updateUpDownButtons(selection.getFirstElement()); 281 getTreePart().setButtonEnabled(1, isSelectionEditable(selection)); 282 } 283 protected void handleDoubleClick(IStructuredSelection selection) { 284 288 } 289 protected void buttonSelected(int index) { 290 switch (index) { 291 case 0 : 292 handleNew(); 293 break; 294 case 1 : 295 handleEdit(); 296 break; 297 case 2: 298 break; 300 case 3 : 301 handleMove(true); 302 break; 303 case 4 : 304 handleMove(false); 305 break; 306 } 307 } 308 309 312 public void dispose() { 313 if (fFilteredTree != null) { 315 fFilteredTree.dispose(); 316 } 317 fEditorWizards = null; 318 IPluginModelBase model = (IPluginModelBase) getPage().getPDEEditor() 319 .getAggregateModel(); 320 if (model!=null) 321 model.removeModelChangedListener(this); 322 super.dispose(); 323 } 324 325 328 public boolean doGlobalAction(String actionId) { 329 330 if (!isEditable()) { return false; } 331 332 if (actionId.equals(ActionFactory.DELETE.getId())) { 333 handleDelete(); 334 return true; 335 } 336 if (actionId.equals(ActionFactory.CUT.getId())) { 337 handleDelete(); 340 return false; 341 } 342 if (actionId.equals(ActionFactory.PASTE.getId())) { 343 doPaste(); 344 return true; 345 } 346 347 return false; 348 } 349 350 public boolean setFormInput(Object object) { 351 if (object instanceof IPluginExtension 352 || object instanceof IPluginElement) { 353 fExtensionTree.setSelection(new StructuredSelection(object), true); 354 return true; 355 } 356 return false; 357 } 358 protected void fillContextMenu(IMenuManager manager) { 359 ISelection selection = fExtensionTree.getSelection(); 360 IStructuredSelection ssel = (IStructuredSelection) selection; 361 IMenuManager newMenu = null; 362 if (ssel.size() == 1) { 363 Object object = ssel.getFirstElement(); 364 if (object instanceof IPluginParent) { 365 IPluginParent parent = (IPluginParent) object; 366 if (parent.getModel().getUnderlyingResource() != null) { 367 newMenu = fillContextMenu(getPage(), parent, manager); 368 manager.add(new Separator()); 369 } 370 } 371 } else if (ssel.size() > 1) { 372 Action delAction = new Action() { 374 public void run() { 375 handleDelete(); 376 } 377 }; 378 delAction.setText(PDEUIMessages.Actions_delete_label); 379 manager.add(delAction); 380 manager.add(new Separator()); 381 delAction.setEnabled(isEditable()); 382 } 383 if (newMenu == null) { 384 newMenu = new MenuManager(PDEUIMessages.Menus_new_label); 385 manager.add(newMenu); 386 } 387 if (ssel.size() == 1) { 388 manager.add(new Separator()); 389 Object object = ssel.getFirstElement(); 390 if (object instanceof IPluginExtension) { 391 PluginSearchActionGroup actionGroup = new PluginSearchActionGroup(); 392 actionGroup.setContext(new ActionContext(selection)); 393 actionGroup.fillContextMenu(manager); 394 manager.add(new Separator()); 395 } 396 } 398 if (!newMenu.isEmpty()) 399 newMenu.add(new Separator()); 400 newMenu.add(fNewExtensionAction); 401 manager.add(new Separator()); 402 manager.add(new Separator()); 403 getPage().getPDEEditor().getContributor().addClipboardActions(manager); 404 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 405 manager, false); 406 407 } 408 static IMenuManager fillContextMenu(PDEFormPage page, 409 final IPluginParent parent, IMenuManager manager) { 410 return fillContextMenu(page, parent, manager, false); 411 } 412 static IMenuManager fillContextMenu(PDEFormPage page, 413 final IPluginParent parent, IMenuManager manager, 414 boolean addSiblingItems) { 415 return fillContextMenu(page, parent, manager, addSiblingItems, true); 416 } 417 static IMenuManager fillContextMenu(PDEFormPage page, 418 final IPluginParent parent, IMenuManager manager, 419 boolean addSiblingItems, boolean fullMenu) { 420 MenuManager menu = new MenuManager(PDEUIMessages.Menus_new_label); 421 IPluginExtension extension = getExtension(parent); 422 ISchema schema = getSchema(extension); 423 if (schema == null) { 424 menu.add(new NewElementAction(null, parent)); 425 } else { 426 addItemsForExtensionWithSchema(menu, extension, parent); 427 if (addSiblingItems) { 428 IPluginObject parentsParent = parent.getParent(); 429 if (!(parentsParent instanceof IPluginExtension)) { 430 IPluginParent pparent = (IPluginParent) parentsParent; 431 menu.add(new Separator()); 432 addItemsForExtensionWithSchema(menu, extension, pparent); 433 } 434 } 435 } 436 manager.add(menu); 437 manager.add(new Separator()); 438 if (fullMenu) { 439 Action deleteAction = new Action(PDEUIMessages.Actions_delete_label) { 440 public void run() { 441 try { 442 IPluginObject parentsParent = parent.getParent(); 443 if (parent instanceof IPluginExtension) { 444 IPluginBase plugin = (IPluginBase) parentsParent; 445 plugin.remove((IPluginExtension) parent); 446 } else { 447 IPluginParent parentElement = (IPluginParent) parent 448 .getParent(); 449 parentElement.remove(parent); 450 } 451 } catch (CoreException e) { 452 } 453 } 454 }; 455 deleteAction.setEnabled(page.getModel().isEditable()); 456 manager.add(deleteAction); 457 } 458 return menu; 459 } 460 static IPluginExtension getExtension(IPluginParent parent) { 461 while (parent != null && !(parent instanceof IPluginExtension)) { 462 parent = (IPluginParent) parent.getParent(); 463 } 464 return (IPluginExtension) parent; 465 } 466 private void handleDelete() { 467 IStructuredSelection sel = (IStructuredSelection) fExtensionTree 468 .getSelection(); 469 if (sel.isEmpty()) 470 return; 471 for (Iterator iter = sel.iterator(); iter.hasNext();) { 472 IPluginObject object = (IPluginObject) iter.next(); 473 try { 474 if (object instanceof IPluginElement) { 475 IPluginElement ee = (IPluginElement) object; 476 IPluginParent parent = (IPluginParent) ee.getParent(); 477 parent.remove(ee); 478 } else if (object instanceof IPluginExtension) { 479 IPluginExtension extension = (IPluginExtension) object; 480 IPluginBase plugin = extension.getPluginBase(); 481 plugin.remove(extension); 482 } 483 } catch (CoreException e) { 484 PDEPlugin.logException(e); 485 } 486 } 487 } 488 private void handleNew() { 489 final IProject project = getPage().getPDEEditor().getCommonProject(); 490 BusyIndicator.showWhile(fExtensionTree.getTree().getDisplay(), 491 new Runnable () { 492 public void run() { 493 ((ManifestEditor)getPage().getEditor()).ensurePluginContextPresence(); 494 NewExtensionWizard wizard = new NewExtensionWizard( 495 project, (IPluginModelBase) getPage() 496 .getModel(), (ManifestEditor) getPage() 497 .getPDEEditor()) { 498 public boolean performFinish() { 499 return super.performFinish(); 500 } 501 }; 502 WizardDialog dialog = new WizardDialog(PDEPlugin 503 .getActiveWorkbenchShell(), wizard); 504 dialog.create(); 505 SWTUtil.setDialogSize(dialog, 500, 500); 506 dialog.open(); 507 } 508 }); 509 } 510 private void handleEdit(IConfigurationElement element, IStructuredSelection selection) { 511 IProject project = getPage().getPDEEditor().getCommonProject(); 512 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 513 try { 514 final IExtensionEditorWizard wizard = (IExtensionEditorWizard)element.createExecutableExtension("class"); wizard.init(project, model, selection); 516 BusyIndicator.showWhile(fExtensionTree.getTree().getDisplay(), 517 new Runnable () { 518 public void run() { 519 WizardDialog dialog = new WizardDialog(PDEPlugin 520 .getActiveWorkbenchShell(), wizard); 521 dialog.create(); 522 SWTUtil.setDialogSize(dialog, 500, 500); 523 dialog.open(); 524 } 525 }); 526 } 527 catch (CoreException e) { 528 PDEPlugin.logException(e); 529 } 530 } 531 private void handleEdit() { 532 final IStructuredSelection selection = (IStructuredSelection)fExtensionTree.getSelection(); 533 ArrayList editorWizards = getEditorWizards(selection); 534 if (editorWizards==null) return; 535 if (editorWizards.size()==1) { 536 handleEdit((IConfigurationElement)editorWizards.get(0), selection); 538 } 539 else { 540 IProject project = getPage().getPDEEditor().getCommonProject(); 541 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 542 final ExtensionEditorWizard wizard = new ExtensionEditorWizard(project, model, selection); 543 BusyIndicator.showWhile(fExtensionTree.getTree().getDisplay(), 544 new Runnable () { 545 public void run() { 546 WizardDialog dialog = new WizardDialog(PDEPlugin 547 .getActiveWorkbenchShell(), wizard); 548 dialog.create(); 549 SWTUtil.setDialogSize(dialog, 500, 500); 550 dialog.open(); 551 } 552 }); 553 } 554 } 555 private ArrayList getEditorWizards(IStructuredSelection selection) { 556 if (selection.size()!=1) return null; 557 Object obj = selection.getFirstElement(); 558 String pointId = null; 559 if (obj instanceof IPluginExtension) { 560 pointId = ((IPluginExtension)obj).getPoint(); 561 } 562 else if (obj instanceof IPluginElement) { 563 IPluginObject parent = ((IPluginElement)obj).getParent(); 564 while (parent!=null) { 565 if (parent instanceof IPluginExtension) { 566 pointId = ((IPluginExtension)parent).getPoint(); 567 break; 568 } 569 parent = parent.getParent(); 570 } 571 } 572 if (pointId==null) return null; 573 if (fEditorWizards==null) 574 loadExtensionWizards(); 575 return (ArrayList )fEditorWizards.get(pointId); 576 } 577 578 private void loadExtensionWizards() { 579 fEditorWizards = new Hashtable (); 580 IConfigurationElement [] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.pde.ui.newExtension"); for (int i=0; i<elements.length; i++) { 582 IConfigurationElement element = elements[i]; 583 if (element.getName().equals("editorWizard")) { String pointId = element.getAttribute("point"); if (pointId==null) continue; 586 ArrayList list = (ArrayList )fEditorWizards.get(pointId); 587 if (list==null) { 588 list = new ArrayList (); 589 fEditorWizards.put(pointId, list); 590 } 591 list.add(element); 592 } 593 } 594 } 595 private boolean isSelectionEditable(IStructuredSelection selection) { 596 if (!getPage().getModel().isEditable()) 597 return false; 598 return getEditorWizards(selection)!=null; 599 } 600 601 public void initialize(IPluginModelBase model) { 602 fExtensionTree.setInput(model.getPluginBase()); 603 selectFirstExtension(); 604 boolean editable = model.isEditable(); 605 TreePart treePart = getTreePart(); 606 treePart.setButtonEnabled(0, editable); 607 treePart.setButtonEnabled(1, false); 608 treePart.setButtonEnabled(3, false); 609 treePart.setButtonEnabled(4, false); 610 model.addModelChangedListener(this); 611 fNewExtensionAction = new Action() { 612 public void run() { 613 handleNew(); 614 } 615 }; 616 fNewExtensionAction.setText(PDEUIMessages.ManifestEditor_DetailExtension_newExtension); 617 fNewExtensionAction 618 .setImageDescriptor(PDEPluginImages.DESC_EXTENSION_OBJ); 619 fNewExtensionAction.setEnabled(editable); 620 } 621 private void selectFirstExtension() { 622 Tree tree = fExtensionTree.getTree(); 623 TreeItem [] items = tree.getItems(); 624 if (items.length==0) return; 625 TreeItem firstItem = items[0]; 626 Object obj = firstItem.getData(); 627 fExtensionTree.setSelection(new StructuredSelection(obj)); 628 } 629 void fireSelection() { 630 fExtensionTree.setSelection(fExtensionTree.getSelection()); 631 } 632 public void initializeImages() { 633 PDELabelProvider provider = PDEPlugin.getDefault().getLabelProvider(); 634 fExtensionImage = provider.get(PDEPluginImages.DESC_EXTENSION_OBJ); 635 fGenericElementImage = provider 636 .get(PDEPluginImages.DESC_GENERIC_XML_OBJ); 637 } 638 public void refresh() { 639 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 640 fExtensionTree.setInput(model.getPluginBase()); 641 selectFirstExtension(); 642 getManagedForm().fireSelectionChanged(ExtensionsSection.this, 643 fExtensionTree.getSelection()); 644 super.refresh(); 645 } 646 647 public void modelChanged(IModelChangedEvent event) { 648 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 649 markStale(); 650 return; 651 } 652 Object changeObject = event.getChangedObjects()[0]; 653 if (changeObject instanceof IPluginBase 654 && event.getChangeType() == IModelChangedEvent.CHANGE 655 && event.getChangedProperty().equals( 656 IExtensions.P_EXTENSION_ORDER)) { 657 IStructuredSelection sel = (IStructuredSelection) fExtensionTree 658 .getSelection(); 659 IPluginExtension extension = (IPluginExtension) sel 660 .getFirstElement(); 661 fExtensionTree.refresh(); 662 fExtensionTree.setSelection(new StructuredSelection(extension)); 663 return; 664 } 665 if (changeObject instanceof IPluginExtension 666 || (changeObject instanceof IPluginElement && ((IPluginElement)changeObject).getParent() instanceof IPluginParent)) { 667 IPluginObject pobj = (IPluginObject) changeObject; 668 IPluginObject parent = changeObject instanceof IPluginExtension 669 ? ((IPluginModelBase) getPage().getModel()).getPluginBase() 670 : pobj.getParent(); 671 if (event.getChangeType() == IModelChangedEvent.INSERT) { 672 fExtensionTree.add(parent, pobj); 673 fExtensionTree.setSelection( 674 new StructuredSelection(changeObject), true); 675 fExtensionTree.getTree().setFocus(); 676 } else if (event.getChangeType() == IModelChangedEvent.REMOVE) { 677 fExtensionTree.remove(pobj); 678 } else { 679 if (event.getChangedProperty().equals( 680 IPluginParent.P_SIBLING_ORDER)) { 681 IStructuredSelection sel = (IStructuredSelection) fExtensionTree 682 .getSelection(); 683 IPluginObject child = (IPluginObject) sel.getFirstElement(); 684 fExtensionTree.refresh(child.getParent()); 685 fExtensionTree.setSelection(new StructuredSelection(child)); 686 } else { 687 fExtensionTree.update(changeObject, null); 688 } 689 } 690 } 691 } 692 693 private Image resolveObjectImage(Object obj) { 694 if (obj instanceof IPluginExtension) { 695 return fExtensionImage; 696 } 697 Image elementImage = fGenericElementImage; 698 if (obj instanceof IPluginElement) { 699 IPluginElement element = (IPluginElement) obj; 700 Image customImage = getCustomImage(element); 701 if (customImage != null) 702 elementImage = customImage; 703 String bodyText = element.getText(); 704 boolean hasBodyText = bodyText!=null&&bodyText.length()>0; 705 if (hasBodyText) { 706 elementImage = PDEPlugin.getDefault().getLabelProvider().get( 707 elementImage, SharedLabelProvider.F_EDIT); 708 } 709 } 710 return elementImage; 711 } 712 713 private static boolean isStorageModel(IPluginObject object) { 714 IPluginModelBase modelBase = object.getPluginModel(); 715 return modelBase.getInstallLocation()==null; 716 } 717 718 static Image getCustomImage(IPluginElement element) { 719 if (isStorageModel(element))return null; 720 ISchemaElement elementInfo = getSchemaElement(element); 721 if (elementInfo != null && elementInfo.getIconProperty() != null) { 722 String iconProperty = elementInfo.getIconProperty(); 723 IPluginAttribute att = element.getAttribute(iconProperty); 724 String iconPath = null; 725 if (att != null && att.getValue() != null) { 726 iconPath = att.getValue(); 727 } 728 if (iconPath != null) { 729 return getImageFromPlugin(element, iconPath); 731 } 732 } 733 return null; 734 } 735 736 private static Image getImageFromPlugin(IPluginElement element, 737 String iconPathName) { 738 if (iconPathName.startsWith("%")) return null; 742 743 IPluginModelBase model = element.getPluginModel(); 744 if (model == null) 745 return null; 746 747 return PDEPlugin.getDefault().getLabelProvider().getImageFromPlugin(model, iconPathName); 748 } 749 private String resolveObjectName(Object obj) { 750 return resolveObjectName(getSchemaRegistry(), obj); 751 } 752 753 private SchemaRegistry getSchemaRegistry() { 754 if (fSchemaRegistry == null) 755 fSchemaRegistry = PDECore.getDefault().getSchemaRegistry(); 756 return fSchemaRegistry; 757 } 758 759 public static String resolveObjectName(SchemaRegistry schemaRegistry, Object obj) { 760 boolean fullNames = PDEPlugin.isFullNameModeEnabled(); 761 if (obj instanceof IPluginExtension) { 762 IPluginExtension extension = (IPluginExtension) obj; 763 if (!fullNames) { 764 return extension.getPoint(); 765 } 766 if (extension.getName() != null) 767 return extension.getTranslatedName(); 768 ISchema schema = schemaRegistry.getSchema(extension.getPoint()); 769 if (schema != null) { 771 return schema.getName(); 773 } 774 return extension.getPoint(); 775 } else if (obj instanceof IPluginElement) { 776 IPluginElement element = (IPluginElement) obj; 777 String baseName = element.getName(); 778 String fullName = null; 779 ISchemaElement elementInfo = getSchemaElement(element); 780 IPluginAttribute labelAtt = null; 781 if (elementInfo != null && elementInfo.getLabelProperty() != null) { 782 labelAtt = element.getAttribute(elementInfo.getLabelProperty()); 783 } 784 if (labelAtt == null) { 785 for (int i = 0; i < COMMON_LABEL_PROPERTIES.length; i++) { 788 labelAtt = element.getAttribute(COMMON_LABEL_PROPERTIES[i]); 789 if (labelAtt != null) 790 break; 791 } 792 if (labelAtt == null) { 793 if (element.getAttributeCount() == 1) 796 labelAtt = element.getAttributes()[0]; 797 } 798 } 799 if (labelAtt != null && labelAtt.getValue() != null) 800 fullName = stripShortcuts(labelAtt.getValue()); 801 fullName = element.getResourceString(fullName); 802 if (fullNames) 803 return fullName != null ? fullName : baseName; 804 if (fullName == null) 805 return baseName; 806 if (BidiUtil.isBidiPlatform()) 811 return fullName + " \u200f(\u200e" + baseName + ')'; return fullName + " (" + baseName + ')'; } 814 return obj.toString(); 815 } 816 public void setFocus() { 817 if (fExtensionTree != null) 818 fExtensionTree.getTree().setFocus(); 819 } 820 public static String stripShortcuts(String input) { 821 StringBuffer output = new StringBuffer (); 822 for (int i = 0; i < input.length(); i++) { 823 char c = input.charAt(i); 824 if (c == '&') 825 continue; 826 else if (c == '@') 827 break; 828 output.append(c); 829 } 830 return output.toString(); 831 } 832 833 836 protected boolean canPaste(Object targetObject, Object [] sourceObjects) { 837 boolean allExtensions = true; 845 boolean allElements = true; 846 for (int i = 0; i < sourceObjects.length; i++) { 847 if (sourceObjects[i] instanceof IPluginExtension) { 848 allElements = false; 849 } else if (sourceObjects[i] instanceof IPluginElement) { 850 allExtensions = false; 851 } else { 852 return false; 853 } 854 } 855 if (allExtensions) { 858 return true; 859 } 860 if (allElements == false) { 863 return false; 864 } 865 if ((targetObject instanceof IPluginParent) == false) { 867 return false; 868 } else if ((targetObject instanceof IDocumentNode) == false) { 869 return false; 870 } 871 IPluginParent targetParent = (IPluginParent)targetObject; 873 ISchema schema = getSchema(targetParent); 874 if (schema == null) { 877 return true; 878 } 879 String tagName = null; 882 if (targetParent instanceof IPluginExtension) { 883 tagName = "extension"; } else { 885 tagName = targetParent.getName(); 886 } 887 ISchemaElement schemaElement = schema.findElement(tagName); 889 if (schemaElement == null) { 891 return false; 893 } else if ((schemaElement.getType() instanceof ISchemaComplexType) == false) { 894 return false; 896 } 897 HashSet elementSet = 901 XMLElementProposalComputer.computeElementProposal( 902 schemaElement, (IDocumentNode)targetObject); 903 if (sourceObjects.length > 1) { 906 return canPasteSourceElements((IPluginElement[])sourceObjects, elementSet); 907 } 908 return canPasteSourceElement((IPluginElement)sourceObjects[0], elementSet); 909 } 910 911 916 private boolean canPasteSourceElements(IPluginElement[] sourceElements, 917 HashSet targetElementSet) { 918 HashSet targetElementNameSet = new HashSet (); 923 Iterator iterator = targetElementSet.iterator(); 924 while (iterator.hasNext()) { 925 targetElementNameSet.add(((ISchemaElement)iterator.next()).getName()); 926 } 927 for (int i = 0; i < sourceElements.length; i++) { 938 String sourceTagName = sourceElements[i].getName(); 939 if (targetElementNameSet.contains(sourceTagName) == false) { 940 return false; 941 } 942 } 943 return true; 944 } 945 946 951 private boolean canPasteSourceElement(IPluginElement sourceElement, 952 HashSet targetElementSet) { 953 boolean canPaste = false; 954 String sourceTagName = sourceElement.getName(); 956 Iterator iterator = targetElementSet.iterator(); 958 while (iterator.hasNext()) { 959 String targetTagName = ((ISchemaElement)iterator.next()).getName(); 961 if (sourceTagName.equals(targetTagName)) { 964 canPaste = true; 965 break; 966 } 967 } 968 return canPaste; 969 } 970 971 974 private IPluginModelBase getPluginModelBase() { 975 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 979 if ((model instanceof IBundlePluginModelBase) == false) { 981 return null; 982 } 983 ISharedExtensionsModel extensionModel = 985 ((IBundlePluginModelBase)model).getExtensionsModel(); 986 if ((extensionModel == null) || 988 ((extensionModel instanceof IPluginModelBase) == false)) { 989 return null; 990 } 991 return ((IPluginModelBase)extensionModel); 992 } 993 994 997 protected void doPaste(Object targetObject, Object [] sourceObjects) { 998 ((ManifestEditor)getPage().getEditor()).ensurePluginContextPresence(); 1003 IPluginModelBase model = getPluginModelBase(); 1007 if (model == null) { 1009 return; 1010 } 1011 IPluginBase pluginBase = model.getPluginBase(); 1012 try { 1013 for (int i = 0; i < sourceObjects.length; i++) { 1015 Object sourceObject = sourceObjects[i]; 1016 1017 if ((sourceObject instanceof IDocumentExtension) && 1018 (sourceObject instanceof IPluginExtension) && 1019 (pluginBase instanceof IDocumentNode)) { 1020 IDocumentExtension extension = (IDocumentExtension)sourceObject; 1022 ISchema schema = getSchema((IPluginExtension)extension); 1024 extension.reconnect(model, schema, (IDocumentNode)pluginBase); 1027 pluginBase.add((IPluginExtension)extension); 1028 1029 } else if ((sourceObject instanceof IDocumentElement) && 1030 (sourceObject instanceof IPluginElement) && 1031 (targetObject instanceof IPluginParent) && 1032 (targetObject instanceof IDocumentNode)) { 1033 IDocumentElement element = (IDocumentElement)sourceObject; 1035 ISchema schema = getSchema((IPluginElement)element); 1037 element.reconnect(model, schema, (IDocumentNode)targetObject); 1040 ((IPluginParent)targetObject).add((IPluginElement)element); 1043 } 1044 } 1045 } catch (CoreException e) { 1046 PDEPlugin.logException(e); 1047 } 1048 } 1049 1050 private void handleMove(boolean up) { 1051 IStructuredSelection sel = (IStructuredSelection) fExtensionTree 1052 .getSelection(); 1053 IPluginObject object = (IPluginObject) sel.getFirstElement(); 1054 if (object instanceof IPluginElement) { 1055 IPluginParent parent = (IPluginParent) object.getParent(); 1056 IPluginObject[] children = parent.getChildren(); 1057 int index = parent.getIndexOf(object); 1058 int newIndex = up ? index - 1 : index + 1; 1059 IPluginObject child2 = children[newIndex]; 1060 try { 1061 parent.swap(object, child2); 1062 } catch (CoreException e) { 1063 PDEPlugin.logException(e); 1064 } 1065 } else if (object instanceof IPluginExtension) { 1066 IPluginExtension extension = (IPluginExtension) object; 1067 IPluginBase plugin = extension.getPluginBase(); 1068 IPluginExtension[] extensions = plugin.getExtensions(); 1069 int index = plugin.getIndexOf(extension); 1070 int newIndex = up ? index - 1 : index + 1; 1071 IPluginExtension e2 = extensions[newIndex]; 1072 try { 1073 plugin.swap(extension, e2); 1074 } catch (CoreException e) { 1075 PDEPlugin.logException(e); 1076 } 1077 } 1078 } 1079 private void updateUpDownButtons(Object item) { 1080 if (getPage().getModel().isEditable() == false) 1081 return; 1082 boolean sorted = fSortAction != null && fSortAction.isChecked(); 1083 if (sorted) { 1084 getTreePart().setButtonEnabled(3, false); 1085 getTreePart().setButtonEnabled(4, false); 1086 return; 1087 } 1088 1089 boolean upEnabled = false; 1090 boolean downEnabled = false; 1091 if (item instanceof IPluginElement) { 1092 IPluginElement element = (IPluginElement) item; 1093 IPluginParent parent = (IPluginParent) element.getParent(); 1094 int index = parent.getIndexOf(element); 1096 if (index > 0) 1097 upEnabled = true; 1098 if (index < parent.getChildCount() - 1) 1099 downEnabled = true; 1100 } else if (item instanceof IPluginExtension) { 1101 IPluginExtension extension = (IPluginExtension) item; 1102 IExtensions extensions = (IExtensions) extension.getParent(); 1103 int index = extensions.getIndexOf(extension); 1104 int size = extensions.getExtensions().length; 1105 if (index > 0) 1106 upEnabled = true; 1107 if (index < size - 1) 1108 downEnabled = true; 1109 } 1110 1111 getTreePart().setButtonEnabled(3, upEnabled); 1112 getTreePart().setButtonEnabled(4, downEnabled); 1113 } 1114 1115 1118 protected TreeViewer createTreeViewer(Composite parent, int style) { 1119 fFilteredTree = new FormFilteredTree(parent, style, new PatternFilter()); 1120 parent.setData("filtered", Boolean.TRUE); return fFilteredTree.getViewer(); 1122 } 1123 1124 public void propertyChange(PropertyChangeEvent event) { 1125 if (fSortAction.equals(event.getSource()) && IAction.RESULT.equals(event.getProperty())) { 1126 StructuredViewer viewer = getStructuredViewerPart().getViewer(); 1127 IStructuredSelection ssel = (IStructuredSelection)viewer.getSelection(); 1128 updateUpDownButtons(ssel.size() != 1 ? null : ssel.getFirstElement()); 1129 } 1130 } 1131 1132 protected void selectExtensionElement(ISelection selection) { 1133 fExtensionTree.setSelection(selection, true); 1134 } 1135} 1136 | Popular Tags |