Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs @ 17284

Last change on this file since 17284 was 17284, checked in by dpiringe, 3 years ago

#3026: renamed namespace and project from HeuristicLab.Manufacture to HeuristicLab.JsonInterface

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.JsonInterface {
9  public class MultiCheckedOperatorConverter : ParameterizedItemConverter {
10    public override JsonItem ExtractData(IItem value) {
11      JsonItem data = base.ExtractData(value);
12
13      data.Default = value.GetType().Name;
14      data.Operators = new List<JsonItem>();
15      dynamic val = value.Cast<dynamic>();
16      foreach (var op in val.Operators) {
17        data.Operators.Add(new JsonItem() {
18          Name = op.Name,
19          Default = val.Operators.ItemChecked(op),
20          Range = new object[] { false, true },
21          Path = data.Path + "." + op.Name
22        });
23      }
24      return data;
25    }
26
27    public override void InjectData(IItem item, JsonItem data) {
28      base.InjectData(item, data);
29
30      dynamic val = item.Cast<dynamic>();
31      foreach (var op in val.Operators) {
32        val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data));
33      }
34    }
35
36    private bool GetOperatorState(string name, JsonItem data) {
37      foreach(var op in data.Operators) {
38        if (op.Name == name) return op.Default.Cast<bool>();
39      }
40      return false;
41    }
42  }
43}
Note: See TracBrowser for help on using the repository browser.