Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.3/DefaultSymbolicExpressionGrammarView.cs @ 4047

Last change on this file since 4047 was 3904, checked in by mkommend, 15 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
33using HeuristicLab.Core.Views;
34using HeuristicLab.Core;
35
36namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
37  [View("Symbolic Expression Grammar View")]
38  [Content(typeof(DefaultSymbolicExpressionGrammar), true)]
39  public partial class DefaultSymbolicExpressionGrammarView : ItemView {
40    private CheckedItemList<Symbol> symbols;
41
42    public new DefaultSymbolicExpressionGrammar Content {
43      get { return (DefaultSymbolicExpressionGrammar)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public DefaultSymbolicExpressionGrammarView() {
48      InitializeComponent();
49      symbols = new CheckedItemList<Symbol>();
50      symbols.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<Symbol>>(symbols_CheckedItemsChanged);
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      UpdateControl();
56    }
57
58    protected override void SetEnabledStateOfControls() {
59      base.SetEnabledStateOfControls();
60      checkedItemListView.Enabled = Content != null;
61      checkedItemListView.ReadOnly = ReadOnly;
62    }
63
64    #region content event handlers
65    private void Content_Changed(object sender, EventArgs e) {
66      UpdateControl();
67    }
68    #endregion
69
70    #region helpers
71    private void UpdateControl() {
72      if (Content == null) {
73        ClearSymbols();
74        checkedItemListView.Content = symbols.AsReadOnly();
75      } else {
76        ClearSymbols();
77        foreach (Symbol symbol in Content.Symbols) {
78          if (!(symbol is ReadOnlySymbol)) {
79            symbol.Changed += new EventHandler(symbol_Changed);
80            symbols.Add(symbol, symbol.InitialFrequency > 0.0);
81          }
82        }
83        checkedItemListView.Content = symbols.AsReadOnly();
84      }
85      SetEnabledStateOfControls();
86    }
87
88
89    private void symbol_Changed(object sender, EventArgs e) {
90      Symbol symbol = (Symbol)sender;
91      symbols.SetItemCheckedState(symbol, symbol.InitialFrequency > 0.0);
92    }
93
94    private void symbols_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<Symbol>> e) {
95      ICheckedItemList<Symbol> checkedItemList = (ICheckedItemList<Symbol>)sender;
96      foreach (var indexedItem in e.Items) {
97        if (checkedItemList.ItemChecked(indexedItem.Value)) {
98          indexedItem.Value.InitialFrequency = 1.0;
99        } else {
100          indexedItem.Value.InitialFrequency = 0.0;
101        }
102      }
103    }
104    private void ClearSymbols() {
105      foreach (Symbol s in symbols)
106        s.Changed -= new EventHandler(symbol_Changed);
107      symbols.Clear();
108    }
109    #endregion
110  }
111}
Note: See TracBrowser for help on using the repository browser.