Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2932 was 2932, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • replaced Changed in IItem by ToStringChanged
File size: 4.4 KB
RevLine 
[2653]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2653]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;
[2790]24using System.Drawing;
[2834]25using System.Linq;
[2653]26using HeuristicLab.Collections;
27using HeuristicLab.Common;
[2790]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2653]29
30namespace HeuristicLab.Core {
[2790]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
[2653]43    [Storable(Name = "RestoreEvents")]
44    private object RestoreEvents {
45      get { return null; }
[2830]46      set { RegisterItemEvents(this); }
[2653]47    }
48
49    public NamedItemCollection() : base() { }
50    public NamedItemCollection(int capacity) : base(capacity) { }
[2830]51    public NamedItemCollection(IEnumerable<T> collection) : base(collection) {
52      RegisterItemEvents(this);
53    }
[2653]54
55    public object Clone() {
56      return Clone(new Cloner());
57    }
[2830]58    public virtual IDeepCloneable Clone(Cloner cloner) {
59      NamedItemCollection<T> clone = (NamedItemCollection<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
[2653]60      cloner.RegisterClonedObject(this, clone);
61      return clone;
62    }
63
[2790]64    public override string ToString() {
65      return ItemName;
66    }
67
[2932]68    public event EventHandler ToStringChanged;
69    protected virtual void OnToStringChanged() {
70      if (ToStringChanged != null)
71        ToStringChanged(this, EventArgs.Empty);
[2653]72    }
73
74    protected override string GetKeyForItem(T item) {
75      return item.Name;
76    }
77
78    protected override void OnItemsAdded(IEnumerable<T> items) {
[2830]79      RegisterItemEvents(items);
[2653]80      base.OnItemsAdded(items);
81    }
82    protected override void OnItemsRemoved(IEnumerable<T> items) {
[2830]83      DeregisterItemEvents(items);
[2653]84      base.OnItemsRemoved(items);
85    }
86    #region NOTE
87    // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
[2676]88    // by ObservableKeyedCollection when the key of an item has changed. The items stays
[2653]89    // in the collection and therefore the NameChanging, NameChanged and Changed event handler
90    // do not have to be removed and added again.
91    #endregion
92    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
[2830]93      DeregisterItemEvents(oldItems);
94      RegisterItemEvents(items);
95      base.OnCollectionReset(items, oldItems);
96    }
97
98    private void RegisterItemEvents(IEnumerable<T> items) {
[2653]99      foreach (T item in items) {
[2833]100        if (item != null) {
101          item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
102          item.NameChanged += new EventHandler(Item_NameChanged);
103        }
[2653]104      }
105    }
[2830]106    private void DeregisterItemEvents(IEnumerable<T> items) {
107      foreach (T item in items) {
[2833]108        if (item != null) {
109          item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
110          item.NameChanged -= new EventHandler(Item_NameChanged);
111        }
[2830]112      }
[2653]113    }
114
115    private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
[2669]116      e.Cancel = e.Cancel || this.ContainsKey(e.Value);
[2653]117    }
118    private void Item_NameChanged(object sender, EventArgs e) {
119      T item = (T)sender;
120      UpdateItemKey(item);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.