Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17411 for branches


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
Files:
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/HeuristicLab.JsonInterface.OptimizerIntegration.csproj

    r17410 r17411  
    133133      <DependentUpon>JsonItemValidValuesControl.cs</DependentUpon>
    134134    </Compile>
    135     <Compile Include="ViewModels\JsonItemVM.cs" />
    136135    <Compile Include="MenuItems\ImportJsonTemplateMenuItem.cs" />
    137136    <Compile Include="MenuItems\ExportJsonTemplateMenuItem.cs" />
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Shared/NumericRangeControl.Designer.cs

    r17410 r17411  
    4343      this.textBoxFrom.Size = new System.Drawing.Size(164, 20);
    4444      this.textBoxFrom.TabIndex = 2;
    45       this.textBoxFrom.Leave += new System.EventHandler(this.textBoxFrom_Leave);
    4645      //
    4746      // label6
     
    8180      this.checkBoxFrom.TabIndex = 4;
    8281      this.checkBoxFrom.UseVisualStyleBackColor = true;
    83       this.checkBoxFrom.CheckStateChanged += new System.EventHandler(this.checkBoxFrom_CheckStateChanged);
    8482      //
    8583      // checkBoxTo
     
    9189      this.checkBoxTo.TabIndex = 7;
    9290      this.checkBoxTo.UseVisualStyleBackColor = true;
    93       this.checkBoxTo.CheckStateChanged += new System.EventHandler(this.checkBoxTo_CheckStateChanged);
    9491      //
    9592      // textBoxTo
     
    10299      this.textBoxTo.Size = new System.Drawing.Size(164, 20);
    103100      this.textBoxTo.TabIndex = 6;
    104       this.textBoxTo.Leave += new System.EventHandler(this.textBoxTo_Leave);
    105101      //
    106102      // label1
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Shared/NumericRangeControl.cs

    r17410 r17411  
    1212namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    1313  public partial class NumericRangeControl : UserControl {
    14 
    15     private JsonItemVM vm;
    16     public JsonItemVM VM {
    17       get => vm;
    18       set {
    19         vm = value;
    20         Init();
    21       }
    22     }
    23     public bool IsDouble { get; set; }
    24 
     14    public TextBox TBMinRange { get; set; }
     15    public TextBox TBMaxRange { get; set; }
     16    public CheckBox EnableMinRange { get; set; }
     17    public CheckBox EnableMaxRange { get; set; }
    2518    public NumericRangeControl() {
    2619      InitializeComponent();
    27       Init();
     20      TBMinRange = textBoxFrom;
     21      TBMaxRange = textBoxTo;
     22      EnableMinRange = checkBoxFrom;
     23      EnableMaxRange = checkBoxTo;
     24      checkBoxFrom.CheckedChanged += ToggleFromInput;
     25      checkBoxTo.CheckedChanged += ToggleToInput;
    2826    }
    2927
    30     private void Init() {
    31       textBoxFrom.Text = "";
    32       textBoxTo.Text = "";
    33       textBoxFrom.ReadOnly = true;
    34       textBoxTo.ReadOnly = true;
    35       checkBoxFrom.Checked = false;
    36       checkBoxFrom.Checked = false;
    37     }
    38    
    39     private void checkBoxFrom_CheckStateChanged(object sender, EventArgs e) {
    40       textBoxFrom.ReadOnly = !checkBoxFrom.Checked;
    41       textBoxFrom.Text = "";
    42       if(!checkBoxFrom.Checked)
    43         SetRange();
     28    private void ToggleToInput(object sender, EventArgs e) {
     29      textBoxTo.ReadOnly = !checkBoxTo.Checked;
    4430    }
    4531
    46     private void checkBoxTo_CheckStateChanged(object sender, EventArgs e) {
    47       textBoxTo.ReadOnly = !checkBoxTo.Checked;
    48       textBoxTo.Text = "";
    49       if (!checkBoxTo.Checked)
    50         SetRange();
    51     }
    52 
    53     private void textBoxFrom_Leave(object sender, EventArgs e) {
    54       if (checkBoxFrom.Checked)
    55         SetRange();
    56     }
    57 
    58     private void textBoxTo_Leave(object sender, EventArgs e) {
    59       if (checkBoxTo.Checked)
    60         SetRange();
    61     }
    62 
    63     private void SetRange() {
    64       object[] range = new object[2];
    65       if (checkBoxFrom.Checked && !string.IsNullOrWhiteSpace(textBoxFrom.Text))
    66         range[0] = Parse(textBoxFrom.Text);
    67       else
    68         range[0] = IsDouble ? double.MinValue : int.MinValue;
    69 
    70       if (checkBoxTo.Checked && !string.IsNullOrWhiteSpace(textBoxTo.Text))
    71         range[1] = Parse(textBoxTo.Text);
    72       else
    73         range[1] = IsDouble ? double.MinValue : int.MinValue;
    74       VM.Item.Range = range;
    75     }
    76 
    77     private object Parse(string s) {
    78       if (IsDouble) {
    79         return double.Parse(s.Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture);
    80       }
    81       return int.Parse(s);
     32    private void ToggleFromInput(object sender, EventArgs e) {
     33      textBoxFrom.ReadOnly = !checkBoxFrom.Checked;
    8234    }
    8335  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/JsonItemVMBase.cs

    r17410 r17411  
    77
    88namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    9   public abstract class JsonItemVMBase : INotifyPropertyChanged {
     9  public class JsonItemVMBase : INotifyPropertyChanged {
    1010    public event PropertyChangedEventHandler PropertyChanged;
    1111    public IJsonItem Item { get; set; }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/RangeVM.cs

    r17410 r17411  
    1818  }
    1919
    20   public abstract class RangeVM<T> : JsonItemVM {
     20  public abstract class RangeVM<T> : JsonItemVMBase {
    2121
    2222    public T MinValue {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/SingleValueVM.cs

    r17410 r17411  
    1010  public class IntValueVM : SingleValueVM<int> {
    1111    public override Type JsonItemType => typeof(IntJsonItem);
     12
     13    protected override int MinTypeValue => int.MinValue;
     14    protected override int MaxTypeValue => int.MaxValue;
     15
    1216    public override JsonItemBaseControl GetControl() =>
    1317      new JsonItemIntValueControl(this);
     
    1519
    1620  public class DoubleValueVM : SingleValueVM<double> {
    17     public override Type JsonItemType => typeof(DoubleJsonItem);
     21    public override Type JsonItemType => typeof(DoubleJsonItem);
     22
     23    protected override double MinTypeValue => double.MinValue;
     24    protected override double MaxTypeValue => double.MaxValue;
     25
    1826    public override JsonItemBaseControl GetControl() =>
    1927       new JsonItemDoubleValueControl(this);
     
    2230  public class BoolValueVM : SingleValueVM<bool> {
    2331    public override Type JsonItemType => typeof(BoolJsonItem);
     32
     33    protected override bool MinTypeValue => false;
     34    protected override bool MaxTypeValue => true;
     35
    2436    public override JsonItemBaseControl GetControl() =>
    2537       new JsonItemBoolControl(this);
    2638  }
    2739
    28   public abstract class SingleValueVM<T> : JsonItemVM {
     40  public abstract class SingleValueVM<T> : JsonItemVMBase {
    2941   
    3042    public T Value {
     
    5264    }
    5365
     66    private bool enableMinRange = false;
     67    public bool EnableMinRange {
     68      get => enableMinRange;
     69      set {
     70        enableMinRange = value;
     71        if (!enableMinRange)
     72          MinRange = MinTypeValue;
     73        OnPropertyChange(this, nameof(EnableMinRange));
     74      }
     75    }
     76
     77    private bool enableMaxRange = false;
     78    public bool EnableMaxRange {
     79      get => enableMaxRange;
     80      set {
     81        enableMaxRange = value;
     82        if (!enableMaxRange)
     83          MaxRange = MaxTypeValue;
     84        OnPropertyChange(this, nameof(EnableMaxRange));
     85      }
     86    }
     87
    5488    private T Cast(object obj) => (T)Convert.ChangeType(obj, typeof(T));
    5589
     
    5993    }
    6094
     95    protected abstract T MinTypeValue { get; }
     96    protected abstract T MaxTypeValue { get; }
    6197  }
    6298}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/StringValueVM.cs

    r17410 r17411  
    66
    77namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    8   public class StringValueVM : JsonItemVM {
     8  public class StringValueVM : JsonItemVMBase {
    99    public override Type JsonItemType => typeof(StringJsonItem);
    1010    public override JsonItemBaseControl GetControl() =>
  • 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) {
  • 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    }
  • 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
  • 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.