1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.ComponentModel;
|
---|
5 | using System.Data;
|
---|
6 | using System.Drawing;
|
---|
7 | using System.IO;
|
---|
8 | using System.Linq;
|
---|
9 | using System.Text;
|
---|
10 | using System.Threading.Tasks;
|
---|
11 | using System.Windows.Forms;
|
---|
12 | using HeuristicLab.Common;
|
---|
13 | using HeuristicLab.Optimization;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
16 | public partial class ExportJsonDialog : Form {
|
---|
17 | private IContent content;
|
---|
18 | private static SaveFileDialog saveFileDialog;
|
---|
19 | private IDictionary<int, UserControl> ctrlCollection = new Dictionary<int, UserControl>();
|
---|
20 | private IJsonItem root;
|
---|
21 | private IOptimizer optimizer;
|
---|
22 | private IList<JsonItemVM> vms;
|
---|
23 | private JCGenerator generator = new JCGenerator();
|
---|
24 | public IContent Content {
|
---|
25 | get => content;
|
---|
26 | set {
|
---|
27 | content = value;
|
---|
28 |
|
---|
29 | //IEnumerable<JsonItem> items = generator.FetchJsonItems(content as IOptimizer);
|
---|
30 | vms = new List<JsonItemVM>();
|
---|
31 | treeView.Nodes.Clear();
|
---|
32 |
|
---|
33 | optimizer = content as IOptimizer;
|
---|
34 | root = JsonItemConverter.Extract(optimizer);
|
---|
35 | TreeNode parent = new TreeNode(root.Name);
|
---|
36 | BuildTreeNode(parent, root);
|
---|
37 | treeView.Nodes.Add(parent);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ExportJsonDialog() {
|
---|
42 | InitializeComponent();
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void exportButton_Click(object sender, EventArgs e) {
|
---|
46 | foreach(var x in vms) {
|
---|
47 | if (!x.Selected) {
|
---|
48 | x.Item.Parent.Children.Remove(x.Item);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (saveFileDialog == null) {
|
---|
53 | saveFileDialog = new SaveFileDialog();
|
---|
54 | saveFileDialog.Title = "Export .json-Template";
|
---|
55 | saveFileDialog.DefaultExt = "json";
|
---|
56 | saveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
|
---|
57 | saveFileDialog.FilterIndex = 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | saveFileDialog.FileName = "template";
|
---|
61 |
|
---|
62 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
63 | File.WriteAllText(saveFileDialog.FileName, generator.GenerateTemplate(root, optimizer));
|
---|
64 | }
|
---|
65 |
|
---|
66 | this.Close();
|
---|
67 | }
|
---|
68 |
|
---|
69 | private JsonItemVM BuildTreeNode(TreeNode node, IJsonItem item) {
|
---|
70 | JsonItemVM vm = new JsonItemVM(item);
|
---|
71 |
|
---|
72 | vms.Add(vm);
|
---|
73 | ctrlCollection.Add(node.GetHashCode(), GenerateControl(vm));
|
---|
74 | if (item.Children != null) {
|
---|
75 | foreach (var c in item.Children) {
|
---|
76 | if (IsDrawableItem(c)) {
|
---|
77 | if (c is ResultItem) {
|
---|
78 |
|
---|
79 | } else {
|
---|
80 | TreeNode childNode = new TreeNode(c.Name);
|
---|
81 | node.Nodes.Add(childNode);
|
---|
82 | vm.AddChild(BuildTreeNode(childNode, c));
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | return vm;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private bool IsDrawableItem(IJsonItem item) {
|
---|
92 | bool b = false;
|
---|
93 | if (item.Children != null) {
|
---|
94 | foreach (var c in item.Children) {
|
---|
95 | b = b || IsDrawableItem(c);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | return b || (item.Value != null || item.Range != null || item.ActualName != null);
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
103 | if(ctrlCollection.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl ctrl)) {
|
---|
104 | panel.Controls.Clear();
|
---|
105 | if (ctrl != null) {
|
---|
106 | panel.Controls.Add(ctrl);
|
---|
107 | }
|
---|
108 | panel.Refresh();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private UserControl GenerateControl(JsonItemVM vm) {
|
---|
113 | IJsonItem item = vm.Item;
|
---|
114 | UserControl control = null;
|
---|
115 | if (!(item is UnsupportedJsonItem)) {
|
---|
116 | if (item.Value is string && item.Range != null) {
|
---|
117 | control = new JsonItemValidValuesControl(vm);
|
---|
118 | } else if (item.Value is bool && item.Range != null) {
|
---|
119 | control = new JsonItemBoolControl(vm);
|
---|
120 | } else if (item.Value is int && item.Range != null) {
|
---|
121 | control = new JsonItemValueControl(vm, false);
|
---|
122 | } else if (item.Value is double && item.Range != null) {
|
---|
123 | control = new JsonItemValueControl(vm, true);
|
---|
124 | } else if (item.Value is Array) {
|
---|
125 | Array arr = (Array)item.Value;
|
---|
126 | if (arr.Length == 2 && arr.GetValue(0) is int && item.Range != null)
|
---|
127 | control = new JsonItemRangeControl(vm, false);
|
---|
128 | else if (arr.Length == 2 && arr.GetValue(0) is double && item.Range != null)
|
---|
129 | control = new JsonItemRangeControl(vm, true);
|
---|
130 | else if (arr.Rank == 1 && arr.GetValue(0) is double) {
|
---|
131 | control = new JsonItemArrayControl(vm);
|
---|
132 | }
|
---|
133 | } else {
|
---|
134 | control = new JsonItemBaseControl(vm);
|
---|
135 | }
|
---|
136 | if (control != null) {
|
---|
137 | control.Dock = DockStyle.Fill;
|
---|
138 | control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return control;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|