1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Windows.Forms;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
6 | /// <summary>
|
---|
7 | /// Abstract VM class for concreted restricted JsonItems
|
---|
8 | /// </summary>
|
---|
9 | /// <typeparam name="T">JsonItemType</typeparam>
|
---|
10 | /// <typeparam name="X">Type of the range array (ConcreteRestrictedItemArray)</typeparam>
|
---|
11 | /// <typeparam name="V">Type of the value property</typeparam>
|
---|
12 | public abstract class ConcreteRestrictedJsonItemVM<T, X, V> : JsonItemVMBase<T>
|
---|
13 | where T : class, IConcreteRestrictedJsonItem<X>, IValueJsonItem<V> {
|
---|
14 | public override UserControl Control {
|
---|
15 | get {
|
---|
16 | var control = ConcreteItemsRestrictor.Create(Item.ConcreteRestrictedItems);
|
---|
17 | control.OnChecked += AddComboOption;
|
---|
18 | control.OnUnchecked += RemoveComboOption;
|
---|
19 | return control;
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public IEnumerable<X> Range {
|
---|
24 | get => Item.ConcreteRestrictedItems;
|
---|
25 | set {
|
---|
26 | Item.ConcreteRestrictedItems = value;
|
---|
27 | //check if value is still in range
|
---|
28 |
|
---|
29 | if (!RangeContainsValue()) {
|
---|
30 | Item.Value = GetDefaultValue();
|
---|
31 |
|
---|
32 | //if no elements exists -> deselect item
|
---|
33 | if (Range.Count() == 0)
|
---|
34 | base.Selected = false;
|
---|
35 | }
|
---|
36 |
|
---|
37 | OnPropertyChange(this, nameof(Range));
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Method to check if the value exists in the allowed range array.
|
---|
43 | /// </summary>
|
---|
44 | /// <returns></returns>
|
---|
45 | protected abstract bool RangeContainsValue();
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Method to get or generate a default value for property "Value".
|
---|
49 | /// </summary>
|
---|
50 | /// <returns></returns>
|
---|
51 | protected abstract V GetDefaultValue();
|
---|
52 |
|
---|
53 | private void AddComboOption(object opt) {
|
---|
54 | var items = new List<X>(Range);
|
---|
55 | items.Add((X)opt);
|
---|
56 | Range = items;
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void RemoveComboOption(object opt) {
|
---|
60 | var items = new List<X>(Range);
|
---|
61 | items.Remove((X)opt);
|
---|
62 | Range = items;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|