Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/SearchAndReplaceCommand.cs @ 18242

Last change on this file since 18242 was 16994, checked in by gkronber, 5 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

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 HEAL.Attic;
31
32namespace HeuristicLab.DataImporter.Command {
33  [StorableType("4C4A7701-D4BF-4E3E-9829-E9659A00AC6E")]
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(StorableConstructorFlag _) : base(_) {
43      changedCells = new Dictionary<Point, IComparable>();
44      oldSorteColumnIndices = new List<int>();
45      oldSortOrder = new Dictionary<ColumnBase, SortOrder>();
46    }
47
48    public SearchAndReplaceCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
49      : base(dataSet, columnGroupName, affectedColumns) {
50      changedCells = new Dictionary<Point, IComparable>();
51      oldSorteColumnIndices = new List<int>();
52      oldSortOrder = new Dictionary<ColumnBase, SortOrder>();
53    }
54
55    public override string Description {
56      get { return "Search and Replace"; }
57    }
58
59    [Storable]
60    private string replaceValue;
61    public string ReplaceValue {
62      get { return this.replaceValue; }
63      set { this.replaceValue = string.IsNullOrEmpty(value) ? null : value.Trim(); }
64    }
65
66    public override void Execute() {
67      base.Execute();
68      Point[] affectedCells = this.GetAffectedCells();
69      ColumnBase column;
70      foreach (Point cell in affectedCells) {
71        column = this.ColumnGroup.GetColumn(cell.X);
72        changedCells[cell] = column.GetValue(cell.Y);
73        column.ChangeValueOrLeaveOldValue(cell.Y, ReplaceValue);
74      }
75
76      foreach (ColumnBase col in this.ColumnGroup.Columns)
77        oldSortOrder.Add(col, col.SortOrder);
78
79      oldSorteColumnIndices = new List<int>(this.ColumnGroup.SortedColumnIndexes);
80      this.ColumnGroup.ResetSorting();
81
82      this.ColumnGroup.FireChanged();
83      ColumnGroup = null;
84    }
85
86    public override void UndoExecute() {
87      base.UndoExecute();
88      //needed because otherwise unchanged cells with the same replaceValue would also be affected
89      ColumnBase column;
90      foreach (KeyValuePair<Point, IComparable> cell in changedCells) {
91        column = this.ColumnGroup.GetColumn(cell.Key.X);
92        column.ChangeValueOrLeaveOldValue(cell.Key.Y, cell.Value);
93      }
94
95      this.ColumnGroup.SortedColumnIndexes = this.oldSorteColumnIndices;
96      foreach (KeyValuePair<ColumnBase, SortOrder> pair in oldSortOrder)
97        pair.Key.SortOrder = pair.Value;
98
99      oldSortOrder.Clear();
100      oldSorteColumnIndices.Clear();
101      changedCells.Clear();
102      this.ColumnGroup.FireChanged();
103      ColumnGroup = null;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.