Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • refactored inheritance structure of json items, now the default JsonItem is an abstract class without properties Value and Range -> splitted up into new interfaces
  • updated view models for new json item structure
  • updated SingleLineArrayJsonWriter
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    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.TargetedJsonItemType, 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             
105            } else {
106              TreeNode childNode = new TreeNode(c.Name);
107              node.Nodes.Add(childNode);
108              BuildTreeNode(childNode, c);
109            }
110          }
111        }
112      }
113    }
114
115    private void RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
116      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) { // TODO: enhance for interfaces?
117        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
118
119        vm.Item = item;
120        vm.TreeNode = node;
121        vm.TreeView = tv;
122        vm.Selected = false;
123
124        VMs.Add(vm);
125        Node2VM.Add(node, vm);
126        UserControl control = new JsonItemBaseControl(vm, vm.Control);
127        Node2Control.Add(node, control);
128      } else {
129        node.ForeColor = Color.LightGray;
130        node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
131      }
132    }
133
134    private bool IsDrawableItem(IJsonItem item) {
135      bool b = false;
136      if (item.Children != null) {
137        foreach (var c in item.Children) {
138          b = b || IsDrawableItem(c);
139        }
140      }
141     
142      return b || !(item is EmptyJsonItem);
143    }
144   
145    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
146      if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
147        SetControlOnPanel(control, panelParameterDetails);
148      }
149    }
150
151    private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e) {
152      if (Node2Control.TryGetValue(treeViewResults.SelectedNode, out UserControl control)) {
153        SetControlOnPanel(control, panelResultDetails);
154      }
155    }
156
157    private void textBoxTemplateName_Validating(object sender, CancelEventArgs e) {
158      if (string.IsNullOrWhiteSpace(textBoxTemplateName.Text)) {
159        errorProvider.SetError(textBoxTemplateName, "Template name must not be empty.");
160        e.Cancel = true;
161      } else {
162        errorProvider.SetError(textBoxTemplateName, null);
163      }
164    }
165
166    private void SetControlOnPanel(UserControl control, Panel panel) {
167      panel.Controls.Clear();
168      if (control != null) {
169        panel.Controls.Add(control);
170        control.Width = panel.Width;
171        control.Height = panel.Height;
172        control.Dock = DockStyle.Fill;
173        control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
174      }
175      panel.Refresh();
176    }
177
178    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
179      if (e.Action != TreeViewAction.Unknown) {
180        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
181          vm.Selected = e.Node.Checked;
182        }
183      }
184    }
185
186    private void treeViewResults_AfterCheck(object sender, TreeViewEventArgs e) {
187      if (e.Action != TreeViewAction.Unknown) {
188        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
189          vm.Selected = e.Node.Checked;
190        }
191      }
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.