Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • set read-only to false for name input (in JsonItemBaseControl)
  • result items now can be selected/deselected in export dialog
  • removed metadata.problem property and renamed metadata.optimizer property to TemplateName in json template
  • fixed bug with wrong description for result items in AlgorithmConverter
  • refactored a lot of code in JCGenerator and added the need for a directory path (location for the template files)
  • fixed a null reference bug in JsonItemValidator (cannot try to iterate through possible null referenced collections)
File size: 4.8 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 SaveFileDialog SaveFileDialog { get; set; }
19    private IDictionary<int, UserControl> Hash2Control { get; set; } = new Dictionary<int, UserControl>();
20    private IJsonItem Root { get; set; }
21    private IOptimizer Optimizer { get; set; }
22    private IList<JsonItemVMBase> VMs { get; set; }
23    private JCGenerator Generator { get; set; } = new JCGenerator();
24    private IDictionary<string, IJsonItem> ResultItems = new Dictionary<string, IJsonItem>();
25
26    private IContent content;
27    public IContent Content {
28      get => content;
29      set {
30        content = value;
31
32        VMs = new List<JsonItemVMBase>();
33        treeView.Nodes.Clear();
34        ResultItems.Clear();
35       
36        Optimizer = content as IOptimizer;
37        Root = JsonItemConverter.Extract(Optimizer);
38        TreeNode parent = new TreeNode(Root.Name);
39
40        BuildTreeNode(parent, Root);
41        treeView.Nodes.Add(parent);
42        treeView.ExpandAll();
43       
44      }
45    }
46
47    private IDictionary<Type, Type> JI2VM { get; set; }
48
49    private void InitCache() {
50      JI2VM = new Dictionary<Type, Type>();
51      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(JsonItemVMBase))) {
52        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
53        JI2VM.Add(vm.JsonItemType, vmType);
54      }
55    }
56
57    public ExportJsonDialog() {
58      InitializeComponent();
59      InitCache();
60    }
61
62    private void exportButton_Click(object sender, EventArgs e) {
63      foreach(var x in VMs) {
64        if (!x.Selected) {
65          x.Item.Parent.Children.Remove(x.Item);
66        }
67      }
68
69      foreach(var x in ResultItems.Values) {
70        x.Parent.Children.Remove(x);
71      }
72
73      foreach(var x in resultItems.CheckedItems) {
74        if(ResultItems.TryGetValue((string)x, out IJsonItem item)) {
75          Root.AddChildren(item);
76        }
77      }
78
79      IList<IJsonItem> faultyItems = new List<IJsonItem>();
80      if(!Root.GetValidator().Validate(ref faultyItems)) {
81        //print faultyItems
82      }
83
84      if (SaveFileDialog == null) {
85        SaveFileDialog = new SaveFileDialog();
86        SaveFileDialog.Title = "Export .json-Template";
87        SaveFileDialog.DefaultExt = "json";
88        SaveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
89        SaveFileDialog.FilterIndex = 1;
90      }
91     
92      SaveFileDialog.FileName = "template";
93
94      if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
95        File.WriteAllText(SaveFileDialog.FileName, Generator.GenerateTemplate(Root, Optimizer));
96      }
97
98      Close();
99    }
100
101    private void BuildTreeNode(TreeNode node, IJsonItem item) {
102      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
103        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
104        VMs.Add(vm);
105        vm.Item = item;
106        vm.TreeNode = node;
107        vm.TreeView = treeView;
108        UserControl control = vm.GetControl();
109        Hash2Control.Add(node.GetHashCode(), control);
110        if (item.Children != null) {
111          foreach (var c in item.Children) {
112            if (IsDrawableItem(c)) {
113              if (c is ResultItem) {
114                resultItems.Items.Add(c.Name, true);
115                ResultItems.Add(c.Name, c);
116              } else {
117                TreeNode childNode = new TreeNode(c.Name);
118                node.Nodes.Add(childNode);
119                BuildTreeNode(childNode, c);
120              }
121            }
122          }
123        }
124      }
125    }
126
127    private bool IsDrawableItem(IJsonItem item) {
128      bool b = false;
129      if (item.Children != null) {
130        foreach (var c in item.Children) {
131          b = b || IsDrawableItem(c);
132        }
133      }
134     
135      return b || (item.Value != null || item.Range != null || item.ActualName != null);
136    }
137   
138    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
139      if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl control)) {
140        panel.Controls.Clear();
141        if (control != null) {
142          panel.Controls.Add(control);
143          control.Width = panel.Width;
144          control.Dock = DockStyle.Fill;
145          control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
146        }
147        panel.Refresh();
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.