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