Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/ItemList.cs @ 2927

Last change on this file since 2927 was 2833, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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