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
|
//
// ItemTemplate.cs
//
// Author:
// Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Xml.Serialization;
using System.Xml;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Crow.IML;
namespace Crow
{
/// <summary> Test func on data, return yes if there's children </summary>
public delegate bool BooleanTestOnInstance(object instance);
/// <summary>
/// Derived from Instantiator with sub data fetching facilities for hierarchical data access.
///
/// ItemTemplate stores the dynamic method for instantiating the control tree defined in a valid IML file.
///
/// </summary>
public class ItemTemplate : Instantiator {
#if DESIGN_MODE
public void getIML (System.Xml.XmlDocument doc, System.Xml.XmlNode parentElem){
if (sourcePath == "#Crow.DefaultItem.template")
return;
XmlElement xe = doc.CreateElement("ItemTemplate");
XmlAttribute xa = null;
if (string.IsNullOrEmpty (sourcePath)) {
//inline item template
using (GraphicObject go = this.CreateInstance())
go.getIML (doc, xe);
} else {
xa = doc.CreateAttribute ("Path");
xa.Value = sourcePath;
xe.Attributes.Append (xa);
}
if (strDataType != "default") {
xa = doc.CreateAttribute ("DataType");
xa.Value = strDataType;
xe.Attributes.Append (xa);
if (dataTest != "TypeOf"){
xa = doc.CreateAttribute ("DataTest");
xa.Value = dataTest;
xe.Attributes.Append (xa);
}
}
if (!string.IsNullOrEmpty(fetchMethodName)){
xa = doc.CreateAttribute ("Data");
xa.Value = fetchMethodName;
xe.Attributes.Append (xa);
}
parentElem.AppendChild (xe);
}
#endif
public EventHandler Expand;
public BooleanTestOnInstance HasSubItems;
string strDataType;
string fetchMethodName;
string dataTest;
#region CTOR
/// <summary>
/// Initializes a new instance of the <see cref="Crow.ItemTemplate"/> class by parsing the file passed as argument.
/// </summary>
/// <param name="path">IML file to parse</param>
/// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
/// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
public ItemTemplate(Interface _iface, string path, string _dataTest = "TypeOf", string _dataType = "default", string _fetchDataMethod = null)
: base(_iface, path) {
strDataType = _dataType;
fetchMethodName = _fetchDataMethod;
dataTest = _dataTest;
}
/// <summary>
/// Initializes a new instance of the <see cref="Crow.ItemTemplate"/> class by parsing the IML fragment passed as arg.
/// </summary>
/// <param name="path">IML fragment to parse</param>
/// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
/// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
public ItemTemplate (Interface _iface, Stream ImlFragment, string _dataTest, string _dataType, string _fetchDataMethod)
:base(_iface, ImlFragment)
{
strDataType = _dataType;
fetchMethodName = _fetchDataMethod;
dataTest = _dataTest;
}
/// <summary>
/// Initializes a new instance of the <see cref="Crow.ItemTemplate"/> class using the opened XmlReader in args.
/// </summary>
/// <param name="path">XML reader positionned before or at the root node</param>
/// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
/// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
public ItemTemplate (Interface _iface, XmlReader reader, string _dataTest = "TypeOf" , string _dataType = null, string _fetchDataMethod = null)
:base(_iface, reader)
{
strDataType = _dataType;
fetchMethodName = _fetchDataMethod;
dataTest = _dataTest;
}
#endregion
/// <summary>
/// Creates the expand delegate.
/// </summary>
/// <param name="host">Host.</param>
public void CreateExpandDelegate (TemplatedGroup host){
Type dataType = null;
//if (host.DataTest == "TypeOf"){
dataType = CompilerServices.tryGetType(strDataType);
// if (dataType == null) {
// Debug.WriteLine ("ItemTemplate error: DataType not found: {0}.", strDataType);
// return;
// }
// }
Type tmpGrpType = typeof(TemplatedGroup);
Type evtType = typeof(EventHandler);
//PropertyInfo piData = tmpGrpType.GetProperty ("Data");
MethodInfo evtInvoke = evtType.GetMethod ("Invoke");
ParameterInfo [] evtParams = evtInvoke.GetParameters ();
Type handlerArgsType = evtParams [1].ParameterType;
Type [] args = { CompilerServices.TObject, CompilerServices.TObject, handlerArgsType };
#region Expand dyn meth
//DM is bound to templatedGroup root (arg0)
//arg1 is the sender of the expand event
DynamicMethod dm = new DynamicMethod ("dyn_expand_" + fetchMethodName,
typeof (void), args,typeof(TemplatedGroup), true);
System.Reflection.Emit.Label gotoEnd;
System.Reflection.Emit.Label ifDataIsNull;
System.Reflection.Emit.Label gotoItemsContainerNotFound;
ILGenerator il = dm.GetILGenerator (256);
il.DeclareLocal(typeof(GraphicObject));
gotoEnd = il.DefineLabel ();
ifDataIsNull = il.DefineLabel ();
gotoItemsContainerNotFound = il.DefineLabel ();
il.Emit (OpCodes.Ldarg_1);//load sender of expand event
il.Emit(OpCodes.Ldstr, "ItemsContainer");//load name to find
il.Emit (OpCodes.Callvirt, CompilerServices.miFindByName);
il.Emit (OpCodes.Stloc_0);//save items container as loc0
//ensure ItemsContainer is not null
il.Emit (OpCodes.Ldloc_0);
il.Emit (OpCodes.Brfalse, gotoItemsContainerNotFound);
//check that node is not already expanded
il.Emit (OpCodes.Ldarg_0);//push root TemplatedGroup into the stack
il.Emit (OpCodes.Ldarg_1);
il.Emit (OpCodes.Call, CompilerServices.miIsAlreadyExpanded);
il.Emit (OpCodes.Brtrue, gotoEnd);
// il.Emit (OpCodes.Ldloc_0);
// il.Emit (OpCodes.Callvirt, piData.GetGetMethod ());
// il.Emit (OpCodes.Brfalse, ifDataIsNull);
// il.Emit (OpCodes.Br, gotoEnd);
// il.MarkLabel(ifDataIsNull);
//copy the ref of ItemTemplates list TODO: maybe find another way to share it among the nodes?
// FieldInfo fiTemplates = tmpGrpType.GetField("ItemTemplates");
// il.Emit (OpCodes.Ldloc_0);
// il.Emit (OpCodes.Ldarg_0);
// il.Emit (OpCodes.Ldfld, fiTemplates);
// il.Emit (OpCodes.Stfld, fiTemplates);
//call 'fetchMethodName' from the dataSource to build the sub nodes list
//il.Emit (OpCodes.Ldarg_0);//load root templatedGroop
il.Emit (OpCodes.Ldarg_0);//push root TemplatedGroup into the stack
il.Emit (OpCodes.Ldarg_1);//load sender node of expand
il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataSource);//get the dataSource of the sender
if (fetchMethodName != "self") {//special keyword self allows the use of recurent list<<<
if (dataType == null) {
//dataTest was not = TypeOF, so we have to get the type of data
//dynamically and fetch
il.Emit (OpCodes.Ldstr, fetchMethodName);
il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataTypeAndFetch);
}else
emitGetSubData(il, dataType);
}
//set 'return' from the fetch method as 'data' of the list
//il.Emit (OpCodes.Callvirt, piData.GetSetMethod ());
il.Emit (OpCodes.Ldloc_0);//load second arg of loadPage, the sender node
il.Emit (OpCodes.Ldstr, dataTest);//load 3rd arg, dataTest kind on subitems
il.Emit (OpCodes.Callvirt, CompilerServices.miLoadPage);
il.Emit (OpCodes.Br, gotoEnd);
il.MarkLabel(gotoItemsContainerNotFound);
il.EmitWriteLine("ItemsContainer not found in ItemTemplate for " + host.ToString());
il.MarkLabel(gotoEnd);
il.Emit (OpCodes.Ret);
Expand = (EventHandler)dm.CreateDelegate (evtType, host);
#endregion
#region Items counting dyn method
//dm is unbound, arg0 is instance of Item container to expand
dm = new DynamicMethod ("dyn_count_" + fetchMethodName,
typeof (bool), new Type[] {CompilerServices.TObject}, true);
il = dm.GetILGenerator (256);
//get the dataSource of the arg0
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataSource);
if (fetchMethodName != "self") {//special keyword self allows the use of recurent list<<<
if (dataType == null) {
//dataTest was not = TypeOF, so we have to get the type of data
//dynamically and fetch
il.Emit (OpCodes.Ldstr, fetchMethodName);
il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataTypeAndFetch);
}else
emitGetSubData(il, dataType);
}
il.Emit (OpCodes.Callvirt, CompilerServices.miGetColCount);
il.Emit (OpCodes.Ldc_I4_0);
il.Emit (OpCodes.Cgt);
il.Emit (OpCodes.Ret);
HasSubItems = (BooleanTestOnInstance)dm.CreateDelegate (typeof(BooleanTestOnInstance));
#endregion
}
//data is on the stack
void emitGetSubData(ILGenerator il, Type dataType){
MethodInfo miGetDatas = dataType.GetMethod (fetchMethodName, new Type[] {});
if (miGetDatas == null)
miGetDatas = CompilerServices.SearchExtMethod (dataType, fetchMethodName);
if (miGetDatas == null) {//in last resort, search among properties
PropertyInfo piDatas = dataType.GetProperty (fetchMethodName);
if (piDatas == null) {
FieldInfo fiDatas = dataType.GetField (fetchMethodName);
if (fiDatas == null)//and among fields
throw new Exception ("Fetch data member not found in ItemTemplate: " + fetchMethodName);
il.Emit (OpCodes.Ldfld, fiDatas);
return;
}
miGetDatas = piDatas.GetGetMethod ();
if (miGetDatas == null)
throw new Exception ("Read only property for fetching data in ItemTemplate: " + fetchMethodName);
}
il.Emit (OpCodes.Callvirt, miGetDatas);
}
}
}
|