1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Collections.ObjectModel;
|
---|
26 | using System.Text;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Core {
|
---|
32 | public class NamedItemCollection<T> : ObservableKeyedCollection<string, T>, IDeepCloneable where T : class, INamedItem {
|
---|
33 | [Storable(Name = "RestoreEvents")]
|
---|
34 | private object RestoreEvents {
|
---|
35 | get { return null; }
|
---|
36 | set {
|
---|
37 | foreach (T item in this) {
|
---|
38 | item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
39 | item.NameChanged += new EventHandler(Item_NameChanged);
|
---|
40 | item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public NamedItemCollection() : base() { }
|
---|
46 | public NamedItemCollection(int capacity) : base(capacity) { }
|
---|
47 | public NamedItemCollection(IEnumerable<T> collection) : base(collection) { }
|
---|
48 |
|
---|
49 | public object Clone() {
|
---|
50 | return Clone(new Cloner());
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | List<T> items = new List<T>();
|
---|
55 | foreach (T item in this)
|
---|
56 | items.Add((T)cloner.Clone(item));
|
---|
57 | NamedItemCollection<T> clone = (NamedItemCollection<T>)Activator.CreateInstance(this.GetType(), items);
|
---|
58 | cloner.RegisterClonedObject(this, clone);
|
---|
59 | return clone;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public event ChangedEventHandler Changed;
|
---|
63 | protected void OnChanged() {
|
---|
64 | OnChanged(new ChangedEventArgs());
|
---|
65 | }
|
---|
66 | protected virtual void OnChanged(ChangedEventArgs e) {
|
---|
67 | if ((e.RegisterChangedObject(this)) && (Changed != null))
|
---|
68 | Changed(this, e);
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected override string GetKeyForItem(T item) {
|
---|
72 | return item.Name;
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected override void OnItemsAdded(IEnumerable<T> items) {
|
---|
76 | foreach (T item in items) {
|
---|
77 | item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
78 | item.NameChanged += new EventHandler(Item_NameChanged);
|
---|
79 | item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
80 | }
|
---|
81 | base.OnItemsAdded(items);
|
---|
82 | }
|
---|
83 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
84 | foreach (T item in items) {
|
---|
85 | item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
86 | item.NameChanged -= new EventHandler(Item_NameChanged);
|
---|
87 | item.Changed -= new ChangedEventHandler(Item_Changed);
|
---|
88 | }
|
---|
89 | base.OnItemsRemoved(items);
|
---|
90 | }
|
---|
91 | #region NOTE
|
---|
92 | // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
|
---|
93 | // by ObservableKeyedCollection when the key of an item has changed. The items stays
|
---|
94 | // in the collection and therefore the NameChanging, NameChanged and Changed event handler
|
---|
95 | // do not have to be removed and added again.
|
---|
96 | #endregion
|
---|
97 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
98 | foreach (T oldItem in oldItems) {
|
---|
99 | oldItem.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
100 | oldItem.NameChanged -= new EventHandler(Item_NameChanged);
|
---|
101 | oldItem.Changed -= new ChangedEventHandler(Item_Changed);
|
---|
102 | }
|
---|
103 | foreach (T item in items) {
|
---|
104 | item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
105 | item.NameChanged += new EventHandler(Item_NameChanged);
|
---|
106 | item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
107 | }
|
---|
108 | base.OnCollectionReset(items, oldItems);
|
---|
109 | }
|
---|
110 | protected override void OnPropertyChanged(string propertyName) {
|
---|
111 | base.OnPropertyChanged(propertyName);
|
---|
112 | OnChanged();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
116 | e.Cancel = e.Cancel || this.ContainsKey(e.Value);
|
---|
117 | }
|
---|
118 | private void Item_NameChanged(object sender, EventArgs e) {
|
---|
119 | T item = (T)sender;
|
---|
120 | UpdateItemKey(item);
|
---|
121 | }
|
---|
122 | private void Item_Changed(object sender, ChangedEventArgs e) {
|
---|
123 | OnChanged(e);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|