source: branches/2839_HiveProjectManagement/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingCheckedVariablesView.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 15 months ago

#2839:

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.DataPreprocessing.Views {
32  [View("Preprocessing Checked Variables View")]
33  [Content(typeof(PreprocessingChartContent), false)]
34  public abstract partial class PreprocessingCheckedVariablesView : ItemView {
35    protected bool SuppressCheckedChangedUpdate = false;
36
37    public new PreprocessingChartContent Content {
38      get { return (PreprocessingChartContent)base.Content; }
39      set { base.Content = value; }
40    }
41
42    protected PreprocessingCheckedVariablesView() {
43      InitializeComponent();
44    }
45
46    protected bool IsVariableChecked(string name) {
47      return Content.VariableItemList.CheckedItems.Any(x => x.Value.Value == name);
48    }
49    protected IList<string> GetCheckedVariables() {
50      return Content.VariableItemList.CheckedItems.Select(i => i.Value.Value).ToList();
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content == null) return;
56
57      variablesListView.ItemChecked -= variablesListView_ItemChecked;
58      variablesListView.Items.Clear();
59      foreach (var variable in Content.VariableItemList) {
60        bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable.Value)
61          || Content.PreprocessingData.TargetVariable == variable.Value;
62        variablesListView.Items.Add(new ListViewItem(variable.Value) {
63          Tag = variable,
64          Checked = IsVariableChecked(variable.Value),
65          ForeColor = isInputTarget ? Color.Black : Color.Gray
66        });
67      }
68      variablesListView.ItemChecked += variablesListView_ItemChecked;
69    }
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72      Content.PreprocessingData.Changed += PreprocessingData_Changed;
73      Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
74    }
75
76    protected override void DeregisterContentEvents() {
77      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
78      Content.VariableItemList.CheckedItemsChanged -= CheckedItemsChanged;
79      base.DeregisterContentEvents();
80    }
81
82    protected virtual void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
83      // sync listview
84      foreach (var item in checkedItems.Items)
85        variablesListView.Items[item.Index].Checked = Content.VariableItemList.ItemChecked(item.Value);
86    }
87    private void variablesListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
88      // sync checked item list
89      var variable = (StringValue)e.Item.Tag;
90      Content.VariableItemList.SetItemCheckedState(variable, e.Item.Checked);
91    }
92
93    private void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
94      switch (e.Type) {
95        case DataPreprocessingChangedEventType.DeleteColumn:
96          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
97          break;
98        case DataPreprocessingChangedEventType.AddColumn:
99          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
100          break;
101        case DataPreprocessingChangedEventType.ChangeColumn:
102        case DataPreprocessingChangedEventType.ChangeItem:
103          UpdateVariable(Content.PreprocessingData.GetVariableName(e.Column));
104          break;
105        default:
106          ResetAllVariables();
107          break;
108      }
109    }
110
111    protected virtual void AddVariable(string name) {
112      Content.VariableItemList.Add(new StringValue(name));
113      if (!Content.PreprocessingData.InputVariables.Contains(name) && Content.PreprocessingData.TargetVariable != name) {
114        var listViewItem = variablesListView.FindItemWithText(name, false, 0, false);
115        listViewItem.ForeColor = Color.LightGray;
116      }
117    }
118    protected virtual void RemoveVariable(string name) {
119      var stringValue = Content.VariableItemList.SingleOrDefault(n => n.Value == name);
120      if (stringValue != null)
121        Content.VariableItemList.Remove(stringValue);
122    }
123    protected virtual void UpdateVariable(string name) { }
124    protected virtual void ResetAllVariables() { }
125    protected virtual void CheckedChangedUpdate() { }
126
127    private void checkInputsTargetButton_Click(object sender, System.EventArgs e) {
128      SuppressCheckedChangedUpdate = true;
129      foreach (var name in Content.VariableItemList) {
130        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
131        Content.VariableItemList.SetItemCheckedState(name, isInputTarget);
132      }
133      SuppressCheckedChangedUpdate = false;
134      CheckedChangedUpdate();
135    }
136
137    private void checkAllButton_Click(object sender, System.EventArgs e) {
138      SuppressCheckedChangedUpdate = true;
139      foreach (var name in Content.VariableItemList) {
140        Content.VariableItemList.SetItemCheckedState(name, true);
141      }
142      SuppressCheckedChangedUpdate = false;
143      CheckedChangedUpdate();
144    }
145
146    private void uncheckAllButton_Click(object sender, System.EventArgs e) {
147      SuppressCheckedChangedUpdate = true;
148      foreach (var name in Content.VariableItemList) {
149        Content.VariableItemList.SetItemCheckedState(name, false);
150      }
151      SuppressCheckedChangedUpdate = false;
152      CheckedChangedUpdate();
153    }
154  }
155}
156
157
Note: See TracBrowser for help on using the repository browser.