Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Command/SortCommand.cs @ 17507

Last change on this file since 17507 was 16994, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

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