#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.Drawing;
using System.Windows.Forms;
using System.Xml;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.DataImporter.Command.View;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Search and Replace", 1, ColumnGroupState.ColumnSelected, "Change Values",
Position = 0, OptionsView = typeof(SearchAndReplaceCommandView))]
public class SearchAndReplaceCommand : SearchWithMatchOperationCommandBase {
private Dictionary changedCells;
private List oldSorteColumnIndices;
private Dictionary oldSortOrder;
private SearchAndReplaceCommand()
: base(null, string.Empty, null) {
changedCells = new Dictionary();
oldSorteColumnIndices = new List();
oldSortOrder = new Dictionary();
}
public SearchAndReplaceCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
: base(dataSet, columnGroupName, affectedColumns) {
changedCells = new Dictionary();
oldSorteColumnIndices = new List();
oldSortOrder = new Dictionary();
}
public override string Description {
get { return "Search and Replace"; }
}
[Storable]
private string replaceValue;
public string ReplaceValue {
get { return this.replaceValue; }
set { this.replaceValue = string.IsNullOrEmpty(value) ? null : value.Trim(); }
}
public override void Execute() {
base.Execute();
Point[] affectedCells = this.GetAffectedCells();
ColumnBase column;
foreach (Point cell in affectedCells) {
column = this.ColumnGroup.GetColumn(cell.X);
changedCells[cell] = column.GetValue(cell.Y);
column.ChangeValueOrLeaveOldValue(cell.Y, ReplaceValue);
}
foreach (ColumnBase col in this.ColumnGroup.Columns)
oldSortOrder.Add(col, col.SortOrder);
oldSorteColumnIndices = new List(this.ColumnGroup.SortedColumnIndexes);
this.ColumnGroup.ResetSorting();
this.ColumnGroup.FireChanged();
ColumnGroup = null;
}
public override void UndoExecute() {
base.UndoExecute();
//needed because otherwise unchanged cells with the same replaceValue would also be affected
ColumnBase column;
foreach (KeyValuePair cell in changedCells) {
column = this.ColumnGroup.GetColumn(cell.Key.X);
column.ChangeValueOrLeaveOldValue(cell.Key.Y, cell.Value);
}
this.ColumnGroup.SortedColumnIndexes = this.oldSorteColumnIndices;
foreach (KeyValuePair pair in oldSortOrder)
pair.Key.SortOrder = pair.Value;
oldSortOrder.Clear();
oldSorteColumnIndices.Clear();
changedCells.Clear();
this.ColumnGroup.FireChanged();
ColumnGroup = null;
}
}
}