Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

File size: 2.6 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    }
18
19    private void PossibilityTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e) {
20      double value;
21      if (double.TryParse(possibilityTextBox.Text, out value)) {
22        possibilityTextBox.Text = value.ToString(PERCENTAGE_FORMAT);
23      } else {
24        possibilityTextBox.Text = "0 %";
25        e.Cancel = true;
26      }
27
28      Content.ErcProbability = value;
29    }
30
31    public new ErcOptions Content
32    {
33      get { return (ErcOptions)base.Content; }
34      set
35      {
36        ClearOldContent();
37        base.Content = value;
38      }
39    }
40
41    protected override void OnContentChanged() {
42      if (Content == null)
43        return;
44
45      nameTextBox.Text = Content.Name;
46      possibilityTextBox.Text = Content.ErcProbability.ToString(PERCENTAGE_FORMAT);
47
48      var ercOptions = typeof(ErcOptions)
49        .GetProperties()
50        .Where(p => typeof(IErcItem).IsAssignableFrom(p.PropertyType))
51        .Select(p => (IErcItem)p.GetValue(Content))
52        .ToArray();
53
54      ercItems = new CheckedItemCollection<IErcItem>(ercOptions);
55
56      ercItems.ForEach(item => {
57        item.EnabledChanged += OptionEnabledChanged;
58        ercItems.SetItemCheckedState(item, item.IsEnabled);
59      });
60
61      ercItems.CheckedItemsChanged += ErcItemsCheckedItemsChanged;
62      ErcOptionListView.Content = new ReadOnlyCheckedItemCollection<IErcItem>(ercItems);
63    }
64
65    private void ClearOldContent() {
66      if (ercItems != null) {
67        ercItems.ForEach(item => item.EnabledChanged -= OptionEnabledChanged);
68        ercItems.CheckedItemsChanged -= ErcItemsCheckedItemsChanged;
69        ercItems = null;
70      }
71    }
72
73    private void ErcItemsCheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IErcItem> e) {
74      foreach (var item in e.Items)
75        item.IsEnabled = ercItems.ItemChecked(item);
76    }
77
78    private void OptionEnabledChanged(object sender, bool state) {
79      var item = (IErcItem)sender;
80
81      // avoid unnecessary state change events
82      if (ercItems.ItemChecked(item) != state)
83        ercItems.SetItemCheckedState(item, state);
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.