Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/28/20 16:21:53 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • deleted JsonItemVM
  • reduced code duplicates in JsonItemValueControl
  • implemented logic for entering ranges -> automatically sets to min/max value of datatype when checkbox is not checked
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs

    r17410 r17411  
    1616namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    1717  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 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
    1825    private IContent content;
    19     private static SaveFileDialog saveFileDialog;
    20     private IDictionary<int, UserControl> ctrlCollection = new Dictionary<int, UserControl>();
    21     private IJsonItem root;
    22     private IOptimizer optimizer;
    23     private IList<JsonItemVM> vms;
    24     private JCGenerator generator = new JCGenerator();
    2526    public IContent Content {
    2627      get => content;
     
    2829        content = value;
    2930
    30         //IEnumerable<JsonItem> items = generator.FetchJsonItems(content as IOptimizer);
    31         vms = new List<JsonItemVM>();
     31        VMs = new List<JsonItemVMBase>();
    3232        treeView.Nodes.Clear();
    3333       
    34         optimizer = content as IOptimizer;
    35         root = JsonItemConverter.Extract(optimizer);
    36         TreeNode parent = new TreeNode(root.Name);
     34        Optimizer = content as IOptimizer;
     35        Root = JsonItemConverter.Extract(Optimizer);
     36        TreeNode parent = new TreeNode(Root.Name);
    3737     
    38         BuildTreeNode(parent, root);
     38        BuildTreeNode(parent, Root);
    3939        treeView.Nodes.Add(parent);
    4040      }
    4141    }
    4242
    43     private IDictionary<Type, JsonItemVMBase> VMs { get; set; }
     43    private IDictionary<Type, Type> JI2VM { get; set; }
    4444
    4545
    4646    private void InitCache() {
    47       VMs = new Dictionary<Type, JsonItemVMBase>();
    48       foreach (var vm in ApplicationManager.Manager.GetInstances<JsonItemVMBase>()) {
    49         VMs.Add(vm.JsonItemType, vm);
     47      JI2VM = new Dictionary<Type, Type>();
     48      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(JsonItemVMBase))) {
     49        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
     50        JI2VM.Add(vm.JsonItemType, vmType);
    5051      }
    5152    }
     
    5758
    5859    private void exportButton_Click(object sender, EventArgs e) {
    59       foreach(var x in vms) {
     60      foreach(var x in VMs) {
    6061        if (!x.Selected) {
    6162          x.Item.Parent.Children.Remove(x.Item);
     
    6364      }
    6465
    65       if (saveFileDialog == null) {
    66         saveFileDialog = new SaveFileDialog();
    67         saveFileDialog.Title = "Export .json-Template";
    68         saveFileDialog.DefaultExt = "json";
    69         saveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
    70         saveFileDialog.FilterIndex = 1;
     66      if (SaveFileDialog == null) {
     67        SaveFileDialog = new SaveFileDialog();
     68        SaveFileDialog.Title = "Export .json-Template";
     69        SaveFileDialog.DefaultExt = "json";
     70        SaveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
     71        SaveFileDialog.FilterIndex = 1;
    7172      }
    7273     
    73       saveFileDialog.FileName = "template";
     74      SaveFileDialog.FileName = "template";
    7475
    75       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    76         File.WriteAllText(saveFileDialog.FileName, generator.GenerateTemplate(root, optimizer));
     76      if (SaveFileDialog.ShowDialog() == DialogResult.OK) {
     77        File.WriteAllText(SaveFileDialog.FileName, Generator.GenerateTemplate(Root, Optimizer));
    7778      }
    7879
     
    8182
    8283    private void BuildTreeNode(TreeNode node, IJsonItem item) {
    83 
    84       if (VMs.TryGetValue(item.GetType(), out JsonItemVMBase vm)) {
    85         //vm.Item = item;
    86         //UserControl control = vm.GetControl();
    87         //if (control != null) {
    88         //  control.Dock = DockStyle.Fill;
    89         //  control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
    90         //}
    91         //ctrlCollection.Add(node.GetHashCode(), control);
     84      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
     85        JsonItemVMBase vm = (JsonItemVMBase)Activator.CreateInstance(vmType);
     86        VMs.Add(vm);
     87        vm.Item = item;
     88        UserControl control = vm.GetControl();
     89        if (control != null) {
     90          control.Dock = DockStyle.Fill;
     91          control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
     92        }
     93        Hash2Control.Add(node.GetHashCode(), control);
    9294        if (item.Children != null) {
    9395          foreach (var c in item.Children) {
     
    99101                node.Nodes.Add(childNode);
    100102                BuildTreeNode(childNode, c);
    101                 //vm.AddChild(BuildTreeNode(childNode, c));
    102103              }
    103104            }
    104105          }
    105106        }
    106       } else {
    107         Console.WriteLine();
    108107      }
    109108    }
     
    121120   
    122121    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
    123       if(ctrlCollection.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl ctrl)) {
     122      if(Hash2Control.TryGetValue(treeView.SelectedNode.GetHashCode(), out UserControl ctrl)) {
    124123        panel.Controls.Clear();
    125124        if (ctrl != null) {
  • TabularUnified branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemBoolControl.cs

    r17410 r17411  
    1111namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    1212  public partial class JsonItemBoolControl : JsonItemBaseControl {
     13    private BoolValueVM VM { get; set; }
    1314
    1415    public JsonItemBoolControl(BoolValueVM vm) : base(vm) {
    1516      InitializeComponent();
     17      VM = vm;
     18      checkBoxValue.DataBindings.Add("Checked", VM, nameof(BoolValueVM.Value));
    1619      //checkBoxValue.Checked = (bool)vm.Item.Value;
    1720    }
  • TabularUnified branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemValueControl.Designer.cs

    r17410 r17411  
    4949      // numericRangeControl1
    5050      //
    51       this.numericRangeControl1.IsDouble = false;
    5251      this.numericRangeControl1.Location = new System.Drawing.Point(9, 101);
    5352      this.numericRangeControl1.Name = "numericRangeControl1";
    5453      this.numericRangeControl1.Size = new System.Drawing.Size(487, 112);
    5554      this.numericRangeControl1.TabIndex = 16;
    56       this.numericRangeControl1.VM = null;
    5755      //
    5856      // JsonItemValueControl
  • TabularUnified branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemValueControl.cs

    r17410 r17411  
    1313
    1414  public class JsonItemIntValueControl : JsonItemValueControl {
     15    private readonly IntValueVM vm;
    1516
    16     public JsonItemIntValueControl(SingleValueVM<int> vm) : base(vm) { }
     17    #region Overriden Properties
     18    protected override object VM => vm;
     19    protected override string ValuePropertyId => nameof(IntValueVM.Value);
     20    protected override string MinRangePropertyId => nameof(IntValueVM.MinRange);
     21    protected override string MaxRangePropertyId => nameof(IntValueVM.MaxRange);
     22    protected override string EnableMinRangePropertyId => nameof(IntValueVM.EnableMinRange);
     23    protected override string EnableMaxRangePropertyId => nameof(IntValueVM.EnableMaxRange);
     24    #endregion
     25
     26    public JsonItemIntValueControl(IntValueVM vm) : base(vm) {
     27      this.vm = vm;
     28      Init();
     29    }
    1730
    1831  }
    1932
    2033  public class JsonItemDoubleValueControl : JsonItemValueControl {
    21     private SingleValueVM<double> VM { get; set; }
     34    private readonly DoubleValueVM vm;
    2235
    23     public JsonItemDoubleValueControl(SingleValueVM<double> vm) : base(vm) {
    24       VM = vm;
     36    #region Overriden Properties
     37    protected override object VM => vm;
     38    protected override string ValuePropertyId => nameof(DoubleValueVM.Value);
     39    protected override string MinRangePropertyId => nameof(DoubleValueVM.MinRange);
     40    protected override string MaxRangePropertyId => nameof(DoubleValueVM.MaxRange);
     41    protected override string EnableMinRangePropertyId => nameof(DoubleValueVM.EnableMinRange);
     42    protected override string EnableMaxRangePropertyId => nameof(DoubleValueVM.EnableMaxRange);
     43    #endregion
     44
     45    public JsonItemDoubleValueControl(DoubleValueVM vm) : base(vm) {
     46      this.vm = vm;
     47      Init();
    2548    }
    2649
     
    2851
    2952  public abstract partial class JsonItemValueControl : JsonItemBaseControl {
     53    #region Protected Properties
     54    protected TextBox TBValue { get; set; }
     55    protected NumericRangeControl NumericRangeControl { get; set; }
     56    #endregion
     57
     58    #region Abstract Properties
     59    protected abstract object VM { get; }
     60    protected abstract string ValuePropertyId { get; }
     61    protected abstract string MinRangePropertyId { get; }
     62    protected abstract string MaxRangePropertyId { get; }
     63    protected abstract string EnableMinRangePropertyId { get; }
     64    protected abstract string EnableMaxRangePropertyId { get; }
     65    #endregion
    3066
    3167    public JsonItemValueControl(JsonItemVMBase vm) : base(vm) {
    3268      InitializeComponent();
     69      TBValue = textBoxValue;
     70      NumericRangeControl = numericRangeControl1;
     71    }
     72
     73    protected void Init() {
     74      TBValue.DataBindings.Add("Text", VM, ValuePropertyId);
     75      NumericRangeControl.TBMinRange.DataBindings.Add("Text", VM, MinRangePropertyId);
     76      NumericRangeControl.TBMaxRange.DataBindings.Add("Text", VM, MaxRangePropertyId);
     77      NumericRangeControl.EnableMinRange.DataBindings.Add("Checked", VM, EnableMinRangePropertyId,
     78        false, DataSourceUpdateMode.OnPropertyChanged);
     79      NumericRangeControl.EnableMaxRange.DataBindings.Add("Checked", VM, EnableMaxRangePropertyId,
     80        false, DataSourceUpdateMode.OnPropertyChanged);
    3381    }
    3482
Note: See TracChangeset for help on using the changeset viewer.