1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.IO;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using HeuristicLab.PluginInfrastructure;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
14 | public partial class ExportJsonDialog : Form {
|
---|
15 |
|
---|
16 | #region Private Properties
|
---|
17 | private static FolderBrowserDialog FolderBrowserDialog { get; set; }
|
---|
18 | private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
|
---|
19 | private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
|
---|
20 | private IDictionary<Type, Type> JI2VM { get; set; }
|
---|
21 | private IJsonItem Root { get; set; }
|
---|
22 | private IOptimizer Optimizer { get; set; }
|
---|
23 | private IList<IJsonItemVM> VMs { get; set; }
|
---|
24 | private ICheckedItemList<IRunCollectionModifier> RunCollectionModifiers { get; set; }
|
---|
25 | #endregion
|
---|
26 |
|
---|
27 | private IContent content;
|
---|
28 | public IContent Content {
|
---|
29 | get => content;
|
---|
30 | set {
|
---|
31 | content = value;
|
---|
32 | #region Clear
|
---|
33 | VMs = new List<IJsonItemVM>();
|
---|
34 | treeView.Nodes.Clear();
|
---|
35 | #endregion
|
---|
36 | Optimizer = content as IOptimizer;
|
---|
37 | if(Optimizer != null) {
|
---|
38 | Optimizer = (IOptimizer)Optimizer.Clone(); // clone the optimizer
|
---|
39 | Root = JsonItemConverter.Extract(Optimizer);
|
---|
40 | TreeNode parent = new TreeNode(Root.Name);
|
---|
41 | treeView.AfterCheck += TreeView_AfterCheck;
|
---|
42 | BuildTreeNode(parent, Root);
|
---|
43 | treeView.Nodes.Add(parent);
|
---|
44 | treeView.ExpandAll();
|
---|
45 | panelParameterDetails.Controls.Clear();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void InitCache() {
|
---|
51 | JI2VM = new Dictionary<Type, Type>();
|
---|
52 | foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
|
---|
53 | IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
|
---|
54 | JI2VM.Add(vm.TargetedJsonItemType, vmType);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public ExportJsonDialog() {
|
---|
59 | InitializeComponent();
|
---|
60 | RunCollectionModifiers = postProcessorListControl.Content;
|
---|
61 | Icon = Common.Resources.HeuristicLab.Icon;
|
---|
62 | InitCache();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void exportButton_Click(object sender, EventArgs e) {
|
---|
66 | if (FolderBrowserDialog == null) {
|
---|
67 | FolderBrowserDialog = new FolderBrowserDialog();
|
---|
68 | FolderBrowserDialog.Description = "Select .json-Template Directory";
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
|
---|
72 | try {
|
---|
73 | JsonTemplateGenerator.GenerateTemplate(
|
---|
74 | Path.Combine(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text),
|
---|
75 | Optimizer, Root, RunCollectionModifiers.CheckedItems.Select(x => x.Value));
|
---|
76 | Close();
|
---|
77 | } catch (Exception ex) {
|
---|
78 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void BuildTreeNode(TreeNode node, IJsonItem item) {
|
---|
84 | RegisterItem(node, item, treeView);
|
---|
85 | if (item.Children != null) {
|
---|
86 | foreach (var c in item.Children) {
|
---|
87 | if (IsDrawableItem(c)) {
|
---|
88 | TreeNode childNode = new TreeNode(c.Name);
|
---|
89 | node.Nodes.Add(childNode);
|
---|
90 | BuildTreeNode(childNode, c);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private IJsonItemVM RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
|
---|
97 | if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
|
---|
98 | IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
|
---|
99 |
|
---|
100 | vm.Item = item;
|
---|
101 | vm.TreeNode = node;
|
---|
102 | vm.TreeView = tv;
|
---|
103 | vm.Selected = false;
|
---|
104 |
|
---|
105 | VMs.Add(vm);
|
---|
106 | Node2VM.Add(node, vm);
|
---|
107 | UserControl control = JsonItemBaseControl.Create(vm, vm.Control);
|
---|
108 | Node2Control.Add(node, control);
|
---|
109 | return vm;
|
---|
110 | } else {
|
---|
111 | node.ForeColor = Color.LightGray;
|
---|
112 | node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
|
---|
113 | }
|
---|
114 | return null;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private bool IsDrawableItem(IJsonItem item) {
|
---|
118 | bool b = false;
|
---|
119 | if (item.Children != null)
|
---|
120 | foreach (var c in item.Children)
|
---|
121 | b = b || IsDrawableItem(c);
|
---|
122 |
|
---|
123 | return b || !(item is EmptyJsonItem) || !(item is UnsupportedJsonItem);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
127 | if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
|
---|
128 | SetControlOnPanel(control, panelParameterDetails);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void textBoxTemplateName_Validating(object sender, CancelEventArgs e) {
|
---|
133 | if (string.IsNullOrWhiteSpace(textBoxTemplateName.Text)) {
|
---|
134 | errorProvider.SetError(textBoxTemplateName, "Template name must not be empty.");
|
---|
135 | e.Cancel = true;
|
---|
136 | } else {
|
---|
137 | errorProvider.SetError(textBoxTemplateName, null);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void SetControlOnPanel(UserControl control, Panel panel) {
|
---|
142 | panel.Controls.Clear();
|
---|
143 | if (control != null) {
|
---|
144 | panel.Controls.Add(control);
|
---|
145 | control.Width = panel.Width;
|
---|
146 | control.Height = panel.Height;
|
---|
147 | control.Dock = DockStyle.Fill;
|
---|
148 | control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
---|
149 | }
|
---|
150 | panel.Refresh();
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
154 | if (e.Action != TreeViewAction.Unknown)
|
---|
155 | if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm))
|
---|
156 | vm.Selected = e.Node.Checked;
|
---|
157 | }
|
---|
158 |
|
---|
159 | private void treeViewResults_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
160 | if (e.Action != TreeViewAction.Unknown)
|
---|
161 | if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm))
|
---|
162 | vm.Selected = e.Node.Checked;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|