1 17 package org.eclipse.emf.edit.provider; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import org.eclipse.emf.common.command.Command; 32 import org.eclipse.emf.common.command.CommandWrapper; 33 import org.eclipse.emf.common.command.UnexecutableCommand; 34 import org.eclipse.emf.common.notify.AdapterFactory; 35 import org.eclipse.emf.common.notify.Notification; 36 import org.eclipse.emf.ecore.EStructuralFeature; 37 import org.eclipse.emf.edit.command.CommandActionDelegate; 38 import org.eclipse.emf.edit.command.CommandParameter; 39 import org.eclipse.emf.edit.command.DragAndDropCommand; 40 import org.eclipse.emf.edit.command.SetCommand; 41 import org.eclipse.emf.edit.domain.EditingDomain; 42 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor.OverrideableCommandOwner; 43 44 45 50 public class DelegatingWrapperItemProvider extends WrapperItemProvider 51 implements 52 IStructuredItemContentProvider, 53 ITreeItemContentProvider, 54 IItemLabelProvider, 55 IItemPropertySource, 56 IEditingDomainItemProvider, 57 IChangeNotifier, 58 INotifyChangedListener 59 { 60 63 protected Object delegateItemProvider; 64 65 68 protected Map childrenMap; 69 70 73 protected Collection delegateChildren; 74 75 78 protected List propertyDescriptors; 79 80 83 protected IChangeNotifier changeNotifier; 84 85 92 public DelegatingWrapperItemProvider(Object value, Object owner, EStructuralFeature feature, int index, AdapterFactory adapterFactory) 93 { 94 super(value, owner, feature, index, adapterFactory); 95 96 if (value == null) 97 { 98 throw new IllegalArgumentException ("value=null"); 99 } 100 101 Object delegateValue = getDelegateValue(); 102 if (delegateValue != null) 103 { 104 delegateItemProvider = getRootAdapterFactory().adapt(delegateValue, IStructuredItemContentProvider.class); 105 if (delegateItemProvider instanceof IChangeNotifier) 106 { 107 ((IChangeNotifier)delegateItemProvider).addListener(this); 108 } 109 } 110 } 111 112 120 public DelegatingWrapperItemProvider(Object value, Object owner, AdapterFactory adapterFactory) 121 { 122 this(value, owner, null, CommandParameter.NO_INDEX, adapterFactory); 123 } 124 125 128 public void dispose() 129 { 130 if (delegateItemProvider instanceof IChangeNotifier) 131 { 132 ((IChangeNotifier)delegateItemProvider).removeListener(this); 133 } 134 135 if (childrenMap != null) 136 { 137 for (Iterator i = childrenMap.values().iterator(); i.hasNext();) 138 { 139 ((IDisposable)i.next()).dispose(); 140 } 141 } 142 } 143 144 149 protected Object getDelegateValue() 150 { 151 return value; 152 } 153 154 157 public Collection getElements(Object object) 158 { 159 return delegateItemProvider instanceof IStructuredItemContentProvider ? 160 ((IStructuredItemContentProvider)delegateItemProvider).getElements(getDelegateValue()) : 161 Collections.EMPTY_LIST; 162 } 163 164 169 public Collection getChildren(Object object) 170 { 171 updateChildren(); 172 173 Collection result = new ArrayList (delegateChildren.size()); 174 for (Iterator i = delegateChildren.iterator(); i.hasNext(); ) 175 { 176 result.add(childrenMap.get(i.next())); 177 } 178 return result; 179 } 180 181 186 protected void updateChildren() 187 { 188 if (delegateItemProvider instanceof ITreeItemContentProvider) 189 { 190 boolean changed = false; 191 Set oldDelegateChildren = delegateChildren != null ? new HashSet (delegateChildren) : Collections.EMPTY_SET; 192 delegateChildren = ((ITreeItemContentProvider)delegateItemProvider).getChildren(getDelegateValue()); 193 194 if (childrenMap == null && !delegateChildren.isEmpty()) 195 { 196 childrenMap = new HashMap (); 197 } 198 199 for (Iterator i = delegateChildren.iterator(); i.hasNext(); ) 202 { 203 Object child = i.next(); 204 205 if (!childrenMap.containsKey(child)) 206 { 207 IWrapperItemProvider wrapper = createWrapper(child, this, adapterFactory); 208 childrenMap.put(child, wrapper); 209 changed = true; 210 } 211 oldDelegateChildren.remove(child); 212 } 213 214 if (!oldDelegateChildren.isEmpty()) 217 { 218 changed = true; 219 220 for (Iterator i = oldDelegateChildren.iterator(); i.hasNext(); ) 221 { 222 Object child = i.next(); 223 224 IWrapperItemProvider wrapper = (IWrapperItemProvider)childrenMap.remove(child); 225 if (wrapper != null) 226 { 227 wrapper.dispose(); 228 } 229 } 230 } 231 232 if (changed) 234 { 235 int index = 0; 236 for (Iterator i = delegateChildren.iterator(); i.hasNext(); index++) 237 { 238 ((IWrapperItemProvider)childrenMap.get(i.next())).setIndex(index); 239 } 240 } 241 } 242 else 243 { 244 delegateChildren = Collections.EMPTY_LIST; 245 } 246 } 247 248 251 protected IWrapperItemProvider createWrapper(Object value, Object owner, AdapterFactory adapterFactory) 252 { 253 return new DelegatingWrapperItemProvider(value, owner, adapterFactory); 254 } 255 256 259 public boolean hasChildren(Object object) 260 { 261 return delegateItemProvider instanceof ITreeItemContentProvider ? 262 ((ITreeItemContentProvider)delegateItemProvider).hasChildren(getDelegateValue()) : 263 false; 264 } 265 266 269 public String getText(Object object) 270 { 271 return delegateItemProvider instanceof IItemLabelProvider ? 272 ((IItemLabelProvider)delegateItemProvider).getText(getDelegateValue()) : 273 null; 274 } 275 276 279 public Object getImage(Object object) 280 { 281 return delegateItemProvider instanceof IItemLabelProvider ? 282 ((IItemLabelProvider)delegateItemProvider).getImage(getDelegateValue()) : 283 null; 284 } 285 286 289 public List getPropertyDescriptors(Object object) 290 { 291 if (propertyDescriptors == null) 292 { 293 if (delegateItemProvider instanceof IItemPropertySource) 294 { 295 List l = ((IItemPropertySource)delegateItemProvider).getPropertyDescriptors(getDelegateValue()); 296 propertyDescriptors = new ArrayList (l.size()); 297 298 for (Iterator i = l.iterator(); i.hasNext(); ) 299 { 300 IItemPropertyDescriptor desc = (IItemPropertyDescriptor)i.next(); 301 propertyDescriptors.add(new DelegatingWrapperItemPropertyDescriptor(getDelegateValue(), desc)); 302 } 303 } 304 else 305 { 306 propertyDescriptors = Collections.EMPTY_LIST; 307 } 308 } 309 return propertyDescriptors; 310 } 311 312 315 public Object getEditableValue(Object object) 316 { 317 return delegateItemProvider instanceof IItemPropertySource ? 318 ((IItemPropertySource)delegateItemProvider).getEditableValue(getDelegateValue()) : 319 null; 320 } 321 322 325 public Collection getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling) 326 { 327 return delegateItemProvider instanceof IEditingDomainItemProvider ? 328 ((IEditingDomainItemProvider)delegateItemProvider).getNewChildDescriptors(getDelegateValue(), editingDomain, sibling) : 329 Collections.EMPTY_LIST; 330 } 331 332 337 public Command createCommand(Object object, EditingDomain domain, Class commandClass, CommandParameter commandParameter) 338 { 339 if (commandClass == DragAndDropCommand.class) 340 { 341 DragAndDropCommand.Detail detail = (DragAndDropCommand.Detail)commandParameter.getFeature(); 342 return createDragAndDropCommand(domain, commandParameter.getOwner(), detail.location, detail.operations, detail.operation, commandParameter.getCollection()); 343 } 344 345 if (delegateItemProvider instanceof IEditingDomainItemProvider) 346 { 347 Object commandOwner = getDelegateValue(); 348 Command result = null; 349 350 if (commandClass == SetCommand.class) 353 { 354 Object feature = commandParameter.getFeature(); 355 result = SetCommand.create(domain, commandOwner, feature, commandParameter.getValue(), commandParameter.getIndex()); 356 357 if (feature == null) 361 { 362 return new ReplacementAffectedObjectCommand(result); 363 } 364 } 365 else 366 { 367 commandParameter.setOwner(commandOwner); 368 result = ((IEditingDomainItemProvider)delegateItemProvider).createCommand(commandOwner, domain, commandClass, commandParameter); 369 } 370 return wrapCommand(result, commandClass); 371 } 372 return UnexecutableCommand.INSTANCE; 373 } 374 375 381 protected Command wrapCommand(Command command, Class commandClass) 382 { 383 return command instanceof CommandActionDelegate ? 384 new AffectedObjectsWrappingCommandActionDelegate((CommandActionDelegate)command) : 385 new AffectedObjectsWrappingCommand(command); 386 } 387 388 392 protected class AffectedObjectsWrappingCommand extends CommandWrapper 393 { 394 public AffectedObjectsWrappingCommand(Command command) 395 { 396 super(command); 397 } 398 399 public Collection getAffectedObjects() 400 { 401 List result = new ArrayList (super.getAffectedObjects()); 402 updateChildren(); 403 404 for (ListIterator i = result.listIterator(); i.hasNext(); ) 405 { 406 Object object = i.next(); 407 408 if (object == getDelegateValue()) 409 { 410 i.set(DelegatingWrapperItemProvider.this); 411 } 412 else if (childrenMap != null) 413 { 414 Object wrapper = childrenMap.get(object); 415 if (wrapper != null) 416 { 417 i.set(wrapper); 418 } 419 } 420 } 421 return result; 422 } 423 } 424 425 431 protected class AffectedObjectsWrappingCommandActionDelegate extends AffectedObjectsWrappingCommand 432 implements CommandActionDelegate 433 { 434 CommandActionDelegate commandActionDelegate; 435 436 440 public AffectedObjectsWrappingCommandActionDelegate(CommandActionDelegate command) 441 { 442 super((Command)command); 443 commandActionDelegate = command; 444 } 445 446 public boolean canExecute() 447 { 448 return commandActionDelegate.canExecute(); 449 } 450 451 public Object getImage() 452 { 453 return commandActionDelegate.getImage(); 454 } 455 456 public String getText() 457 { 458 return commandActionDelegate.getText(); 459 } 460 461 public String getDescription() 462 { 463 return commandActionDelegate.getDescription(); 464 } 465 466 public String getToolTipText() 467 { 468 return commandActionDelegate.getToolTipText(); 469 } 470 } 471 472 475 public void addListener(INotifyChangedListener listener) 476 { 477 if (changeNotifier == null) 478 { 479 changeNotifier = new ChangeNotifier(); 480 } 481 changeNotifier.addListener(listener); 482 } 483 484 487 public void removeListener(INotifyChangedListener listener) 488 { 489 if (changeNotifier != null) 490 { 491 changeNotifier.removeListener(listener); 492 } 493 } 494 495 498 public void fireNotifyChanged(Notification notification) 499 { 500 if (adapterFactory instanceof IChangeNotifier) 501 { 502 IChangeNotifier adapterFactoryChangeNotifier = (IChangeNotifier)adapterFactory; 503 adapterFactoryChangeNotifier.fireNotifyChanged(notification); 504 } 505 if (changeNotifier != null) 506 { 507 changeNotifier.fireNotifyChanged(notification); 508 } 509 } 510 511 516 public void notifyChanged(Notification notification) 517 { 518 if (getRefreshElement(notification) == getDelegateValue()) 519 { 520 fireNotifyChanged(wrapNotification(notification)); 521 } 522 } 523 524 529 protected Object getRefreshElement(Notification notification) 530 { 531 if (notification instanceof IViewerNotification) 532 { 533 return ((IViewerNotification)notification).getElement(); 534 } 535 return notification.getNotifier(); 536 } 537 538 542 protected Notification wrapNotification(Notification notification) 543 { 544 return ViewerNotification.wrapNotification(notification, this); 545 } 546 547 555 protected class DelegatingWrapperItemPropertyDescriptor extends ItemPropertyDescriptorDecorator 556 implements OverrideableCommandOwner 557 { 558 protected Object commandOwner; 559 560 public DelegatingWrapperItemPropertyDescriptor(Object object, IItemPropertyDescriptor itemPropertyDescriptor) 561 { 562 super(object, itemPropertyDescriptor); 563 } 564 565 569 public void setCommandOwner(Object commandOwner) 570 { 571 this.commandOwner = commandOwner; 572 if (itemPropertyDescriptor instanceof OverrideableCommandOwner) 573 { 574 ((OverrideableCommandOwner)itemPropertyDescriptor).setCommandOwner(commandOwner); 575 } 576 } 577 578 581 public Object getCommandOwner() 582 { 583 return commandOwner; 584 } 585 586 589 public void resetPropertyValue(Object thisObject) 590 { 591 boolean hasCommandOwner = commandOwner != null; 592 if (!hasCommandOwner) 593 { 594 setCommandOwner(DelegatingWrapperItemProvider.this); 595 } 596 itemPropertyDescriptor.resetPropertyValue(object); 597 if (!hasCommandOwner) 598 { 599 setCommandOwner(null); 600 } 601 } 602 603 607 public void setPropertyValue(Object thisObject, Object value) 608 { 609 boolean hasCommandOwner = commandOwner != null; 610 if (!hasCommandOwner) 611 { 612 setCommandOwner(DelegatingWrapperItemProvider.this); 613 } 614 itemPropertyDescriptor.setPropertyValue(object, value); 615 if (!hasCommandOwner) 616 { 617 setCommandOwner(null); 618 } 619 } 620 } 621 } | Popular Tags |