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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.DataImporter.Command.View;
|
---|
27 | using HeuristicLab.DataImporter.Data;
|
---|
28 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
29 | using HeuristicLab.DataImporter.Data.Model;
|
---|
30 | using HEAL.Attic;
|
---|
31 |
|
---|
32 | namespace 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 | }
|
---|