Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/Collections/NamedItemCollection.cs @ 14581

Last change on this file since 14581 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  [Item("NamedItemCollection", "Represents a collection of named items.")]
29  [StorableClass]
30  public class NamedItemCollection<T> : KeyedItemCollection<string, T> where T : class, INamedItem {
31    [StorableConstructor]
32    protected NamedItemCollection(bool deserializing) : base(deserializing) { }
33    protected NamedItemCollection(NamedItemCollection<T> original, Cloner cloner)
34      : base(original, cloner) {
35      RegisterItemEvents(this);
36    }
37    public NamedItemCollection() : base() { }
38    public NamedItemCollection(int capacity) : base(capacity) { }
39    public NamedItemCollection(IEnumerable<T> collection)
40      : base(collection) {
41      RegisterItemEvents(this);
42    }
43
44    [StorableHook(HookType.AfterDeserialization)]
45    private void AfterDeserialization() {
46      RegisterItemEvents(this);
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new NamedItemCollection<T>(this, cloner);
51    }
52
53    protected override string GetKeyForItem(T item) {
54      return item.Name;
55    }
56
57    protected override void OnItemsAdded(IEnumerable<T> items) {
58      RegisterItemEvents(items);
59      base.OnItemsAdded(items);
60    }
61    protected override void OnItemsRemoved(IEnumerable<T> items) {
62      DeregisterItemEvents(items);
63      base.OnItemsRemoved(items);
64    }
65    #region NOTE
66    // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
67    // by ObservableKeyedCollection when the key of an item has changed. The items stays
68    // in the collection and therefore the NameChanging, NameChanged and Changed event handler
69    // do not have to be removed and added again.
70    #endregion
71    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
72      DeregisterItemEvents(oldItems);
73      RegisterItemEvents(items);
74      base.OnCollectionReset(items, oldItems);
75    }
76
77    protected virtual void RegisterItemEvents(IEnumerable<T> items) {
78      foreach (T item in items) {
79        if (item != null) {
80          item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
81          item.NameChanged += new EventHandler(Item_NameChanged);
82        }
83      }
84    }
85    protected virtual void DeregisterItemEvents(IEnumerable<T> items) {
86      foreach (T item in items) {
87        if (item != null) {
88          item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
89          item.NameChanged -= new EventHandler(Item_NameChanged);
90        }
91      }
92    }
93
94    private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
95      e.Cancel = e.Cancel || this.ContainsKey(e.Value);
96    }
97    private void Item_NameChanged(object sender, EventArgs e) {
98      T item = (T)sender;
99      UpdateItemKey(item);
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.