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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace 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 | public NamedItemCollection() : base() { }
|
---|
32 | public NamedItemCollection(int capacity) : base(capacity) { }
|
---|
33 | public NamedItemCollection(IEnumerable<T> collection)
|
---|
34 | : base(collection) {
|
---|
35 | Initialize();
|
---|
36 | }
|
---|
37 | [StorableConstructor]
|
---|
38 | protected NamedItemCollection(bool deserializing) : base(deserializing) { }
|
---|
39 |
|
---|
40 | [StorableHook(HookType.AfterDeserialization)]
|
---|
41 | protected void Initialize() {
|
---|
42 | RegisterItemEvents(this);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
46 | NamedItemCollection<T> clone = (NamedItemCollection<T>)base.Clone(cloner);
|
---|
47 | clone.Initialize();
|
---|
48 | return clone;
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override string GetKeyForItem(T item) {
|
---|
52 | return item.Name;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void OnItemsAdded(IEnumerable<T> items) {
|
---|
56 | RegisterItemEvents(items);
|
---|
57 | base.OnItemsAdded(items);
|
---|
58 | }
|
---|
59 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
60 | DeregisterItemEvents(items);
|
---|
61 | base.OnItemsRemoved(items);
|
---|
62 | }
|
---|
63 | #region NOTE
|
---|
64 | // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
|
---|
65 | // by ObservableKeyedCollection when the key of an item has changed. The items stays
|
---|
66 | // in the collection and therefore the NameChanging, NameChanged and Changed event handler
|
---|
67 | // do not have to be removed and added again.
|
---|
68 | #endregion
|
---|
69 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
70 | DeregisterItemEvents(oldItems);
|
---|
71 | RegisterItemEvents(items);
|
---|
72 | base.OnCollectionReset(items, oldItems);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void RegisterItemEvents(IEnumerable<T> items) {
|
---|
76 | foreach (T item in items) {
|
---|
77 | if (item != null) {
|
---|
78 | item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
79 | item.NameChanged += new EventHandler(Item_NameChanged);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | private void DeregisterItemEvents(IEnumerable<T> items) {
|
---|
84 | foreach (T item in items) {
|
---|
85 | if (item != null) {
|
---|
86 | item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
87 | item.NameChanged -= new EventHandler(Item_NameChanged);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
93 | e.Cancel = e.Cancel || this.ContainsKey(e.Value);
|
---|
94 | }
|
---|
95 | private void Item_NameChanged(object sender, EventArgs e) {
|
---|
96 | T item = (T)sender;
|
---|
97 | UpdateItemKey(item);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|