#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using System.Collections.Generic; using System.Drawing; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Core { [Item("NamedItemCollection", "Represents a collection of named items.")] public class NamedItemCollection : ObservableKeyedCollection, IItem where T : class, INamedItem { public virtual string ItemName { get { return ItemAttribute.GetName(this.GetType()); } } public virtual string ItemDescription { get { return ItemAttribute.GetDescription(this.GetType()); } } public virtual Image ItemImage { get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; } } [Storable(Name = "RestoreEvents")] private object RestoreEvents { get { return null; } set { RegisterItemEvents(this); } } public NamedItemCollection() : base() { } public NamedItemCollection(int capacity) : base(capacity) { } public NamedItemCollection(IEnumerable collection) : base(collection) { RegisterItemEvents(this); } public object Clone() { return Clone(new Cloner()); } public virtual IDeepCloneable Clone(Cloner cloner) { NamedItemCollection clone = (NamedItemCollection)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x))); cloner.RegisterClonedObject(this, clone); return clone; } public override string ToString() { return ItemName; } public event ChangedEventHandler Changed; protected void OnChanged() { OnChanged(new ChangedEventArgs()); } protected virtual void OnChanged(ChangedEventArgs e) { if ((e.RegisterChangedObject(this)) && (Changed != null)) Changed(this, e); } protected override string GetKeyForItem(T item) { return item.Name; } protected override void OnItemsAdded(IEnumerable items) { RegisterItemEvents(items); base.OnItemsAdded(items); } protected override void OnItemsRemoved(IEnumerable items) { DeregisterItemEvents(items); base.OnItemsRemoved(items); } #region NOTE // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired // by ObservableKeyedCollection when the key of an item has changed. The items stays // in the collection and therefore the NameChanging, NameChanged and Changed event handler // do not have to be removed and added again. #endregion protected override void OnCollectionReset(IEnumerable items, IEnumerable oldItems) { DeregisterItemEvents(oldItems); RegisterItemEvents(items); base.OnCollectionReset(items, oldItems); } protected override void OnPropertyChanged(string propertyName) { base.OnPropertyChanged(propertyName); OnChanged(); } private void RegisterItemEvents(IEnumerable items) { foreach (T item in items) { item.NameChanging += new EventHandler>(Item_NameChanging); item.NameChanged += new EventHandler(Item_NameChanged); item.Changed += new ChangedEventHandler(Item_Changed); } } private void DeregisterItemEvents(IEnumerable items) { foreach (T item in items) { item.NameChanging -= new EventHandler>(Item_NameChanging); item.NameChanged -= new EventHandler(Item_NameChanged); item.Changed -= new ChangedEventHandler(Item_Changed); } } private void Item_NameChanging(object sender, CancelEventArgs e) { e.Cancel = e.Cancel || this.ContainsKey(e.Value); } private void Item_NameChanged(object sender, EventArgs e) { T item = (T)sender; UpdateItemKey(item); } private void Item_Changed(object sender, ChangedEventArgs e) { OnChanged(e); } } }