Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17410 was 17410, checked in by dpiringe, 4 years ago

#3026:

  • deleted JsonItemArrayControl and JsonItemDefaultControl
  • redesigned architecture for JsonItem: now there are different types of JsonItem (IntJsonItem, BoolJsonItem, ...) -> for better type safety and expandability
  • fixed bug in BaseConverter for GetMinValue and GetMaxValue for IntValue, but ignored for other value types (DoubleValue, DateTimeValue, ...) because the redesign of JsonItem-Architecture can make these two methods obsolet soon
  • fixed bug in JsonItemConverter to prevent null pointer exceptions
  • refactored value and range converters -> removed complicated generic ValueTypeValueConverter and ValueRangeConverter and implemented the necessary methods directly in concrete classes (improves readability and removes the need of reflection)
  • redesigned view handling in OptimizerIntegration -> dynamically seaches for JsonItemVMBase implementations, which are connected with a view
    • this enables better scaling with more user controls
  • JsonItemVMBase implements MVVM architecture
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> : JsonItemVM {
21
22    public T MinValue {
23      get => Cast(((Array)base.Item.Value).GetValue(0));
24      set {
25        SetValue(value, ((Array)base.Item.Value).GetValue(1));
26        OnPropertyChange(this, nameof(MinValue));
27      }
28    }
29
30    public T MaxValue {
31      get => Cast(((Array)base.Item.Value).GetValue(1));
32      set {
33        SetValue(((Array)base.Item.Value).GetValue(0), value);
34        OnPropertyChange(this, nameof(MaxValue));
35      }
36    }
37
38    public T MinRange {
39      get => Cast(base.Item.Range.First());
40      set {
41        SetRange(value, base.Item.Range.Last());
42        OnPropertyChange(this, nameof(MinRange));
43      }
44    }
45
46    public T MaxRange {
47      get => Cast(base.Item.Range.Last());
48      set {
49        SetRange(base.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      base.Item.Value = new object[] { min, max };
58
59    private void SetRange(object min, object max) =>
60      base.Item.Range = new object[] { min, max };
61  }
62}
Note: See TracBrowser for help on using the repository browser.