[5112] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Common;
|
---|
| 7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5277] | 8 | using HeuristicLab.Collections;
|
---|
[5112] | 9 |
|
---|
| 10 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 11 | // todo: check that at least 1 elements needs to be selected
|
---|
| 12 | // todo: control creatable item types
|
---|
| 13 | [StorableClass]
|
---|
[5277] | 14 | public class CheckedValueConfigurationList : CheckedItemList<IValueConfiguration>, ICheckedValueConfigurationList {
|
---|
[5112] | 15 | [Storable]
|
---|
| 16 | private int minItemCount = 1;
|
---|
[5361] | 17 | public int MinItemCount {
|
---|
| 18 | get { return minItemCount; }
|
---|
[5112] | 19 | }
|
---|
| 20 |
|
---|
| 21 | [Storable]
|
---|
| 22 | protected IItemSet<IItem> validValues;
|
---|
| 23 | public IItemSet<IItem> ValidValues {
|
---|
| 24 | get { return validValues; }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[5313] | 27 | public CheckedValueConfigurationList(IItemSet<IItem> validValues) {
|
---|
[5231] | 28 | this.validValues = validValues;
|
---|
[5112] | 29 | RegisterEvents();
|
---|
| 30 | }
|
---|
[5277] | 31 | public CheckedValueConfigurationList() {
|
---|
[5112] | 32 | RegisterEvents();
|
---|
| 33 | }
|
---|
| 34 | [StorableConstructor]
|
---|
[5361] | 35 | protected CheckedValueConfigurationList(bool deserializing)
|
---|
| 36 | : base(deserializing) {
|
---|
[5112] | 37 | RegisterEvents();
|
---|
| 38 | }
|
---|
[5361] | 39 | protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
|
---|
| 40 | : base(original, cloner) {
|
---|
[5112] | 41 | this.minItemCount = original.MinItemCount;
|
---|
| 42 | this.validValues = original.validValues;
|
---|
| 43 | RegisterEvents();
|
---|
| 44 | }
|
---|
| 45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5277] | 46 | return new CheckedValueConfigurationList(this, cloner);
|
---|
[5112] | 47 | }
|
---|
| 48 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 49 | private void AfterDeserialization() {
|
---|
| 50 | RegisterEvents();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private void RegisterEvents() {
|
---|
[5359] | 54 | this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
[5277] | 55 | this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
[5112] | 56 | }
|
---|
[5277] | 57 |
|
---|
[5112] | 58 | private void DeregisterEvents() {
|
---|
[5277] | 59 | this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
[5359] | 60 | this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
[5112] | 61 | }
|
---|
| 62 |
|
---|
[5359] | 63 | void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
| 64 | foreach (var item in e.Items) {
|
---|
[5361] | 65 | var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType);
|
---|
| 66 | if (matchingItems.Count() > 0) {
|
---|
| 67 | int maxNumber = matchingItems.Select(x => x.Number).Max();
|
---|
[5359] | 68 | item.Value.Number = maxNumber + 1;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[5277] | 73 | private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5112] | 74 | // auch collectionreset gehört berücksichtigt
|
---|
| 75 | // funktioniert so nicht ganz, weil die view das hinzufügen nicht mitkriegt
|
---|
| 76 | //if (this.Count < minItemCount) {
|
---|
| 77 | // foreach (var item in e.Items) {
|
---|
| 78 | // if (this.Count >= minItemCount)
|
---|
| 79 | // break;
|
---|
| 80 | // this.Add(item);
|
---|
| 81 | // }
|
---|
| 82 | //}
|
---|
| 83 | }
|
---|
[5277] | 84 |
|
---|
[5112] | 85 | }
|
---|
| 86 | }
|
---|