#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Collections;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
namespace HeuristicLab.DataPreprocessing.Views {
[View("TransformationList View")]
[Content(typeof(IItemList), false)]
public partial class TransformationListView : ItemListView {
internal IFilteredPreprocessingData PreprocessingData { get; set; }
public TransformationListView() {
InitializeComponent();
itemsGroupBox.Text = "Transformations";
}
protected override PreprocessingTransformation CreateItem() {
var variableNames = PreprocessingData.VariableNames.Select(x => new StringValue(x))
.Concat(Content.Select(x => new StringValue(x.TransformedVariable))).Distinct();
var newTransformation = new PreprocessingTransformation(variableNames);
newTransformation.TransformedVariableParameter.ValidValues.Add(new StringValue(""));
return newTransformation;
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) return;
int i = 0;
foreach (ListViewItem item in ItemsListView.Items) {
var transformation = PreprocessingData.Transformations[i];
item.ForeColor = transformation.IsApplied ? Color.Black : Color.Gray;
i++;
}
UpdateButtonsState();
}
protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
base.Content_ItemsAdded(sender, e);
foreach (var transformation in e.Items) {
UpdateListViewItemsColor(transformation.Value);
}
}
protected override void RegisterItemEvents(PreprocessingTransformation item) {
base.RegisterItemEvents(item);
item.PropertyChanged += Item_PropertyChanged;
}
protected override void DeregisterItemEvents(PreprocessingTransformation item) {
item.PropertyChanged -= Item_PropertyChanged;
base.DeregisterItemEvents(item);
}
private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
if (sender is PreprocessingTransformation transformation)
UpdateListViewItemsColor(transformation);
}
protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
base.itemsListView_SelectedIndexChanged(sender, e);
UpdateButtonsState();
}
private void UpdateListViewItemsColor(PreprocessingTransformation transformation) {
foreach (ListViewItem item in itemListViewItemMapping[transformation]) {
item.ForeColor = transformation.IsApplied ? Color.Black : Color.Gray;
}
}
private void UpdateButtonsState() {
if (itemsListView.SelectedItems.Count != 1) return;
int selectedIndex = itemsListView.SelectedIndices[0];
// TODO: do not move/remove already applied transformations or disallow of movement generally?
if (Content[selectedIndex].IsApplied) {
moveUpButton.Enabled = selectedIndex > 0 && Content[selectedIndex - 1].IsApplied;
moveDownButton.Enabled = false;
removeButton.Enabled = false;
}
}
}
}