Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedList.cs @ 17932

Last change on this file since 17932 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.1 KB
RevLine 
[2618]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2618]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
[16565]24using HEAL.Attic;
[2618]25
26namespace HeuristicLab.Collections {
[16565]27  [StorableType("58D6326E-BD52-4B5D-8915-89948CEB50AA")]
[2618]28  [Serializable]
[8610]29  public class ReadOnlyObservableKeyedList<TKey, TItem> : ReadOnlyObservableList<TItem>, IObservableKeyedList<TKey, TItem> {
[2618]30
[8610]31    protected IObservableKeyedList<TKey, TItem> KeyedList {
32      get { return (IObservableKeyedList<TKey, TItem>)base.list; }
[2618]33    }
[8610]34
35    bool ICollection<TItem>.IsReadOnly {
[2618]36      get { return true; }
37    }
[8610]38    public TItem this[TKey key] {
39      get { return KeyedList[key]; }
[2618]40    }
41
42    #region Constructors
[8610]43    protected ReadOnlyObservableKeyedList() { }
44    public ReadOnlyObservableKeyedList(IObservableKeyedList<TKey, TItem> keyedList)
45      : base(keyedList) {
[3002]46      RegisterEvents();
47    }
[3560]48    [StorableConstructor]
[16565]49    protected ReadOnlyObservableKeyedList(StorableConstructorFlag _) : base(_) { }
[4722]50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      RegisterEvents();
53    }
[2618]54    #endregion
55
[8610]56    public bool ContainsKey(TKey key) {
57      return KeyedList.ContainsKey(key);
[2618]58    }
[8610]59    public bool TryGetValue(TKey key, out TItem item) {
60      return KeyedList.TryGetValue(key, out item);
[2618]61    }
62
[8610]63    bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
[2618]64      throw new NotSupportedException();
65    }
66
[8610]67    #region events
68    private void RegisterEvents() {
69      ((INotifyObservableKeyedCollectionItemsChanged<TKey, TItem>)KeyedList).ItemsReplaced += keyedList_ItemsReplaced;
[2618]70    }
71
72    [field: NonSerialized]
[8610]73    private event CollectionItemsChangedEventHandler<TItem> itemsReplaced;
74    event CollectionItemsChangedEventHandler<TItem> INotifyObservableKeyedCollectionItemsChanged<TKey, TItem>.ItemsReplaced {
75      add { itemsReplaced += value; }
76      remove { itemsReplaced -= value; }
[2618]77    }
[8610]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));
[2623]81    }
82
[8610]83    private void keyedList_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
84      OnItemsReplaced(e.Items, e.OldItems);
[2618]85    }
86
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.