Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • fixed a bug with JsonItemMultiValueControl -> the size of the matrix should now be saved correctly
  • fixed a bug with RegressionProblemDataConverter -> should now set the row/col sizes correctly
  • simplified the code for saving matrix data in JsonItemMultiValueControl, MatrixValueVM and ArrayValueVM
  • removed unnecessary casts
File size: 6.6 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
19    #region Private Properties
20    private static FolderBrowserDialog FolderBrowserDialog { get; set; }
21    private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
22    private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
23    private IDictionary<Type, Type> JI2VM { get; set; }
24    private IJsonItem Root { get; set; }
25    private IOptimizer Optimizer { get; set; }
26    private IList<IJsonItemVM> VMs { get; set; }
27    #endregion
28
29    private IContent content;
30    public IContent Content {
31      get => content;
32      set {
33        content = value;
34
35        #region Clear
36        VMs = new List<IJsonItemVM>();
37        treeView.Nodes.Clear();
38        treeViewResults.Nodes.Clear();
39        #endregion
40
41        Optimizer = content as IOptimizer;
42        Root = JsonItemConverter.Extract(Optimizer);
43        TreeNode parent = new TreeNode(Root.Name);
44        treeView.AfterCheck += TreeView_AfterCheck;
45        BuildTreeNode(parent, Root);
46        treeView.Nodes.Add(parent);
47        treeView.ExpandAll();
48        panelParameterDetails.Controls.Clear();
49        panelResultDetails.Controls.Clear();
50
51      }
52    }
53
54    private void InitCache() {
55      JI2VM = new Dictionary<Type, Type>();
56      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
57        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
58        JI2VM.Add(vm.TargetedJsonItemType, vmType);
59      }
60    }
61
62    public ExportJsonDialog() {
63      InitializeComponent();
64      InitCache();
65    }
66
67    private void exportButton_Click(object sender, EventArgs e) {
68      // to set default value for disabled items
69      JsonItemConverter.Inject(Optimizer, Root);
70
71      // clear all runs
72      Optimizer.Runs.Clear();
73
74      var validationResult = Root.GetValidator().Validate();
75      if (!validationResult.Success) {
76        IList<Exception> list = new List<Exception>();
77        //print faultyItems
78        foreach (var x in validationResult.Errors) {
79          list.Add(new Exception(x));
80        }
81        ErrorHandling.ShowErrorDialog(this, new AggregateException(list));
82      } else {
83        if (FolderBrowserDialog == null) {
84          FolderBrowserDialog = new FolderBrowserDialog();
85          FolderBrowserDialog.Description = "Select .json-Template Dictionary";
86        }
87
88        if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
89          JCGenerator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root);
90          Close();
91        }
92      }
93    }
94
95    private void BuildTreeNode(TreeNode node, IJsonItem item) {
96      RegisterItem(node, item, treeView);
97      if (item.Children != null) {
98        foreach (var c in item.Children) {
99          if (IsDrawableItem(c)) {
100            if (c is IResultJsonItem) {
101              TreeNode childNode = new TreeNode(c.Name);
102              treeViewResults.Nodes.Add(childNode);
103              RegisterItem(childNode, c, treeViewResults);
104              if(Node2VM.TryGetValue(childNode, out IJsonItemVM vm))
105                vm.Selected = true;
106             
107            } else {
108              TreeNode childNode = new TreeNode(c.Name);
109              node.Nodes.Add(childNode);
110              BuildTreeNode(childNode, c);
111            }
112          }
113        }
114      }
115    }
116
117    private void RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
118      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) { // TODO: enhance for interfaces?
119        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
120
121        vm.Item = item;
122        vm.TreeNode = node;
123        vm.TreeView = tv;
124        vm.Selected = false;
125
126        VMs.Add(vm);
127        Node2VM.Add(node, vm);
128        UserControl control = new JsonItemBaseControl(vm, vm.Control);
129        Node2Control.Add(node, control);
130      } else {
131        node.ForeColor = Color.LightGray;
132        node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
133      }
134    }
135
136    private bool IsDrawableItem(IJsonItem item) {
137      bool b = false;
138      if (item.Children != null) {
139        foreach (var c in item.Children) {
140          b = b || IsDrawableItem(c);
141        }
142      }
143     
144      return b || !(item is EmptyJsonItem);
145    }
146   
147    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
148      if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
149        SetControlOnPanel(control, panelParameterDetails);
150      }
151    }
152
153    private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e) {
154      if (Node2Control.TryGetValue(treeViewResults.SelectedNode, out UserControl control)) {
155        SetControlOnPanel(control, panelResultDetails);
156      }
157    }
158
159    private void textBoxTemplateName_Validating(object sender, CancelEventArgs e) {
160      if (string.IsNullOrWhiteSpace(textBoxTemplateName.Text)) {
161        errorProvider.SetError(textBoxTemplateName, "Template name must not be empty.");
162        e.Cancel = true;
163      } else {
164        errorProvider.SetError(textBoxTemplateName, null);
165      }
166    }
167
168    private void SetControlOnPanel(UserControl control, Panel panel) {
169      panel.Controls.Clear();
170      if (control != null) {
171        panel.Controls.Add(control);
172        control.Width = panel.Width;
173        control.Height = panel.Height;
174        control.Dock = DockStyle.Fill;
175        control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
176      }
177      panel.Refresh();
178    }
179
180    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
181      if (e.Action != TreeViewAction.Unknown) {
182        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
183          vm.Selected = e.Node.Checked;
184        }
185      }
186    }
187
188    private void treeViewResults_AfterCheck(object sender, TreeViewEventArgs e) {
189      if (e.Action != TreeViewAction.Unknown) {
190        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
191          vm.Selected = e.Node.Checked;
192        }
193      }
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.