Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/ItemSet.cs @ 2754

Last change on this file since 2754 was 2653, checked in by swagner, 15 years ago

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.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
32
33namespace HeuristicLab.Core {
34  [EmptyStorableClass]
35  [Item("ItemSet<T>", "Represents a set of items.")]
36  public class ItemSet<T> : ObservableSet<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 ItemSet() : base() { }
57    public ItemSet(IEnumerable<T> collection) : base(collection) { }
58
59    public object Clone() {
60      return Clone(new Cloner());
61    }
62
63    public IDeepCloneable Clone(Cloner cloner) {
64      List<T> items = new List<T>();
65      foreach (T item in this)
66        items.Add((T)cloner.Clone(item));
67      ItemSet<T> clone = (ItemSet<T>)Activator.CreateInstance(this.GetType(), items);
68      cloner.RegisterClonedObject(this, clone);
69      return clone;
70    }
71
72    public override string ToString() {
73      return ItemName;
74    }
75
76    public event ChangedEventHandler Changed;
77    protected void OnChanged() {
78      OnChanged(new ChangedEventArgs());
79    }
80    protected virtual void OnChanged(ChangedEventArgs e) {
81      if ((e.RegisterChangedObject(this)) && (Changed != null))
82        Changed(this, e);
83    }
84
85    protected override void OnItemsAdded(IEnumerable<T> items) {
86      foreach (T item in items)
87        if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
88      base.OnItemsAdded(items);
89    }
90    protected override void OnItemsRemoved(IEnumerable<T> items) {
91      foreach (T item in items)
92        if (item != null) item.Changed -= new ChangedEventHandler(Item_Changed);
93      base.OnItemsRemoved(items);
94    }
95    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
96      foreach (T oldItem in oldItems)
97        if (oldItem != null) oldItem.Changed -= new ChangedEventHandler(Item_Changed);
98      foreach (T item in items)
99        if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
100      base.OnCollectionReset(items, oldItems);
101    }
102    protected override void OnPropertyChanged(string propertyName) {
103      base.OnPropertyChanged(propertyName);
104      OnChanged();
105    }
106
107    private void Item_Changed(object sender, ChangedEventArgs e) {
108      OnChanged(e);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.