forked from gw2scratch/evtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridViewSorter.cs
More file actions
148 lines (125 loc) · 3.83 KB
/
GridViewSorter.cs
File metadata and controls
148 lines (125 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using Eto.Forms;
namespace GW2Scratch.ArcdpsLogManager
{
public class GridViewSorter<T> where T : class
{
private readonly GridView<T> gridView;
private readonly IReadOnlyDictionary<GridColumn, Comparison<T>> customSorts;
private bool sortedAscending = false;
private GridColumn sortColumn = null;
/// <summary>
/// Create a new <see cref="GridView"/> sorter.
/// </summary>
/// <remarks>If the <see cref="GridView.DataStore"/> is changed, <see cref="UpdateDataStore"/> has to be called
/// to apply the sort.</remarks>
/// <param name="gridView">A <see cref="GridView"/> that will be sorted.</param>
/// <param name="customSorts">Optional custom sorting logic for specified columns.</param>
public GridViewSorter(GridView<T> gridView, IReadOnlyDictionary<GridColumn, Comparison<T>> customSorts = null)
{
this.gridView = gridView;
this.customSorts = customSorts;
}
/// <summary>
/// Has to be called if the <see cref="GridView.DataStore"/> changes to apply the sort again.
/// </summary>
public void UpdateDataStore()
{
ApplySort();
}
/// <summary>
/// Enable sorting on the <see cref="GridView"/>. Registers handlers for clicks on the column headers.
/// </summary>
public void EnableSorting()
{
foreach (var gridColumn in gridView.Columns)
{
gridColumn.Sortable = true;
}
gridView.ColumnHeaderClick += (sender, args) => { ClickColumn(args.Column); };
}
/// <summary>
/// Sort the <see cref="GridView"/> by the provided column in ascending order.
/// </summary>
/// <param name="column">A column in the <see cref="GridView"/>.</param>
public void SortByAscending(GridColumn column) => SortBy(column, true);
/// <summary>
/// Sort the <see cref="GridView"/> by the provided column in descending order.
/// </summary>
/// <param name="column">A column in the <see cref="GridView"/>.</param>
public void SortByDescending(GridColumn column) => SortBy(column, false);
private void SortBy(GridColumn column, bool ascending)
{
if (sortColumn != null)
{
sortColumn.Style = "sort-hidden";
}
sortColumn = column;
sortedAscending = ascending;
sortColumn.Style = sortedAscending ? "sort-ascending" : "sort-descending";
ApplySort();
}
private void ClickColumn(GridColumn column)
{
bool ascending;
if (sortColumn == column)
{
ascending = !sortedAscending;
}
else
{
ascending = true;
}
SortBy(column, ascending);
}
private void ApplySort()
{
if (sortColumn == null)
{
// No sort has been applied yet.
return;
}
if (gridView.DataStore == null)
{
return;
}
if (!(gridView.DataStore is FilterCollection<T> collection))
{
throw new NotSupportedException("Only FilterCollections can be sorted.");
}
Comparison<T> sort = null;
customSorts?.TryGetValue(sortColumn, out sort);
if (sort == null && sortColumn.DataCell is TextBoxCell textBoxCell)
{
sort = GetTextComparison(textBoxCell);
}
if (sort == null)
{
// If we don't know how to sort out the current column, we keep the old sort.
return;
}
if (!sortedAscending)
{
var sortToReverse = sort; // This copy is required
sort = (x, y) => -sortToReverse(x, y);
}
collection.Sort = sort;
}
private Comparison<T> GetTextComparison(TextBoxCell textBoxCell)
{
return (first, second) =>
{
string property1 = textBoxCell.Binding.GetValue(first);
string property2 = textBoxCell.Binding.GetValue(second);
// Try to compare as numbers if possible. Many columns will only contain numbers.
if (long.TryParse(property1, out long value1) &&
long.TryParse(property2, out long value2))
{
return value1.CompareTo(value2);
}
return String.Compare(property1, property2, StringComparison.Ordinal);
};
}
}
}