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