Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17473 was 17473, checked in by dpiringe, 4 years ago

#3026:

  • refactored inheritance structure of json items, now the default JsonItem is an abstract class without properties Value and Range -> splitted up into new interfaces
  • updated view models for new json item structure
  • updated SingleLineArrayJsonWriter
File size: 2.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using System.Windows.Forms;
9
10namespace HeuristicLab.JsonInterface.OptimizerIntegration {
11  public abstract class JsonItemVMBase<JsonItemType> : IJsonItemVM<JsonItemType>
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        OnPropertyChange(this, nameof(Selected));
42      }
43    }
44    public string Name {
45      get => Item.Name;
46      set {
47        Item.Name = value;
48        OnPropertyChange(this, nameof(Name));
49      }
50    }
51    public string Description {
52      get => Item.Description;
53      set {
54        Item.Description = value;
55        OnPropertyChange(this, nameof(Description));
56      }
57    }
58
59    public abstract Type TargetedJsonItemType { get; }
60    public abstract UserControl Control { get; }
61
62    public event PropertyChangedEventHandler PropertyChanged;
63    public event Action ItemChanged;
64
65
66    protected void OnPropertyChange(object sender, string propertyName) {
67      // Make a temporary copy of the event to avoid possibility of
68      // a race condition if the last subscriber unsubscribes
69      // immediately after the null check and before the event is raised.
70      // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-raise-base-class-events-in-derived-classes
71     
72      PropertyChangedEventHandler tmp = PropertyChanged;
73      tmp?.Invoke(this, new PropertyChangedEventArgs(propertyName));
74    }
75   
76    #region IDisposable Support
77    private bool disposedValue = false; // To detect redundant calls
78
79    protected virtual void Dispose(bool disposing) {
80      if (!disposedValue) {
81        if (disposing) {
82          item.LoosenPath();
83          item = null;
84        }
85        disposedValue = true;
86      }
87    }
88
89    // This code added to correctly implement the disposable pattern.
90    public void Dispose() {
91      // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
92      Dispose(true);
93    }
94    #endregion
95  }
96}
Note: See TracBrowser for help on using the repository browser.