Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/RangeVM.cs @ 17412

Last change on this file since 17412 was 17412, checked in by dpiringe, 5 years ago

#3026 code cleanup

File size: 1.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace HeuristicLab.JsonInterface.OptimizerIntegration {
9
10  public class IntRangeVM : RangeVM<int> {
11    public override Type JsonItemType => typeof(IntArrayJsonItem);
12  }
13
14  public class DoubleRangeVM : RangeVM<double> {
15    public override Type JsonItemType => typeof(DoubleArrayJsonItem);
16    public override JsonItemBaseControl GetControl() =>
17      new JsonItemRangeControl(this);
18  }
19
20  public abstract class RangeVM<T> : JsonItemVMBase {
21
22    public T MinValue {
23      get => Cast(((Array)Item.Value).GetValue(0));
24      set {
25        SetValue(value, ((Array)Item.Value).GetValue(1));
26        OnPropertyChange(this, nameof(MinValue));
27      }
28    }
29
30    public T MaxValue {
31      get => Cast(((Array)Item.Value).GetValue(1));
32      set {
33        SetValue(((Array)Item.Value).GetValue(0), value);
34        OnPropertyChange(this, nameof(MaxValue));
35      }
36    }
37
38    public T MinRange {
39      get => Cast(Item.Range.First());
40      set {
41        SetRange(value, Item.Range.Last());
42        OnPropertyChange(this, nameof(MinRange));
43      }
44    }
45
46    public T MaxRange {
47      get => Cast(Item.Range.Last());
48      set {
49        SetRange(Item.Range.First(), value);
50        OnPropertyChange(this, nameof(MaxRange));
51      }
52    }
53
54    private T Cast(object obj) => (T)Convert.ChangeType(obj, typeof(T));
55
56    private void SetValue(object min, object max) =>
57      Item.Value = new object[] { min, max };
58
59    private void SetRange(object min, object max) =>
60      Item.Range = new object[] { min, max };
61  }
62}
Note: See TracBrowser for help on using the repository browser.