[17410] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
[17431] | 4 | using System.Drawing;
|
---|
[17410] | 5 | using System.Linq;
|
---|
| 6 | using System.Text;
|
---|
| 7 | using System.Threading.Tasks;
|
---|
[17431] | 8 | using System.Windows.Forms;
|
---|
[17410] | 9 |
|
---|
| 10 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
[17519] | 11 | public abstract class JsonItemVMBase<JsonItemType> : IJsonItemVM<JsonItemType> //TODO: RENAME, oder vlt JsonItems direkt als VM?
|
---|
[17473] | 12 | where JsonItemType : class, IJsonItem
|
---|
| 13 | {
|
---|
| 14 | IJsonItem IJsonItemVM.Item {
|
---|
[17477] | 15 | get => Item;
|
---|
| 16 | set => Item = (JsonItemType)value;
|
---|
[17473] | 17 | }
|
---|
| 18 |
|
---|
| 19 | private JsonItemType item;
|
---|
| 20 | public JsonItemType Item {
|
---|
[17420] | 21 | get => item;
|
---|
| 22 | set {
|
---|
[17444] | 23 | item?.LoosenPath();
|
---|
[17420] | 24 | item = value;
|
---|
[17444] | 25 | item.FixatePath();
|
---|
[17420] | 26 | ItemChanged?.Invoke();
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
[17431] | 29 | public TreeNode TreeNode { get; set; }
|
---|
| 30 | public TreeView TreeView { get; set; }
|
---|
[17410] | 31 | public bool Selected {
|
---|
[17451] | 32 | get => Item.Active;
|
---|
[17410] | 33 | set {
|
---|
[17451] | 34 | Item.Active = value;
|
---|
[17439] | 35 | if(TreeNode != null) {
|
---|
[17473] | 36 | TreeNode.ForeColor = (Selected ? Color.Green : Color.Black);
|
---|
[17439] | 37 | TreeNode.Checked = value;
|
---|
| 38 | }
|
---|
[17431] | 39 | if (TreeView != null)
|
---|
| 40 | TreeView.Refresh();
|
---|
[17477] | 41 | SelectedChanged?.Invoke();
|
---|
[17410] | 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 | }
|
---|
[17433] | 52 | public string Description {
|
---|
| 53 | get => Item.Description;
|
---|
| 54 | set {
|
---|
| 55 | Item.Description = value;
|
---|
| 56 | OnPropertyChange(this, nameof(Description));
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[17519] | 60 | public virtual Type TargetedJsonItemType => typeof(JsonItemType);
|
---|
[17471] | 61 | public abstract UserControl Control { get; }
|
---|
| 62 |
|
---|
| 63 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 64 | public event Action ItemChanged;
|
---|
[17477] | 65 | public event Action SelectedChanged;
|
---|
[17471] | 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));
|
---|
[17410] | 76 | }
|
---|
[17446] | 77 |
|
---|
[17444] | 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
|
---|
[17410] | 97 | }
|
---|
| 98 | }
|
---|