Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/ItemArray.cs @ 2806

Last change on this file since 2806 was 2790, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 4.0 KB
RevLine 
[2746]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2746]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;
24using System.Collections.Generic;
25using System.Collections.ObjectModel;
26using System.Text;
27using System.Drawing;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Common.Resources;
30using HeuristicLab.Collections;
31
32namespace HeuristicLab.Core {
33  [EmptyStorableClass]
34  [Item("ItemArray<T>", "Represents an array of items.")]
35  public class ItemArray<T> : ObservableArray<T>, IItem where T : class, IItem {
36    [Storable(Name = "RestoreEvents")]
37    private object RestoreEvents {
38      get { return null; }
39      set {
40        foreach (T item in this)
41          if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
42      }
43    }
44
45    public virtual string ItemName {
46      get { return ItemAttribute.GetName(this.GetType()); }
47    }
48    public virtual string ItemDescription {
49      get { return ItemAttribute.GetDescription(this.GetType()); }
50    }
51    public virtual Image ItemImage {
52      get { return VS2008ImageLibrary.Class; }
53    }
54
55    public ItemArray() : base() { }
56    public ItemArray(int length) : base(length) { }
57    public ItemArray(T[] array) : base(array) { }
58    public ItemArray(IEnumerable<T> collection) : base(collection) { }
59
60    public object Clone() {
61      return Clone(new Cloner());
62    }
63
64    public IDeepCloneable Clone(Cloner cloner) {
65      T[] items = new T[Length];
66      for (int i = 0; i < items.Length; i++)
67        items[i] = (T)cloner.Clone(this[i]);
68      ItemArray<T> clone = (ItemArray<T>)Activator.CreateInstance(this.GetType(), new object[] { items });
69      cloner.RegisterClonedObject(this, clone);
70      return clone;
71    }
72
73    public override string ToString() {
74      return ItemName;
75    }
76
77    public event ChangedEventHandler Changed;
78    protected void OnChanged() {
79      OnChanged(new ChangedEventArgs());
80    }
81    protected virtual void OnChanged(ChangedEventArgs e) {
82      if ((e.RegisterChangedObject(this)) && (Changed != null))
83        Changed(this, e);
84    }
85
86    protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
87      foreach (IndexedItem<T> oldItem in oldItems)
88        if (oldItem.Value != null) oldItem.Value.Changed -= new ChangedEventHandler(Item_Changed);
89      foreach (IndexedItem<T> item in items)
90        if (item.Value != null) item.Value.Changed += new ChangedEventHandler(Item_Changed);
91      base.OnItemsReplaced(items, oldItems);
92    }
93    protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
94      foreach (IndexedItem<T> oldItem in oldItems)
95        if (oldItem.Value != null) oldItem.Value.Changed -= new ChangedEventHandler(Item_Changed);
96      foreach (IndexedItem<T> item in items)
97        if (item.Value != null) item.Value.Changed += new ChangedEventHandler(Item_Changed);
98      base.OnCollectionReset(items, oldItems);
99    }
100    protected override void OnPropertyChanged(string propertyName) {
101      base.OnPropertyChanged(propertyName);
102      OnChanged();
103    }
104
105    private void Item_Changed(object sender, ChangedEventArgs e) {
106      OnChanged(e);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.