Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9691 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.2 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;
27
28namespace 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}
Note: See TracBrowser for help on using the repository browser.