Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17530 was 17484, checked in by dpiringe, 5 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
RevLine 
[17404]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;
[17410]14using HeuristicLab.PluginInfrastructure;
[17404]15
16namespace HeuristicLab.JsonInterface.OptimizerIntegration {
17  public partial class ExportJsonDialog : Form {
[17477]18
19    #region Private Properties
[17444]20    private static FolderBrowserDialog FolderBrowserDialog { get; set; }
[17451]21    private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
[17446]22    private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
[17451]23    private IDictionary<Type, Type> JI2VM { get; set; }
[17411]24    private IJsonItem Root { get; set; }
25    private IOptimizer Optimizer { get; set; }
[17446]26    private IList<IJsonItemVM> VMs { get; set; }
[17477]27    #endregion
[17411]28
[17404]29    private IContent content;
30    public IContent Content {
31      get => content;
32      set {
33        content = value;
34
[17477]35        #region Clear
[17446]36        VMs = new List<IJsonItemVM>();
[17405]37        treeView.Nodes.Clear();
[17451]38        treeViewResults.Nodes.Clear();
[17477]39        #endregion
[17436]40
[17411]41        Optimizer = content as IOptimizer;
42        Root = JsonItemConverter.Extract(Optimizer);
43        TreeNode parent = new TreeNode(Root.Name);
[17439]44        treeView.AfterCheck += TreeView_AfterCheck;
[17411]45        BuildTreeNode(parent, Root);
[17405]46        treeView.Nodes.Add(parent);
[17432]47        treeView.ExpandAll();
[17484]48        panelParameterDetails.Controls.Clear();
49        panelResultDetails.Controls.Clear();
50
[17404]51      }
52    }
53
[17410]54    private void InitCache() {
[17411]55      JI2VM = new Dictionary<Type, Type>();
[17471]56      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
57        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
[17473]58        JI2VM.Add(vm.TargetedJsonItemType, vmType);
[17410]59      }
60    }
61
[17404]62    public ExportJsonDialog() {
63      InitializeComponent();
[17410]64      InitCache();
[17404]65    }
66
67    private void exportButton_Click(object sender, EventArgs e) {
[17451]68      // to set default value for disabled items
69      JsonItemConverter.Inject(Optimizer, Root);
[17453]70
71      // clear all runs
72      Optimizer.Runs.Clear();
[17481]73
74      var validationResult = Root.GetValidator().Validate();
75      if (!validationResult.Success) {
[17444]76        IList<Exception> list = new List<Exception>();
[17435]77        //print faultyItems
[17481]78        foreach (var x in validationResult.Errors) {
79          list.Add(new Exception(x));
[17444]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        }
[17435]87
[17444]88        if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
[17477]89          JCGenerator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root);
[17446]90          Close();
[17444]91        }
[17446]92      }
93    }
[17404]94
[17446]95    private void BuildTreeNode(TreeNode node, IJsonItem item) {
[17451]96      RegisterItem(node, item, treeView);
[17446]97      if (item.Children != null) {
98        foreach (var c in item.Children) {
99          if (IsDrawableItem(c)) {
[17471]100            if (c is IResultJsonItem) {
[17446]101              TreeNode childNode = new TreeNode(c.Name);
102              treeViewResults.Nodes.Add(childNode);
[17451]103              RegisterItem(childNode, c, treeViewResults);
104              if(Node2VM.TryGetValue(childNode, out IJsonItemVM vm))
105                vm.Selected = true;
[17473]106             
[17446]107            } else {
108              TreeNode childNode = new TreeNode(c.Name);
109              node.Nodes.Add(childNode);
110              BuildTreeNode(childNode, c);
111            }
112          }
113        }
[17404]114      }
115    }
[17405]116
[17451]117    private void RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
[17471]118      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) { // TODO: enhance for interfaces?
[17446]119        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
120
[17411]121        vm.Item = item;
[17431]122        vm.TreeNode = node;
[17451]123        vm.TreeView = tv;
124        vm.Selected = false;
[17446]125
126        VMs.Add(vm);
[17439]127        Node2VM.Add(node, vm);
[17471]128        UserControl control = new JsonItemBaseControl(vm, vm.Control);
[17451]129        Node2Control.Add(node, control);
[17471]130      } else {
[17473]131        node.ForeColor = Color.LightGray;
132        node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
[17405]133      }
134    }
135
[17406]136    private bool IsDrawableItem(IJsonItem item) {
[17405]137      bool b = false;
138      if (item.Children != null) {
139        foreach (var c in item.Children) {
140          b = b || IsDrawableItem(c);
141        }
142      }
143     
[17473]144      return b || !(item is EmptyJsonItem);
[17405]145    }
146   
147    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
[17451]148      if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
[17446]149        SetControlOnPanel(control, panelParameterDetails);
[17405]150      }
151    }
[17444]152
[17451]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
[17444]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    }
[17446]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    }
[17451]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    }
[17404]195  }
196}
Note: See TracBrowser for help on using the repository browser.