source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/JsonItemVMBase.cs @ 17843

Last change on this file since 17843 was 17843, checked in by dpiringe, 3 months ago

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
File size: 2.2 KB
RevLine 
[17410]1using System;
2using System.ComponentModel;
[17431]3using System.Drawing;
4using System.Windows.Forms;
[17410]5
6namespace HeuristicLab.JsonInterface.OptimizerIntegration {
[17828]7  public abstract class JsonItemVMBase<JsonItemType> : IJsonItemVM<JsonItemType>
[17473]8    where JsonItemType : class, IJsonItem
9  {
10    IJsonItem IJsonItemVM.Item {
[17477]11      get => Item;
12      set => Item = (JsonItemType)value;
[17473]13    }
14
15    private JsonItemType item;
16    public JsonItemType Item {
[17420]17      get => item;
18      set {
19        item = value;
20        ItemChanged?.Invoke();
21      }
22    }
[17431]23    public TreeNode TreeNode { get; set; }
24    public TreeView TreeView { get; set; }
[17410]25    public bool Selected {
[17451]26      get => Item.Active;
[17410]27      set {
[17451]28        Item.Active = value;
[17439]29        if(TreeNode != null) {
[17473]30          TreeNode.ForeColor = (Selected ? Color.Green : Color.Black);
[17439]31          TreeNode.Checked = value;
32        }
[17431]33        if (TreeView != null)
34          TreeView.Refresh();
[17477]35        SelectedChanged?.Invoke();
[17410]36        OnPropertyChange(this, nameof(Selected));
37      }
38    }
39    public string Name {
40      get => Item.Name;
41      set {
42        Item.Name = value;
43        OnPropertyChange(this, nameof(Name));
44      }
45    }
[17433]46    public string Description {
47      get => Item.Description;
48      set {
49        Item.Description = value;
50        OnPropertyChange(this, nameof(Description));
51      }
52    }
53
[17519]54    public virtual Type TargetedJsonItemType => typeof(JsonItemType);
[17471]55    public abstract UserControl Control { get; }
56
57    public event PropertyChangedEventHandler PropertyChanged;
58    public event Action ItemChanged;
[17477]59    public event Action SelectedChanged;
[17471]60
61    protected void OnPropertyChange(object sender, string propertyName) {
62      // Make a temporary copy of the event to avoid possibility of
63      // a race condition if the last subscriber unsubscribes
64      // immediately after the null check and before the event is raised.
65      // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-raise-base-class-events-in-derived-classes
66     
67      PropertyChangedEventHandler tmp = PropertyChanged;
68      tmp?.Invoke(this, new PropertyChangedEventArgs(propertyName));
[17410]69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.