~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/ICSharpCode.SharpDevelop.Widgets/Project/ListViewSorting/ListViewMultipleColumnsComparer.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Windows.Forms;
 
7
 
 
8
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting
 
9
{
 
10
        /// <summary>
 
11
        /// Compares ListViewItems by comparing multiple columns,
 
12
        /// using an object that implements <see cref="IListViewItemComparer"/>
 
13
        /// for every column to compare.
 
14
        /// </summary>
 
15
        public class ListViewMultipleColumnsComparer : IListViewItemComparer
 
16
        {
 
17
                readonly List<IListViewItemComparer> comparers = new List<IListViewItemComparer>(2);
 
18
                readonly List<int> columns = new List<int>(2);
 
19
                
 
20
                /// <summary>
 
21
                /// Initializes a new instance of the <see cref="ListViewMultipleColumnsComparer"/> class.
 
22
                /// </summary>
 
23
                /// <param name="firstComparer">The <see cref="IListViewItemComparer"/> to use to compare the first column.</param>
 
24
                /// <param name="firstColumn">The 0-based index of the first column to compare.</param>
 
25
                /// <param name="secondComparer">The <see cref="IListViewItemComparer"/> to use to compare the second column.</param>
 
26
                /// <param name="secondColumn">The 0-based index of the second column to compare.</param>
 
27
                /// <remarks>
 
28
                /// You can add more columns to compare by using the <see cref="AddComparer"/> method.
 
29
                /// </remarks>
 
30
                public ListViewMultipleColumnsComparer(IListViewItemComparer firstComparer, int firstColumn, IListViewItemComparer secondComparer, int secondColumn)
 
31
                {
 
32
                        if (firstComparer == null) {
 
33
                                throw new ArgumentNullException("firstComparer");
 
34
                        }
 
35
                        if (secondComparer == null) {
 
36
                                throw new ArgumentNullException("secondComparer");
 
37
                        }
 
38
                        
 
39
                        this.AddComparer(firstComparer, firstColumn);
 
40
                        this.AddComparer(secondComparer, secondColumn);
 
41
                }
 
42
                
 
43
                /// <summary>
 
44
                /// Adds another column to compare.
 
45
                /// </summary>
 
46
                /// <param name="comparer">The <see cref="IListViewItemComparer"/> to use to compare this column.</param>
 
47
                /// <param name="column">The 0-based index of the column to compare.</param>
 
48
                public void AddComparer(IListViewItemComparer comparer, int column)
 
49
                {
 
50
                        if (comparer == null) {
 
51
                                throw new ArgumentNullException("comparer");
 
52
                        }
 
53
                        this.comparers.Add(comparer);
 
54
                        this.columns.Add(column);
 
55
                }
 
56
                
 
57
                public int Compare(ListViewItem lhs, ListViewItem rhs, int column)
 
58
                {
 
59
                        int compareResult;
 
60
                        
 
61
                        for (int i = 0; i < this.comparers.Count; i++) {
 
62
                                if ((compareResult = this.comparers[i].Compare(lhs, rhs, this.columns[i])) != 0) {
 
63
                                        return compareResult;
 
64
                                }
 
65
                        }
 
66
                        
 
67
                        return 0;
 
68
                }
 
69
        }
 
70
}