Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/SearchAndReplaceCommand.cs @ 12065

Last change on this file since 12065 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.DataImporter.Command.View;
27using HeuristicLab.DataImporter.Data;
28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.DataImporter.Command {
33  [StorableClass]
34  [ViewableCommandInfoAttribute("Search and Replace", 1, ColumnGroupState.ColumnSelected, "Change Values",
35  Position = 0, OptionsView = typeof(SearchAndReplaceCommandView))]
36  public class SearchAndReplaceCommand : SearchWithMatchOperationCommandBase {
37    private Dictionary<Point, IComparable> changedCells;
38    private List<int> oldSorteColumnIndices;
39    private Dictionary<ColumnBase, SortOrder> oldSortOrder;
40
41    [StorableConstructor]
42    protected SearchAndReplaceCommand(bool deserializing)
43      : base(deserializing) {
44      changedCells = new Dictionary<Point, IComparable>();
45      oldSorteColumnIndices = new List<int>();
46      oldSortOrder = new Dictionary<ColumnBase, SortOrder>();
47    }
48
49    public SearchAndReplaceCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
50      : base(dataSet, columnGroupName, affectedColumns) {
51      changedCells = new Dictionary<Point, IComparable>();
52      oldSorteColumnIndices = new List<int>();
53      oldSortOrder = new Dictionary<ColumnBase, SortOrder>();
54    }
55
56    public override string Description {
57      get { return "Search and Replace"; }
58    }
59
60    [Storable]
61    private string replaceValue;
62    public string ReplaceValue {
63      get { return this.replaceValue; }
64      set { this.replaceValue = string.IsNullOrEmpty(value) ? null : value.Trim(); }
65    }
66
67    public override void Execute() {
68      base.Execute();
69      Point[] affectedCells = this.GetAffectedCells();
70      ColumnBase column;
71      foreach (Point cell in affectedCells) {
72        column = this.ColumnGroup.GetColumn(cell.X);
73        changedCells[cell] = column.GetValue(cell.Y);
74        column.ChangeValueOrLeaveOldValue(cell.Y, ReplaceValue);
75      }
76
77      foreach (ColumnBase col in this.ColumnGroup.Columns)
78        oldSortOrder.Add(col, col.SortOrder);
79
80      oldSorteColumnIndices = new List<int>(this.ColumnGroup.SortedColumnIndexes);
81      this.ColumnGroup.ResetSorting();
82
83      this.ColumnGroup.FireChanged();
84      ColumnGroup = null;
85    }
86
87    public override void UndoExecute() {
88      base.UndoExecute();
89      //needed because otherwise unchanged cells with the same replaceValue would also be affected
90      ColumnBase column;
91      foreach (KeyValuePair<Point, IComparable> cell in changedCells) {
92        column = this.ColumnGroup.GetColumn(cell.Key.X);
93        column.ChangeValueOrLeaveOldValue(cell.Key.Y, cell.Value);
94      }
95
96      this.ColumnGroup.SortedColumnIndexes = this.oldSorteColumnIndices;
97      foreach (KeyValuePair<ColumnBase, SortOrder> pair in oldSortOrder)
98        pair.Key.SortOrder = pair.Value;
99
100      oldSortOrder.Clear();
101      oldSorteColumnIndices.Clear();
102      changedCells.Clear();
103      this.ColumnGroup.FireChanged();
104      ColumnGroup = null;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.