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.Common.Resources;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Core {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("ItemCollection<T>", "Represents a collection of items.")]
|
---|
34 | public class ItemCollection<T> : ObservableCollection<T>, IItemCollection<T> where T : 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 List<T> Items {
|
---|
47 | get { return list; }
|
---|
48 | set { list = 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<T>)list).IsReadOnly) {
|
---|
57 | readOnlyView = value;
|
---|
58 | OnReadOnlyViewChanged();
|
---|
59 | OnPropertyChanged("ReadOnlyView");
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public ItemCollection()
|
---|
65 | : base() {
|
---|
66 | readOnlyView = ((ICollection<T>)list).IsReadOnly;
|
---|
67 | }
|
---|
68 | public ItemCollection(int capacity)
|
---|
69 | : base(capacity) {
|
---|
70 | readOnlyView = ((ICollection<T>)list).IsReadOnly;
|
---|
71 | }
|
---|
72 | public ItemCollection(IEnumerable<T> collection)
|
---|
73 | : base(collection) {
|
---|
74 | readOnlyView = ((ICollection<T>)list).IsReadOnly;
|
---|
75 | }
|
---|
76 | [StorableConstructor]
|
---|
77 | protected ItemCollection(bool deserializing) { }
|
---|
78 |
|
---|
79 | public object Clone() {
|
---|
80 | return Clone(new Cloner());
|
---|
81 | }
|
---|
82 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
83 | ItemCollection<T> clone = (ItemCollection<T>)Activator.CreateInstance(this.GetType());
|
---|
84 | cloner.RegisterClonedObject(this, clone);
|
---|
85 | clone.readOnlyView = readOnlyView;
|
---|
86 | clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
|
---|
87 | return clone;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public new ReadOnlyItemCollection<T> AsReadOnly() {
|
---|
91 | return new ReadOnlyItemCollection<T>(this);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override string ToString() {
|
---|
95 | return ItemName;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public event EventHandler ItemImageChanged;
|
---|
99 | protected virtual void OnItemImageChanged() {
|
---|
100 | EventHandler handler = ItemImageChanged;
|
---|
101 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
102 | }
|
---|
103 | public event EventHandler ToStringChanged;
|
---|
104 | protected virtual void OnToStringChanged() {
|
---|
105 | EventHandler handler = ToStringChanged;
|
---|
106 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
107 | }
|
---|
108 | public event EventHandler ReadOnlyViewChanged;
|
---|
109 | protected virtual void OnReadOnlyViewChanged() {
|
---|
110 | EventHandler handler = ReadOnlyViewChanged;
|
---|
111 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|