-
Notifications
You must be signed in to change notification settings - Fork 27
/
TypeMappingEditor.cs
96 lines (88 loc) · 4.51 KB
/
TypeMappingEditor.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
using Bonsai.Expressions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace Bonsai.Design
{
/// <summary>
/// Provides a user interface for selecting method overloads from a workflow operator.
/// </summary>
public class TypeMappingEditor : UITypeEditor
{
/// <inheritdoc/>
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
static IEnumerable<MethodInfo> GetProcessMethods(Type combinatorType)
{
const BindingFlags bindingAttributes = BindingFlags.Instance | BindingFlags.Public;
var combinatorAttributes = combinatorType.GetCustomAttributes(typeof(CombinatorAttribute), true);
if (combinatorAttributes.Length != 1) return Enumerable.Empty<MethodInfo>();
var methodName = ((CombinatorAttribute)combinatorAttributes.Single()).MethodName;
return combinatorType.GetMethods(bindingAttributes).Where(m => m.Name == methodName);
}
static TypeMapping CreateTypeMapping(Type targetType)
{
var mappingType = typeof(TypeMapping<>).MakeGenericType(targetType);
return (TypeMapping)Activator.CreateInstance(mappingType);
}
/// <inheritdoc/>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (context != null && editorService != null)
{
var workflow = (ExpressionBuilderGraph)context.GetService(typeof(ExpressionBuilderGraph));
if (workflow != null)
{
var builderNode = workflow.FirstOrDefault(node =>
ExpressionBuilder.GetWorkflowElement(node.Value) == context.Instance);
if (builderNode != null)
{
var intersection = default(HashSet<Type>);
foreach (var successor in builderNode.Successors)
{
var combinator = ExpressionBuilder.Unwrap(successor.Target.Value) switch
{
CombinatorBuilder combinatorBuilder => combinatorBuilder.Combinator,
ICustomTypeDescriptor typeDescriptor =>
TypeDescriptor.GetDefaultProperty(typeDescriptor.GetType())?.GetValue(typeDescriptor),
_ => null
};
if (combinator == null) continue;
var inputTypes = from method in GetProcessMethods(combinator.GetType())
where !method.IsGenericMethod
let parameters = method.GetParameters()
where parameters.Length > successor.Label.Index &&
parameters[successor.Label.Index].ParameterType.IsGenericType
select parameters[successor.Label.Index].ParameterType.GetGenericArguments()[0];
if (intersection == null) intersection = new HashSet<Type>(inputTypes);
else intersection.IntersectWith(inputTypes);
}
var values = new List<TypeMapping> { null };
if (intersection != null)
{
values.AddRange(intersection.Select(CreateTypeMapping));
}
using (var editorDialog = new TypeMappingEditorDialog(values))
{
editorDialog.Mapping = (TypeMapping)value;
editorDialog.Converter = context.PropertyDescriptor.Converter;
if (editorService.ShowDialog(editorDialog) == DialogResult.OK)
{
return editorDialog.Mapping;
}
}
}
}
}
return base.EditValue(context, provider, value);
}
}
}