using OpenTap;
|
using OpenTap.Addin.Annotation;
|
using OpenTapEditor.UI;
|
using System.ComponentModel;
|
using System.Reflection;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Windows.Data;
|
using System.Xml.Serialization;
|
using UILib.DpUtil;
|
|
namespace OpenTapEditor.Provider
|
{
|
public class GridControlProvider_2
|
{
|
private static ITypeData resourceTypeData = TypeData.FromType(typeof(IResource));
|
|
/// <summary>
|
/// 获取需要渲染的属性
|
/// </summary>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static Dictionary<string, PropertyInfo> GetRenderProperty(object obj)
|
{
|
var res = new Dictionary<string, PropertyInfo>();
|
var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
foreach (var property in properties)
|
{
|
if (property.GetCustomAttribute<DisplayAttribute>() != null)
|
{
|
res[property.Name] = property;
|
}
|
}
|
return res;
|
}
|
|
/// <summary>
|
/// 获取需要渲染的方法
|
/// </summary>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static Dictionary<string, MethodInfo> GetRenderMethods(object obj)
|
{
|
var res = new Dictionary<string, MethodInfo>();
|
var methods = obj.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);
|
foreach (var method in methods)
|
{
|
if (method.GetCustomAttribute<DisplayAttribute>() != null)
|
{
|
res[method.Name] = method;
|
}
|
}
|
return res;
|
}
|
|
public static bool FilterMember(IMemberData member)
|
{
|
if (member.DeclaringType.DescendsTo(resourceTypeData) && member.Name == nameof(Resource.Name))
|
return true;
|
if (member.GetAttribute<BrowsableAttribute>() is BrowsableAttribute attr)
|
return attr.Browsable;
|
if (member.HasAttribute<OutputAttribute>())
|
return true;
|
return member.Attributes.Any(a => a is XmlIgnoreAttribute) == false && member.Writable;
|
}
|
|
public static Grid GetControl(object obj, StepSettingView parent)
|
{
|
var grid = new Grid()
|
{
|
|
};
|
grid.ColumnDefinitions.Add(new ColumnDefinition()
|
{
|
Width = new GridLength(1, GridUnitType.Auto)
|
});
|
grid.ColumnDefinitions.Add(new ColumnDefinition()
|
{
|
Width = new GridLength(1, GridUnitType.Star)
|
});
|
|
var properties = GetRenderProperty(obj);
|
|
int i = 0;
|
foreach (var key in properties.Keys)
|
{
|
|
var property = properties[key];
|
var display = property.GetCustomAttribute<DisplayAttribute>();
|
var label = new Label
|
{
|
Content = display.Name
|
};
|
|
grid.RowDefinitions.Add(new RowDefinition()
|
{
|
Height = new GridLength(1, GridUnitType.Auto)
|
});
|
|
grid.Children.Add(label);
|
Grid.SetRow(label, i);
|
Grid.SetColumn(label, 0);
|
|
var element = FormGroupProvider.RanderElement(obj, display, property);
|
|
if (property.GetCustomAttribute<DependOnAttribute>() is DependOnAttribute doa)
|
{
|
BindingOperations.SetBinding(element, FrameworkElement.VisibilityProperty, new Binding(doa.BindingName)
|
{
|
Source = obj,
|
Converter = VisibilityConvertor.Instance,
|
ConverterParameter = doa.TargetValue
|
});
|
}
|
|
if (element != null)
|
{
|
grid.Children.Add(element);
|
Grid.SetRow(element, i);
|
Grid.SetColumn(element, 1);
|
}
|
i++;
|
}
|
|
|
foreach (var node in GetRenderMethods(obj))
|
{
|
|
var method = node.Value;
|
var display = method.GetCustomAttribute<DisplayAttribute>();
|
var btn = new Button
|
{
|
Content = display.Name
|
};
|
|
btn.Click += (sender, e) =>
|
{
|
method.Invoke(obj, null);
|
};
|
|
grid.RowDefinitions.Add(new RowDefinition()
|
{
|
Height = new GridLength(1, GridUnitType.Auto)
|
});
|
|
grid.Children.Add(btn);
|
Grid.SetRow(btn, i);
|
Grid.SetColumn(btn, 0);
|
Grid.SetColumnSpan(btn, 2);
|
i++;
|
}
|
|
if (obj is ITestStepParent tsp)
|
{
|
grid.RowDefinitions.Add(new RowDefinition()
|
{
|
Height = new GridLength(1, GridUnitType.Auto)
|
});
|
|
var label = new Label
|
{
|
Content = "Break Condition"
|
};
|
grid.Children.Add(label);
|
Grid.SetRow(label, i);
|
Grid.SetColumn(label, 0);
|
|
var s = BreakConditionProperty.GetBreakCondition(tsp);
|
var element = new BreakConditionCtrl
|
{
|
Source = tsp
|
};
|
if (element != null)
|
{
|
grid.Children.Add(element);
|
Grid.SetRow(element, i);
|
Grid.SetColumn(element, 1);
|
}
|
}
|
GridHelper.SetSpace(grid, 5);
|
GridHelper.RefreshGridSpace(grid);
|
return grid;
|
}
|
}
|
}
|