1 | using System;
|
---|
2 | using System.ComponentModel;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Windows.Forms;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
7 | public abstract class JsonItemVMBase<JsonItemType> : IJsonItemVM<JsonItemType>
|
---|
8 | where JsonItemType : class, IJsonItem
|
---|
9 | {
|
---|
10 | IJsonItem IJsonItemVM.Item {
|
---|
11 | get => Item;
|
---|
12 | set => Item = (JsonItemType)value;
|
---|
13 | }
|
---|
14 |
|
---|
15 | private JsonItemType item;
|
---|
16 | public JsonItemType Item {
|
---|
17 | get => item;
|
---|
18 | set {
|
---|
19 | item = value;
|
---|
20 | ItemChanged?.Invoke();
|
---|
21 | }
|
---|
22 | }
|
---|
23 | public TreeNode TreeNode { get; set; }
|
---|
24 | public TreeView TreeView { get; set; }
|
---|
25 | public bool Selected {
|
---|
26 | get => Item.Active;
|
---|
27 | set {
|
---|
28 | Item.Active = value;
|
---|
29 | if(TreeNode != null) {
|
---|
30 | TreeNode.ForeColor = (Selected ? Color.Green : Color.Black);
|
---|
31 | TreeNode.Checked = value;
|
---|
32 | }
|
---|
33 | if (TreeView != null)
|
---|
34 | TreeView.Refresh();
|
---|
35 | SelectedChanged?.Invoke();
|
---|
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 | }
|
---|
46 | public string Description {
|
---|
47 | get => Item.Description;
|
---|
48 | set {
|
---|
49 | Item.Description = value;
|
---|
50 | OnPropertyChange(this, nameof(Description));
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public virtual Type TargetedJsonItemType => typeof(JsonItemType);
|
---|
55 | public abstract UserControl Control { get; }
|
---|
56 |
|
---|
57 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
58 | public event Action ItemChanged;
|
---|
59 | public event Action SelectedChanged;
|
---|
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));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|