This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
CollectionStrategy.cs
348 lines (278 loc) · 8.47 KB
/
CollectionStrategy.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
/// <summary>
/// This Class handles all List's with IList
/// Access to Data is allway's done by using the 'IndexList'
/// </summary>
namespace ICSharpCode.Reports.Core {
internal sealed class CollectionStrategy : BaseListStrategy {
private Type itemType;
private object firstItem;
private object current;
private PropertyDescriptorCollection listProperties;
private DataCollection<object> baseList;
#region Constructor
public CollectionStrategy(IList list,ReportSettings reportSettings):base(reportSettings)
{
if (list.Count > 0) {
firstItem = list[0];
itemType = firstItem.GetType();
this.baseList = new DataCollection <object>(itemType);
this.baseList.AddRange(list);
}
this.listProperties = this.baseList.GetItemProperties(null);
}
#endregion
#region build sorting
private PropertyDescriptor[] BuildSortProperties (SortColumnCollection col)
{
PropertyDescriptor[] sortProperties = new PropertyDescriptor[col.Count];
PropertyDescriptorCollection c = this.baseList.GetItemProperties(null);
for (int criteriaIndex = 0; criteriaIndex < col.Count; criteriaIndex++){
PropertyDescriptor descriptor = c.Find (col[criteriaIndex].ColumnName,true);
if (descriptor == null){
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
"Die Liste enthält keine Spalte [{0}].",
col[criteriaIndex].ColumnName));
}
sortProperties[criteriaIndex] = descriptor;
}
return sortProperties;
}
private IndexList BuildSortIndex(SortColumnCollection col)
{
IndexList arrayList = new IndexList();
PropertyDescriptor[] sortProperties = BuildSortProperties (col);
for (int rowIndex = 0; rowIndex < this.baseList.Count; rowIndex++){
object rowItem = this.baseList[rowIndex];
object[] values = new object[col.Count];
// Hier bereits Wertabruf um dies nicht während des Sortierens tun zu müssen.
for (int criteriaIndex = 0; criteriaIndex < sortProperties.Length; criteriaIndex++){
object value = sortProperties[criteriaIndex].GetValue(rowItem);
// Hier auf Verträglichkeit testen um Vergleiche bei Sortierung zu vereinfachen.
// Muss IComparable und gleicher Typ sein.
if (value != null && value != DBNull.Value)
{
if (!(value is IComparable)){
throw new InvalidOperationException("ReportDataSource:BuildSortArray - > This type doesn't support IComparable." + value.ToString());
}
values[criteriaIndex] = value;
}
}
arrayList.Add(new SortComparer(col, rowIndex, values));
}
if (arrayList[0].ObjectArray.GetLength(0) == 1) {
List<BaseComparer> lbc = BaseListStrategy.GenericSorter (arrayList);
arrayList.Clear();
arrayList.AddRange(lbc);
}
else {
arrayList.Sort();
}
return arrayList;
}
// if we have no sorting, we build the indexlist as well, so we don't need to
private IndexList IndexBuilder(SortColumnCollection col)
{
IndexList arrayList = new IndexList();
for (int rowIndex = 0; rowIndex < this.baseList.Count; rowIndex++){
object[] values = new object[1];
arrayList.Add(new SortComparer(col, rowIndex, values));
}
return arrayList;
}
private void BuildAvailableFields ()
{
base.AvailableFields.Clear();
foreach (PropertyDescriptor p in this.listProperties){
base.AvailableFields.Add (new AbstractColumn(p.Name,p.PropertyType));
}
}
#endregion
#region SharpReportCore.IDataViewStrategy interface implementation
public override AvailableFieldsCollection AvailableFields
{
get {
BuildAvailableFields();
return base.AvailableFields;
}
}
public override object Current
{
get {
return this.baseList[((BaseComparer)base.IndexList[base.CurrentPosition]).ListIndex];
}
}
public override int Count
{
get {
return this.baseList.Count;
}
}
public override int CurrentPosition
{
get {
return base.IndexList.CurrentPosition;
}
set {
base.CurrentPosition = value;
current = this.baseList[((BaseComparer)base.IndexList[value]).ListIndex];
}
}
public override void Group ()
{
base.Group();
IndexList gl = new IndexList("group");
gl = this.BuildSortIndex (ReportSettings.GroupColumnsCollection);
// ShowIndexList(gl);
base.BuildGroup(gl);
}
public override void Sort()
{
base.Sort();
if ((base.ReportSettings.SortColumnsCollection != null)) {
if (base.ReportSettings.SortColumnsCollection.Count > 0) {
base.IndexList = this.BuildSortIndex (ReportSettings.SortColumnsCollection);
base.IsSorted = true;
} else {
base.IndexList = this.IndexBuilder(ReportSettings.SortColumnsCollection);
base.IsSorted = false;
}
}
}
public override void Reset()
{
this.CurrentPosition = 0;
base.Reset();
}
public override void Bind()
{
base.Bind();
if (base.ReportSettings.GroupColumnsCollection.Count > 0) {
this.Group();
} else {
this.Sort ();
}
Reset();
}
#endregion
public override void Fill(IDataItem item)
{
if (item is BaseDataItem)
{
if (item.ColumnName.Contains(".")) {
string[] splittedNames = SplitNames(ref item);
// PropDes for SubClass
PropertyDescriptor classProperty = this.listProperties.Find(splittedNames[0], true);
if (classProperty == null)
{
WrongColumnName (item);
}
else
{
//all Props for SubClass
var propCollection = ExtendedTypeDescriptor.GetProperties(classProperty.PropertyType);
var subProperty = propCollection.Find(splittedNames[1], true);
var classValue = classProperty.GetValue(this.Current);
SetReturnValue(subProperty,classValue,item);
}
}
else
{
PropertyDescriptor p = this.listProperties.Find(item.ColumnName, true);
if (p != null) {
SetReturnValue(p,this.Current,item);
} else {
WrongColumnName (item);
}
}
return;
}
else
{
//image processing from IList
BaseImageItem baseImageItem = item as BaseImageItem;
if (baseImageItem != null) {
PropertyDescriptor p = this.listProperties.Find(baseImageItem.ColumnName, true);
if (p != null) {
baseImageItem.Image = p.GetValue(this.Current) as System.Drawing.Image;
}
return;
}
}
}
void WrongColumnName(IDataItem item)
{
item.DBValue = string.Format(CultureInfo.InvariantCulture, "Error : <{0}> missing!", item.ColumnName);
}
void SetReturnValue(PropertyDescriptor p,object component,IDataItem item)
{
if (p != null)
{
var o = p.GetValue(component);
if (o != null) {
item.DBValue = o.ToString();
} else {
item.DBValue = String.Empty;
}
} else {
WrongColumnName(item);
}
}
string[] SplitNames(ref IDataItem item)
{
var i = item.ColumnName.IndexOf(".");
char[] delimiters = new char[] { '.' };
var splittedNames = item.ColumnName.Split(delimiters);
return splittedNames;
}
#region test
public override CurrentItemsCollection FillDataRow()
{
CurrentItemsCollection ci = base.FillDataRow();
if (current != null) {
CurrentItem c = null;
foreach (PropertyDescriptor pd in this.listProperties)
{
c = new CurrentItem();
c.ColumnName = pd.Name;
c.DataType = pd.PropertyType;
c.Value = pd.GetValue(this.Current).ToString();
ci.Add(c);
}
}
return ci;
}
#endregion
#region IDisposable
public override void Dispose(){
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing){
try {
if (disposing) {
if (this.baseList != null) {
this.baseList.Clear();
this.baseList = null;
}
if (this.listProperties != null) {
this.listProperties = null;
}
}
} finally {
base.Dispose(disposing);
// Release unmanaged resources.
// Set large fields to null.
// Call Dispose on your base class.
}
}
#endregion
}
}