Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs @ 17443

Last change on this file since 17443 was 17443, checked in by dpiringe, 5 years ago

#3026:

  • added support for string values without range limitation (dropdown gets automatically replaced by textbox)
  • fixed a UI bug for JsonItemValidValuesControl -> now the control resizes correctly
  • fixed a bug in AlgorithmConverter -> now it searches for the ItemName (instead of Name) in method Inject
  • fixed a bug in StringValueConverter -> now it sets the correct name
File size: 5.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.IO;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11using System.Windows.Forms;
12using HeuristicLab.Common;
13using HeuristicLab.Optimization;
14using HeuristicLab.PluginInfrastructure;
15
16namespace 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 IDictionary<TreeNode, JsonItemVMBase> Node2VM { get; set; } = new Dictionary<TreeNode, JsonItemVMBase>();
21    private IJsonItem Root { get; set; }
22    private IOptimizer Optimizer { get; set; }
23    private IList<JsonItemVMBase> VMs { get; set; }
24    private JCGenerator Generator { get; set; } = new JCGenerator();
25    private IDictionary<string, IJsonItem> ResultItems { get; set; } = new Dictionary<string, IJsonItem>();
26
27    private IContent content;
28    public IContent Content {
29      get => content;
30      set {
31        content = value;
32
33        VMs = new List<JsonItemVMBase>();
34        treeView.Nodes.Clear();
35        ResultItems.Clear();
36        resultItems.Items.Clear();
37
38        Optimizer = content as IOptimizer;
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       
46      }
47    }
48
49    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
50      if (e.Action != TreeViewAction.Unknown) {
51        if (Node2VM.TryGetValue(e.Node, out JsonItemVMBase vm)) {
52          vm.Selected = e.Node.Checked;
53        }
54      }
55    }
56
57    private IDictionary<Type, Type> JI2VM { get; set; }
58
59    private void InitCache() {
60      JI2VM = new Dictionary<Type, Type>();
61      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(JsonItemVMBase))) {
62        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
63        JI2VM.Add(vm.JsonItemType, vmType);
64      }
65    }
66
67    public ExportJsonDialog() {
68      InitializeComponent();
69      InitCache();
70    }
71
72    private void exportButton_Click(object sender, EventArgs e) {
73      foreach(var x in VMs) {
74        if (!x.Selected) {
75          x.Item.Parent.Children.Remove(x.Item);
76        }
77      }
78
79      foreach(var x in ResultItems.Values) {
80        x.Parent.Children.Remove(x);
81      }
82
83      foreach(var x in resultItems.CheckedItems) {
84        if(ResultItems.TryGetValue((string)x, out IJsonItem item)) {
85          Root.AddChildren(item);
86        }
87      }
88
89      IList<IJsonItem> faultyItems = new List<IJsonItem>();
90      if(!Root.GetValidator().Validate(ref faultyItems)) {
91        //print faultyItems
92      }
93
94      if (SaveFileDialog == null) {
95        SaveFileDialog = new SaveFileDialog();
96        SaveFileDialog.Title = "Export .json-Template";
97        SaveFileDialog.DefaultExt = "json";
98        SaveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
99        SaveFileDialog.FilterIndex = 1;
100      }
101     
102      SaveFileDialog.FileName = "template";
103
104      if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
105        Generator.GenerateTemplate(@"C:\Users\p41997\Desktop", "template", Optimizer, Root);
106      }
107
108      Close();
109    }
110
111    private void BuildTreeNode(TreeNode node, IJsonItem item) {
112      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
113        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
114        VMs.Add(vm);
115        vm.Item = item;
116        vm.TreeNode = node;
117        vm.TreeView = treeView;
118        Node2VM.Add(node, vm);
119        node.Checked = vm.Selected;
120        UserControl control = vm.GetControl();
121        Hash2Control.Add(node.GetHashCode(), control);
122        if (item.Children != null) {
123          foreach (var c in item.Children) {
124            if (IsDrawableItem(c)) {
125              if (c is ResultItem) {
126                resultItems.Items.Add(c.Name, true);
127               
128                ResultItems.Add(c.Name, c);
129              } else {
130                TreeNode childNode = new TreeNode(c.Name);
131                node.Nodes.Add(childNode);
132                BuildTreeNode(childNode, c);
133              }
134            }
135          }
136        }
137      }
138    }
139
140    private bool IsDrawableItem(IJsonItem item) {
141      bool b = false;
142      if (item.Children != null) {
143        foreach (var c in item.Children) {
144          b = b || IsDrawableItem(c);
145        }
146      }
147     
148      return b || (item.Value != null || item.Range != null || item.ActualName != null);
149    }
150   
151    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
152      if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl control)) {
153        panel.Controls.Clear();
154        if (control != null) {
155          panel.Controls.Add(control);
156          control.Width = panel.Width;
157          control.Height = panel.Height;
158          control.Dock = DockStyle.Fill;
159          control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
160        }
161        panel.Refresh();
162      }
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.