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