using System.Collections.Generic; using System.Linq; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { // todo: check that at least 1 elements needs to be selected // todo: control creatable item types [StorableType("D6F285A9-E8F7-4136-A685-B2EBEA53FEA0")] public class CheckedValueConfigurationList : CheckedItemList, ICheckedValueConfigurationList { [Storable] private int minItemCount = 1; public int MinItemCount { get { return minItemCount; } } [Storable] protected IItemSet validValues; public IItemSet ValidValues { get { return validValues; } } public CheckedValueConfigurationList(IItemSet validValues) { this.validValues = validValues; RegisterEvents(); } public CheckedValueConfigurationList(IEnumerable collection) : base(collection) { validValues = null; // event wiring not needed } public CheckedValueConfigurationList() { RegisterEvents(); } [StorableConstructor] protected CheckedValueConfigurationList(StorableConstructorFlag _) : base(_) { RegisterEvents(); } protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner) : base(original, cloner) { this.minItemCount = original.MinItemCount; this.validValues = cloner.Clone(original.validValues); RegisterEvents(); } public override IDeepCloneable Clone(Cloner cloner) { return new CheckedValueConfigurationList(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterEvents(); } private void RegisterEvents() { this.ItemsAdded += new CollectionItemsChangedEventHandler>(CheckedValueConfigurationList_ItemsAdded); this.ItemsRemoved += new CollectionItemsChangedEventHandler>(CheckedValueConfigurationList_ItemsRemoved); } private void DeregisterEvents() { this.ItemsRemoved -= new CollectionItemsChangedEventHandler>(CheckedValueConfigurationList_ItemsRemoved); this.ItemsAdded -= new CollectionItemsChangedEventHandler>(CheckedValueConfigurationList_ItemsAdded); } private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { foreach (var item in e.Items) { var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType); if (matchingItems.Count() > 0) { int maxNumber = matchingItems.Select(x => x.Number).Max(); item.Value.Number = maxNumber + 1; } } } private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) { // auch collectionreset gehört berücksichtigt // funktioniert so nicht ganz, weil die view das hinzufügen nicht mitkriegt //if (this.Count < minItemCount) { // foreach (var item in e.Items) { // if (this.Count >= minItemCount) // break; // this.Add(item); // } //} } } }