Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/NamedItemCollection.cs @ 3286

Last change on this file since 3286 was 3286, checked in by swagner, 13 years ago

Continued work on algorithm batch processing (#947).

File size: 4.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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());
61      cloner.RegisterClonedObject(this, clone);
62      foreach (string key in dict.Keys)
63        clone.dict.Add(key, (T)cloner.Clone(dict[key]));
64      return clone;
65    }
66
67    public override string ToString() {
68      return ItemName;
69    }
70
71    public event EventHandler ToStringChanged;
72    protected virtual void OnToStringChanged() {
73      if (ToStringChanged != null)
74        ToStringChanged(this, EventArgs.Empty);
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
101    private void RegisterItemEvents(IEnumerable<T> items) {
102      foreach (T item in items) {
103        if (item != null) {
104          item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
105          item.NameChanged += new EventHandler(Item_NameChanged);
106        }
107      }
108    }
109    private void DeregisterItemEvents(IEnumerable<T> items) {
110      foreach (T item in items) {
111        if (item != null) {
112          item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
113          item.NameChanged -= new EventHandler(Item_NameChanged);
114        }
115      }
116    }
117
118    private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
119      e.Cancel = e.Cancel || this.ContainsKey(e.Value);
120    }
121    private void Item_NameChanged(object sender, EventArgs e) {
122      T item = (T)sender;
123      UpdateItemKey(item);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.