1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Collections;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
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 | [StorableClass]
|
---|
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(bool deserializing)
|
---|
39 | : base(deserializing) {
|
---|
40 | RegisterEvents();
|
---|
41 | }
|
---|
42 | protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | this.minItemCount = original.MinItemCount;
|
---|
45 | this.validValues = cloner.Clone(original.validValues);
|
---|
46 | RegisterEvents();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new CheckedValueConfigurationList(this, cloner);
|
---|
51 | }
|
---|
52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
53 | private void AfterDeserialization() {
|
---|
54 | RegisterEvents();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void RegisterEvents() {
|
---|
58 | this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
59 | this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
60 | }
|
---|
61 |
|
---|
62 | private void DeregisterEvents() {
|
---|
63 | this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
|
---|
64 | this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
68 | foreach (var item in e.Items) {
|
---|
69 | var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType);
|
---|
70 | if (matchingItems.Count() > 0) {
|
---|
71 | int maxNumber = matchingItems.Select(x => x.Number).Max();
|
---|
72 | item.Value.Number = maxNumber + 1;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void CheckedValueConfigurationList_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
|
---|
78 | // auch collectionreset gehört berücksichtigt
|
---|
79 | // funktioniert so nicht ganz, weil die view das hinzufügen nicht mitkriegt
|
---|
80 | //if (this.Count < minItemCount) {
|
---|
81 | // foreach (var item in e.Items) {
|
---|
82 | // if (this.Count >= minItemCount)
|
---|
83 | // break;
|
---|
84 | // this.Add(item);
|
---|
85 | // }
|
---|
86 | //}
|
---|
87 | }
|
---|
88 |
|
---|
89 | }
|
---|
90 | }
|
---|