Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.DataProcessor/Command/ReorderColumnsCommand.cs @ 6134

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

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

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