Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3602 was 3560, checked in by swagner, 15 years ago

Fixed persistence exceptions by restoring the reference on HeuristicLab.Persistence in HeuristicLab.Collections (#977)

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Core {
31  [Item("NamedItemCollection<T>", "Represents a collection of named items.")]
32  [StorableClass]
33  public class NamedItemCollection<T> : KeyedItemCollection<string, T> where T : class, INamedItem {
34    public NamedItemCollection() : base() { }
35    public NamedItemCollection(int capacity) : base(capacity) { }
36    public NamedItemCollection(IEnumerable<T> collection) : base(collection) {
37      Initialize();
38    }
39    [StorableConstructor]
40    protected NamedItemCollection(bool deserializing) : base(deserializing) { }
41
42    [StorableHook(HookType.AfterDeserialization)]
43    protected void Initialize() {
44      RegisterItemEvents(this);
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      NamedItemCollection<T> clone = (NamedItemCollection<T>)base.Clone(cloner);
49      clone.Initialize();
50      return clone;
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    private 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    private 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.