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("KeyedItemCollection<TKey, TItem>", "Represents a keyed collection of items.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class KeyedItemCollection<TKey, TItem> : ObservableKeyedCollection<TKey, TItem>, IKeyedItemCollection<TKey, TItem> where TItem : class, IItem {
|
---|
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]
|
---|
45 | private Dictionary<TKey, TItem> Items {
|
---|
46 | get { return dict; }
|
---|
47 | set { dict = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [Storable]
|
---|
51 | private bool readOnlyView;
|
---|
52 | public virtual bool ReadOnlyView {
|
---|
53 | get { return readOnlyView; }
|
---|
54 | set {
|
---|
55 | if ((readOnlyView != value) && !((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly) {
|
---|
56 | readOnlyView = value;
|
---|
57 | OnReadOnlyViewChanged();
|
---|
58 | OnPropertyChanged("ReadOnlyView");
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected KeyedItemCollection() : base() {
|
---|
64 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
65 | }
|
---|
66 | protected KeyedItemCollection(int capacity) : base(capacity) {
|
---|
67 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
68 | }
|
---|
69 | protected KeyedItemCollection(IEnumerable<TItem> collection) : base(collection) {
|
---|
70 | readOnlyView = ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly;
|
---|
71 | }
|
---|
72 | [StorableConstructor]
|
---|
73 | protected KeyedItemCollection(bool deserializing) { }
|
---|
74 |
|
---|
75 | public object Clone() {
|
---|
76 | return Clone(new Cloner());
|
---|
77 | }
|
---|
78 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
79 | KeyedItemCollection<TKey, TItem> clone = (KeyedItemCollection<TKey, TItem>)Activator.CreateInstance(this.GetType());
|
---|
80 | cloner.RegisterClonedObject(this, clone);
|
---|
81 | clone.readOnlyView = readOnlyView;
|
---|
82 | foreach (TItem item in dict.Values) {
|
---|
83 | TItem clonedItem = (TItem)cloner.Clone(item);
|
---|
84 | clone.dict.Add(GetKeyForItem(clonedItem), clonedItem);
|
---|
85 | }
|
---|
86 | return clone;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public new ReadOnlyKeyedItemCollection<TKey, TItem> AsReadOnly() {
|
---|
90 | return new ReadOnlyKeyedItemCollection<TKey, TItem>(this);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override string ToString() {
|
---|
94 | return ItemName;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public event EventHandler ItemImageChanged;
|
---|
98 | protected virtual void OnItemImageChanged() {
|
---|
99 | EventHandler handler = ItemImageChanged;
|
---|
100 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
101 | }
|
---|
102 | public event EventHandler ToStringChanged;
|
---|
103 | protected virtual void OnToStringChanged() {
|
---|
104 | EventHandler handler = ToStringChanged;
|
---|
105 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
106 | }
|
---|
107 | public event EventHandler ReadOnlyViewChanged;
|
---|
108 | protected virtual void OnReadOnlyViewChanged() {
|
---|
109 | EventHandler handler = ReadOnlyViewChanged;
|
---|
110 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|