Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Command/SortCommand.cs @ 8587

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

#1734 updated copyright year in all files of the DataImporter branch

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Windows.Forms;
27using System.Xml;
28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31namespace HeuristicLab.DataImporter.Data.Command {
32  [StorableClass]
33  public class SortCommand : ColumnGroupCommandBase {
34    [Storable]
35    private bool addToSortedColumns;
36    [Storable]
37    private int sortColumnIndex;
38
39    private ICollection<int> oldSortedColumnIndices;
40    private IEnumerable<SortOrder> oldSortOrdersForColumns;
41    private int[] indices;
42
43    private SortCommand()
44      : base(null, string.Empty) {
45    }
46
47    public SortCommand(DataSet dataSet, string columnGroupName, int sortColumnIndex) :
48      this(dataSet, columnGroupName, sortColumnIndex, false) {
49    }
50
51    public SortCommand(DataSet dataSet, string columnGroupName, int sortColumnIndex, bool addToSortedColumns)
52      : base(dataSet, columnGroupName) {
53      this.addToSortedColumns = addToSortedColumns;
54      this.sortColumnIndex = sortColumnIndex;
55    }
56
57    public override void Execute() {
58      base.Execute();
59      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
60      oldSortOrdersForColumns = ColumnGroup.SortOrdersForColumns.ToList();
61
62      if (!addToSortedColumns) {
63        ColumnGroup.SortedColumnIndexes.Clear();
64        for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
65          if (i != sortColumnIndex)
66            ColumnGroup.Columns.ElementAt(i).SortOrder = SortOrder.None;
67      }
68      if (!ColumnGroup.SortedColumnIndexes.Contains(sortColumnIndex))
69        ColumnGroup.SortedColumnIndexes.Add(sortColumnIndex);
70      ICollection<int> tempSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
71
72      RowComparer rowComparer = new RowComparer(ColumnGroup, sortColumnIndex);
73      indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy(k => k, rowComparer).ToArray();
74
75      List<ColumnBase> newColumns = Sort();
76      ColumnGroup.ClearColumns();
77      ColumnGroup.AddColumns(newColumns);
78      ColumnGroup.SortedColumnIndexes = tempSortedColumnIndices;
79      ColumnGroup.FireChanged();
80      this.ColumnGroup = null;
81    }
82
83    public override void UndoExecute() {
84      base.UndoExecute();
85      indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy<int, int>(i => indices[i]).ToArray();
86      List<ColumnBase> newColumns = Sort();
87      ColumnGroup.ClearColumns();
88      ColumnGroup.AddColumns(newColumns);
89      ColumnGroup.SortedColumnIndexes = this.oldSortedColumnIndices;
90      ColumnGroup.SortOrdersForColumns = oldSortOrdersForColumns;
91      indices = null;
92      oldSortedColumnIndices = null;
93      oldSortOrdersForColumns = null;
94      ColumnGroup.FireChanged();
95      this.ColumnGroup = null;
96    }
97
98    public override string Description {
99      get { return "Sort ColumnGroup"; }
100    }
101
102    private List<ColumnBase> Sort() {
103      List<ColumnBase> newColumns = new List<ColumnBase>();
104      ColumnBase newColumn;
105      ColumnBase oldColumn;
106
107      for (int col = 0; col < ColumnGroup.Columns.Count(); col++) {
108        oldColumn = ColumnGroup.GetColumn(col);
109        newColumn = oldColumn.CreateCopyOfColumnWithoutValues();
110        newColumn.SortOrder = oldColumn.SortOrder;
111        if (col == sortColumnIndex) {
112          if (newColumn.SortOrder == SortOrder.None || newColumn.SortOrder == SortOrder.Descending)
113            newColumn.SortOrder = SortOrder.Ascending;
114          else
115            newColumn.SortOrder = SortOrder.Descending;
116        }
117        newColumns.Add(newColumn);
118      }
119
120      for (int j = 0; j < indices.Length; j++) {
121        for (int col = 0; col < ColumnGroup.Columns.Count(); col++)
122          newColumns[col].AddValue(ColumnGroup.Columns.ElementAt(col).GetValue(indices[j]));
123      }
124      return newColumns;
125    }
126
127    private class RowComparer : IComparer<int> {
128      private ColumnGroup columnGroup;
129      private int sortColumnIndex;
130      public RowComparer(ColumnGroup columnGroup, int sortColumnIndex) {
131        this.columnGroup = columnGroup;
132        this.sortColumnIndex = sortColumnIndex;
133      }
134
135      #region IComparer<int> Members
136      public int Compare(int i, int j) {
137        IEnumerator<int> e = columnGroup.SortedColumnIndexes.GetEnumerator();
138        int result = 0;
139        while (result == 0 && e.MoveNext()) {
140          IComparable x = columnGroup.GetColumn(e.Current).GetValue(i);
141          IComparable y = columnGroup.GetColumn(e.Current).GetValue(j);
142          if (x == null) {
143            result = -1;
144            if (y == null)
145              result = 0;
146          } else
147            result = x.CompareTo(y);
148          if (columnGroup.SortOrdersForColumns.ElementAt(e.Current) == SortOrder.Descending)
149            result *= -1;
150          if (e.Current == sortColumnIndex && columnGroup.Columns.ElementAt(e.Current).SortOrder != SortOrder.None)
151            result *= -1;
152        }
153        return result;
154      }
155      #endregion
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.