#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Windows.Forms; using HeuristicLab.Analysis; using HeuristicLab.Collections; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; namespace HeuristicLab.DataPreprocessing.Views { [View("Preprocessing Chart View")] [Content(typeof(PreprocessingChartContent), false)] public partial class PreprocessingChartView : ItemView { private IChartLogic logic; private DataTable dataTable; private ICheckedItemList variableItemList; protected DataRowVisualProperties.DataRowChartType chartType; protected string chartTitle; private const string DEFAULT_CHART_TITLE = "Chart"; public PreprocessingChartView() { InitializeComponent(); chartType = DataRowVisualProperties.DataRowChartType.Line; chartTitle = DEFAULT_CHART_TITLE; } private void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs> checkedItems) { foreach(IndexedItem item in checkedItems.Items) { string variableName = item.Value.Value; if (VariableIsDisplayed(variableName)) { dataTable.Rows.Remove(variableName); } else { dataTable.Rows.Add(logic.CreateDataRow(variableName, chartType)); } } } public bool VariableIsDisplayed(string name) { foreach (var item in dataTable.Rows) { if (item.Name == name) return true; } return false; } private void InitDataTable() { dataTable = logic.CreateDataTable(chartTitle, chartType); dataTableView.Content = dataTable; } private void InitVariablesListBox() { variableItemList = logic.CreateVariableItemList(); checkedItemList.Content = variableItemList; } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.Changed += Content_Changed; } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.Changed -= Content_Changed; } void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) { dataTableView.Refresh(); } public new PreprocessingChartContent Content { get { return (PreprocessingChartContent)base.Content; } set { base.Content = value; } } protected override void OnContentChanged() { base.OnContentChanged(); if (Content != null) { logic = Content.ChartLogic; InitDataTable(); InitVariablesListBox(); variableItemList.CheckedItemsChanged += CheckedItemsChanged; logic.Changed += PreprocessingData_Changed; } } //TODO: refactor: possible code duplication with HistogramLogic void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) { switch (e.Type) { case DataPreprocessingChangedEventType.DeleteColumn: RemoveVariable(logic.GetVariableNameByIndex(e.Column)); break; case DataPreprocessingChangedEventType.AddColumn: AddVariable(logic.GetVariableNameByIndex(e.Column)); break; case DataPreprocessingChangedEventType.ChangeColumn: case DataPreprocessingChangedEventType.ChangeItem: dataTable.Rows.Remove(logic.GetVariableNameByIndex(e.Column)); dataTable.Rows.Add(logic.CreateDataRow(logic.GetVariableNameByIndex(e.Column),chartType)); break; case DataPreprocessingChangedEventType.DeleteRow: case DataPreprocessingChangedEventType.AddRow: InitDataTable(); break; } } // add variable to data table and item list private void AddVariable(string name) { dataTable.Rows.Add(logic.CreateDataRow(name, chartType)); variableItemList.Add(new StringValue(name)); } // remove variable from data table and item list private void RemoveVariable(string name) { dataTable.Rows.Remove(name); StringValue stringValue = FindVariableItemList(name); if (stringValue != null) variableItemList.Remove(stringValue); } private StringValue FindVariableItemList(string name) { foreach (StringValue stringValue in variableItemList) { if (stringValue.Value == name) return stringValue; } return null; } } }