Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17431


Ignore:
Timestamp:
02/10/20 16:46:09 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • renamed JsonItemArrayValueControl to JsonItemMultiValueControl
  • extracted converter logic from property Value in JsonItem<V,R> into new private method ConvertObject
  • added references to TreeNode and TreeView in JsonItemVMBase -> for additional user feedback when enabling/disabling an item (changes the fore color of referenced node)
  • changed implementation and interplay between ArrayValueVM and JsonItemMultiValueControl -> removed the direct access of property DataSource of DataGridView -> now: dynamically adds new columns/rows and accesses grid cells manually (better handling with matrices)
  • added new VM MatrixValueVM
Location:
branches/3026_IntegrationIntoSymSpace
Files:
1 added
6 edited
3 moved

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/HeuristicLab.JsonInterface.OptimizerIntegration.csproj

    r17420 r17431  
    9797    <Compile Include="ViewModels\ArrayValueVM.cs" />
    9898    <Compile Include="ViewModels\JsonItemVMBase.cs" />
     99    <Compile Include="ViewModels\MatrixValueVM.cs" />
    99100    <Compile Include="ViewModels\RangedValueBaseVM.cs" />
    100101    <Compile Include="ViewModels\RangeVM.cs" />
     
    108109    </Compile>
    109110    <Compile Include="FileManager.cs" />
    110     <Compile Include="Views\JsonItemArrayValueControl.cs">
    111       <SubType>UserControl</SubType>
    112     </Compile>
    113     <Compile Include="Views\JsonItemArrayValueControl.Designer.cs">
    114       <DependentUpon>JsonItemArrayValueControl.cs</DependentUpon>
     111    <Compile Include="Views\JsonItemMultiValueControl.cs">
     112      <SubType>UserControl</SubType>
     113    </Compile>
     114    <Compile Include="Views\JsonItemMultiValueControl.Designer.cs">
     115      <DependentUpon>JsonItemMultiValueControl.cs</DependentUpon>
    115116    </Compile>
    116117    <Compile Include="Views\JsonItemBoolControl.cs">
     
    199200      <DependentUpon>ExportJsonDialog.cs</DependentUpon>
    200201    </EmbeddedResource>
    201     <EmbeddedResource Include="Views\JsonItemArrayValueControl.resx">
    202       <DependentUpon>JsonItemArrayValueControl.cs</DependentUpon>
     202    <EmbeddedResource Include="Views\JsonItemMultiValueControl.resx">
     203      <DependentUpon>JsonItemMultiValueControl.cs</DependentUpon>
    203204    </EmbeddedResource>
    204205    <EmbeddedResource Include="Views\JsonItemBoolControl.resx">
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Shared/JsonItemBaseControl.cs

    r17417 r17431  
    2121      VM = vm;
    2222
    23       checkBoxActive.DataBindings.Add("Checked", VM, nameof(JsonItemVMBase.Selected));
     23      checkBoxActive.DataBindings.Add("Checked", VM, nameof(JsonItemVMBase.Selected),
     24        false, DataSourceUpdateMode.OnPropertyChanged);
    2425      textBoxName.DataBindings.Add("Text", VM, nameof(JsonItemVMBase.Name));
    2526      textBoxActualName.DataBindings.Add("Text", VM, nameof(JsonItemVMBase.ActualName));
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/ArrayValueVM.cs

    r17420 r17431  
    1616
    1717    public override JsonItemBaseControl GetControl() => new JsonItemDoubleArrayValueControl(this);
    18 
    19     protected override void OnItemChanged() {
    20       IList<IndexedArrayValueVM> list = new List<IndexedArrayValueVM>();
    21       var arr = ((DoubleArrayJsonItem)Item).Value;
    22       for(int i = 0; i < arr.Length; ++i) {
    23         list.Add(new IndexedArrayValueVM(arr, i));
     18   
     19    public override double[] Value {
     20      get => ((DoubleArrayJsonItem)Item).Value;
     21      set {
     22        ((DoubleArrayJsonItem)Item).Value = value;
     23        OnPropertyChange(this, nameof(Value));
    2424      }
    25       Value = list.ToArray();
    2625    }
    2726  }
     
    3534
    3635    public override JsonItemBaseControl GetControl() => new JsonItemIntArrayValueControl(this);
    37 
    38     protected override void OnItemChanged() {
    39       IList<IndexedArrayValueVM> list = new List<IndexedArrayValueVM>();
    40       var arr = ((IntArrayJsonItem)Item).Value;
    41       for (int i = 0; i < arr.Length; ++i) {
    42         list.Add(new IndexedArrayValueVM(arr, i));
     36   
     37    public override int[] Value {
     38      get => ((IntArrayJsonItem)Item).Value;
     39      set {
     40        ((IntArrayJsonItem)Item).Value = value;
     41        OnPropertyChange(this, nameof(Value));
    4342      }
    44       Value = list.ToArray();
    4543    }
    4644  }
    4745
    4846  public abstract class ArrayValueVM<T> : RangedValueBaseVM<T> {
    49 
    50     public class IndexedArrayValueVM : INotifyPropertyChanged {
    51       private T[] arr;
    52       private int index;
    53 
    54       public IndexedArrayValueVM(T[] arr, int index) {
    55         this.arr = arr;
    56         this.index = index;
    57       }
    58 
    59       public T Value {
    60         get => arr[index];
    61         set {
    62           arr[index] = value;
    63           PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
    64         }
    65       }
    66 
    67       public event PropertyChangedEventHandler PropertyChanged;
     47   
     48    public ArrayValueVM() {
     49      //base.ItemChanged += OnItemChanged;
    6850    }
    6951
    70     public ArrayValueVM() {
    71       base.ItemChanged += OnItemChanged;
     52    public void SetIndexValue(object obj, int index) {
     53      T[] tmp = Value;
     54      if(index >= tmp.Length) { // increasing array
     55        T[] newArr = new T[index+1];
     56        Array.Copy(tmp, 0, newArr, 0, tmp.Length);
     57        tmp = newArr;
     58      }
     59      tmp[index] = (T)Convert.ChangeType(obj, typeof(T));
     60      Value = tmp;
    7261    }
    7362
    74     protected abstract void OnItemChanged();
    75 
    76     IndexedArrayValueVM[] vms;
    77     public IndexedArrayValueVM[] Value {
    78       get => vms;
    79       set {
    80         vms = value;
    81         OnPropertyChange(this, nameof(Value));
    82       }
    83     }
     63    public abstract T[] Value { get; set; }
    8464  }
    8565}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/JsonItemVMBase.cs

    r17420 r17431  
    22using System.Collections.Generic;
    33using System.ComponentModel;
     4using System.Drawing;
    45using System.Linq;
    56using System.Text;
    67using System.Threading.Tasks;
     8using System.Windows.Forms;
    79
    810namespace HeuristicLab.JsonInterface.OptimizerIntegration {
     
    1921      }
    2022    }
     23
     24    public TreeNode TreeNode { get; set; }
     25    public TreeView TreeView { get; set; }
    2126
    2227    protected void OnPropertyChange(object sender, string propertyName) {
     
    3742      set {
    3843        selected = value;
     44        if(TreeNode != null)
     45          TreeNode.ForeColor = (selected ? Color.Black : Color.Red);
     46        if (TreeView != null)
     47          TreeView.Refresh();
    3948        OnPropertyChange(this, nameof(Selected));
    4049      }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs

    r17417 r17431  
    8585        VMs.Add(vm);
    8686        vm.Item = item;
     87        vm.TreeNode = node;
     88        vm.TreeView = treeView;
    8789        UserControl control = vm.GetControl();
    8890        if (control != null) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemMultiValueControl.Designer.cs

    r17430 r17431  
    11namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    2   partial class JsonItemArrayValueControl {
     2  partial class JsonItemMultiValueControl<T> {
    33    /// <summary>
    44    /// Required designer variable.
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemMultiValueControl.cs

    r17430 r17431  
    1010
    1111namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    12  
    13   public class JsonItemIntArrayValueControl : JsonItemArrayValueControl {
    14     public JsonItemIntArrayValueControl(IntArrayValueVM vm) : base(vm, vm.Value) { }
     12
     13  public class JsonItemDoubleMatrixValueControl : JsonItemMultiValueControl<double> {
     14
     15    public JsonItemDoubleMatrixValueControl(DoubleMatrixValueVM vm) : base(vm, vm.Value) { }
     16    protected override void SaveCellData(object data, int col, int row) {
     17      DoubleMatrixValueVM vm = VM as DoubleMatrixValueVM;
     18      vm.SetCellValue(data, col, row);
     19    }
    1520  }
    1621
    17   public class JsonItemDoubleArrayValueControl : JsonItemArrayValueControl {
     22  public class JsonItemIntArrayValueControl : JsonItemMultiValueControl<int> {
     23    public JsonItemIntArrayValueControl(IntArrayValueVM vm) : base(vm, vm.Value) { }
     24
     25    protected override void SaveCellData(object data, int col, int row) {
     26      IntArrayValueVM vm = VM as IntArrayValueVM;
     27      vm.SetIndexValue(data, row);
     28    }
     29  }
     30
     31  public class JsonItemDoubleArrayValueControl : JsonItemMultiValueControl<double> {
    1832    public JsonItemDoubleArrayValueControl(DoubleArrayValueVM vm) : base(vm, vm.Value) { }
     33
     34    protected override void SaveCellData(object data, int col, int row) {
     35      DoubleArrayValueVM vm = VM as DoubleArrayValueVM;
     36      vm.SetIndexValue(data, row);
     37    }
    1938  }
    2039 
    21   public abstract partial class JsonItemArrayValueControl : JsonItemBaseControl {
     40  public abstract partial class JsonItemMultiValueControl<T> : JsonItemBaseControl {
    2241    protected NumericRangeControl NumericRangeControl { get; set; }
    2342
    24     public JsonItemArrayValueControl(JsonItemVMBase vm, object dataSource) : base(vm) {
     43    public JsonItemMultiValueControl(JsonItemVMBase vm, T[][] matrix) : base(vm) {
    2544      InitializeComponent();
    26       dataGridView.DataSource = dataSource;
     45      int rows = matrix.Length;
     46      int cols = matrix.Max(x => x.Length);
     47
     48      // columns must added first
     49      for (int c = 0; c < cols; ++c) {
     50        dataGridView.Columns.Add($"Column {c}", $"Column {c}");
     51      }
     52
     53      dataGridView.Rows.Add(rows);
     54      for (int c = 0; c < cols; ++c) {
     55        for(int r = 0; r < rows; ++r) {
     56          dataGridView[c, r].Value = matrix[r][c];
     57        }
     58      }
     59
     60      dataGridView.CellEndEdit += DataGridView_CellEndEdit;
     61      InitRangeBinding();
     62    }
     63
     64    public JsonItemMultiValueControl(JsonItemVMBase vm, T[] array) : base(vm) {
     65      InitializeComponent();
     66      int length = array.Length;
     67
     68      dataGridView.Columns.Add("Value", "Value");
     69      dataGridView.Rows.Add(length);
     70      for(int i = 0; i < length; ++i) {
     71        dataGridView[0, i].Value = array[i];
     72      }
     73      dataGridView.CellEndEdit += DataGridView_CellEndEdit;
     74      InitRangeBinding();
     75    }
     76
     77    private void DataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
     78      SaveCellData(dataGridView[e.ColumnIndex, e.RowIndex].Value, e.ColumnIndex, e.RowIndex);
     79    }
     80
     81    protected abstract void SaveCellData(object data, int col, int row);
     82
     83    /*
     84public JsonItemMultiValueControl(JsonItemVMBase vm, object dataSource) : base(vm) {
     85 InitializeComponent();
     86 dataGridView.DataSource = dataSource;
     87
     88 InitRangeBinding();
     89}
     90*/
     91    private void InitRangeBinding() {
    2792      NumericRangeControl = numericRangeControl1;
    28 
    2993      NumericRangeControl.TBMinRange.DataBindings.Add("Text", VM, nameof(RangedValueBaseVM.MinRange));
    3094      NumericRangeControl.TBMaxRange.DataBindings.Add("Text", VM, nameof(RangedValueBaseVM.MaxRange));
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/GenericJsonItem.cs

    r17417 r17431  
    1313  public class JsonItem<V,R> : JsonItem {
    1414    public new V Value {
    15       get {
    16         if(base.Value is IConvertible)
    17           return (V)Convert.ChangeType(base.Value, typeof(V));
    18 
    19         if(base.Value is JToken token)
    20           return token.ToObject<V>();
    21 
    22         return (V)base.Value;
    23       }
     15      get => ConvertObject(base.Value);
    2416      set => base.Value = value;
    2517    }
    26 
     18   
    2719    public new IEnumerable<R> Range {
    2820      get => base.Range?.Cast<R>();
    2921      set => base.Range = value.Cast<object>();
    3022    }
     23
     24    private V ConvertObject(object obj) {
     25      if (obj is IConvertible)
     26        return (V)Convert.ChangeType(obj, typeof(V));
     27
     28      if (obj is JToken token)
     29        return token.ToObject<V>();
     30
     31      return (V)obj;
     32    }
    3133  }
    3234}
Note: See TracChangeset for help on using the changeset viewer.