Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.3/DefaultSymbolicExpressionGrammarView.cs @ 12438

Last change on this file since 12438 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
31  [View("Symbolic Expression Grammar View")]
32  [Content(typeof(DefaultSymbolicExpressionGrammar), true)]
33  public partial class DefaultSymbolicExpressionGrammarView : ItemView {
34    private CheckedItemList<Symbol> symbols;
35
36    public new DefaultSymbolicExpressionGrammar Content {
37      get { return (DefaultSymbolicExpressionGrammar)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public DefaultSymbolicExpressionGrammarView() {
42      InitializeComponent();
43      symbols = new CheckedItemList<Symbol>();
44      symbols.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<Symbol>>(symbols_CheckedItemsChanged);
45    }
46
47    protected override void OnContentChanged() {
48      base.OnContentChanged();
49      UpdateControl();
50    }
51
52    protected override void SetEnabledStateOfControls() {
53      base.SetEnabledStateOfControls();
54      checkedItemListView.Enabled = Content != null;
55      checkedItemListView.ReadOnly = ReadOnly;
56    }
57
58    #region content event handlers
59    private void Content_Changed(object sender, EventArgs e) {
60      UpdateControl();
61    }
62    #endregion
63
64    #region helpers
65    private void UpdateControl() {
66      if (Content == null) {
67        ClearSymbols();
68        checkedItemListView.Content = symbols.AsReadOnly();
69      } else {
70        ClearSymbols();
71        foreach (Symbol symbol in Content.Symbols) {
72          if (!(symbol is ReadOnlySymbol)) {
73            symbol.Changed += new EventHandler(symbol_Changed);
74            symbols.Add(symbol, symbol.InitialFrequency > 0.0);
75          }
76        }
77        checkedItemListView.Content = symbols.AsReadOnly();
78      }
79      SetEnabledStateOfControls();
80    }
81
82
83    private void symbol_Changed(object sender, EventArgs e) {
84      Symbol symbol = (Symbol)sender;
85      symbols.SetItemCheckedState(symbol, symbol.InitialFrequency > 0.0);
86    }
87
88    private void symbols_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<Symbol>> e) {
89      ICheckedItemList<Symbol> checkedItemList = (ICheckedItemList<Symbol>)sender;
90      foreach (var indexedItem in e.Items) {
91        if (checkedItemList.ItemChecked(indexedItem.Value)) {
92          indexedItem.Value.InitialFrequency = 1.0;
93        } else {
94          indexedItem.Value.InitialFrequency = 0.0;
95        }
96      }
97    }
98    private void ClearSymbols() {
99      foreach (Symbol s in symbols)
100        s.Changed -= new EventHandler(symbol_Changed);
101      symbols.Clear();
102    }
103    #endregion
104  }
105}
Note: See TracBrowser for help on using the repository browser.