-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncronizedList.cs
168 lines (145 loc) · 4.63 KB
/
SyncronizedList.cs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace PT_Sguil
{
public class SyncList<T> : BindingList<T>
{
private Action<ListChangedEventArgs> _FireEventAction;
private ISynchronizeInvoke _SyncObject;
private bool isSorted;
private ListSortDirection sortDirection;
private PropertyDescriptor sortProperty;
public SyncList(ISynchronizeInvoke syncObject)
{
this.isSorted = false;
this.sortDirection = ListSortDirection.Ascending;
this.sortProperty = null;
this._SyncObject = syncObject;
this._FireEventAction = new Action<ListChangedEventArgs>(base.OnListChanged);
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
for (int i = 0; i < Count; ++i)
{
if (key.Equals(prop.GetValue(Items[i])))
return i;
}
return -1;
}
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
this.sortProperty = prop;
this.sortDirection = direction;
List<T> items = base.Items as List<T>;
try
{
if (items != null)
{
PropertyComparer<T> comparer = new PropertyComparer<T>(prop, direction);
items.Sort(comparer);
this.isSorted = true;
}
else
{
this.isSorted = false;
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
finally
{
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
/**
private void FireEvent(ListChangedEventArgs args)
{
base.OnListChanged(args);
}
**/
protected override void OnListChanged(ListChangedEventArgs args)
{
lock (this)
{
if (_SyncObject != null && _SyncObject.InvokeRequired)
{
_SyncObject.Invoke(this._FireEventAction, new object[] { args });
}
else base.OnListChanged(args);
}
}
protected override bool IsSortedCore
{
get
{
return this.isSorted;
}
}
protected override ListSortDirection SortDirectionCore
{
get
{
return this.sortDirection;
}
}
protected override PropertyDescriptor SortPropertyCore
{
get
{
return this.sortProperty;
}
}
protected override bool SupportsSearchingCore
{
get
{
return true;
}
}
protected override bool SupportsSortingCore
{
get
{
return true;
}
}
public int Find(string property, object key)
{
// Check the properties for a property with the specified name.
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
PropertyDescriptor prop = properties.Find(property, true);
// If there is not a match, return -1 otherwise pass search to
// FindCore method.
if (prop == null)
return -1;
else
return FindCore(prop, key);
}
}
public class PropertyComparer<T> : IComparer<T>
{
private PropertyDescriptor _property;
private ListSortDirection _sortDirection;
public PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
{
this._property = property;
this._sortDirection = direction;
}
public int Compare(T x, T y)
{
int num = 0;
object a = this._property.GetValue(x);
object b = this._property.GetValue(y);
num = Comparer.Default.Compare(a, b);
if (this._sortDirection == ListSortDirection.Descending)
{
num = -num;
}
return num;
}
}
}