[2653] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Collections.ObjectModel;
|
---|
| 26 | using System.Text;
|
---|
| 27 | using System.Drawing;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Common.Resources;
|
---|
| 30 | using HeuristicLab.Collections;
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Core {
|
---|
| 34 | [EmptyStorableClass]
|
---|
| 35 | [Item("ItemCollection<T>", "Represents a collection of items.")]
|
---|
| 36 | public class ItemCollection<T> : ObservableCollection<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 ItemCollection() : base() { }
|
---|
| 57 | public ItemCollection(int capacity) : base(capacity) { }
|
---|
| 58 | public ItemCollection(IEnumerable<T> collection) : base(collection) { }
|
---|
| 59 |
|
---|
| 60 | public object Clone() {
|
---|
| 61 | return Clone(new Cloner());
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | List<T> items = new List<T>();
|
---|
| 66 | foreach (T item in this)
|
---|
| 67 | items.Add((T)cloner.Clone(item));
|
---|
| 68 | ItemCollection<T> clone = (ItemCollection<T>)Activator.CreateInstance(this.GetType(), 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 OnItemsAdded(IEnumerable<T> items) {
|
---|
| 87 | foreach (T item in items)
|
---|
| 88 | if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
| 89 | base.OnItemsAdded(items);
|
---|
| 90 | }
|
---|
| 91 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
| 92 | foreach (T item in items)
|
---|
| 93 | if (item != null) item.Changed -= new ChangedEventHandler(Item_Changed);
|
---|
| 94 | base.OnItemsRemoved(items);
|
---|
| 95 | }
|
---|
| 96 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
| 97 | foreach (T oldItem in oldItems)
|
---|
| 98 | if (oldItem != null) oldItem.Changed -= new ChangedEventHandler(Item_Changed);
|
---|
| 99 | foreach (T item in items)
|
---|
| 100 | if (item != null) item.Changed += new ChangedEventHandler(Item_Changed);
|
---|
| 101 | base.OnCollectionReset(items, oldItems);
|
---|
| 102 | }
|
---|
| 103 | protected override void OnPropertyChanged(string propertyName) {
|
---|
| 104 | base.OnPropertyChanged(propertyName);
|
---|
| 105 | OnChanged();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private void Item_Changed(object sender, ChangedEventArgs e) {
|
---|
| 109 | OnChanged(e);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|