Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs @ 17471

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

#3026:

  • deleted INamedMatrixJsonItem and all corresponding classes/views, because of bad design
  • added ILookupJsonItem and IValueLookupJsonItem (incl. all corresponding implementations, VMs, Views)
  • added IResultJsonItem
  • changed type of property Control from JsonItemBaseControl to UserControl in IJsonItemVM (because the details control now builds up with linked user controls -> allows better construction of dynamic controls)
  • added all properties of INamedMatrixJsonItem in IMatrixJsonItem
  • refactored a lot of views for better usage (TableLayoutPanel is used a lot now -> for better item positioning)
  • property ActualName is now located in ILookupJsonItem instead of IJsonItem
File size: 6.5 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.IO;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11using System.Windows.Forms;
12using HeuristicLab.Common;
13using HeuristicLab.Optimization;
14using HeuristicLab.PluginInfrastructure;
15
16namespace HeuristicLab.JsonInterface.OptimizerIntegration {
17  public partial class ExportJsonDialog : Form {
18    private static FolderBrowserDialog FolderBrowserDialog { get; set; }
19    private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
20    private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
21    private IDictionary<Type, Type> JI2VM { get; set; }
22    private IJsonItem Root { get; set; }
23    private IOptimizer Optimizer { get; set; }
24    private IList<IJsonItemVM> VMs { get; set; }
25    private JCGenerator Generator { get; set; } = new JCGenerator();
26
27    private IContent content;
28    public IContent Content {
29      get => content;
30      set {
31        content = value;
32
33        VMs = new List<IJsonItemVM>();
34        treeView.Nodes.Clear();
35        treeViewResults.Nodes.Clear();
36
37        Optimizer = content as IOptimizer;
38        Root = JsonItemConverter.Extract(Optimizer);
39        TreeNode parent = new TreeNode(Root.Name);
40        treeView.AfterCheck += TreeView_AfterCheck;
41        BuildTreeNode(parent, Root);
42        treeView.Nodes.Add(parent);
43        treeView.ExpandAll();
44       
45      }
46    }
47
48   
49
50
51    private void InitCache() {
52      JI2VM = new Dictionary<Type, Type>();
53      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
54        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
55        JI2VM.Add(vm.JsonItemType, vmType);
56      }
57    }
58
59    public ExportJsonDialog() {
60      InitializeComponent();
61      InitCache();
62    }
63
64    private void exportButton_Click(object sender, EventArgs e) {
65      // to set default value for disabled items
66      JsonItemConverter.Inject(Optimizer, Root);
67
68      // clear all runs
69      Optimizer.Runs.Clear();
70     
71      IList<IJsonItem> faultyItems = new List<IJsonItem>();
72     
73      if (!Root.GetValidator().Validate(ref faultyItems)) {
74        IList<Exception> list = new List<Exception>();
75        //print faultyItems
76        foreach (var x in faultyItems) {
77          list.Add(new Exception($"Combination of value and range is not valid for {x.Name}"));
78        }
79        ErrorHandling.ShowErrorDialog(this, new AggregateException(list));
80      } else {
81        if (FolderBrowserDialog == null) {
82          FolderBrowserDialog = new FolderBrowserDialog();
83          FolderBrowserDialog.Description = "Select .json-Template Dictionary";
84        }
85
86        if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
87          Generator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root);
88          Close();
89        }
90      }
91    }
92
93    private void BuildTreeNode(TreeNode node, IJsonItem item) {
94      RegisterItem(node, item, treeView);
95      if (item.Children != null) {
96        foreach (var c in item.Children) {
97          if (IsDrawableItem(c)) {
98            if (c is IResultJsonItem) {
99              TreeNode childNode = new TreeNode(c.Name);
100              treeViewResults.Nodes.Add(childNode);
101              RegisterItem(childNode, c, treeViewResults);
102              if(Node2VM.TryGetValue(childNode, out IJsonItemVM vm))
103                vm.Selected = true;
104            } else {
105              TreeNode childNode = new TreeNode(c.Name);
106              node.Nodes.Add(childNode);
107              BuildTreeNode(childNode, c);
108            }
109          }
110        }
111      }
112    }
113
114    private void RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
115      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) { // TODO: enhance for interfaces?
116        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
117
118        vm.Item = item;
119        vm.TreeNode = node;
120        vm.TreeView = tv;
121        vm.Selected = false;
122
123        VMs.Add(vm);
124        Node2VM.Add(node, vm);
125        UserControl control = new JsonItemBaseControl(vm, vm.Control);
126        Node2Control.Add(node, control);
127      } else {
128        //node.
129      }
130    }
131
132    private bool IsDrawableItem(IJsonItem item) {
133      bool b = false;
134      if (item.Children != null) {
135        foreach (var c in item.Children) {
136          b = b || IsDrawableItem(c);
137        }
138      }
139     
140      return b || (item.Value != null || item.Range != null || item is ILookupJsonItem || item is IResultJsonItem);
141    }
142   
143    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
144      if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
145        SetControlOnPanel(control, panelParameterDetails);
146      }
147    }
148
149    private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e) {
150      if (Node2Control.TryGetValue(treeViewResults.SelectedNode, out UserControl control)) {
151        SetControlOnPanel(control, panelResultDetails);
152      }
153    }
154
155    private void textBoxTemplateName_Validating(object sender, CancelEventArgs e) {
156      if (string.IsNullOrWhiteSpace(textBoxTemplateName.Text)) {
157        errorProvider.SetError(textBoxTemplateName, "Template name must not be empty.");
158        e.Cancel = true;
159      } else {
160        errorProvider.SetError(textBoxTemplateName, null);
161      }
162    }
163
164    private void SetControlOnPanel(UserControl control, Panel panel) {
165      panel.Controls.Clear();
166      if (control != null) {
167        panel.Controls.Add(control);
168        control.Width = panel.Width;
169        control.Height = panel.Height;
170        control.Dock = DockStyle.Fill;
171        control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
172      }
173      panel.Refresh();
174    }
175
176    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
177      if (e.Action != TreeViewAction.Unknown) {
178        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
179          vm.Selected = e.Node.Checked;
180        }
181      }
182    }
183
184    private void treeViewResults_AfterCheck(object sender, TreeViewEventArgs e) {
185      if (e.Action != TreeViewAction.Unknown) {
186        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
187          vm.Selected = e.Node.Checked;
188        }
189      }
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.