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