#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;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HEAL.Attic;
namespace HeuristicLab.DataImporter.Data.Command {
[StorableType("DA73B49F-B7EE-434F-BCE5-F473AB2C5C7D")]
public class PasteValuesCommand : ColumnGroupCommandBase {
[Storable]
private IComparable[,] newValues;
[Storable]
private int columnIndex;
[Storable]
private int rowIndex;
private IComparable[,] oldValues;
private int insertedRows;
private ICollection oldSortedColumnIndices;
private IEnumerable oldSortOrder;
[StorableConstructor]
protected PasteValuesCommand(StorableConstructorFlag _) : base(_) { }
public PasteValuesCommand(DataSet dataSet, string columnGroupName, int columnIndex, int rowIndex, string values)
: base(dataSet, columnGroupName) {
this.columnIndex = columnIndex;
this.rowIndex = rowIndex;
string[] lines = values.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string[] cells;
for (int i = 0; i < lines.Length; i++) {
cells = lines[i].Split('\t');
if (this.newValues == null) {
newValues = new IComparable[cells.Length, lines.Length];
}
for (int j = 0; j < cells.Length; j++)
newValues[j, i] = string.IsNullOrEmpty(cells[j]) ? null : cells[j];
}
}
public override void Execute() {
base.Execute();
oldValues = new IComparable[
Math.Min(ColumnGroup.Columns.Count() - columnIndex, newValues.GetLength(0)),
Math.Min(ColumnGroup.RowCount - rowIndex, newValues.GetLength(1))];
bool rowAdded = false;
oldSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes);
oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
for (int row = 0; row < newValues.GetLength(1); row++) {
if (row + rowIndex >= ColumnGroup.RowCount) {
ColumnGroup.AddRow(ColumnGroup.GetEmptyRow());
this.insertedRows++;
rowAdded = true;
}
for (int col = 0; col < newValues.GetLength(0); col++) {
if (col + columnIndex < ColumnGroup.Columns.Count()) {
if (!rowAdded)
oldValues[col, row] = ColumnGroup.Columns.ElementAt(col + columnIndex).GetValue(row + rowIndex);
ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValueOrNull(row + rowIndex, newValues[col, row]);
}
}
}
ColumnGroup.FireChanged();
this.ColumnGroup = null;
}
public override void UndoExecute() {
base.UndoExecute();
for (int row = 0; row < oldValues.GetLength(1); row++) {
for (int col = 0; col < oldValues.GetLength(0); col++) {
ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValue(row + rowIndex, oldValues[col, row]);
}
}
for (int i = 0; i < insertedRows; i++)
ColumnGroup.DeleteRow(ColumnGroup.RowCount - 1);
oldValues = null;
insertedRows = 0;
ColumnGroup.SortOrdersForColumns = oldSortOrder;
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
oldSortedColumnIndices = null;
oldSortOrder = null;
ColumnGroup.FireChanged();
this.ColumnGroup = null;
}
public override string Description {
get { return "Copied values pasted"; }
}
}
}