Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026 code cleanup

File size: 4.1 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
25    private IContent content;
26    public IContent Content {
27      get => content;
28      set {
29        content = value;
30
31        VMs = new List<JsonItemVMBase>();
32        treeView.Nodes.Clear();
33       
34        Optimizer = content as IOptimizer;
35        Root = JsonItemConverter.Extract(Optimizer);
36        TreeNode parent = new TreeNode(Root.Name);
37     
38        BuildTreeNode(parent, Root);
39        treeView.Nodes.Add(parent);
40      }
41    }
42
43    private IDictionary<Type, Type> JI2VM { get; set; }
44
45
46    private void InitCache() {
47      JI2VM = new Dictionary<Type, Type>();
48      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(JsonItemVMBase))) {
49        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
50        JI2VM.Add(vm.JsonItemType, vmType);
51      }
52    }
53
54    public ExportJsonDialog() {
55      InitializeComponent();
56      InitCache();
57    }
58
59    private void exportButton_Click(object sender, EventArgs e) {
60      foreach(var x in VMs) {
61        if (!x.Selected) {
62          x.Item.Parent.Children.Remove(x.Item);
63        }
64      }
65
66      if (SaveFileDialog == null) {
67        SaveFileDialog = new SaveFileDialog();
68        SaveFileDialog.Title = "Export .json-Template";
69        SaveFileDialog.DefaultExt = "json";
70        SaveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
71        SaveFileDialog.FilterIndex = 1;
72      }
73     
74      SaveFileDialog.FileName = "template";
75
76      if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
77        File.WriteAllText(SaveFileDialog.FileName, Generator.GenerateTemplate(Root, Optimizer));
78      }
79
80      Close();
81    }
82
83    private void BuildTreeNode(TreeNode node, IJsonItem item) {
84      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
85        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
86        VMs.Add(vm);
87        vm.Item = item;
88        UserControl control = vm.GetControl();
89        if (control != null) {
90          control.Dock = DockStyle.Fill;
91          control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
92        }
93        Hash2Control.Add(node.GetHashCode(), control);
94        if (item.Children != null) {
95          foreach (var c in item.Children) {
96            if (IsDrawableItem(c)) {
97              if (c is ResultItem) {
98
99              } else {
100                TreeNode childNode = new TreeNode(c.Name);
101                node.Nodes.Add(childNode);
102                BuildTreeNode(childNode, c);
103              }
104            }
105          }
106        }
107      }
108    }
109
110    private bool IsDrawableItem(IJsonItem item) {
111      bool b = false;
112      if (item.Children != null) {
113        foreach (var c in item.Children) {
114          b = b || IsDrawableItem(c);
115        }
116      }
117     
118      return b || (item.Value != null || item.Range != null || item.ActualName != null);
119    }
120   
121    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
122      if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl ctrl)) {
123        panel.Controls.Clear();
124        if (ctrl != null) {
125          panel.Controls.Add(ctrl);
126        }
127        panel.Refresh();
128      }
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.