Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core/3.3/Collections/NamedItemCollection.cs @ 17399

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

#2875: Removed years in copyrights

File size: 3.8 KB
RevLine 
[2653]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2653]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;
[16565]25using HEAL.Attic;
[2653]26
27namespace HeuristicLab.Core {
[3822]28  [Item("NamedItemCollection", "Represents a collection of named items.")]
[16565]29  [StorableType("A7DC6650-2486-472C-8515-0D66C93C03F6")]
[3390]30  public class NamedItemCollection<T> : KeyedItemCollection<string, T> where T : class, INamedItem {
[4722]31    [StorableConstructor]
[16565]32    protected NamedItemCollection(StorableConstructorFlag _) : base(_) { }
[4722]33    protected NamedItemCollection(NamedItemCollection<T> original, Cloner cloner)
34      : base(original, cloner) {
35      RegisterItemEvents(this);
36    }
[2653]37    public NamedItemCollection() : base() { }
38    public NamedItemCollection(int capacity) : base(capacity) { }
[4068]39    public NamedItemCollection(IEnumerable<T> collection)
40      : base(collection) {
[4722]41      RegisterItemEvents(this);
[3287]42    }
43
44    [StorableHook(HookType.AfterDeserialization)]
[4722]45    private void AfterDeserialization() {
[2830]46      RegisterItemEvents(this);
47    }
[2653]48
[3390]49    public override IDeepCloneable Clone(Cloner cloner) {
[4722]50      return new NamedItemCollection<T>(this, cloner);
[2653]51    }
52
53    protected override string GetKeyForItem(T item) {
54      return item.Name;
55    }
56
57    protected override void OnItemsAdded(IEnumerable<T> items) {
[2830]58      RegisterItemEvents(items);
[2653]59      base.OnItemsAdded(items);
60    }
61    protected override void OnItemsRemoved(IEnumerable<T> items) {
[2830]62      DeregisterItemEvents(items);
[2653]63      base.OnItemsRemoved(items);
64    }
65    #region NOTE
66    // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
[2676]67    // by ObservableKeyedCollection when the key of an item has changed. The items stays
[2653]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) {
[2830]72      DeregisterItemEvents(oldItems);
73      RegisterItemEvents(items);
74      base.OnCollectionReset(items, oldItems);
75    }
76
[11330]77    protected virtual void RegisterItemEvents(IEnumerable<T> items) {
[2653]78      foreach (T item in items) {
[2833]79        if (item != null) {
80          item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
81          item.NameChanged += new EventHandler(Item_NameChanged);
82        }
[2653]83      }
84    }
[11330]85    protected virtual void DeregisterItemEvents(IEnumerable<T> items) {
[2830]86      foreach (T item in items) {
[2833]87        if (item != null) {
88          item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
89          item.NameChanged -= new EventHandler(Item_NameChanged);
90        }
[2830]91      }
[2653]92    }
93
94    private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
[2669]95      e.Cancel = e.Cancel || this.ContainsKey(e.Value);
[2653]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.