Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/Views/ErcOptionsView.cs @ 15017

Last change on this file since 15017 was 15017, checked in by pkimmesw, 7 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 3.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Views {
2  using System.Linq;
3  using Core;
4  using Core.Views;
5  using MainForm;
6
7  [View("ERC Options")]
8  [Content(typeof(ErcOptions), true)]
9  public partial class ErcOptionsView : NamedItemView {
10    private const string PERCENTAGE_FORMAT = "0%";
11    private CheckedItemCollection<IErcItem> ercItems;
12
13    public ErcOptionsView() {
14      InitializeComponent();
15
16      possibilityTextBox.Validating += PossibilityTextBoxValidating;
17      possibilityTextBox.KeyDown += PossibilityTextBoxKeyDown;
18    }
19
20    private void PossibilityTextBoxKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
21      if (e.KeyCode == System.Windows.Forms.Keys.Enter) {
22        Validate();
23      }
24    }
25
26    private void PossibilityTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e) {
27      var text = possibilityTextBox.Text.Trim();
28      if (text.EndsWith("%")) {
29        text = text.Substring(0, text.Length - 1);
30      }
31
32      double value;
33      if (double.TryParse(text, out value)) {
34        possibilityTextBox.Text = (value / 100.0).ToString(PERCENTAGE_FORMAT);
35      } else {
36        possibilityTextBox.Text = "0%";
37        e.Cancel = true;
38      }
39
40      Content.ErcProbability = value;
41    }
42
43    public new ErcOptions Content
44    {
45      get { return (ErcOptions)base.Content; }
46      set
47      {
48        ClearOldContent();
49        base.Content = value;
50      }
51    }
52
53    protected override void OnReadOnlyChanged() {
54      base.OnReadOnlyChanged();
55
56      possibilityTextBox.ReadOnly = ReadOnly;
57    }
58
59    protected override void OnContentChanged() {
60      if (Content == null)
61        return;
62
63      nameTextBox.Text = Content.Name;
64      possibilityTextBox.Text = Content.ErcProbability.ToString(PERCENTAGE_FORMAT);
65
66      var ercOptions = typeof(ErcOptions)
67        .GetProperties()
68        .Where(p => typeof(IErcItem).IsAssignableFrom(p.PropertyType))
69        .Select(p => (IErcItem)p.GetValue(Content))
70        .ToArray();
71
72      ercItems = new CheckedItemCollection<IErcItem>(ercOptions);
73
74      ercItems.ForEach(item => {
75        item.EnabledChanged += OptionEnabledChanged;
76        ercItems.SetItemCheckedState(item, item.IsEnabled);
77      });
78
79      ercItems.CheckedItemsChanged += ErcItemsCheckedItemsChanged;
80      ErcOptionListView.Content = new ReadOnlyCheckedItemCollection<IErcItem>(ercItems);
81    }
82
83    private void ClearOldContent() {
84      if (ercItems != null) {
85        ercItems.ForEach(item => item.EnabledChanged -= OptionEnabledChanged);
86        ercItems.CheckedItemsChanged -= ErcItemsCheckedItemsChanged;
87        ercItems = null;
88      }
89    }
90
91    private void ErcItemsCheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IErcItem> e) {
92      foreach (var item in e.Items)
93        item.IsEnabled = ercItems.ItemChecked(item);
94    }
95
96    private void OptionEnabledChanged(object sender, bool state) {
97      var item = (IErcItem)sender;
98
99      // avoid unnecessary state change events
100      if (ercItems.ItemChecked(item) != state)
101        ercItems.SetItemCheckedState(item, state);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.