source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/ConcreteRestrictedJsonItemVM.cs @ 17828

Last change on this file since 17828 was 17828, checked in by dpiringe, 2 months ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 2.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows.Forms;
7
8namespace 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}
Note: See TracBrowser for help on using the repository browser.