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 |
|
---|
19 | #region Private Properties
|
---|
20 | private static FolderBrowserDialog FolderBrowserDialog { get; set; }
|
---|
21 | private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
|
---|
22 | private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
|
---|
23 | private IDictionary<Type, Type> JI2VM { get; set; }
|
---|
24 | private IJsonItem Root { get; set; }
|
---|
25 | private IOptimizer Optimizer { get; set; }
|
---|
26 | private IList<IJsonItemVM> VMs { get; set; }
|
---|
27 | #endregion
|
---|
28 |
|
---|
29 | private IContent content;
|
---|
30 | public IContent Content {
|
---|
31 | get => content;
|
---|
32 | set {
|
---|
33 | content = value;
|
---|
34 |
|
---|
35 | #region Clear
|
---|
36 | VMs = new List<IJsonItemVM>();
|
---|
37 | treeView.Nodes.Clear();
|
---|
38 | treeViewResults.Nodes.Clear();
|
---|
39 | #endregion
|
---|
40 |
|
---|
41 | Optimizer = content as IOptimizer;
|
---|
42 | if(Optimizer != null) {
|
---|
43 | Optimizer = (IOptimizer)Optimizer.Clone(); // clone the optimizer
|
---|
44 | Root = JsonItemConverter.Extract(Optimizer);
|
---|
45 | TreeNode parent = new TreeNode(Root.Name);
|
---|
46 | treeView.AfterCheck += TreeView_AfterCheck;
|
---|
47 | BuildTreeNode(parent, Root);
|
---|
48 | treeView.Nodes.Add(parent);
|
---|
49 | treeView.ExpandAll();
|
---|
50 | panelParameterDetails.Controls.Clear();
|
---|
51 | panelResultDetails.Controls.Clear();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void InitCache() {
|
---|
57 | JI2VM = new Dictionary<Type, Type>();
|
---|
58 | foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
|
---|
59 | IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
|
---|
60 | JI2VM.Add(vm.TargetedJsonItemType, vmType);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public ExportJsonDialog() {
|
---|
65 | InitializeComponent();
|
---|
66 | this.Icon = HeuristicLab.Common.Resources.HeuristicLab.Icon;
|
---|
67 | InitCache();
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void exportButton_Click(object sender, EventArgs e) {
|
---|
71 | if (FolderBrowserDialog == null) {
|
---|
72 | FolderBrowserDialog = new FolderBrowserDialog();
|
---|
73 | FolderBrowserDialog.Description = "Select .json-Template Directory";
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
|
---|
77 | try {
|
---|
78 | JsonTemplateGenerator.GenerateTemplate(
|
---|
79 | Path.Combine(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text),
|
---|
80 | Optimizer, Root);
|
---|
81 | Close();
|
---|
82 | } catch (Exception ex) {
|
---|
83 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void BuildTreeNode(TreeNode node, IJsonItem item) {
|
---|
89 | RegisterItem(node, item, treeView);
|
---|
90 | if (item.Children != null) {
|
---|
91 | foreach (var c in item.Children) {
|
---|
92 | if (IsDrawableItem(c)) {
|
---|
93 | TreeNode childNode = new TreeNode(c.Name);
|
---|
94 | if (c is IResultJsonItem) {
|
---|
95 | treeViewResults.Nodes.Add(childNode);
|
---|
96 | IJsonItemVM vm = RegisterItem(childNode, c, treeViewResults);
|
---|
97 | if (vm != null) vm.Selected = true;
|
---|
98 | } else {
|
---|
99 | node.Nodes.Add(childNode);
|
---|
100 | BuildTreeNode(childNode, c);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private IJsonItemVM RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
|
---|
108 | if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
|
---|
109 | IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
|
---|
110 |
|
---|
111 | vm.Item = item;
|
---|
112 | vm.TreeNode = node;
|
---|
113 | vm.TreeView = tv;
|
---|
114 | vm.Selected = false;
|
---|
115 |
|
---|
116 | VMs.Add(vm);
|
---|
117 | Node2VM.Add(node, vm);
|
---|
118 | UserControl control = new JsonItemBaseControl(vm, vm.Control);
|
---|
119 | Node2Control.Add(node, control);
|
---|
120 | return vm;
|
---|
121 | } else {
|
---|
122 | node.ForeColor = Color.LightGray;
|
---|
123 | node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
|
---|
124 | }
|
---|
125 | return null;
|
---|
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 is EmptyJsonItem) || !(item is UnsupportedJsonItem);
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
140 | if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
|
---|
141 | SetControlOnPanel(control, panelParameterDetails);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
146 | if (Node2Control.TryGetValue(treeViewResults.SelectedNode, out UserControl control)) {
|
---|
147 | SetControlOnPanel(control, panelResultDetails);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void textBoxTemplateName_Validating(object sender, CancelEventArgs e) {
|
---|
152 | if (string.IsNullOrWhiteSpace(textBoxTemplateName.Text)) {
|
---|
153 | errorProvider.SetError(textBoxTemplateName, "Template name must not be empty.");
|
---|
154 | e.Cancel = true;
|
---|
155 | } else {
|
---|
156 | errorProvider.SetError(textBoxTemplateName, null);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void SetControlOnPanel(UserControl control, Panel panel) {
|
---|
161 | panel.Controls.Clear();
|
---|
162 | if (control != null) {
|
---|
163 | panel.Controls.Add(control);
|
---|
164 | control.Width = panel.Width;
|
---|
165 | control.Height = panel.Height;
|
---|
166 | control.Dock = DockStyle.Fill;
|
---|
167 | control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
---|
168 | }
|
---|
169 | panel.Refresh();
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
173 | if (e.Action != TreeViewAction.Unknown) {
|
---|
174 | if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
|
---|
175 | vm.Selected = e.Node.Checked;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void treeViewResults_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
181 | if (e.Action != TreeViewAction.Unknown) {
|
---|
182 | if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
|
---|
183 | vm.Selected = e.Node.Checked;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|