Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6706 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 4.0 KB
RevLine 
[6134]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
[6133]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Drawing;
27using System.Windows.Forms;
28using System.Xml;
29using HeuristicLab.DataImporter.Data;
30using HeuristicLab.DataImporter.Data.CommandBase;
31using HeuristicLab.DataImporter.Data.Model;
32using HeuristicLab.DataImporter.Command.View;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.DataImporter.Command {
36  [StorableClass]
37  [ViewableCommandInfoAttribute("Search and Replace", 1, ColumnGroupState.ColumnSelected, "Change Values",
38  Position = 0, OptionsView = typeof(SearchAndReplaceCommandView))]
39  public class SearchAndReplaceCommand : SearchWithMatchOperationCommandBase {
40    private Dictionary<Point, IComparable> changedCells;
41    private List<int> oldSorteColumnIndices;
42    private Dictionary<ColumnBase, SortOrder> oldSortOrder;
43
44    private SearchAndReplaceCommand()
45      : base(null, string.Empty, null) {
46      changedCells = new Dictionary<Point, IComparable>();
47      oldSorteColumnIndices = new List<int>();
48      oldSortOrder = new Dictionary<ColumnBase, SortOrder>();
49    }
50
51    public SearchAndReplaceCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
52      : base(dataSet, columnGroupName, affectedColumns) {
53      changedCells = new Dictionary<Point, IComparable>();
54      oldSorteColumnIndices = new List<int>();
55      oldSortOrder = new Dictionary<ColumnBase, SortOrder>();
56    }
57
58    public override string Description {
59      get { return "Search and Replace"; }
60    }
61
62    [Storable]
63    private string replaceValue;
64    public string ReplaceValue {
65      get { return this.replaceValue; }
66      set { this.replaceValue = string.IsNullOrEmpty(value) ? null : value.Trim(); }
67    }
68
69    public override void Execute() {
70      base.Execute();
71      Point[] affectedCells = this.GetAffectedCells();
72      ColumnBase column;
73      foreach (Point cell in affectedCells) {
74        column = this.ColumnGroup.GetColumn(cell.X);
75        changedCells[cell] = column.GetValue(cell.Y);
76        column.ChangeValueOrLeaveOldValue(cell.Y, ReplaceValue);
77      }
78
79      foreach (ColumnBase col in this.ColumnGroup.Columns)
80        oldSortOrder.Add(col, col.SortOrder);
81
82      oldSorteColumnIndices = new List<int>(this.ColumnGroup.SortedColumnIndexes);
83      this.ColumnGroup.ResetSorting();
84
85      this.ColumnGroup.FireChanged();
86      ColumnGroup = null;
87    }
88
89    public override void UndoExecute() {
90      base.UndoExecute();
91      //needed because otherwise unchanged cells with the same replaceValue would also be affected
92      ColumnBase column;
93      foreach (KeyValuePair<Point, IComparable> cell in changedCells) {
94        column = this.ColumnGroup.GetColumn(cell.Key.X);
95        column.ChangeValueOrLeaveOldValue(cell.Key.Y, cell.Value);
96      }
97
98      this.ColumnGroup.SortedColumnIndexes = this.oldSorteColumnIndices;
99      foreach (KeyValuePair<ColumnBase, SortOrder> pair in oldSortOrder)
100        pair.Key.SortOrder = pair.Value;
101
102      oldSortOrder.Clear();
103      oldSorteColumnIndices.Clear();
104      changedCells.Clear();
105      this.ColumnGroup.FireChanged();
106      ColumnGroup = null;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.