Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented ReadOnlyView property for items (#969).

File size: 4.8 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    public NamedItemCollection() : base() { }
45    public NamedItemCollection(int capacity) : base(capacity) { }
46    public NamedItemCollection(IEnumerable<T> collection) : base(collection) {
47      Initialize();
48    }
49
50    [StorableHook(HookType.AfterDeserialization)]
51    protected void Initialize() {
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());
60      cloner.RegisterClonedObject(this, clone);
61      clone.ReadOnlyView = ReadOnlyView;
62      foreach (string key in dict.Keys)
63        clone.dict.Add(key, (T)cloner.Clone(dict[key]));
64      clone.Initialize();
65      return clone;
66    }
67
68    public override string ToString() {
69      return ItemName;
70    }
71
72    public event EventHandler ItemImageChanged;
73    protected virtual void OnItemImageChanged() {
74      EventHandler handler = ItemImageChanged;
75      if (handler != null) handler(this, EventArgs.Empty);
76    }
77    public event EventHandler ToStringChanged;
78    protected virtual void OnToStringChanged() {
79      EventHandler handler = ToStringChanged;
80      if (handler != null) handler(this, EventArgs.Empty);
81    }
82
83    protected override string GetKeyForItem(T item) {
84      return item.Name;
85    }
86
87    protected override void OnItemsAdded(IEnumerable<T> items) {
88      RegisterItemEvents(items);
89      base.OnItemsAdded(items);
90    }
91    protected override void OnItemsRemoved(IEnumerable<T> items) {
92      DeregisterItemEvents(items);
93      base.OnItemsRemoved(items);
94    }
95    #region NOTE
96    // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
97    // by ObservableKeyedCollection when the key of an item has changed. The items stays
98    // in the collection and therefore the NameChanging, NameChanged and Changed event handler
99    // do not have to be removed and added again.
100    #endregion
101    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
102      DeregisterItemEvents(oldItems);
103      RegisterItemEvents(items);
104      base.OnCollectionReset(items, oldItems);
105    }
106
107    private void RegisterItemEvents(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    private void DeregisterItemEvents(IEnumerable<T> items) {
116      foreach (T item in items) {
117        if (item != null) {
118          item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
119          item.NameChanged -= new EventHandler(Item_NameChanged);
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  }
132}
Note: See TracBrowser for help on using the repository browser.