[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;
|
---|
| 17 | public int MinItemCount {
|
---|
| 18 | get { return minItemCount; }
|
---|
| 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]
|
---|
[5277] | 35 | protected CheckedValueConfigurationList(bool deserializing) : base(deserializing) {
|
---|
[5112] | 36 | RegisterEvents();
|
---|
| 37 | }
|
---|
[5277] | 38 | protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner) : base(original, cloner) {
|
---|
[5112] | 39 | this.minItemCount = original.MinItemCount;
|
---|
| 40 | this.validValues = original.validValues;
|
---|
| 41 | RegisterEvents();
|
---|
| 42 | }
|
---|
| 43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5277] | 44 | return new CheckedValueConfigurationList(this, cloner);
|
---|
[5112] | 45 | }
|
---|
| 46 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 47 | private void AfterDeserialization() {
|
---|
| 48 | RegisterEvents();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | private void RegisterEvents() {
|
---|
[5359] | 52 | this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
[5277] | 53 | this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
[5112] | 54 | }
|
---|
[5277] | 55 |
|
---|
[5112] | 56 | private void DeregisterEvents() {
|
---|
[5277] | 57 | this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
[5359] | 58 | this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
[5112] | 59 | }
|
---|
| 60 |
|
---|
[5359] | 61 | void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
| 62 | foreach (var item in e.Items) {
|
---|
| 63 | if(this.Where(x => x.ItemName == item.Value.ItemName).Count() > 0) {
|
---|
| 64 | int maxNumber = this.Where(x => x.ItemName == item.Value.ItemName).Select(x => x.Number).Max();
|
---|
| 65 | item.Value.Number = maxNumber + 1;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[5277] | 70 | private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
[5112] | 71 | // auch collectionreset gehört berücksichtigt
|
---|
| 72 | // funktioniert so nicht ganz, weil die view das hinzufügen nicht mitkriegt
|
---|
| 73 | //if (this.Count < minItemCount) {
|
---|
| 74 | // foreach (var item in e.Items) {
|
---|
| 75 | // if (this.Count >= minItemCount)
|
---|
| 76 | // break;
|
---|
| 77 | // this.Add(item);
|
---|
| 78 | // }
|
---|
| 79 | //}
|
---|
| 80 | }
|
---|
[5277] | 81 |
|
---|
[5112] | 82 | }
|
---|
| 83 | }
|
---|