Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Collections/ItemDictionary.cs @ 3390

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

Refactored HeuristicLab.Collections (#977)

File size: 4.1 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.Common.Resources;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Core {
32  [StorableClass]
33  [Item("ItemDictionary<TKey, TValue>", "Represents a dictionary of items.")]
34  public class ItemDictionary<TKey, TValue> : ObservableDictionary<TKey, TValue>, IItemDictionary<TKey, TValue> where TKey : class, IItem where TValue : class, IItem {
35    public virtual string ItemName {
36      get { return ItemAttribute.GetName(this.GetType()); }
37    }
38    public virtual string ItemDescription {
39      get { return ItemAttribute.GetDescription(this.GetType()); }
40    }
41    public virtual Image ItemImage {
42      get { return VS2008ImageLibrary.Class; }
43    }
44
45    [Storable]
46    private Dictionary<TKey, TValue> Items {
47      get { return dict; }
48      set { dict = value; }
49    }
50
51    [Storable]
52    private bool readOnlyView;
53    public virtual bool ReadOnlyView {
54      get { return readOnlyView; }
55      set {
56        if ((readOnlyView != value) && !((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly) {
57          readOnlyView = value;
58          OnReadOnlyViewChanged();
59          OnPropertyChanged("ReadOnlyView");
60        }
61      }
62    }
63
64    public ItemDictionary()
65      : base() {
66      readOnlyView = ((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly;
67    }
68    public ItemDictionary(int capacity)
69      : base(capacity) {
70      readOnlyView = ((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly;
71    }
72    public ItemDictionary(IDictionary<TKey, TValue> dictionary)
73      : base(dictionary) {
74      readOnlyView = ((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly;
75    }
76    [StorableConstructor]
77    protected ItemDictionary(bool deserializing) { }
78
79    public object Clone() {
80      return Clone(new Cloner());
81    }
82    public virtual IDeepCloneable Clone(Cloner cloner) {
83      ItemDictionary<TKey, TValue> clone = (ItemDictionary<TKey, TValue>)Activator.CreateInstance(this.GetType());
84      cloner.RegisterClonedObject(this, clone);
85      clone.readOnlyView = readOnlyView;
86      foreach (TKey key in dict.Keys)
87        clone.dict.Add((TKey)cloner.Clone(key), (TValue)cloner.Clone(dict[key]));
88      return clone;
89    }
90
91    public new ReadOnlyItemDictionary<TKey, TValue> AsReadOnly() {
92      return new ReadOnlyItemDictionary<TKey, TValue>(this);
93    }
94
95    public override string ToString() {
96      return ItemName;
97    }
98
99    public event EventHandler ItemImageChanged;
100    protected virtual void OnItemImageChanged() {
101      EventHandler handler = ItemImageChanged;
102      if (handler != null) handler(this, EventArgs.Empty);
103    }
104    public event EventHandler ToStringChanged;
105    protected virtual void OnToStringChanged() {
106      EventHandler handler = ToStringChanged;
107      if (handler != null) handler(this, EventArgs.Empty);
108    }
109    public event EventHandler ReadOnlyViewChanged;
110    protected virtual void OnReadOnlyViewChanged() {
111      EventHandler handler = ReadOnlyViewChanged;
112      if (handler != null) handler(this, EventArgs.Empty);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.