Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • made interfaces for array/matrix JsonItems and for their VMs aswell (IArrayJsonItem, IArrayJsonItemVM, IMatrixJsonItem, IMatrixJsonItemVM), incl. base classes (ArrayJsonItemBase, ArrayValueVM, MatrixJsonItemBase, MatrixValueVM)
  • changed inheritance structure for already existing array/matrix JsonItems -> they inherit now from new base array/matrix base classes
  • added input elements to configure the size of an matrix or array in JsonItemMultiValueControl (incl. VM binding and validation)
  • splitted file JsonItems.cs into separate files for their corresponding types (IntJsonItems.cs, DoubleJsonItems.cs, BoolJsonItems.cs, StringJsonItem.cs, DateTimeJsonItem.cs)
  • changed location of deserialization of json values from JsonTemplateInstantiator into IJsonItem (implemented in JsonItem and set to virtual, overridden in MatrixJsonItemBase and ArrayJsonItemBase)
  • added new CLI argument StringArgument
  • some little UI improvements (location fixes, anchor fixes, ...)
File size: 2.9 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 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}
Note: See TracBrowser for help on using the repository browser.