Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis.Views/Erc/ErcOptionsView.cs @ 16101

Last change on this file since 16101 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 3.2 KB
Line 
1using System.Linq;
2using HeuristicLab.Collections;
3using HeuristicLab.Core;
4using HeuristicLab.Core.Views;
5using HeuristicLab.MainForm;
6
7namespace HeuristicLab.Problems.ProgramSynthesis {
8  [View("ERC Options")]
9  [Content(typeof(ErcOptions), true)]
10  public partial class ErcOptionsView : NamedItemView {
11    private const string PERCENTAGE_FORMAT = "0%";
12    private CheckedItemCollection<IErcItem> ercItems;
13
14    public ErcOptionsView() {
15      InitializeComponent();
16
17      possibilityTextBox.Validating += PossibilityTextBoxValidating;
18      possibilityTextBox.KeyDown += PossibilityTextBoxKeyDown;
19    }
20
21    private void PossibilityTextBoxKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
22      if (e.KeyCode == System.Windows.Forms.Keys.Enter) {
23        Validate();
24      }
25    }
26
27    private void PossibilityTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e) {
28      var text = possibilityTextBox.Text.Trim();
29      if (text.EndsWith("%")) {
30        text = text.Substring(0, text.Length - 1);
31      }
32
33      double value;
34      if (double.TryParse(text, out value)) {
35        if (value > 1) value /= 100.0;
36        possibilityTextBox.Text = value.ToString(PERCENTAGE_FORMAT);
37      } else {
38        possibilityTextBox.Text = "0%";
39        e.Cancel = true;
40      }
41
42      Content.ErcProbability = value;
43    }
44
45    public new ErcOptions Content {
46      get { return (ErcOptions)base.Content; }
47      set {
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.