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