#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Data.Command { [StorableClass] public class SortCommand : ColumnGroupCommandBase { [Storable] private bool addToSortedColumns; [Storable] private int sortColumnIndex; private ICollection oldSortedColumnIndices; private IEnumerable oldSortOrdersForColumns; private int[] indices; [StorableConstructor] protected SortCommand(bool deserializing) : base(deserializing) { } public SortCommand(DataSet dataSet, string columnGroupName, int sortColumnIndex) : this(dataSet, columnGroupName, sortColumnIndex, false) { } public SortCommand(DataSet dataSet, string columnGroupName, int sortColumnIndex, bool addToSortedColumns) : base(dataSet, columnGroupName) { this.addToSortedColumns = addToSortedColumns; this.sortColumnIndex = sortColumnIndex; } public override void Execute() { base.Execute(); oldSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes); oldSortOrdersForColumns = ColumnGroup.SortOrdersForColumns.ToList(); if (!addToSortedColumns) { ColumnGroup.SortedColumnIndexes.Clear(); for (int i = 0; i < ColumnGroup.Columns.Count(); i++) if (i != sortColumnIndex) ColumnGroup.Columns.ElementAt(i).SortOrder = SortOrder.None; } if (!ColumnGroup.SortedColumnIndexes.Contains(sortColumnIndex)) ColumnGroup.SortedColumnIndexes.Add(sortColumnIndex); ICollection tempSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes); RowComparer rowComparer = new RowComparer(ColumnGroup, sortColumnIndex); indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy(k => k, rowComparer).ToArray(); List newColumns = Sort(); ColumnGroup.ClearColumns(); ColumnGroup.AddColumns(newColumns); ColumnGroup.SortedColumnIndexes = tempSortedColumnIndices; ColumnGroup.FireChanged(); this.ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy(i => indices[i]).ToArray(); List newColumns = Sort(); ColumnGroup.ClearColumns(); ColumnGroup.AddColumns(newColumns); ColumnGroup.SortedColumnIndexes = this.oldSortedColumnIndices; ColumnGroup.SortOrdersForColumns = oldSortOrdersForColumns; indices = null; oldSortedColumnIndices = null; oldSortOrdersForColumns = null; ColumnGroup.FireChanged(); this.ColumnGroup = null; } public override string Description { get { return "Sort ColumnGroup"; } } private List Sort() { List newColumns = new List(); ColumnBase newColumn; ColumnBase oldColumn; for (int col = 0; col < ColumnGroup.Columns.Count(); col++) { oldColumn = ColumnGroup.GetColumn(col); newColumn = oldColumn.CreateCopyOfColumnWithoutValues(); newColumn.SortOrder = oldColumn.SortOrder; if (col == sortColumnIndex) { if (newColumn.SortOrder == SortOrder.None || newColumn.SortOrder == SortOrder.Descending) newColumn.SortOrder = SortOrder.Ascending; else newColumn.SortOrder = SortOrder.Descending; } newColumns.Add(newColumn); } for (int j = 0; j < indices.Length; j++) { for (int col = 0; col < ColumnGroup.Columns.Count(); col++) newColumns[col].AddValue(ColumnGroup.Columns.ElementAt(col).GetValue(indices[j])); } return newColumns; } private class RowComparer : IComparer { private ColumnGroup columnGroup; private int sortColumnIndex; public RowComparer(ColumnGroup columnGroup, int sortColumnIndex) { this.columnGroup = columnGroup; this.sortColumnIndex = sortColumnIndex; } #region IComparer Members public int Compare(int i, int j) { IEnumerator e = columnGroup.SortedColumnIndexes.GetEnumerator(); int result = 0; while (result == 0 && e.MoveNext()) { IComparable x = columnGroup.GetColumn(e.Current).GetValue(i); IComparable y = columnGroup.GetColumn(e.Current).GetValue(j); if (x == null) { result = -1; if (y == null) result = 0; } else result = x.CompareTo(y); if (columnGroup.SortOrdersForColumns.ElementAt(e.Current) == SortOrder.Descending) result *= -1; if (e.Current == sortColumnIndex && columnGroup.Columns.ElementAt(e.Current).SortOrder != SortOrder.None) result *= -1; } return result; } #endregion } } }