[8702] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8702] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Windows.Forms;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Clients.Hive.JobManager {
|
---|
| 27 | /// <summary>
|
---|
| 28 | /// Comparer for sorting items in a list view by date
|
---|
| 29 | /// See: http://msdn.microsoft.com/en-us/library/ms996467.aspx
|
---|
| 30 | /// </summary>
|
---|
[8718] | 31 | public class ListViewItemDateComparer : IComparer {
|
---|
[8702] | 32 | private int col;
|
---|
[9523] | 33 | public SortOrder Order { get; set; }
|
---|
[8702] | 34 |
|
---|
[8718] | 35 | public ListViewItemDateComparer() {
|
---|
[8702] | 36 | col = 0;
|
---|
[9523] | 37 | Order = SortOrder.Ascending;
|
---|
[8702] | 38 | }
|
---|
| 39 |
|
---|
[8718] | 40 | public ListViewItemDateComparer(int column, SortOrder order) {
|
---|
[8702] | 41 | col = column;
|
---|
[9523] | 42 | Order = order;
|
---|
[8702] | 43 | }
|
---|
| 44 |
|
---|
| 45 | public int Compare(object x, object y) {
|
---|
| 46 | int returnVal;
|
---|
[8708] | 47 | bool result;
|
---|
| 48 | DateTime firstDate, secondDate;
|
---|
| 49 | ListViewItem listViewItemX, listViewItemY;
|
---|
| 50 | listViewItemX = x as ListViewItem;
|
---|
| 51 | listViewItemY = y as ListViewItem;
|
---|
[8702] | 52 |
|
---|
[8708] | 53 | if (listViewItemX == null || listViewItemY == null) {
|
---|
[8718] | 54 | throw new ArgumentException(string.Format("The ListViewItemDateComparer expects ListViewItems but received {0} and {1}.",
|
---|
[8702] | 55 | x.GetType().ToString(), y.GetType().ToString()));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[8708] | 58 | result = DateTime.TryParse(listViewItemX.SubItems[col].Text, out firstDate);
|
---|
| 59 | result = DateTime.TryParse(listViewItemY.SubItems[col].Text, out secondDate) && result;
|
---|
| 60 |
|
---|
| 61 | if (result) {
|
---|
[8702] | 62 | returnVal = DateTime.Compare(firstDate, secondDate);
|
---|
[8708] | 63 | } else {
|
---|
[8718] | 64 | throw new ArgumentException(string.Format("The ListViewItemDateComparer expects DateTimes. Can't parse {0} and {1}.",
|
---|
| 65 | listViewItemX.SubItems[col].Text, listViewItemY.SubItems[col].Text));
|
---|
[8702] | 66 | }
|
---|
| 67 |
|
---|
[9523] | 68 | if (Order == SortOrder.Descending) {
|
---|
[8702] | 69 | // invert the value returned by Compare.
|
---|
| 70 | returnVal *= -1;
|
---|
| 71 | }
|
---|
| 72 | return returnVal;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|