#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
public abstract class ChangeColumnCommandBase : ColumnGroupCommandWithAffectedColumnsBase {
[Storable]
protected Type columnType;
protected List newColumns;
protected List oldColumns;
protected List positions;
protected ICollection oldSortedColumnIndexes;
protected IEnumerable oldSortOrder;
private ChangeColumnCommandBase()
: base(null, string.Empty, null) {
newColumns = new List();
oldColumns = new List();
positions = new List();
}
protected ChangeColumnCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns, Type newColumnType) :
base(dataSet, columnGroupName, affectedColumns) {
if (!newColumnType.IsSubclassOf(typeof(ColumnBase)))
throw new CommandExecutionException("Given columntype no subtype of ColumnBase.", this);
this.columnType = newColumnType;
newColumns = new List();
oldColumns = new List();
positions = new List();
}
public override void Execute() {
base.Execute();
ColumnBase newCol;
ColumnBase oldCol;
oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
oldSortedColumnIndexes = new List(ColumnGroup.SortedColumnIndexes);
for (int j = 0; j < AffectedColumns.Length; j++) {
oldCol = ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
if (oldCol.GetType() != columnType) {
positions.Add(AffectedColumns[j]);
oldColumns.Add(oldCol);
newCol = (ColumnBase)Activator.CreateInstance(columnType, new object[] { oldCol.Name, oldCol.TotalValuesCount });
for (int i = 0; i < oldCol.TotalValuesCount; i++)
newCol.AddValueOrNull(oldCol.GetValue(i));
newColumns.Add(newCol);
}
}
for (int i = 0; i < positions.Count; i++)
ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
ColumnGroup.FireChanged();
}
public override void UndoExecute() {
base.UndoExecute();
for (int i = 0; i < positions.Count; i++)
ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
ColumnGroup.SortOrdersForColumns = oldSortOrder;
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
oldSortedColumnIndexes = null;
oldSortOrder = null;
oldColumns.Clear();
newColumns.Clear();
positions.Clear();
ColumnGroup.FireChanged();
}
}
}