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