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