[17404] | 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;
|
---|
[17410] | 14 | using HeuristicLab.PluginInfrastructure;
|
---|
[17404] | 15 |
|
---|
| 16 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
| 17 | public partial class ExportJsonDialog : Form {
|
---|
[17411] | 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 |
|
---|
[17404] | 25 | private IContent content;
|
---|
| 26 | public IContent Content {
|
---|
| 27 | get => content;
|
---|
| 28 | set {
|
---|
| 29 | content = value;
|
---|
| 30 |
|
---|
[17411] | 31 | VMs = new List<JsonItemVMBase>();
|
---|
[17405] | 32 | treeView.Nodes.Clear();
|
---|
[17410] | 33 |
|
---|
[17411] | 34 | Optimizer = content as IOptimizer;
|
---|
| 35 | Root = JsonItemConverter.Extract(Optimizer);
|
---|
| 36 | TreeNode parent = new TreeNode(Root.Name);
|
---|
[17410] | 37 |
|
---|
[17411] | 38 | BuildTreeNode(parent, Root);
|
---|
[17405] | 39 | treeView.Nodes.Add(parent);
|
---|
[17432] | 40 | treeView.ExpandAll();
|
---|
| 41 |
|
---|
[17404] | 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[17411] | 45 | private IDictionary<Type, Type> JI2VM { get; set; }
|
---|
[17410] | 46 |
|
---|
| 47 | private void InitCache() {
|
---|
[17411] | 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);
|
---|
[17410] | 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[17404] | 55 | public ExportJsonDialog() {
|
---|
| 56 | InitializeComponent();
|
---|
[17410] | 57 | InitCache();
|
---|
[17404] | 58 | }
|
---|
| 59 |
|
---|
| 60 | private void exportButton_Click(object sender, EventArgs e) {
|
---|
[17411] | 61 | foreach(var x in VMs) {
|
---|
[17405] | 62 | if (!x.Selected) {
|
---|
| 63 | x.Item.Parent.Children.Remove(x.Item);
|
---|
| 64 | }
|
---|
[17404] | 65 | }
|
---|
| 66 |
|
---|
[17411] | 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;
|
---|
[17404] | 73 | }
|
---|
| 74 |
|
---|
[17411] | 75 | SaveFileDialog.FileName = "template";
|
---|
[17404] | 76 |
|
---|
[17411] | 77 | if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 78 | File.WriteAllText(SaveFileDialog.FileName, Generator.GenerateTemplate(Root, Optimizer));
|
---|
[17404] | 79 | }
|
---|
| 80 |
|
---|
[17412] | 81 | Close();
|
---|
[17404] | 82 | }
|
---|
[17405] | 83 |
|
---|
[17410] | 84 | private void BuildTreeNode(TreeNode node, IJsonItem item) {
|
---|
[17411] | 85 | if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
|
---|
| 86 | JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
|
---|
| 87 | VMs.Add(vm);
|
---|
| 88 | vm.Item = item;
|
---|
[17431] | 89 | vm.TreeNode = node;
|
---|
| 90 | vm.TreeView = treeView;
|
---|
[17411] | 91 | UserControl control = vm.GetControl();
|
---|
| 92 | Hash2Control.Add(node.GetHashCode(), control);
|
---|
[17410] | 93 | if (item.Children != null) {
|
---|
| 94 | foreach (var c in item.Children) {
|
---|
| 95 | if (IsDrawableItem(c)) {
|
---|
| 96 | if (c is ResultItem) {
|
---|
[17405] | 97 |
|
---|
[17410] | 98 | } else {
|
---|
| 99 | TreeNode childNode = new TreeNode(c.Name);
|
---|
| 100 | node.Nodes.Add(childNode);
|
---|
| 101 | BuildTreeNode(childNode, c);
|
---|
| 102 | }
|
---|
[17405] | 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[17406] | 109 | private bool IsDrawableItem(IJsonItem item) {
|
---|
[17405] | 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) {
|
---|
[17432] | 121 | if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl control)) {
|
---|
[17405] | 122 | panel.Controls.Clear();
|
---|
[17432] | 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;
|
---|
[17405] | 128 | }
|
---|
| 129 | panel.Refresh();
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[17404] | 132 | }
|
---|
| 133 | }
|
---|