Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • removed unnecessary code in FileManager
  • updated ExportJsonDialog and Testprogram (Heuristiclab.ConfigStarter/Program.cs) to use the new GenerateTemplate method
File size: 4.9 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 { get; set; } = 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        resultItems.Items.Clear();
36
37        Optimizer = content as IOptimizer;
38        Root = JsonItemConverter.Extract(Optimizer);
39        TreeNode parent = new TreeNode(Root.Name);
40
41        BuildTreeNode(parent, Root);
42        treeView.Nodes.Add(parent);
43        treeView.ExpandAll();
44       
45      }
46    }
47
48    private IDictionary<Type, Type> JI2VM { get; set; }
49
50    private void InitCache() {
51      JI2VM = new Dictionary<Type, Type>();
52      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(JsonItemVMBase))) {
53        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
54        JI2VM.Add(vm.JsonItemType, vmType);
55      }
56    }
57
58    public ExportJsonDialog() {
59      InitializeComponent();
60      InitCache();
61    }
62
63    private void exportButton_Click(object sender, EventArgs e) {
64      foreach(var x in VMs) {
65        if (!x.Selected) {
66          x.Item.Parent.Children.Remove(x.Item);
67        }
68      }
69
70      foreach(var x in ResultItems.Values) {
71        x.Parent.Children.Remove(x);
72      }
73
74      foreach(var x in resultItems.CheckedItems) {
75        if(ResultItems.TryGetValue((string)x, out IJsonItem item)) {
76          Root.AddChildren(item);
77        }
78      }
79
80      IList<IJsonItem> faultyItems = new List<IJsonItem>();
81      if(!Root.GetValidator().Validate(ref faultyItems)) {
82        //print faultyItems
83      }
84
85      if (SaveFileDialog == null) {
86        SaveFileDialog = new SaveFileDialog();
87        SaveFileDialog.Title = "Export .json-Template";
88        SaveFileDialog.DefaultExt = "json";
89        SaveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
90        SaveFileDialog.FilterIndex = 1;
91      }
92     
93      SaveFileDialog.FileName = "template";
94
95      if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
96        Generator.GenerateTemplate(@"C:\Users\p41997\Desktop", "template", Optimizer, Root);
97      }
98
99      Close();
100    }
101
102    private void BuildTreeNode(TreeNode node, IJsonItem item) {
103      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
104        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
105        VMs.Add(vm);
106        vm.Item = item;
107        vm.TreeNode = node;
108        vm.TreeView = treeView;
109        UserControl control = vm.GetControl();
110        Hash2Control.Add(node.GetHashCode(), control);
111        if (item.Children != null) {
112          foreach (var c in item.Children) {
113            if (IsDrawableItem(c)) {
114              if (c is ResultItem) {
115                resultItems.Items.Add(c.Name, true);
116                ResultItems.Add(c.Name, c);
117              } else {
118                TreeNode childNode = new TreeNode(c.Name);
119                node.Nodes.Add(childNode);
120                BuildTreeNode(childNode, c);
121              }
122            }
123          }
124        }
125      }
126    }
127
128    private bool IsDrawableItem(IJsonItem item) {
129      bool b = false;
130      if (item.Children != null) {
131        foreach (var c in item.Children) {
132          b = b || IsDrawableItem(c);
133        }
134      }
135     
136      return b || (item.Value != null || item.Range != null || item.ActualName != null);
137    }
138   
139    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
140      if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl control)) {
141        panel.Controls.Clear();
142        if (control != null) {
143          panel.Controls.Add(control);
144          control.Width = panel.Width;
145          control.Dock = DockStyle.Fill;
146          control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
147        }
148        panel.Refresh();
149      }
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.