Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.DataProcessor/Command/ReorderColumnsCommand.cs @ 16567

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

#2520: changed StorableConstructors and added StorableType attributes in HeuristicLab.DataImporter addon

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