[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();
|
---|
[17435] | 24 | private IDictionary<string, IJsonItem> ResultItems = new Dictionary<string, IJsonItem>();
|
---|
[17411] | 25 |
|
---|
[17404] | 26 | private IContent content;
|
---|
| 27 | public IContent Content {
|
---|
| 28 | get => content;
|
---|
| 29 | set {
|
---|
| 30 | content = value;
|
---|
| 31 |
|
---|
[17411] | 32 | VMs = new List<JsonItemVMBase>();
|
---|
[17405] | 33 | treeView.Nodes.Clear();
|
---|
[17435] | 34 | ResultItems.Clear();
|
---|
[17410] | 35 |
|
---|
[17411] | 36 | Optimizer = content as IOptimizer;
|
---|
| 37 | Root = JsonItemConverter.Extract(Optimizer);
|
---|
| 38 | TreeNode parent = new TreeNode(Root.Name);
|
---|
[17435] | 39 |
|
---|
[17411] | 40 | BuildTreeNode(parent, Root);
|
---|
[17405] | 41 | treeView.Nodes.Add(parent);
|
---|
[17432] | 42 | treeView.ExpandAll();
|
---|
| 43 |
|
---|
[17404] | 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[17411] | 47 | private IDictionary<Type, Type> JI2VM { get; set; }
|
---|
[17410] | 48 |
|
---|
| 49 | private void InitCache() {
|
---|
[17411] | 50 | JI2VM = new Dictionary<Type, Type>();
|
---|
| 51 | foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(JsonItemVMBase))) {
|
---|
| 52 | JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
|
---|
| 53 | JI2VM.Add(vm.JsonItemType, vmType);
|
---|
[17410] | 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[17404] | 57 | public ExportJsonDialog() {
|
---|
| 58 | InitializeComponent();
|
---|
[17410] | 59 | InitCache();
|
---|
[17404] | 60 | }
|
---|
| 61 |
|
---|
| 62 | private void exportButton_Click(object sender, EventArgs e) {
|
---|
[17411] | 63 | foreach(var x in VMs) {
|
---|
[17405] | 64 | if (!x.Selected) {
|
---|
| 65 | x.Item.Parent.Children.Remove(x.Item);
|
---|
| 66 | }
|
---|
[17404] | 67 | }
|
---|
| 68 |
|
---|
[17435] | 69 | foreach(var x in ResultItems.Values) {
|
---|
| 70 | x.Parent.Children.Remove(x);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | foreach(var x in resultItems.CheckedItems) {
|
---|
| 74 | if(ResultItems.TryGetValue((string)x, out IJsonItem item)) {
|
---|
| 75 | Root.AddChildren(item);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | IList<IJsonItem> faultyItems = new List<IJsonItem>();
|
---|
| 80 | if(!Root.GetValidator().Validate(ref faultyItems)) {
|
---|
| 81 | //print faultyItems
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[17411] | 84 | if (SaveFileDialog == null) {
|
---|
| 85 | SaveFileDialog = new SaveFileDialog();
|
---|
| 86 | SaveFileDialog.Title = "Export .json-Template";
|
---|
| 87 | SaveFileDialog.DefaultExt = "json";
|
---|
| 88 | SaveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
|
---|
| 89 | SaveFileDialog.FilterIndex = 1;
|
---|
[17404] | 90 | }
|
---|
| 91 |
|
---|
[17411] | 92 | SaveFileDialog.FileName = "template";
|
---|
[17404] | 93 |
|
---|
[17411] | 94 | if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 95 | File.WriteAllText(SaveFileDialog.FileName, Generator.GenerateTemplate(Root, Optimizer));
|
---|
[17404] | 96 | }
|
---|
| 97 |
|
---|
[17412] | 98 | Close();
|
---|
[17404] | 99 | }
|
---|
[17405] | 100 |
|
---|
[17410] | 101 | private void BuildTreeNode(TreeNode node, IJsonItem item) {
|
---|
[17411] | 102 | if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
|
---|
| 103 | JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
|
---|
| 104 | VMs.Add(vm);
|
---|
| 105 | vm.Item = item;
|
---|
[17431] | 106 | vm.TreeNode = node;
|
---|
| 107 | vm.TreeView = treeView;
|
---|
[17411] | 108 | UserControl control = vm.GetControl();
|
---|
| 109 | Hash2Control.Add(node.GetHashCode(), control);
|
---|
[17410] | 110 | if (item.Children != null) {
|
---|
| 111 | foreach (var c in item.Children) {
|
---|
| 112 | if (IsDrawableItem(c)) {
|
---|
| 113 | if (c is ResultItem) {
|
---|
[17435] | 114 | resultItems.Items.Add(c.Name, true);
|
---|
| 115 | ResultItems.Add(c.Name, c);
|
---|
[17410] | 116 | } else {
|
---|
| 117 | TreeNode childNode = new TreeNode(c.Name);
|
---|
| 118 | node.Nodes.Add(childNode);
|
---|
| 119 | BuildTreeNode(childNode, c);
|
---|
| 120 | }
|
---|
[17405] | 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[17406] | 127 | private bool IsDrawableItem(IJsonItem item) {
|
---|
[17405] | 128 | bool b = false;
|
---|
| 129 | if (item.Children != null) {
|
---|
| 130 | foreach (var c in item.Children) {
|
---|
| 131 | b = b || IsDrawableItem(c);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return b || (item.Value != null || item.Range != null || item.ActualName != null);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
[17432] | 139 | if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl control)) {
|
---|
[17405] | 140 | panel.Controls.Clear();
|
---|
[17432] | 141 | if (control != null) {
|
---|
| 142 | panel.Controls.Add(control);
|
---|
| 143 | control.Width = panel.Width;
|
---|
| 144 | control.Dock = DockStyle.Fill;
|
---|
| 145 | control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
---|
[17405] | 146 | }
|
---|
| 147 | panel.Refresh();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[17404] | 150 | }
|
---|
| 151 | }
|
---|