Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17828 was 17828, checked in by dpiringe, 3 years ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 6.4 KB
RevLine 
[17404]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;
[17410]14using HeuristicLab.PluginInfrastructure;
[17404]15
16namespace HeuristicLab.JsonInterface.OptimizerIntegration {
17  public partial class ExportJsonDialog : Form {
[17477]18
19    #region Private Properties
[17444]20    private static FolderBrowserDialog FolderBrowserDialog { get; set; }
[17451]21    private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
[17446]22    private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
[17451]23    private IDictionary<Type, Type> JI2VM { get; set; }
[17411]24    private IJsonItem Root { get; set; }
25    private IOptimizer Optimizer { get; set; }
[17446]26    private IList<IJsonItemVM> VMs { get; set; }
[17477]27    #endregion
[17411]28
[17404]29    private IContent content;
30    public IContent Content {
31      get => content;
32      set {
33        content = value;
34
[17477]35        #region Clear
[17446]36        VMs = new List<IJsonItemVM>();
[17405]37        treeView.Nodes.Clear();
[17451]38        treeViewResults.Nodes.Clear();
[17477]39        #endregion
[17436]40
[17411]41        Optimizer = content as IOptimizer;
[17828]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        }
[17404]53      }
54    }
55
[17410]56    private void InitCache() {
[17411]57      JI2VM = new Dictionary<Type, Type>();
[17471]58      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
59        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
[17473]60        JI2VM.Add(vm.TargetedJsonItemType, vmType);
[17410]61      }
62    }
63
[17404]64    public ExportJsonDialog() {
65      InitializeComponent();
[17828]66      this.Icon = HeuristicLab.Common.Resources.HeuristicLab.Icon;
[17410]67      InitCache();
[17404]68    }
69
70    private void exportButton_Click(object sender, EventArgs e) {
[17828]71      if (FolderBrowserDialog == null) {
72        FolderBrowserDialog = new FolderBrowserDialog();
73        FolderBrowserDialog.Description = "Select .json-Template Directory";
74      }
[17453]75
[17828]76      if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
77        try {
78          JsonTemplateGenerator.GenerateTemplate(
79            Path.Combine(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text),
80            Optimizer, Root);
[17446]81          Close();
[17828]82        } catch (Exception ex) {
83          ErrorHandling.ShowErrorDialog(this, ex);
[17444]84        }
[17446]85      }
86    }
[17404]87
[17446]88    private void BuildTreeNode(TreeNode node, IJsonItem item) {
[17451]89      RegisterItem(node, item, treeView);
[17446]90      if (item.Children != null) {
91        foreach (var c in item.Children) {
92          if (IsDrawableItem(c)) {
[17828]93            TreeNode childNode = new TreeNode(c.Name);
[17471]94            if (c is IResultJsonItem) {
[17446]95              treeViewResults.Nodes.Add(childNode);
[17828]96              IJsonItemVM vm = RegisterItem(childNode, c, treeViewResults);
97              if (vm != null) vm.Selected = true;             
[17446]98            } else {
99              node.Nodes.Add(childNode);
100              BuildTreeNode(childNode, c);
101            }
102          }
103        }
[17404]104      }
105    }
[17405]106
[17828]107    private IJsonItemVM RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
108      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
[17446]109        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
110
[17411]111        vm.Item = item;
[17431]112        vm.TreeNode = node;
[17451]113        vm.TreeView = tv;
114        vm.Selected = false;
[17446]115
116        VMs.Add(vm);
[17439]117        Node2VM.Add(node, vm);
[17471]118        UserControl control = new JsonItemBaseControl(vm, vm.Control);
[17451]119        Node2Control.Add(node, control);
[17828]120        return vm;
[17471]121      } else {
[17473]122        node.ForeColor = Color.LightGray;
123        node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
[17405]124      }
[17828]125      return null;
[17405]126    }
127
[17406]128    private bool IsDrawableItem(IJsonItem item) {
[17405]129      bool b = false;
130      if (item.Children != null) {
131        foreach (var c in item.Children) {
132          b = b || IsDrawableItem(c);
133        }
134      }
135     
[17828]136      return b || !(item is EmptyJsonItem) || !(item is UnsupportedJsonItem);
[17405]137    }
138   
139    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
[17451]140      if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
[17446]141        SetControlOnPanel(control, panelParameterDetails);
[17405]142      }
143    }
[17444]144
[17451]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
[17444]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    }
[17446]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    }
[17451]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    }
[17404]187  }
188}
Note: See TracBrowser for help on using the repository browser.