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