Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 40da2c63870f849c0f6406c6dde6d11789fccac4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/**
 * Copyright (c) 2002-2006 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 */
package org.eclipse.emf.edit.provider;


import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.notify.impl.NotificationChainImpl;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.emf.common.notify.impl.NotifyingListImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.domain.EditingDomain;


/**
 * This item provider implementation is a convenient reusable base 
 * that can be used for an item provider that isn't an adapter for an EMF object.
 *
 * This default implementation is highly functional and is plastic enough for a wide variety of uses
 * (as will be illustrated in the examples to come).
 * The plasticity is the reason for providing a huge number of constructors.
 *
 * <p>
 * The {@link #children} list is implemented using {@link ItemProviderNotifyingArrayList}.
 * As a result, any modification of the collection (using the standard {@link java.util.List} interface) 
 * will automatically fire the correct call to each {@link INotifyChangedListener} in the {@link #changeNotifier}.
 * Furthermore, {@link IUpdateableItemParent#setParent IUpdateableItemParent.setParent} 
 * is called to update {@link #parent} for the objects that are added to or removed from the list,
 * but optionally, i.e., only if the interface is implemented---the {@link #adapterFactory} is used if it isn't null.
 *
 * <p>
 * There is also a {@link #text} and an {@link #image}, 
 * which can be set via {@link #setText(String) setText} and {@link #setImage(Object) setImage}
 * to cause appropriate domain event notifications to be fired.
 * The set methods use the stateless adapter signature for uniformity and to support 
 * {@link IUpdateableItemText#setText(Object, String)}.
 *
 * <p>
 * This class is useful as a convenient wrapper object to act as the input to a view, e.g.,
 * <pre>
 *   viewer.setInput(new ItemProvider(text, image, collection));
 * </pre>
 * lets you take a mixed collection of model objects and item providers,
 * and show it as the elements of a structured view, i.e., as the visible roots of the view.
 * Although a structured viewer <b>does not</b> show it's input object within the view, 
 * it <b>does</b> show the input object on the pane title.
 * The above pattern allows you to inject a collection or the object itself into the structured viewer 
 * and to control the pane title at the same time, e.g.,
 * <pre>
 *   viewer.setInput(new ItemProvider(Collections.singleton(object)));
 * </pre>
 * will leave the pane title blank and show the object as the root of the structured view.
 *
 * <p>
 * One could use more of these item providers to build up a scaffolding within views.
 * Consider the following block of code
 * which has access to a collection of {@link INotifyChangedListener}s.
 * <pre>
 *   // final Collection listeners = ...;
 *   // final StructuredContentViewer contentViewer = ...;
 *   //
 *   // These create the items and build up the structure.
 *   //
 *   final ItemProvider child11     = new ItemProvider(listeners, "Child 1");
 *   final ItemProvider child12     = new ItemProvider(listeners, "Child 2");
 *   final ItemProvider parent1     = new ItemProvider(listeners, "Parent 1", Arrays.asList(new Object [] {child11, child12}));
 *   final ItemProvider child21     = new ItemProvider(listeners, "Child 1");
 *   final ItemProvider child22     = new ItemProvider(listeners, "Child 2");
 *   final ItemProvider parent2     = new ItemProvider(listeners, "Parent 2", Arrays.asList(new Object [] {child21, child22}));
 *   final ItemProvider grandParent = new ItemProvider(listeners, "Grand Parent", Arrays.asList(new Object [] {parent1, parent2}));
 *   
 *   // Set the items into the visible roots of the structured content viewer.
 *   //
 *   contentViewer.setInput(new ItemProvider("Pane Tile", Collections.singleton(grandParent)));
 *   
 *   // Create some delayed actions that modify the item structure.
 *   //
 *   if (contentViewer.isControlOkToUse())
 *   {
 *     contentViewer.getControl().getDisplay().asyncExec
 *      (new Runnable()
 *       {
 *         public void run()
 *         {
 *           // Use standard list modification that has the effect of producing a domain event notification.
 *           //
 *           parent1.getChildren().removeAll(Arrays.asList(new Object [] {child11, child12}));
 *  
 *           contentViewer.getControl().getDisplay().asyncExec
 *             (new Runnable()
 *               {
 *                 public void run()
 *                 {
 *                   // This also as the effect of producing a correct a domain event notification.
 *                   //
 *                   parent2.setText("Parent 2!");
 *                 }
 *               });
 *          }
 *       });
 *  }
 * </pre>
 * The structure will be displayed within the contentViewer and will then change a little bit later; 
 * the flickering should be noticeable if the viewer is set to auto expand.
 *
 * <p>
 * Another common pattern of usage will be to inject scaffolding within an EMF structure.
 * In the following example, a new factory is defined to replace the adapters for Company and Department
 * so as to inject an <em>item</em> that acts as the child of the Company and the parent of each Department.
 * (Normally, this would not be done with all these inner classes.)
 * <pre>
 *  ItemProviderAdapterFactory myItemProviderAdapterFactory = 
 *    new ItemProviderAdapterFactory()
 *    {
 *      public Adapter createCompanyAdapter()
 *      {
 *        // This returns a new instance each time.
 *        // The instance stores an injected child that in turn will have this original object's children as its children.
 *        //
 *        return
 *          new CompanyItemProvider(this)
 *          {
 *            // Keep track of the new child added below company.
 *            //
 *            ItemProvider injectedChild;
 *
 *            public Collection getChildren(final Object object)
 *            {
 *              // Create one on demand.
 *              //
 *              if (injectedChild == null)
 *              {
 *                injectedChild = 
 *                  (new ItemProvider("Injected Child")
 *                   {
 *                     public Collection getChildren(Object o)
 *                     {
 *                       // Return the department of the company. 
 *                       // Note that we ignore o in favour of object.
 *                       //
 *                       return ((Company)object).getDepartment();
 *                     }
 *                     public boolean hasChildren(Object o)
 *                     {
 *                       // You have to make sure you override this method to match the above.
 *                       //
 *                       return !((Company)object).getDepartment().isEmpty();
 *                     }
 *                   });
 *              }
 *
 *              return Collections.singleton(injectedChild);
 *            }
 *
 *            public boolean hasChildren(Object object)
 *            {
 *              // You have to make sure you override this method to match the above.
 *              //
 *              return true;
 *            }
 *
 *            public void notifyChanged(Notification msg)
 *            {
 *              // If the departments are affected...
 *              //
 *              Company company = (Company)msg.getNotifier();
 *              if (msg.getStructuralFeature() == company.ePackageCompany().getCompany_Deparment())
 *              {
 *                // If there's a child around to care...
 *                //
 *                if (injectedChild != null)
 *                {
 *                  // Fire the domain event as if it came from the child.
 *                  //
 *                  // EATM TODO
 *                  fireNotifyChanged(injectedChild, msg.getEventType(), msg.getStructuralFeature(), msg.getOldValue(), msg.getNewValue(), msg.getPostition());
 *                }
 *              }
 *              else
 *              {
 *                // Behave as normal.
 *                //
 *                super.notifyChanged(msg);
 *              }
 *            }
 *          };
 *      }
 *
 *      public Adapter createDepartmentAdapter()
 *      {
 *        // This is still stateless.
 *        //
 *        if (departmentItemProvider == null)
 *        {
 *          departmentItemProvider = 
 *            new DepartmentItemProvider(this)
 *            {
 *              public Object getParent(Object object)
 *              {
 *                // Use the stateful adapter of the containing parent to determine the injected item.
 *                //
 *                Company company = ((Department)object).getCompany();
 *                ITreeItemContentProvider companyAdapter = 
 *                  (ITreeItemContentProvider)this.adapterFactory.adapt(company, ITreeItemContentProvider.class);
 *                if (companyAdapter != null)
 *                {
 *                  // Get the first child of the company's adapter.
 *                  //
 *                  return companyAdapter.getChildren(company).iterator().next();
 *                }
 *                else
 *                {
 *                  return null;
 *                }
 *              }
 *            };
 *         }
 *
 *         // Return the single factory instance.
 *         //
 *         return departmentItemProvider;
 *      }
 *    };
 * </pre>
 *
 */
public class ItemProvider
  implements 
    IChangeNotifier,
    IDisposable,
    IItemLabelProvider, 
    IItemColorProvider,
    IItemFontProvider,
    IItemStyledLabelProvider,
    IStructuredItemContentProvider, 
    ITreeItemContentProvider, 
    IUpdateableItemParent
{
  /**
   * This is the text returned by {@link IItemLabelProvider#getText getText(Object)}.
   */
  protected String text;

  /**
   * This is the image returned by {@link IItemLabelProvider#getImage getImage(Object)}.
   */
  protected Object image;

  /**
   * This is the font returned by {@link IItemFontProvider#getFont getFont(Object)}.
   */
  protected Object font;

  /**
   * This is the color returned by {@link IItemColorProvider#getForeground getForeground(Object)}.
   */
  protected Object foreground;

  /**
   * This is the color returned by {@link IItemColorProvider#getBackground getBackground(Object)}.
   */
  protected Object background;

  /**
   * This is the text returned by {@link IItemStyledLabelProvider#getStyledText getStyledText(Object)}.
   * @since 2.10
   */
  protected Object styledText;

  /**
   * This is the parent returned by {@link ITreeItemContentProvider#getParent getParent(Object)}.
   */
  protected Object parent;

  /**
   * This is the children returned by {@link ITreeItemContentProvider#getChildren getChildren(Object)}.
   */
  protected ItemProviderNotifyingArrayList<Object> children;

  /**
   * This is the optional adapter factory that is used to get adapters for parent or child objects.
   */
  protected AdapterFactory adapterFactory;

  /**
   * This is the optional collection used for changes to the text, parent, or children.
   */
  protected IChangeNotifier changeNotifier;

  /**
   * This class implements a Notification for an ItemProvider.
   */
  public class ItemProviderNotification extends NotificationImpl implements IViewerNotification
  {
    protected boolean isContentRefresh;
    protected boolean isLabelUpdate;

    public ItemProviderNotification(int eventType, Object oldValue, Object newValue, int position)
    {
      this(eventType, oldValue, newValue, position, false);
    }

    public ItemProviderNotification(int eventType, Object oldValue, Object newValue, int position, boolean wasSet)
    {
      this(eventType, oldValue, newValue, position, wasSet, true, true);
    }

    public ItemProviderNotification
      (int eventType, 
       Object oldValue, 
       Object newValue, 
       int position, 
       boolean wasSet, 
       boolean isContentRefesh,
       boolean isLabelUpdate)
    {
      super(eventType, oldValue, newValue, position, wasSet);
      this.isContentRefresh = isContentRefesh;
      this.isLabelUpdate = isLabelUpdate;
    }

    @Override
    public Object getNotifier()
    {
      return ItemProvider.this;
    }

    @Override
    public void dispatch()
    {
      Object notifier = getNotifier();
      if (notifier != null && getEventType() != -1)
      {
        ((IChangeNotifier)notifier).fireNotifyChanged(this);
      }

      if (next != null)
      {
        next.dispatch();
      }
    }

    public Object getElement()
    {
      return getNotifier();
    }

    public boolean isContentRefresh()
    {
      return isContentRefresh;
    }

    public boolean isLabelUpdate()
    {
      return isLabelUpdate;
    }
  }

  /**
   * This class overrides the "notify" methods to fire {@link org.eclipse.emf.edit.provider.INotifyChangedListener} calls
   * and it overrides the "inverse basic" methods to maintain referential integrity 
   * by calling {@link IUpdateableItemParent#setParent IUpdateableItemParent.setParent}.
   */
  public class ItemProviderNotifyingArrayList<E> extends NotifyingListImpl<E>
  {
    private static final long serialVersionUID = 1L;

    /**
     * This constructs an empty instance.
     */
    public ItemProviderNotifyingArrayList()
    {
      super();
    }

    /**
     * This constructs an instance with this initial capacity.
     */
    public ItemProviderNotifyingArrayList(int initialCapacity)
    {
      super(initialCapacity);
    }

    /**
     * This always notifies.
     */
    @Override
    protected boolean isNotificationRequired()
    {
      return true;
    }

    /**
     * This has an inverse
     */
    @Override
    protected boolean hasInverse()
    {
      return true;
    }

    /**
     * This constructs an instance with the same initial content as the given collection.
     * Note that the add methods are called to do this and hence calls to basic methods are produced.
     * This means there will be notification, 
     * but you can make sure the domain notifier is null during this constructor invocation to change that behaviour.
     * All the basic item provider constructors ensure that no domain events are fired.
     */
    public ItemProviderNotifyingArrayList(Collection<? extends E> collection)
    {
      super();
      addAll(collection);
    }

    /**
     * This implementation directs the notification the containing item provider.
     */
    @Override
    protected void dispatchNotification(Notification notification)
    {
      ((IChangeNotifier)notification.getNotifier()).fireNotifyChanged(notification);
    }

    /**
     * This implementation creates an {@link ItemProvider.ItemProviderNotification}.
     */
    @Override
    protected NotificationImpl createNotification(int eventType, Object oldObject, Object newObject, int index, boolean wasSet)
    {
      return new ItemProviderNotification(eventType, oldObject, newObject, index, wasSet, true, false);
    }
    
    @Override
    protected NotificationChain createNotificationChain(int capacity)
    {
      return 
        new NotificationChainImpl(capacity)
        {
          private static final long serialVersionUID = 1L;

          @Override
          protected void dispatch(Notification notification)
          {
            ItemProviderNotifyingArrayList.this.dispatchNotification(notification);
          }
        };
    }

    /**
     * This implementation will call {@link IUpdateableItemParent#setParent IUpdateableItemParent.setParent}, if appropriate.
     */
    @Override
    protected NotificationChain inverseAdd(Object object, NotificationChain notifications)
    {
      Object adapter = object;
      if (adapterFactory != null)
      {
        adapter = adapterFactory.adapt(object, IUpdateableItemParent.class);
      }

      if (adapter instanceof IUpdateableItemParent)
      {
        ((IUpdateableItemParent)adapter).setParent(object, ItemProvider.this);
      }

      return notifications;
    }
  
    /**
     * This implementation will call {@link IUpdateableItemParent#setParent IUpdateableItemParent.setParent}, if appropriate.
     */
    @Override
    protected NotificationChain inverseRemove(Object object, NotificationChain notifications)
    {
      Object adapter = object;
      if (adapterFactory != null)
      {
        adapter = adapterFactory.adapt(object, IUpdateableItemParent.class);
      }

      if (adapter instanceof IUpdateableItemParent)
      {
        ((IUpdateableItemParent)adapter).setParent(object, null);
      }

      return notifications;
    }
  }
  
  /**
   * This creates an instance with an empty text that yields no children.
   */
  public ItemProvider()
  {
    this.text =  "";
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with an empty text that yields the given children.
   */
  public ItemProvider(Collection<?> children)
  {
    this.text =  "";
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given text that yields the no children.
   */
  public ItemProvider(String text)
  {
    this.text = text;
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given text that yields the given children.
   */
  public ItemProvider(String text, Collection<?> children)
  {
    this.text = text;
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given text and image that yields the no children.
   */
  public ItemProvider(String text, Object image)
  {
    this.text = text;
    this.image = image;
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given text and image that yields the given children.
   */
  public ItemProvider(String text, Object image, Collection<?> children)
  {
    this.text = text;
    this.image = image;
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given text, image, and parent that yields no children.
   */
  public ItemProvider(String text, Object image, Object parent)
  {
    this.text = text;
    this.image = image;
    this.parent = parent;
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given text, image, and parent that yields the given children.
   */
  public ItemProvider(String text, Object image, Object parent, Collection<?> children)
  {
    this.text = text;
    this.image = image;
    this.parent = parent;
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given adapter factory and an empty text that yields no children.
   */
  public ItemProvider(AdapterFactory adapterFactory)
  {
    this.adapterFactory = adapterFactory;
    this.text =  "";
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given adapter factor and text that yields no children.
   */
  public ItemProvider(AdapterFactory adapterFactory, String text)
  {
    this.adapterFactory = adapterFactory;
    this.text = text;
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given adapter factory, text, and image that yields no children.
   */
  public ItemProvider(AdapterFactory adapterFactory, String text, Object image)
  {
    this.adapterFactory = adapterFactory;
    this.text = text;
    this.image = image;
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given adapter factory, text, image, and parent that yields no children.
   */
  public ItemProvider(AdapterFactory adapterFactory, String text, Object image, Object parent)
  {
    this.adapterFactory = adapterFactory;
    this.text = text;
    this.image = image;
    this.parent = parent;
    this.children = new ItemProviderNotifyingArrayList<Object>();
  }

  /**
   * This creates an instance with the given adapter factory that yields the given children.
   */
  public ItemProvider(AdapterFactory adapterFactory, Collection<?> children)
  {
    this.adapterFactory = adapterFactory;
    this.text =  "";
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given adapter factory and text that yields the given children.
   */
  public ItemProvider(AdapterFactory adapterFactory, String text, Collection<?> children)
  {
    this.adapterFactory = adapterFactory;
    this.text = text;
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given adapter factory, text and image that yields the given children.
   */
  public ItemProvider
    (AdapterFactory adapterFactory, String text, Object image, Collection<?> children)
  {
    this.adapterFactory = adapterFactory;
    this.text = text;
    this.image = image;
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This creates an instance with the given adapter factory, notifier, text, image, and parent that yields the given children.
   * This is a fully specified instance.
   */
  public ItemProvider
    (AdapterFactory adapterFactory, 
     String text, 
     Object image, 
     Object parent, 
     Collection<?> children)
  {
    this.adapterFactory = adapterFactory;
    this.text = text;
    this.image = image;
    this.parent = parent;
    this.children = new ItemProviderNotifyingArrayList<Object>(children);
  }

  /**
   * This yields the optional adapter factory.
   */
  public AdapterFactory getAdapterFactory()
  {
    return adapterFactory;
  }

  /**
   * This sets the optional adapter factory.
   */
  public void setAdapterFactory(AdapterFactory adapterFactory)
  {
    this.adapterFactory = adapterFactory;
  }

  public void addListener(INotifyChangedListener listener)
  {
    if (changeNotifier == null)
    {
      changeNotifier = new ChangeNotifier();
    }
    changeNotifier.addListener(listener);
  }

  public void removeListener(INotifyChangedListener listener)
  {
    if (changeNotifier != null)
    {
      changeNotifier.removeListener(listener);
    }
  }

  public void fireNotifyChanged(Notification notification)
  {
    if (changeNotifier != null)
    {
      changeNotifier.fireNotifyChanged(notification);
    }

    if (adapterFactory instanceof IChangeNotifier)
    {
      ((IChangeNotifier)adapterFactory).fireNotifyChanged(notification);
    }
  }

  /**
   * This implements {@link IStructuredItemContentProvider#getElements IStructuredItemContentProvider.getElements} 
   * by returning {@link #getChildren(Object)}.
   * It seems that you almost always want getElements and getChildren to return the same thing, so this makes that easy.
   */
  public Collection<?> getElements(Object object)
  {
    return getChildren(object);
  }

  /**
   * This returns {@link #getChildren()}.
   * It seems that you almost always want getElements and getChildren to return the same thing, so this makes that easy.
   */
  public EList<Object> getElements()
  {
    return getChildren();
  }

  /**
   * This implements {@link ITreeItemContentProvider#getChildren ITreeItemContentProvider.getChildren} 
   * return {@link #children}.
   * You can also choose to ignore the {@link #children} entirely and implement a virtual collection;
   * In this case, you must implement notification is some other way yourself,
   * and you should override {@link #hasChildren(Object)} appropriately.
   */
  public Collection<?> getChildren(Object object)
  {
    return children;
  }

  /**
   * This returns {@link #getChildren() getChildren(this)}.
   */
  public EList<Object> getChildren()
  {
    return children;
  }

  /**
   * This implements {@link ITreeItemContentProvider#hasChildren ITreeItemContentProvider.hasChildren} 
   * by simply testing whether {@link #children} is empty.
   * This implementation will always be right, 
   * however, for efficiency you may want to override it to return false for a leaf item, or true for an item that always has children.
   */
  public boolean hasChildren(Object object)
  {
    return !children.isEmpty();
  }

  /**
   * This returns {@link #hasChildren() hasChildren(this)}.
   */
  public boolean hasChildren()
  {
    return hasChildren(this);
  }

  /**
   * This implements {@link ITreeItemContentProvider#getParent ITreeItemContentProvider.getParent} 
   * by returning {@link #parent}.
   */
  public Object getParent(Object object)
  {
    return parent;
  }

  /**
   * This returns {@link #getParent() getParent(this)}.
   */
  public Object getParent()
  {
    return getParent(this);
  }

  /**
   * This implements {@link IUpdateableItemParent#setParent IUpdateableItemParent.setParent} 
   * by delegating to {@link #setParent(Object)}.
   */
  public void setParent(Object object, Object parent)
  {
    this.parent = parent;
  }

  /**
   * This calls {@link #setParent(Object, Object) setParent(this, parent)}.
   */
  public void setParent(Object parent)
  {
    setParent(this, parent);
  }

  /**
   * This implements {@link IItemLabelProvider#getImage IItemLabelProvider.getImage} 
   * by returning {@link #image}.
   */
  public Object getImage(Object object)
  {
    return image;
  }

  /**
   * This delegates to {@link #getImage(Object) getImage(this)}.
   */
  public Object getImage()
  {
    return getImage(this);
  }

  /**
   * This allows {@link #image} to be set.
   * If there is a domain notifier, it fires the appropriate domain event.
   */
  public void setImage(Object object, Object image)
  {
    this.image = image;

    fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, image, Notification.NO_INDEX, false, false, true));
  }

  /**
   * This delegates to {@link #setImage(Object, Object) setImage(this, image)}.
   */
  public void setImage(Object image)
  {
    setImage(this, image);
  }

  /**
   * This implements {@link IItemLabelProvider#getText IItemLabelProvider.getText} by returning {@link #text}.
   */
  public String getText(Object object)
  {
    return text;
  }

  /**
   * This delegates to {@link #getText(Object) getText(this)}.
   */
  public String getText()
  {
    return getText(this);
  }

  /**
   * This implements {@link IUpdateableItemText#getUpdateableText IUpdateableItemText.getUpdateableText},
   * although the class doesn't declare that it implements this interface.
   */
  public String getUpdateableText(Object object)
  {
    return getText(object);
  }

  /**
   * This implements {@link IUpdateableItemText#setText IUpdateableItemText.setText},
   * although the class doesn't declare that it implements this interface.
   * If there is a domain notifier, it fires the appropriate domain event.
   */
  public void setText(Object object, String text)
  {
    this.text = text;

    fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, text, Notification.NO_INDEX, false, false, true));
  }

  /**
   * This delegates to {@link #setText(Object, String) setText(this, text)}.
   */
  public void setText(String text)
  {
    setText(this, text);
  }

  /**
   * This implements {@link IItemFontProvider#getFont IItemFontProvider.getFont} 
   * by returning {@link #font}.
   */
  public Object getFont(Object object)
  {
    return font;
  }

  /**
   * This delegates to {@link #getFont(Object) getFont(this)}.
   */
  public Object getFont()
  {
    return getFont(this);
  }

  /**
   * This allows {@link #font} to be set.
   * If there is a domain notifier, it fires the appropriate domain event.
   */
  public void setFont(Object object, Object font)
  {
    this.font = font;

    fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, font, Notification.NO_INDEX, false, false, true));
  }

  /**
   * This delegates to {@link #setFont(Object, Object) setFont(this, font)}.
   */
  public void setFont(Object font)
  {
    setFont(this, font);
  }

  /**
   * This implements {@link IItemColorProvider#getForeground IItemColorProvider.getForeground} 
   * by returning {@link #foreground}.
   */
  public Object getForeground(Object object)
  {
    return foreground;
  }

  /**
   * This delegates to {@link #getForeground(Object) getForeground(this)}.
   */
  public Object getForeground()
  {
    return getForeground(this);
  }

  /**
   * This allows {@link #foreground} to be set.
   * If there is a domain notifier, it fires the appropriate domain event.
   */
  public void setForeground(Object object, Object foreground)
  {
    this.foreground = foreground;

    fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, foreground, Notification.NO_INDEX, false, false, true));
  }

  /**
   * This delegates to {@link #setForeground(Object, Object) setForeground(this, foreground)}.
   */
  public void setForeground(Object foreground)
  {
    setForeground(this, foreground);
  }

  /**
   * This implements {@link IItemColorProvider#getBackground IItemColorProvider.getBackground} 
   * by returning {@link #background}.
   */
  public Object getBackground(Object object)
  {
    return background;
  }

  /**
   * This delegates to {@link #getBackground(Object) getBackground(this)}.
   */
  public Object getBackground()
  {
    return getBackground(this);
  }

  /**
   * This allows {@link #background} to be set.
   * If there is a domain notifier, it fires the appropriate domain event.
   */
  public void setBackground(Object object, Object background)
  {
    this.background = background;

    fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, background, Notification.NO_INDEX, false, false, true));
  }

  /**
   * This delegates to {@link #setBackground(Object, Object) setBackground(this, background)}.
   */
  public void setBackground(Object background)
  {
    setBackground(this, background);
  }

  /**
   * This returns the super result with the {@link #text} appended to it.
   */
  @Override
  public String toString()
  {
    return super.toString() + "[text=\"" + text + "\"]";
  }

  public void dispose()
  {
    // Ignore
  }

  /**
   * This implements {@link IEditingDomainItemProvider#getNewChildDescriptors
   * IEditingDomainItemProvider.getNewChildDescriptors}, returning an empty
   * list.
   */
  public Collection<CommandParameter> getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling)
  {
    return Collections.emptyList();
  }

  /**
   * This implements {@link IEditingDomainItemProvider#createCommand
   * IEditingDomainItemProvider.createCommand()}, returning the unexecutable
   * command.
   */
  public Command createCommand(Object object, EditingDomain editingDomain, Class<? extends Command> commandClass, CommandParameter commandParameter)
  {
    return UnexecutableCommand.INSTANCE;
  }

  /**
   * This implements {@link IItemStyledLabelProvider#getStyledText(Object) IItemStyledLabelProvider.getStyledText} 
   * by returning {@link #styledText}.
   * @since 2.10
   */
  public Object getStyledText(Object object) 
  {
    if (styledText == null) 
    {
      return new StyledString(getText(object));
    }
    return styledText;
  }

  /**
   * This delegates to {@link #getStyledText(Object) getStyledText(this)}.
   * @since 2.10
   */
  public Object getStyledText() 
  {
    return getStyledText(this);
  }

  /**
   * This allows {@link #styledText} to be set.
   * If there is a domain notifier, it fires the appropriate domain event.
   * @since 2.10
   */
  public void setStyledText(Object object, Object styledText)
  {
    this.styledText = styledText;
    
    fireNotifyChanged(new ItemProviderNotification(Notification.SET, null, styledText, Notification.NO_INDEX, false, false, true));
  }

  /**
   * This delegates to {@link #setStyledText(Object, Object) setStyledText(this, styledText)}.
   * @since 2.10
   */
  public void setStyledText(Object styledText)
  {
	  setStyledText(this, styledText);
  }
}

Back to the top