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 : IJsonItemVM {
|
---|
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?.LoosenPath();
|
---|
20 | item = value;
|
---|
21 | item.FixatePath();
|
---|
22 | ItemChanged?.Invoke();
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public TreeNode TreeNode { get; set; }
|
---|
27 | public TreeView TreeView { get; set; }
|
---|
28 |
|
---|
29 | protected void OnPropertyChange(object sender, string propertyName) {
|
---|
30 | // Make a temporary copy of the event to avoid possibility of
|
---|
31 | // a race condition if the last subscriber unsubscribes
|
---|
32 | // immediately after the null check and before the event is raised.
|
---|
33 | // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-raise-base-class-events-in-derived-classes
|
---|
34 |
|
---|
35 | PropertyChangedEventHandler tmp = PropertyChanged;
|
---|
36 | tmp?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
---|
37 | }
|
---|
38 |
|
---|
39 | public virtual Type JsonItemType => typeof(JsonItem);
|
---|
40 |
|
---|
41 | private bool selected = true;
|
---|
42 | public bool Selected {
|
---|
43 | get => selected;
|
---|
44 | set {
|
---|
45 | selected = value;
|
---|
46 | if(TreeNode != null) {
|
---|
47 | TreeNode.ForeColor = (selected ? Color.Black : Color.Red);
|
---|
48 | TreeNode.Checked = value;
|
---|
49 | }
|
---|
50 | if (TreeView != null)
|
---|
51 | TreeView.Refresh();
|
---|
52 | OnPropertyChange(this, nameof(Selected));
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public string Name {
|
---|
57 | get => Item.Name;
|
---|
58 | set {
|
---|
59 | Item.Name = value;
|
---|
60 | OnPropertyChange(this, nameof(Name));
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public string Description {
|
---|
65 | get => Item.Description;
|
---|
66 | set {
|
---|
67 | Item.Description = value;
|
---|
68 | OnPropertyChange(this, nameof(Description));
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public string ActualName {
|
---|
73 | get => Item.ActualName;
|
---|
74 | set {
|
---|
75 | Item.ActualName = value;
|
---|
76 | OnPropertyChange(this, nameof(ActualName));
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public virtual JsonItemBaseControl Control => new JsonItemBaseControl(this);
|
---|
81 |
|
---|
82 | #region IDisposable Support
|
---|
83 | private bool disposedValue = false; // To detect redundant calls
|
---|
84 |
|
---|
85 | protected virtual void Dispose(bool disposing) {
|
---|
86 | if (!disposedValue) {
|
---|
87 | if (disposing) {
|
---|
88 | item.LoosenPath();
|
---|
89 | item = null;
|
---|
90 | }
|
---|
91 | disposedValue = true;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | // This code added to correctly implement the disposable pattern.
|
---|
96 | public void Dispose() {
|
---|
97 | // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
|
---|
98 | Dispose(true);
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 | }
|
---|
102 | }
|
---|