Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingCheckedVariablesView.cs @ 16189

Last change on this file since 16189 was 14562, checked in by abeham, 8 years ago

#2701: Updated branch to trunk

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33  [View("Preprocessing Checked Variables View")]
34  [Content(typeof(PreprocessingChartContent), false)]
35  public abstract partial class PreprocessingCheckedVariablesView : ItemView {
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 checkedItemList.Content.CheckedItems.Select(i => i.Value.Value).ToList();
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content == null) return;
56
57      if (Content.VariableItemList == null) {
58        IList<string> inputs = Content.PreprocessingData.InputVariables;
59        if (Content.PreprocessingData.TargetVariable != null)
60          inputs = inputs.Union(new[] { Content.PreprocessingData.TargetVariable }).ToList();
61        Content.VariableItemList = Content.CreateVariableItemList(inputs);
62      } else {
63        var checkedNames = Content.VariableItemList.CheckedItems.Select(x => x.Value.Value);
64        Content.VariableItemList = Content.CreateVariableItemList(checkedNames.ToList());
65      }
66      Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
67
68      checkedItemList.Content = Content.VariableItemList;
69      var target = Content.PreprocessingData.TargetVariable;
70      var inputAndTarget = Content.PreprocessingData.InputVariables.Union(target != null ? new[] { target } : new string[] { });
71      foreach (var col in Content.PreprocessingData.GetDoubleVariableNames().Except(inputAndTarget)) {
72        var listViewItem = checkedItemList.ItemsListView.FindItemWithText(col, false, 0, false);
73        listViewItem.ForeColor = Color.LightGray;
74      }
75    }
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.PreprocessingData.Changed += PreprocessingData_Changed;
79      Content.PreprocessingData.SelectionChanged += PreprocessingData_SelctionChanged;
80    }
81    protected override void DeregisterContentEvents() {
82      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
83      Content.PreprocessingData.SelectionChanged -= PreprocessingData_SelctionChanged;
84      base.DeregisterContentEvents();
85    }
86
87    protected virtual void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
88    }
89
90    private void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
91      switch (e.Type) {
92        case DataPreprocessingChangedEventType.DeleteColumn:
93          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
94          break;
95        case DataPreprocessingChangedEventType.AddColumn:
96          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
97          break;
98        case DataPreprocessingChangedEventType.ChangeColumn:
99        case DataPreprocessingChangedEventType.ChangeItem:
100          UpdateVariable(Content.PreprocessingData.GetVariableName(e.Column));
101          break;
102        default:
103          ResetAllVariables();
104          break;
105      }
106    }
107
108    protected virtual void AddVariable(string name) {
109      Content.VariableItemList.Add(new StringValue(name));
110      if (!Content.PreprocessingData.InputVariables.Contains(name) && Content.PreprocessingData.TargetVariable != name) {
111        var listViewItem = checkedItemList.ItemsListView.FindItemWithText(name, false, 0, false);
112        listViewItem.ForeColor = Color.LightGray;
113      }
114    }
115    protected virtual void RemoveVariable(string name) {
116      var stringValue = Content.VariableItemList.SingleOrDefault(n => n.Value == name);
117      if (stringValue != null)
118        Content.VariableItemList.Remove(stringValue);
119    }
120    protected virtual void UpdateVariable(string name) {
121    }
122    protected virtual void ResetAllVariables() {
123    }
124
125    protected virtual void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
126    }
127
128    #region ContextMenu Events
129    private void variablesListcontextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
130      var data = Content.PreprocessingData;
131      checkInputsTargetToolStripMenuItem.Text = "Check Inputs" + (data.TargetVariable != null ? "+Target" : "");
132      checkOnlyInputsTargetToolStripMenuItem.Text = "Check only Inputs" + (data.TargetVariable != null ? "+Target" : "");
133    }
134    private void checkInputsTargetToolStripMenuItem_Click(object sender, EventArgs e) {
135      foreach (var name in checkedItemList.Content) {
136        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
137        if (isInputTarget) {
138          checkedItemList.Content.SetItemCheckedState(name, true);
139        }
140      }
141    }
142    private void checkOnlyInputsTargetToolStripMenuItem_Click(object sender, EventArgs e) {
143      foreach (var name in checkedItemList.Content) {
144        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
145        checkedItemList.Content.SetItemCheckedState(name, isInputTarget);
146      }
147    }
148    private void checkAllToolStripMenuItem_Click(object sender, EventArgs e) {
149      foreach (var name in checkedItemList.Content) {
150        checkedItemList.Content.SetItemCheckedState(name, true);
151      }
152    }
153    private void uncheckAllToolStripMenuItem_Click(object sender, EventArgs e) {
154      foreach (var name in checkedItemList.Content) {
155        checkedItemList.Content.SetItemCheckedState(name, false);
156      }
157    }
158    #endregion
159  }
160}
161
162
Note: See TracBrowser for help on using the repository browser.