1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Text;
|
---|
7 | using System.Threading.Tasks;
|
---|
8 | using System.Windows.Forms;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
11 | public class JsonItemVMBase : INotifyPropertyChanged {
|
---|
12 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
13 | public event Action ItemChanged;
|
---|
14 |
|
---|
15 | private IJsonItem item;
|
---|
16 | public IJsonItem Item {
|
---|
17 | get => item;
|
---|
18 | set {
|
---|
19 | item = value;
|
---|
20 | ItemChanged?.Invoke();
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public TreeNode TreeNode { get; set; }
|
---|
25 | public TreeView TreeView { get; set; }
|
---|
26 |
|
---|
27 | protected void OnPropertyChange(object sender, string propertyName) {
|
---|
28 | // Make a temporary copy of the event to avoid possibility of
|
---|
29 | // a race condition if the last subscriber unsubscribes
|
---|
30 | // immediately after the null check and before the event is raised.
|
---|
31 | // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-raise-base-class-events-in-derived-classes
|
---|
32 |
|
---|
33 | PropertyChangedEventHandler tmp = PropertyChanged;
|
---|
34 | tmp?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
---|
35 | }
|
---|
36 |
|
---|
37 | public virtual Type JsonItemType => typeof(JsonItem);
|
---|
38 |
|
---|
39 | private bool selected = true;
|
---|
40 | public bool Selected {
|
---|
41 | get => selected;
|
---|
42 | set {
|
---|
43 | selected = value;
|
---|
44 | if(TreeNode != null)
|
---|
45 | TreeNode.ForeColor = (selected ? Color.Black : Color.Red);
|
---|
46 | if (TreeView != null)
|
---|
47 | TreeView.Refresh();
|
---|
48 | OnPropertyChange(this, nameof(Selected));
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public string Name {
|
---|
53 | get => Item.Name;
|
---|
54 | set {
|
---|
55 | Item.Name = value;
|
---|
56 | OnPropertyChange(this, nameof(Name));
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public string ActualName {
|
---|
61 | get => Item.ActualName;
|
---|
62 | set {
|
---|
63 | Item.ActualName = value;
|
---|
64 | OnPropertyChange(this, nameof(ActualName));
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public virtual JsonItemBaseControl GetControl() {
|
---|
69 | return new JsonItemBaseControl(this);
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 | }
|
---|