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.Linq;
|
---|
24 | using System.Collections;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Collections.ObjectModel;
|
---|
27 | using System.Text;
|
---|
28 | using System.Drawing;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Common.Resources;
|
---|
31 | using HeuristicLab.Collections;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Core {
|
---|
34 | [EmptyStorableClass]
|
---|
35 | [Item("ItemArray<T>", "Represents an array of items.")]
|
---|
36 | public class ItemArray<T> : ObservableArray<T>, IItem where T : class, IItem {
|
---|
37 | [Storable(Name = "RestoreEvents")]
|
---|
38 | private object RestoreEvents {
|
---|
39 | get { return null; }
|
---|
40 | set {
|
---|
41 | foreach (T item in this)
|
---|
42 | if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public virtual string ItemName {
|
---|
47 | get { return ItemAttribute.GetName(this.GetType()); }
|
---|
48 | }
|
---|
49 | public virtual string ItemDescription {
|
---|
50 | get { return ItemAttribute.GetDescription(this.GetType()); }
|
---|
51 | }
|
---|
52 | public virtual Image ItemImage {
|
---|
53 | get { return VS2008ImageLibrary.Class; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public ItemArray() : base() { }
|
---|
57 | public ItemArray(int length) : base(length) { }
|
---|
58 | public ItemArray(T[] array) : base(array) {
|
---|
59 | foreach (T item in this)
|
---|
60 | if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
61 | }
|
---|
62 | public ItemArray(IEnumerable<T> collection) : base(collection) {
|
---|
63 | foreach (T item in this)
|
---|
64 | if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public object Clone() {
|
---|
68 | return Clone(new Cloner());
|
---|
69 | }
|
---|
70 |
|
---|
71 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | ItemArray<T> clone = (ItemArray<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
|
---|
73 | cloner.RegisterClonedObject(this, clone);
|
---|
74 | return clone;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override string ToString() {
|
---|
78 | return ItemName;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public event ChangedEventHandler Changed;
|
---|
82 | protected void OnChanged() {
|
---|
83 | OnChanged(new ChangedEventArgs());
|
---|
84 | }
|
---|
85 | protected virtual void OnChanged(ChangedEventArgs e) {
|
---|
86 | if ((e.RegisterChangedObject(this)) && (Changed != null))
|
---|
87 | Changed(this, e);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
91 | foreach (IndexedItem<T> oldItem in oldItems)
|
---|
92 | if (oldItem.Value != null) oldItem.Value.Changed -= new ChangedEventHandler(Item_Changed);
|
---|
93 | foreach (IndexedItem<T> item in items)
|
---|
94 | if (item.Value != null) item.Value.Changed += new ChangedEventHandler(Item_Changed);
|
---|
95 | base.OnItemsReplaced(items, oldItems);
|
---|
96 | }
|
---|
97 | protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
98 | foreach (IndexedItem<T> oldItem in oldItems)
|
---|
99 | if (oldItem.Value != null) oldItem.Value.Changed -= new ChangedEventHandler(Item_Changed);
|
---|
100 | foreach (IndexedItem<T> item in items)
|
---|
101 | if (item.Value != null) item.Value.Changed += new ChangedEventHandler(Item_Changed);
|
---|
102 | base.OnCollectionReset(items, oldItems);
|
---|
103 | }
|
---|
104 | protected override void OnPropertyChanged(string propertyName) {
|
---|
105 | base.OnPropertyChanged(propertyName);
|
---|
106 | OnChanged();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void Item_Changed(object sender, ChangedEventArgs e) {
|
---|
110 | OnChanged(e);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|