Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/10/12 13:17:25 (12 years ago)
Author:
mkommend
Message:

#1946:

  • Added 'ObservableKeyedList` and related classes
  • Reordered firing events in ObservableList
  • Adapted cloning ctor of ReadOnlyItemList
Location:
trunk/sources/HeuristicLab.Collections/3.3
Files:
2 added
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Collections/3.3/HeuristicLab.Collections-3.3.csproj

    r8600 r8610  
    114114  <ItemGroup>
    115115    <None Include="Plugin.cs.frame" />
     116    <Compile Include="ReadOnlyObservableKeyedList.cs" />
    116117    <Compile Include="INotifyObservableListItemsChanged.cs" />
    117118    <Compile Include="INotifyObservableKeyedCollectionItemsChanged.cs" />
     
    119120    <Compile Include="INotifyObservableCollectionItemsChanged.cs" />
    120121    <Compile Include="INotifyObservableArrayItemsChanged.cs" />
     122    <Compile Include="IObservableKeyedList.cs" />
     123    <Compile Include="ObservableKeyedList.cs" />
    121124    <Compile Include="Plugin.cs" />
    122125    <Compile Include="ReadOnlyObservableArray.cs" />
     
    189192  -->
    190193  <PropertyGroup>
    191    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     194    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    192195set ProjectDir=$(ProjectDir)
    193196set SolutionDir=$(SolutionDir)
     
    196199call PreBuildEvent.cmd
    197200</PreBuildEvent>
    198 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     201    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
    199202export ProjectDir=$(ProjectDir)
    200203export SolutionDir=$(SolutionDir)
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableList.cs

    r7259 r8610  
    158158      int capacity = list.Capacity;
    159159      list.Add(item);
     160      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(list.Count - 1, item) });
     161      OnItemsAdded(new T[] { item });
    160162      if (list.Capacity != capacity)
    161163        OnPropertyChanged("Capacity");
    162164      OnPropertyChanged("Item[]");
    163165      OnPropertyChanged("Count");
    164       OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(list.Count - 1, item) });
    165       OnItemsAdded(new T[] { item });
    166166    }
    167167    public void AddRange(IEnumerable<T> collection) {
     
    175175      }
    176176      if (items.Count > 0) {
     177        OnItemsAdded(items);
     178        OnItemsAdded(collection);
    177179        if (list.Capacity != capacity)
    178180          OnPropertyChanged("Capacity");
    179181        OnPropertyChanged("Item[]");
    180182        OnPropertyChanged("Count");
    181         OnItemsAdded(items);
    182         OnItemsAdded(collection);
    183183      }
    184184    }
     
    187187      int capacity = list.Capacity;
    188188      list.Insert(index, item);
     189      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
     190      OnItemsAdded(new T[] { item });
    189191      if (list.Capacity != capacity)
    190192        OnPropertyChanged("Capacity");
    191193      OnPropertyChanged("Item[]");
    192194      OnPropertyChanged("Count");
    193       OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
    194       OnItemsAdded(new T[] { item });
    195195    }
    196196    public void InsertRange(int index, IEnumerable<T> collection) {
     
    203203      }
    204204      if (items.Count > 0) {
     205        OnItemsAdded(items);
     206        OnItemsAdded(collection);
    205207        if (list.Capacity != capacity)
    206208          OnPropertyChanged("Capacity");
    207209        OnPropertyChanged("Item[]");
    208210        OnPropertyChanged("Count");
    209         OnItemsAdded(items);
    210         OnItemsAdded(collection);
    211211      }
    212212    }
     
    229229      else items = new List<IndexedItem<T>>();
    230230
     231      OnItemsReplaced(items, oldItems);
    231232      if (oldCapacity != list.Capacity) OnPropertyChanged("Capacity");
    232233      OnPropertyChanged("Item[]");
    233234      if (oldItems.Count != items.Count) OnPropertyChanged("Count");
    234       OnItemsReplaced(items, oldItems);
    235235    }
    236236
     
    239239      if (index != -1) {
    240240        list.RemoveAt(index);
    241         OnPropertyChanged("Item[]");
    242         OnPropertyChanged("Count");
    243241        OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
    244242        OnItemsRemoved(new T[] { item });
     243        OnPropertyChanged("Item[]");
     244        OnPropertyChanged("Count");
    245245        return true;
    246246      }
     
    260260      if (indexedItems.Count > 0) {
    261261        result = list.RemoveAll(match);
    262         OnPropertyChanged("Item[]");
    263         OnPropertyChanged("Count");
    264262        OnItemsRemoved(indexedItems);
    265263        OnItemsRemoved(items);
     264        OnPropertyChanged("Item[]");
     265        OnPropertyChanged("Count");
    266266      }
    267267      return result;
     
    270270      T item = list[index];
    271271      list.RemoveAt(index);
     272      OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
     273      OnItemsRemoved(new T[] { item });
    272274      OnPropertyChanged("Item[]");
    273275      OnPropertyChanged("Count");
    274       OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
    275       OnItemsRemoved(new T[] { item });
    276276    }
    277277    public void RemoveRange(int index, int count) {
     
    281281        list.CopyTo(index, items, 0, count);
    282282        list.RemoveRange(index, count);
    283         OnPropertyChanged("Item[]");
    284         OnPropertyChanged("Count");
    285283        OnItemsRemoved(indexedItems);
    286284        OnItemsRemoved(items);
     285        OnPropertyChanged("Item[]");
     286        OnPropertyChanged("Count");
    287287      }
    288288    }
     
    293293        T[] items = list.ToArray();
    294294        list.Clear();
    295         OnPropertyChanged("Item[]");
    296         OnPropertyChanged("Count");
    297295        OnCollectionReset(new IndexedItem<T>[0], indexedItems);
    298296        OnCollectionReset(new T[0], items);
     297        OnPropertyChanged("Item[]");
     298        OnPropertyChanged("Count");
    299299      }
    300300    }
     
    304304        IndexedItem<T>[] oldItems = GetIndexedItems();
    305305        list.Reverse();
    306         OnPropertyChanged("Item[]");
    307306        OnItemsMoved(GetIndexedItems(), oldItems);
     307        OnPropertyChanged("Item[]");
    308308      }
    309309    }
     
    312312        IndexedItem<T>[] oldItems = GetIndexedItems(index, count);
    313313        list.Reverse(index, count);
    314         OnPropertyChanged("Item[]");
    315314        OnItemsMoved(GetIndexedItems(index, count), oldItems);
     315        OnPropertyChanged("Item[]");
    316316      }
    317317    }
     
    321321        IndexedItem<T>[] oldItems = GetIndexedItems();
    322322        list.Sort();
    323         OnPropertyChanged("Item[]");
    324323        OnItemsMoved(GetIndexedItems(), oldItems);
     324        OnPropertyChanged("Item[]");
    325325      }
    326326    }
     
    329329        IndexedItem<T>[] oldItems = GetIndexedItems();
    330330        list.Sort(comparison);
    331         OnPropertyChanged("Item[]");
    332331        OnItemsMoved(GetIndexedItems(), oldItems);
     332        OnPropertyChanged("Item[]");
    333333      }
    334334    }
     
    337337        IndexedItem<T>[] oldItems = GetIndexedItems();
    338338        list.Sort(comparer);
    339         OnPropertyChanged("Item[]");
    340339        OnItemsMoved(GetIndexedItems(), oldItems);
     340        OnPropertyChanged("Item[]");
    341341      }
    342342    }
     
    345345        IndexedItem<T>[] oldItems = GetIndexedItems(index, count);
    346346        list.Sort(index, count, comparer);
    347         OnPropertyChanged("Item[]");
    348347        OnItemsMoved(GetIndexedItems(index, count), oldItems);
     348        OnPropertyChanged("Item[]");
    349349      }
    350350    }
     
    355355      return new ReadOnlyObservableList<T>(this);
    356356    }
     357
    357358    public T[] ToArray() {
    358359      return list.ToArray();
  • trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedList.cs

    r8606 r8610  
    2121
    2222using System;
    23 using System.Collections;
    2423using System.Collections.Generic;
    25 using System.ComponentModel;
    2624using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2725
     
    2927  [StorableClass]
    3028  [Serializable]
    31   public class ReadOnlyObservableList<T> : IObservableList<T> {
    32     [Storable]
    33     protected IObservableList<T> list;
     29  public class ReadOnlyObservableKeyedList<TKey, TItem> : ReadOnlyObservableList<TItem>, IObservableKeyedList<TKey, TItem> {
    3430
    35     #region Properties
    36     public int Count {
    37       get { return ((ICollection<T>)list).Count; }
     31    protected IObservableKeyedList<TKey, TItem> KeyedList {
     32      get { return (IObservableKeyedList<TKey, TItem>)base.list; }
    3833    }
    39     bool ICollection<T>.IsReadOnly {
     34
     35    bool ICollection<TItem>.IsReadOnly {
    4036      get { return true; }
    4137    }
    42 
    43     public T this[int index] {
    44       get { return list[index]; }
     38    public TItem this[TKey key] {
     39      get { return KeyedList[key]; }
    4540    }
    46     T IList<T>.this[int index] {
    47       get { return list[index]; }
    48       set { throw new NotSupportedException(); }
    49     }
    50     #endregion
    5141
    5242    #region Constructors
    53     protected ReadOnlyObservableList() { }
    54     public ReadOnlyObservableList(IObservableList<T> list) {
    55       if (list == null) throw new ArgumentNullException();
    56       this.list = list;
     43    protected ReadOnlyObservableKeyedList() { }
     44    public ReadOnlyObservableKeyedList(IObservableKeyedList<TKey, TItem> keyedList)
     45      : base(keyedList) {
    5746      RegisterEvents();
    5847    }
    5948    [StorableConstructor]
    60     protected ReadOnlyObservableList(bool deserializing) { }
    61 
     49    protected ReadOnlyObservableKeyedList(bool deserializing) { }
    6250    [StorableHook(HookType.AfterDeserialization)]
    6351    private void AfterDeserialization() {
     
    6654    #endregion
    6755
    68     #region Access
    69     public bool Contains(T item) {
    70       return list.Contains(item);
     56    public bool ContainsKey(TKey key) {
     57      return KeyedList.ContainsKey(key);
     58    }
     59    public bool TryGetValue(TKey key, out TItem item) {
     60      return KeyedList.TryGetValue(key, out item);
    7161    }
    7262
    73     public int IndexOf(T item) {
    74       return list.IndexOf(item);
    75     }
    76     #endregion
    77 
    78     #region Manipulation
    79     void ICollection<T>.Add(T item) {
     63    bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
    8064      throw new NotSupportedException();
    8165    }
    8266
    83     void IList<T>.Insert(int index, T item) {
    84       throw new NotSupportedException();
    85     }
    86 
    87     bool ICollection<T>.Remove(T item) {
    88       throw new NotSupportedException();
    89     }
    90     void IList<T>.RemoveAt(int index) {
    91       throw new NotSupportedException();
    92     }
    93 
    94     void ICollection<T>.Clear() {
    95       throw new NotSupportedException();
    96     }
    97 
    98     void IObservableList<T>.Reverse() {
    99       throw new NotSupportedException();
    100     }
    101     void IObservableList<T>.Reverse(int index, int count) {
    102       throw new NotSupportedException();
    103     }
    104     #endregion
    105 
    106     #region Conversion
    107     public void CopyTo(T[] array, int arrayIndex) {
    108       list.CopyTo(array, arrayIndex);
    109     }
    110     #endregion
    111 
    112     #region Enumeration
    113     public IEnumerator<T> GetEnumerator() {
    114       return ((ICollection<T>)list).GetEnumerator();
    115     }
    116     IEnumerator IEnumerable.GetEnumerator() {
    117       return ((IEnumerable)list).GetEnumerator();
    118     }
    119     #endregion
    120 
    121     #region Events
    122     protected void RegisterEvents() {
    123       list.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsAdded);
    124       ((IObservableCollection<T>)list).ItemsAdded += new CollectionItemsChangedEventHandler<T>(list_ItemsAdded);
    125       list.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsRemoved);
    126       ((IObservableCollection<T>)list).ItemsRemoved += new CollectionItemsChangedEventHandler<T>(list_ItemsRemoved);
    127       list.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsReplaced);
    128       list.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsMoved);
    129       list.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_CollectionReset);
    130       ((IObservableCollection<T>)list).CollectionReset += new CollectionItemsChangedEventHandler<T>(list_CollectionReset);
    131       list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
     67    #region events
     68    private void RegisterEvents() {
     69      ((INotifyObservableKeyedCollectionItemsChanged<TKey, TItem>)KeyedList).ItemsReplaced += keyedList_ItemsReplaced;
    13270    }
    13371
    13472    [field: NonSerialized]
    135     public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
    136     protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
    137       CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsAdded;
    138       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
     73    private event CollectionItemsChangedEventHandler<TItem> itemsReplaced;
     74    event CollectionItemsChangedEventHandler<TItem> INotifyObservableKeyedCollectionItemsChanged<TKey, TItem>.ItemsReplaced {
     75      add { itemsReplaced += value; }
     76      remove { itemsReplaced -= value; }
     77    }
     78    private void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
     79      var handler = itemsReplaced;
     80      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
    13981    }
    14082
    141     [field: NonSerialized]
    142     private event CollectionItemsChangedEventHandler<T> itemsAdded;
    143     event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsAdded {
    144       add { itemsAdded += value; }
    145       remove { itemsAdded -= value; }
    146     }
    147     private void OnItemsAdded(IEnumerable<T> items) {
    148       CollectionItemsChangedEventHandler<T> handler = itemsAdded;
    149       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
     83    private void keyedList_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
     84      OnItemsReplaced(e.Items, e.OldItems);
    15085    }
    15186
    152     [field: NonSerialized]
    153     public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
    154     protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
    155       CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsRemoved;
    156       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
    157     }
    158 
    159     [field: NonSerialized]
    160     private event CollectionItemsChangedEventHandler<T> itemsRemoved;
    161     event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsRemoved {
    162       add { itemsRemoved += value; }
    163       remove { itemsRemoved -= value; }
    164     }
    165     private void OnItemsRemoved(IEnumerable<T> items) {
    166       CollectionItemsChangedEventHandler<T> handler = itemsRemoved;
    167       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
    168     }
    169 
    170     [field: NonSerialized]
    171     public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
    172     protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
    173       CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
    174       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
    175     }
    176 
    177     [field: NonSerialized]
    178     public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
    179     protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
    180       CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
    181       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
    182     }
    183 
    184     [field: NonSerialized]
    185     public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
    186     protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
    187       CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
    188       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
    189     }
    190 
    191     [field: NonSerialized]
    192     private event CollectionItemsChangedEventHandler<T> collectionReset;
    193     event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.CollectionReset {
    194       add { collectionReset += value; }
    195       remove { collectionReset -= value; }
    196     }
    197     private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
    198       CollectionItemsChangedEventHandler<T> handler = collectionReset;
    199       if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
    200     }
    201 
    202     [field: NonSerialized]
    203     public event PropertyChangedEventHandler PropertyChanged;
    204     protected virtual void OnPropertyChanged(string propertyName) {
    205       PropertyChangedEventHandler handler = PropertyChanged;
    206       if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    207     }
    208 
    209     private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    210       OnItemsAdded(e.Items);
    211     }
    212     private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
    213       OnItemsAdded(e.Items);
    214     }
    215     private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    216       OnItemsRemoved(e.Items);
    217     }
    218     private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
    219       OnItemsRemoved(e.Items);
    220     }
    221     private void list_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    222       OnItemsReplaced(e.Items, e.OldItems);
    223     }
    224     private void list_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    225       OnItemsMoved(e.Items, e.OldItems);
    226     }
    227     private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    228       OnCollectionReset(e.Items, e.OldItems);
    229     }
    230     private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
    231       OnCollectionReset(e.Items, e.OldItems);
    232     }
    233     private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
    234       if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
    235         OnPropertyChanged(e.PropertyName);
    236     }
    23787    #endregion
    23888  }
  • trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableList.cs

    r7259 r8610  
    120120
    121121    #region Events
    122     protected void RegisterEvents() {
     122    private void RegisterEvents() {
    123123      list.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsAdded);
    124124      ((IObservableCollection<T>)list).ItemsAdded += new CollectionItemsChangedEventHandler<T>(list_ItemsAdded);
Note: See TracChangeset for help on using the changeset viewer.