1 17 package org.eclipse.emf.edit.domain; 18 19 20 import java.io.File ; 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.ListIterator ; 30 import java.util.Map ; 31 32 import org.eclipse.emf.common.CommonPlugin; 33 import org.eclipse.emf.common.command.Command; 34 import org.eclipse.emf.common.command.CommandStack; 35 import org.eclipse.emf.common.command.CompoundCommand; 36 import org.eclipse.emf.common.command.UnexecutableCommand; 37 import org.eclipse.emf.common.notify.AdapterFactory; 38 import org.eclipse.emf.common.notify.impl.AdapterImpl; 39 import org.eclipse.emf.common.util.AbstractTreeIterator; 40 import org.eclipse.emf.common.util.TreeIterator; 41 import org.eclipse.emf.common.util.URI; 42 import org.eclipse.emf.ecore.EObject; 43 import org.eclipse.emf.ecore.resource.Resource; 44 import org.eclipse.emf.ecore.resource.ResourceSet; 45 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 46 import org.eclipse.emf.ecore.util.EcoreUtil; 47 import org.eclipse.emf.ecore.util.FeatureMap; 48 import org.eclipse.emf.edit.EMFEditPlugin; 49 import org.eclipse.emf.edit.command.CommandParameter; 50 import org.eclipse.emf.edit.command.CopyToClipboardCommand; 51 import org.eclipse.emf.edit.command.CreateChildCommand; 52 import org.eclipse.emf.edit.command.CutToClipboardCommand; 53 import org.eclipse.emf.edit.command.OverrideableCommand; 54 import org.eclipse.emf.edit.command.PasteFromClipboardCommand; 55 import org.eclipse.emf.edit.command.RemoveCommand; 56 import org.eclipse.emf.edit.command.ReplaceCommand; 57 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; 58 import org.eclipse.emf.edit.provider.IWrapperItemProvider; 59 import org.eclipse.emf.edit.provider.ItemProviderAdapter; 60 61 62 63 67 public class AdapterFactoryEditingDomain implements EditingDomain 68 { 69 72 public static class EditingDomainProvider extends AdapterImpl implements IEditingDomainProvider 73 { 74 77 protected EditingDomain editingDomain; 78 79 public EditingDomainProvider(EditingDomain editingDomain) 80 { 81 this.editingDomain = editingDomain; 82 } 83 84 public EditingDomain getEditingDomain() 85 { 86 return editingDomain; 87 } 88 89 public boolean isAdapterForType(Object type) 90 { 91 return type == IEditingDomainProvider.class; 92 } 93 } 94 95 105 static public EditingDomain getEditingDomainFor(EObject object) 106 { 107 Resource resource = object.eResource(); 108 if (resource != null) 109 { 110 IEditingDomainProvider editingDomainProvider = 111 (IEditingDomainProvider)EcoreUtil.getExistingAdapter(resource, IEditingDomainProvider.class); 112 if (editingDomainProvider != null) 113 { 114 return editingDomainProvider.getEditingDomain(); 115 } 116 else 117 { 118 ResourceSet resourceSet = resource.getResourceSet(); 119 if (resourceSet instanceof IEditingDomainProvider) 120 { 121 EditingDomain editingDomain = ((IEditingDomainProvider)resourceSet).getEditingDomain(); 122 return editingDomain; 123 } 124 else if (resourceSet != null) 125 { 126 editingDomainProvider = (IEditingDomainProvider)EcoreUtil.getExistingAdapter(resourceSet, IEditingDomainProvider.class); 127 if (editingDomainProvider != null) 128 { 129 return editingDomainProvider.getEditingDomain(); 130 } 131 } 132 } 133 } 134 135 return null; 136 } 137 138 154 static public EditingDomain getEditingDomainFor(Object object) 155 { 156 if (object instanceof IEditingDomainProvider) 157 { 158 EditingDomain editingDomain = ((IEditingDomainProvider)object).getEditingDomain(); 159 return editingDomain; 160 } 161 else if (object instanceof EObject) 162 { 163 EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor((EObject)object); 164 return editingDomain; 165 } 166 else if (object instanceof FeatureMap.Entry) 167 { 168 return getEditingDomainFor(((FeatureMap.Entry)object).getValue()); 169 } 170 else if (object instanceof IWrapperItemProvider) 171 { 172 return getEditingDomainFor(((IWrapperItemProvider)object).getValue()); 173 } 174 else 175 { 176 return null; 177 } 178 } 179 180 190 static public IEditingDomainItemProvider getEditingDomainItemProviderFor(Object object) 191 { 192 if (object instanceof EObject) 193 { 194 EObject eObject = (EObject)object; 195 EditingDomain editingDomain = getEditingDomainFor(eObject); 196 if (editingDomain instanceof AdapterFactoryEditingDomain) 197 { 198 object = ((AdapterFactoryEditingDomain)editingDomain).getAdapterFactory().adapt(eObject, IEditingDomainItemProvider.class); 199 } 200 } 201 202 return 203 object instanceof IEditingDomainItemProvider ? 204 (IEditingDomainItemProvider)object : 205 object instanceof IWrapperItemProvider ? 206 getEditingDomainItemProviderFor(((IWrapperItemProvider)object).getValue()) : 207 object instanceof FeatureMap.Entry ? 208 getEditingDomainItemProviderFor(((FeatureMap.Entry)object).getValue()) : 209 null; 210 } 211 212 220 protected class AdapterFactoryEditingDomainResourceSet extends ResourceSetImpl implements IEditingDomainProvider 221 { 222 public AdapterFactoryEditingDomainResourceSet() 223 { 224 super(); 225 } 228 229 public EditingDomain getEditingDomain() 230 { 231 return AdapterFactoryEditingDomain.this; 232 } 233 } 234 235 238 protected AdapterFactory adapterFactory; 239 240 243 protected CommandStack commandStack; 244 245 248 protected ResourceSet resourceSet; 249 250 253 protected Collection clipboard; 254 255 258 protected boolean optimizeCopy = true; 259 260 263 protected Map resourceToReadOnlyMap; 264 265 268 public AdapterFactoryEditingDomain(AdapterFactory adapterFactory, CommandStack commandStack) 269 { 270 this.adapterFactory = adapterFactory; 271 this.commandStack = commandStack; 272 this.resourceSet = new AdapterFactoryEditingDomainResourceSet(); 273 } 274 275 278 public AdapterFactoryEditingDomain(AdapterFactory adapterFactory, CommandStack commandStack, Map resourceToReadOnlyMap) 279 { 280 this.adapterFactory = adapterFactory; 281 this.commandStack = commandStack; 282 this.resourceSet = new AdapterFactoryEditingDomainResourceSet(); 283 this.resourceToReadOnlyMap = resourceToReadOnlyMap; 284 } 285 286 291 public AdapterFactoryEditingDomain(AdapterFactory adapterFactory, CommandStack commandStack, ResourceSet resourceSet) 292 { 293 this.adapterFactory = adapterFactory; 294 this.commandStack = commandStack; 295 this.resourceSet = resourceSet; 296 297 304 } 305 306 309 public AdapterFactory getAdapterFactory() 310 { 311 return adapterFactory; 312 } 313 314 317 public void setAdapterFactory(AdapterFactory adapterFactory) 318 { 319 this.adapterFactory = adapterFactory; 320 } 321 322 325 public Resource createResource(String fileNameURI) 326 { 327 URI uri = URI.createURI(fileNameURI); 328 Resource resource = resourceSet.createResource(uri); 329 return resource; 330 } 331 332 335 public Resource loadResource(String fileNameURI) 336 { 337 try 338 { 339 Resource resource = resourceSet.getResource(URI.createURI(fileNameURI), true); 340 return resource; 341 } 342 catch (Exception exception) 343 { 344 EMFEditPlugin.INSTANCE.log(exception); 345 } 346 347 return null; 348 } 349 350 353 public ResourceSet getResourceSet() 354 { 355 return resourceSet; 356 } 357 358 362 public Command createCommand(Class commandClass, CommandParameter commandParameter) 363 { 364 Object owner = commandParameter.getOwner(); 367 if (commandClass == CopyToClipboardCommand.class) 368 { 369 return new CopyToClipboardCommand(this, commandParameter.getCollection()); 370 } 371 else if (commandClass == PasteFromClipboardCommand.class) 372 { 373 return new PasteFromClipboardCommand 374 (this, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getIndex(), getOptimizeCopy()); 375 } 376 else if (commandClass == CutToClipboardCommand.class) 377 { 378 return new CutToClipboardCommand 379 (this, RemoveCommand.create(this, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getCollection())); 380 } 381 else if (owner != null) 382 { 383 IEditingDomainItemProvider editingDomainItemProvider = 386 (IEditingDomainItemProvider) 387 adapterFactory.adapt(owner, IEditingDomainItemProvider.class); 388 389 return 390 editingDomainItemProvider != null ? 391 editingDomainItemProvider.createCommand(owner, this, commandClass, commandParameter) : 392 new ItemProviderAdapter(null).createCommand(owner, this, commandClass, commandParameter); 393 } 394 else 395 { 396 if (commandClass == RemoveCommand.class) 399 { 400 CompoundCommand removeCommand = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL); 403 404 List objects = new ArrayList (commandParameter.getCollection()); 405 while (!objects.isEmpty()) 406 { 407 ListIterator remainingObjects = objects.listIterator(); 410 411 Object object = remainingObjects.next(); 414 remainingObjects.remove(); 415 416 Object parent = getParent(object); 419 420 if (parent != null) 421 { 422 List siblings = new ArrayList (); 426 siblings.add(object); 427 428 while (remainingObjects.hasNext()) 429 { 430 Object otherObject = remainingObjects.next(); 433 Object otherParent = getParent(otherObject); 434 if (otherParent == parent) 435 { 436 remainingObjects.remove(); 439 siblings.add(otherObject); 440 } 441 } 442 443 removeCommand.append(createCommand(RemoveCommand.class, new CommandParameter(parent, null, siblings))); 446 } 447 else 448 { 449 removeCommand.append(createCommand(RemoveCommand.class, new CommandParameter(object, null, Collections.singleton(object)))); 452 } 453 } 454 455 return removeCommand.unwrap(); 456 } 457 else if (commandClass == ReplaceCommand.class) 458 { 459 Object obj = commandParameter.getValue(); 460 Object parent = (obj == null) ? null : getParent(obj); 461 if (parent == null) parent = obj; 462 return createCommand(ReplaceCommand.class, new CommandParameter(parent, null, commandParameter.getCollection())); 463 } 464 else if (commandClass == CreateChildCommand.class) 465 { 466 Collection sel = commandParameter.getCollection(); 468 Object parent = sel == null ? null : getParent(sel.iterator().next()); 469 if (parent == null) 470 { 471 return UnexecutableCommand.INSTANCE; 472 } 473 return createCommand(CreateChildCommand.class, new CommandParameter(parent, commandParameter.getFeature(), commandParameter.getValue(), commandParameter.getCollection(), commandParameter.getIndex())); 474 } 475 } 476 477 try 478 { 479 Constructor constructor = commandClass.getConstructor(new Class [] { EditingDomain.class, CommandParameter.class }); 480 Object command = constructor.newInstance(new Object [] { this, commandParameter }); 481 return (Command)command; 482 } 483 catch (IllegalAccessException exception) 484 { 485 } 486 catch (InstantiationException exception) 487 { 488 } 489 catch (NoSuchMethodException exception) 490 { 491 } 492 catch (InvocationTargetException exception) 493 { 494 } 495 496 return UnexecutableCommand.INSTANCE; 497 } 498 499 502 public Command createOverrideCommand(OverrideableCommand command) 503 { 504 return null; 505 } 506 507 510 public CommandStack getCommandStack() 511 { 512 return commandStack; 513 } 514 515 519 public Collection getChildren(Object object) 520 { 521 IEditingDomainItemProvider editingDomainItemProvider = 524 (IEditingDomainItemProvider) 525 adapterFactory.adapt(object, IEditingDomainItemProvider.class); 526 527 return 528 editingDomainItemProvider != null ? 529 editingDomainItemProvider.getChildren(object) : 530 Collections.EMPTY_LIST; 531 } 532 533 537 public Object getParent(Object object) 538 { 539 IEditingDomainItemProvider editingDomainItemProvider = 542 (IEditingDomainItemProvider) 543 adapterFactory.adapt(object, IEditingDomainItemProvider.class); 544 545 return 546 editingDomainItemProvider != null ? 547 editingDomainItemProvider.getParent(object) : 548 null; 549 } 550 551 public Object getRoot(Object object) 552 { 553 Object result = object; 554 for (Object parent = getParent(object); parent != null; parent = getParent(parent)) 555 { 556 result = parent; 557 } 558 return result; 559 } 560 561 public Object getWrapper(Object object) 562 { 563 if (object != null) 564 { 565 for (Iterator i = treeIterator(getRoot(object)); i.hasNext(); ) 566 { 567 Object element = i.next(); 568 Object elementValue = element; 569 while (elementValue instanceof IWrapperItemProvider) 570 { 571 elementValue = ((IWrapperItemProvider)elementValue).getValue(); 572 } 573 if (elementValue == object) 574 { 575 return element; 576 } 577 else if (elementValue instanceof FeatureMap.Entry) 578 { 579 Object entryValue = ((FeatureMap.Entry)elementValue).getValue(); 580 if (entryValue == object) 581 { 582 return element; 583 } 584 } 585 } 586 } 587 return object; 588 } 589 590 public static Object unwrap(Object object) 591 { 592 while (object instanceof IWrapperItemProvider) 593 { 594 object = ((IWrapperItemProvider)object).getValue(); 595 } 596 if (object instanceof FeatureMap.Entry) 597 { 598 object = ((FeatureMap.Entry)object).getValue(); 599 } 600 return object; 601 } 602 603 607 public Collection getNewChildDescriptors(Object object, Object sibling) 608 { 609 if (object == null) 613 { 614 object = getParent(sibling); 615 } 616 617 IEditingDomainItemProvider editingDomainItemProvider = 620 (IEditingDomainItemProvider) 621 adapterFactory.adapt(object, IEditingDomainItemProvider.class); 622 623 return 624 editingDomainItemProvider != null ? 625 editingDomainItemProvider.getNewChildDescriptors(object, this, sibling) : 626 Collections.EMPTY_LIST; 627 } 628 629 632 public Collection getClipboard() 633 { 634 return clipboard; 635 } 636 637 640 public void setClipboard(Collection clipboard) 641 { 642 this.clipboard = clipboard; 643 } 644 645 648 public boolean getOptimizeCopy() 649 { 650 return optimizeCopy; 651 } 652 653 656 public void setOptimizeCopy(boolean optimizeCopy) 657 { 658 this.optimizeCopy = optimizeCopy; 659 } 660 661 664 public Map getResourceToReadOnlyMap() 665 { 666 return resourceToReadOnlyMap; 667 } 668 669 672 public void setResourceToReadOnlyMap(Map resourceToReadOnlyMap) 673 { 674 this.resourceToReadOnlyMap = resourceToReadOnlyMap; 675 } 676 677 680 public boolean isReadOnly(Resource resource) 681 { 682 if (resourceToReadOnlyMap == null) 683 { 684 return false; 685 } 686 else 687 { 688 Object result = resourceToReadOnlyMap.get(resource); 689 if (result == null && resource != null) 690 { 691 URI uri = 692 CommonPlugin.asLocalURI 693 ((resource.getResourceSet() == null ? resourceSet : resource.getResourceSet()).getURIConverter().normalize(resource.getURI())); 694 if (uri.isFile() && !uri.isRelative()) 695 { 696 File file = new File (uri.toFileString()); 697 resourceToReadOnlyMap.put(resource, result = !file.exists() || file.canWrite() ? Boolean.FALSE : Boolean.TRUE); 698 } 699 } 700 return Boolean.TRUE.equals(result); 701 } 702 } 703 704 707 public static class DomainTreeIterator extends AbstractTreeIterator 708 { 709 712 protected EditingDomain domain; 713 714 717 public DomainTreeIterator(EditingDomain domain, Object object) 718 { 719 super(object); 720 this.domain = domain; 721 } 722 723 727 public DomainTreeIterator(EditingDomain domain, Object object, boolean includeRoot) 728 { 729 super(object, includeRoot); 730 this.domain = domain; 731 } 732 733 protected Iterator getChildren(Object o) 734 { 735 return domain.getChildren(o).iterator(); 736 } 737 } 738 739 742 public TreeIterator treeIterator(Object object) 743 { 744 return new DomainTreeIterator(this, object); 745 } 746 747 750 public List getTreePath(Object object) 751 { 752 LinkedList result = new LinkedList (); 753 result.addFirst(object); 754 while ((object = getParent(object)) != null) 755 { 756 result.addFirst(object); 757 } 758 759 return result; 760 } 761 } 762 763 773 803 | Popular Tags |