Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 5.9 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.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).ToArray();
74      Array.Sort(indices, rowComparer);
75
76      List<ColumnBase> newColumns = Sort();
77      ColumnGroup.ClearColumns();
78      ColumnGroup.AddColumns(newColumns);
79      ColumnGroup.SortedColumnIndexes = tempSortedColumnIndices;
80      ColumnGroup.FireChanged();
81      this.ColumnGroup = null;
82    }
83
84    public override void UndoExecute() {
85      base.UndoExecute();
86      indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy<int, int>(i => indices[i]).ToArray();
87      List<ColumnBase> newColumns = Sort();
88      ColumnGroup.ClearColumns();
89      ColumnGroup.AddColumns(newColumns);
90      ColumnGroup.SortedColumnIndexes = this.oldSortedColumnIndices;
91      ColumnGroup.SortOrdersForColumns = oldSortOrdersForColumns;
92      indices = null;
93      oldSortedColumnIndices = null;
94      oldSortOrdersForColumns = null;
95      ColumnGroup.FireChanged();
96      this.ColumnGroup = null;
97    }
98
99    public override string Description {
100      get { return "Sort ColumnGroup"; }
101    }
102
103    private List<ColumnBase> Sort() {
104      List<ColumnBase> newColumns = new List<ColumnBase>();
105      ColumnBase newColumn;
106      ColumnBase oldColumn;
107
108      for (int col = 0; col < ColumnGroup.Columns.Count(); col++) {
109        oldColumn = ColumnGroup.GetColumn(col);
110        newColumn = oldColumn.CreateCopyOfColumnWithoutValues();
111        newColumn.SortOrder = oldColumn.SortOrder;
112        if (col == sortColumnIndex) {
113          if (newColumn.SortOrder == SortOrder.None || newColumn.SortOrder == SortOrder.Descending)
114            newColumn.SortOrder = SortOrder.Ascending;
115          else
116            newColumn.SortOrder = SortOrder.Descending;
117        }
118        newColumns.Add(newColumn);
119      }
120
121      for (int j = 0; j < indices.Length; j++) {
122        for (int col = 0; col < ColumnGroup.Columns.Count(); col++)
123          newColumns[col].AddValue(ColumnGroup.Columns.ElementAt(col).GetValue(indices[j]));
124      }
125      return newColumns;
126    }
127
128    private class RowComparer : IComparer<int> {
129      private ColumnGroup columnGroup;
130      private int sortColumnIndex;
131      public RowComparer(ColumnGroup columnGroup, int sortColumnIndex) {
132        this.columnGroup = columnGroup;
133        this.sortColumnIndex = sortColumnIndex;
134      }
135
136      #region IComparer<int> Members
137      public int Compare(int i, int j) {
138        IEnumerator<int> e = columnGroup.SortedColumnIndexes.GetEnumerator();
139        int result = 0;
140        while (result == 0 && e.MoveNext()) {
141          IComparable x = columnGroup.GetColumn(e.Current).GetValue(i);
142          IComparable y = columnGroup.GetColumn(e.Current).GetValue(j);
143          if (x == null) {
144            result = -1;
145            if (y == null)
146              result = 0;
147          } else
148            result = x.CompareTo(y);
149          if (columnGroup.SortOrdersForColumns.ElementAt(e.Current) == SortOrder.Descending)
150            result *= -1;
151          if (e.Current == sortColumnIndex && columnGroup.Columns.ElementAt(e.Current).SortOrder != SortOrder.None)
152            result *= -1;
153        }
154        return result;
155      }
156      #endregion
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.