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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
25 | using HeuristicLab.DataImporter.Data.Model;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.DataImporter.DataProcessor.Command {
|
---|
29 | [StorableClass]
|
---|
30 | public class ReorderColumnsCommand : DataSetCommandBase {
|
---|
31 | [Storable]
|
---|
32 | private Dictionary<string, int[]> displayedIndexes;
|
---|
33 |
|
---|
34 | [StorableConstructor]
|
---|
35 | protected ReorderColumnsCommand(bool deserializing) : base(deserializing) { }
|
---|
36 | public ReorderColumnsCommand(DataSet ds, Dictionary<string, int[]> displayedIndexes)
|
---|
37 | : base(ds) {
|
---|
38 | this.displayedIndexes = displayedIndexes;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override string Description {
|
---|
42 | get { return "Reorder Columns"; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override void Execute() {
|
---|
46 | base.Execute();
|
---|
47 | ReorderColumns(true);
|
---|
48 | this.DataSet.FireChanged();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public override void UndoExecute() {
|
---|
52 | base.UndoExecute();
|
---|
53 | ReorderColumns(false);
|
---|
54 | this.DataSet.FireChanged();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void ReorderColumns(bool forward) {
|
---|
58 | ColumnGroup grp;
|
---|
59 | int[] indices;
|
---|
60 | List<int> sortedColumnIndices;
|
---|
61 | int newIndex;
|
---|
62 | int oldIndex;
|
---|
63 | List<ColumnBase> columns;
|
---|
64 | foreach (KeyValuePair<string, int[]> pair in displayedIndexes) {
|
---|
65 | grp = this.DataSet.GetColumnGroup(pair.Key);
|
---|
66 | indices = pair.Value;
|
---|
67 | columns = grp.Columns.ToList();
|
---|
68 | sortedColumnIndices = new List<int>(grp.SortedColumnIndexes);
|
---|
69 | for (int j = 0; j < indices.Length; j++) {
|
---|
70 | sortedColumnIndices = new List<int>(grp.SortedColumnIndexes);
|
---|
71 | if (forward) {
|
---|
72 | newIndex = j;
|
---|
73 | oldIndex = indices[j];
|
---|
74 | } else {
|
---|
75 | newIndex = indices[j];
|
---|
76 | oldIndex = j;
|
---|
77 | }
|
---|
78 | grp.ReplaceColumn(newIndex, columns[oldIndex]);
|
---|
79 | grp.SortedColumnIndexes = sortedColumnIndices;
|
---|
80 | //corrects the SortedColumnIndices if sorted colum got reordered
|
---|
81 | if (grp.SortedColumnIndexes.Contains(oldIndex)) {
|
---|
82 | int insertPos = sortedColumnIndices.IndexOf(oldIndex);
|
---|
83 | sortedColumnIndices.Remove(oldIndex);
|
---|
84 | sortedColumnIndices.Insert(insertPos, newIndex);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | grp.SortedColumnIndexes = sortedColumnIndices;
|
---|
88 | grp.FireChanged();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|