1 17 package org.eclipse.emf.edit.provider; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 23 import org.eclipse.emf.common.command.Command; 24 import org.eclipse.emf.common.command.UnexecutableCommand; 25 import org.eclipse.emf.common.notify.AdapterFactory; 26 import org.eclipse.emf.common.notify.Notification; 27 import org.eclipse.emf.common.notify.NotificationChain; 28 import org.eclipse.emf.common.notify.impl.NotificationImpl; 29 import org.eclipse.emf.common.notify.impl.NotifyingListImpl; 30 import org.eclipse.emf.common.util.EList; 31 import org.eclipse.emf.edit.command.CommandParameter; 32 import org.eclipse.emf.edit.domain.EditingDomain; 33 34 35 247 public class ItemProvider 248 implements 249 IChangeNotifier, 250 IDisposable, 251 IItemLabelProvider, 252 IStructuredItemContentProvider, 253 ITreeItemContentProvider, 254 IUpdateableItemParent 255 { 256 259 protected String text; 260 261 264 protected Object image; 265 266 269 protected Object parent; 270 271 274 protected ItemProviderNotifyingArrayList children; 275 276 279 protected AdapterFactory adapterFactory; 280 281 284 protected IChangeNotifier changeNotifier; 285 286 289 public class ItemProviderNotification extends NotificationImpl 290 { 291 public ItemProviderNotification(int eventType, Object oldValue, Object newValue, int position) 292 { 293 this(eventType, oldValue, newValue, position, false); 294 } 295 296 public ItemProviderNotification(int eventType, Object oldValue, Object newValue, int position, boolean wasSet) 297 { 298 super(eventType, oldValue, newValue, position, wasSet); 299 } 300 301 public Object getNotifier() 302 { 303 return ItemProvider.this; 304 } 305 306 public void dispatch() 307 { 308 Object notifier = getNotifier(); 309 if (notifier != null && getEventType() != -1) 310 { 311 ((IChangeNotifier)notifier).fireNotifyChanged(this); 312 } 313 314 if (next != null) 315 { 316 next.dispatch(); 317 } 318 } 319 } 320 321 326 public class ItemProviderNotifyingArrayList extends NotifyingListImpl 327 { 328 331 public ItemProviderNotifyingArrayList() 332 { 333 } 334 335 338 public ItemProviderNotifyingArrayList(int initialCapacity) 339 { 340 super(initialCapacity); 341 } 342 343 346 protected boolean isNotificationRequired() 347 { 348 return true; 349 } 350 351 354 protected boolean hasInverse() 355 { 356 return true; 357 } 358 359 366 public ItemProviderNotifyingArrayList(Collection collection) 367 { 368 super(); 369 addAll(collection); 370 } 371 372 375 protected void dispatchNotification(Notification notification) 376 { 377 ((IChangeNotifier)notification.getNotifier()).fireNotifyChanged(notification); 378 } 379 380 383 protected NotificationImpl createNotification(int eventType, Object oldObject, Object newObject, int index, boolean wasSet) 384 { 385 return new ItemProviderNotification(eventType, oldObject, newObject, index, wasSet); 386 } 387 388 391 protected NotificationChain inverseAdd(Object object, NotificationChain notifications) 392 { 393 Object adapter = object; 394 if (adapterFactory != null) 395 { 396 adapter = adapterFactory.adapt(object, IUpdateableItemParent.class); 397 } 398 399 if (adapter instanceof IUpdateableItemParent) 400 { 401 ((IUpdateableItemParent)adapter).setParent(object, ItemProvider.this); 402 } 403 404 return notifications; 405 } 406 407 410 protected NotificationChain inverseRemove(Object object, NotificationChain notifications) 411 { 412 Object adapter = object; 413 if (adapterFactory != null) 414 { 415 adapter = adapterFactory.adapt(object, IUpdateableItemParent.class); 416 } 417 418 if (adapter instanceof IUpdateableItemParent) 419 { 420 ((IUpdateableItemParent)adapter).setParent(object, null); 421 } 422 423 return notifications; 424 } 425 } 426 427 430 public ItemProvider() 431 { 432 this.text = ""; 433 this.children = new ItemProviderNotifyingArrayList(); 434 } 435 436 439 public ItemProvider(Collection children) 440 { 441 this.text = ""; 442 this.children = new ItemProviderNotifyingArrayList(children); 443 } 444 445 448 public ItemProvider(String text) 449 { 450 this.text = text; 451 this.children = new ItemProviderNotifyingArrayList(); 452 } 453 454 457 public ItemProvider(String text, Collection children) 458 { 459 this.text = text; 460 this.children = new ItemProviderNotifyingArrayList(children); 461 } 462 463 466 public ItemProvider(String text, Object image) 467 { 468 this.text = text; 469 this.image = image; 470 this.children = new ItemProviderNotifyingArrayList(); 471 } 472 473 476 public ItemProvider(String text, Object image, Collection children) 477 { 478 this.text = text; 479 this.image = image; 480 this.children = new ItemProviderNotifyingArrayList(children); 481 } 482 483 486 public ItemProvider(String text, Object image, Object parent) 487 { 488 this.text = text; 489 this.image = image; 490 this.parent = parent; 491 this.children = new ItemProviderNotifyingArrayList(); 492 } 493 494 497 public ItemProvider(String text, Object image, Object parent, Collection children) 498 { 499 this.text = text; 500 this.image = image; 501 this.parent = parent; 502 this.children = new ItemProviderNotifyingArrayList(children); 503 } 504 505 508 public ItemProvider(AdapterFactory adapterFactory) 509 { 510 this.adapterFactory = adapterFactory; 511 this.text = ""; 512 this.children = new ItemProviderNotifyingArrayList(); 513 } 514 515 518 public ItemProvider(AdapterFactory adapterFactory, String text) 519 { 520 this.adapterFactory = adapterFactory; 521 this.text = text; 522 this.children = new ItemProviderNotifyingArrayList(); 523 } 524 525 528 public ItemProvider(AdapterFactory adapterFactory, String text, Object image) 529 { 530 this.adapterFactory = adapterFactory; 531 this.text = text; 532 this.image = image; 533 this.children = new ItemProviderNotifyingArrayList(); 534 } 535 536 539 public ItemProvider(AdapterFactory adapterFactory, String text, Object image, Object parent) 540 { 541 this.adapterFactory = adapterFactory; 542 this.text = text; 543 this.image = image; 544 this.parent = parent; 545 this.children = new ItemProviderNotifyingArrayList(); 546 } 547 548 551 public ItemProvider(AdapterFactory adapterFactory, Collection children) 552 { 553 this.adapterFactory = adapterFactory; 554 this.text = ""; 555 this.children = new ItemProviderNotifyingArrayList(children); 556 } 557 558 561 public ItemProvider(AdapterFactory adapterFactory, String text, Collection children) 562 { 563 this.adapterFactory = adapterFactory; 564 this.text = text; 565 this.children = new ItemProviderNotifyingArrayList(children); 566 } 567 568 571 public ItemProvider 572 (AdapterFactory adapterFactory, String text, Object image, Collection children) 573 { 574 this.adapterFactory = adapterFactory; 575 this.text = text; 576 this.image = image; 577 this.children = new ItemProviderNotifyingArrayList(children); 578 } 579 580 584 public ItemProvider 585 (AdapterFactory adapterFactory, 586 String text, 587 Object image, 588 Object parent, 589 Collection children) 590 { 591 this.adapterFactory = adapterFactory; 592 this.text = text; 593 this.image = image; 594 this.parent = parent; 595 this.children = new ItemProviderNotifyingArrayList(children); 596 } 597 598 601 public AdapterFactory getAdapterFactory() 602 { 603 return adapterFactory; 604 } 605 606 609 public void setAdapterFactory(AdapterFactory adapterFactory) 610 { 611 this.adapterFactory = adapterFactory; 612 } 613 614 public void addListener(INotifyChangedListener listener) 615 { 616 if (changeNotifier == null) 617 { 618 changeNotifier = new ChangeNotifier(); 619 } 620 changeNotifier.addListener(listener); 621 } 622 623 public void removeListener(INotifyChangedListener listener) 624 { 625 if (changeNotifier != null) 626 { 627 changeNotifier.removeListener(listener); 628 } 629 } 630 631 public void fireNotifyChanged(Notification notification) 632 { 633 if (changeNotifier != null) 634 { 635 changeNotifier.fireNotifyChanged(notification); 636 } 637 638 if (adapterFactory instanceof IChangeNotifier) 639 { 640 ((IChangeNotifier)adapterFactory).fireNotifyChanged(notification); 641 } 642 } 643 644 649 public Collection getElements(Object object) 650 { 651 return getChildren(object); 652 } 653 654 658 public EList getElements() 659 { 660 return getChildren(); 661 } 662 663 670 public Collection getChildren(Object object) 671 { 672 return children; 673 } 674 675 678 public EList getChildren() 679 { 680 return children; 681 } 682 683 689 public boolean hasChildren(Object object) 690 { 691 return !children.isEmpty(); 692 } 693 694 697 public boolean hasChildren() 698 { 699 return hasChildren(this); 700 } 701 702 706 public Object getParent(Object object) 707 { 708 return parent; 709 } 710 711 714 public Object getParent() 715 { 716 return getParent(this); 717 } 718 719 723 public void setParent(Object object, Object parent) 724 { 725 this.parent = parent; 726 } 727 728 731 public void setParent(Object parent) 732 { 733 setParent(this, parent); 734 } 735 736 740 public Object getImage(Object object) 741 { 742 return image; 743 } 744 745 748 public Object getImage() 749 { 750 return getImage(this); 751 } 752 753 757 public void setImage(Object object, Object image) 758 { 759 this.image = image; 760 761 fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, image, Notification.NO_INDEX)); 762 } 763 764 767 public void setImage(Object image) 768 { 769 setImage(this, image); 770 } 771 772 775 public String getText(Object object) 776 { 777 return text; 778 } 779 780 783 public String getText() 784 { 785 return getText(this); 786 } 787 788 792 public String getUpdateableText(Object object) 793 { 794 return getText(object); 795 } 796 797 802 public void setText(Object object, String text) 803 { 804 this.text = text; 805 806 fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, text, Notification.NO_INDEX)); 807 } 808 809 812 public void setText(String text) 813 { 814 setText(this, text); 815 } 816 817 820 public String toString() 821 { 822 return super.toString() + "[text=\"" + text + "\"]"; 823 } 824 825 public void dispose() 826 { 827 } 828 829 834 public Collection getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling) 835 { 836 return Collections.EMPTY_LIST; 837 } 838 839 844 public Command createCommand(Object object, EditingDomain editingDomain, Class commandClass, CommandParameter commandParameter) 845 { 846 return UnexecutableCommand.INSTANCE; 847 } 848 } 849 | Popular Tags |