[2653] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
[2790] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2653] | 26 |
|
---|
| 27 | namespace HeuristicLab.Core {
|
---|
[3822] | 28 | [Item("NamedItemCollection", "Represents a collection of named items.")]
|
---|
[3017] | 29 | [StorableClass]
|
---|
[3390] | 30 | public class NamedItemCollection<T> : KeyedItemCollection<string, T> where T : class, INamedItem {
|
---|
[4722] | 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 | }
|
---|
[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 |
|
---|
[11416] | 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 | }
|
---|
[11416] | 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 | }
|
---|