using OpenTap;
|
using OpenTap.Addin.Annotation;
|
using OpenTapEditor.UI;
|
using System.ComponentModel;
|
using System.Windows.Controls;
|
using System.Windows.Data;
|
using System.Xml.Serialization;
|
|
namespace OpenTapEditor.Provider
|
{
|
public class DutControlProvider
|
{
|
private static ITypeData resourceTypeData = TypeData.FromType(typeof(IResource));
|
|
|
public static object? GetItemValue(AnnotationCollection x)
|
{
|
return (x.Get<IAvailableValuesAnnotation>() as IStringReadOnlyValueAnnotation)?.Value
|
?? x.Get<IStringValueAnnotation>()?.Value
|
?? x.Get<IStringReadOnlyValueAnnotation>()?.Value
|
?? x.Get<IAvailableValuesAnnotationProxy>()?.SelectedValue?.Source?.ToString()
|
?? x.Get<IObjectValueAnnotation>()?.Value;
|
}
|
|
public static AnnotationCollection[]? getMembers(AnnotationCollection annotations)
|
{
|
return annotations?.Get<IMembersAnnotation>()?.Members
|
.Where(x =>
|
{
|
var member = x.Get<IMemberAnnotation>().Member;
|
if (member.DeclaringType.DescendsTo(TypeData.FromType(typeof(IResource))) && member.Name == nameof(Resource.Name))
|
return true;
|
|
return x.Get<IAccessAnnotation>()?.IsVisible ?? false;
|
})
|
.Where(x =>
|
{
|
var member = x.Get<IMemberAnnotation>()?.Member;
|
if (member == null || (member.GetAttribute<SubmitAttribute>() != null && x.Get<IAvailableValuesAnnotationProxy>() != null)) return false;
|
return FilterMember(member);
|
})
|
.ToArray();
|
}
|
|
public static StackPanel test(AnnotationCollection annotation)
|
{
|
var valueAnnotation = annotation.Get<IObjectValueAnnotation>();
|
if (valueAnnotation == null) return null;
|
StackPanel stack = new StackPanel()
|
{
|
Orientation = Orientation.Horizontal
|
};
|
stack.Children.Add(new TextBlock { Text = annotation.Get<DisplayAttribute>().Name });
|
|
/// 列表
|
if (annotation.Get<ICollectionAnnotation>() is ICollectionAnnotation caa)
|
{
|
if (annotation.Get<ReadOnlyMemberAnnotation>() != null) return null;
|
var fixedSize = annotation.Get<IFixedSizeCollectionAnnotation>()?.IsFixedSize ?? false;
|
var items = caa.AnnotatedElements.Select(a => a.Source).ToArray();
|
var dataType = caa.AnnotatedElements.GetType();
|
//Type genericArgument = dataType.GetGenericArguments()[0];
|
|
bool placeholderElementAdded = false;
|
var dataGrid = new DataGrid();
|
|
if (annotation.Get<BindingListAttribute>() is BindingListAttribute bla)
|
{
|
BindingOperations.SetBinding(dataGrid,
|
DataGrid.ItemsSourceProperty,
|
new Binding(bla.BindingName)
|
{
|
Source = annotation.Source,
|
Mode = BindingMode.TwoWay,
|
});
|
dataGrid.CanUserAddRows = bla.CanAdd;
|
dataGrid.AutoGenerateColumns = false;
|
|
Type propertyType = annotation?.Source?.GetType()?.GetProperty(bla.BindingName)?.PropertyType;
|
if (propertyType?.IsGenericType == true)
|
{
|
DataGridHelper.SetDataGridColumns(dataGrid, propertyType.GetGenericArguments()[0]);
|
}
|
else
|
{
|
DataGridHelper.SetDataGridColumns(dataGrid, propertyType);
|
}
|
}
|
else
|
{
|
dataGrid.ItemsSource = items;
|
}
|
stack.Children.Add(dataGrid);
|
return stack;
|
}
|
|
// StringValue是正常的输入
|
// IAvailableValuesAnnotation 是枚举
|
// IStringReadOnlyValueAnnotation是只读
|
var stringValueAnnotation = annotation.Get<IStringValueAnnotation>();
|
if (stringValueAnnotation == null) return null;
|
|
var selectAnnotation = annotation.Get<IAvailableValuesAnnotationProxy>();
|
if (selectAnnotation != null)
|
{
|
var source = selectAnnotation.AvailableValues;
|
var comboBox = new ComboBox
|
{
|
DisplayMemberPath = "Source.Name",
|
ItemsSource = source
|
};
|
BindingOperations.SetBinding(comboBox, ComboBox.SelectedItemProperty,
|
|
new Binding(nameof(IAvailableValuesAnnotationProxy.SelectedValue))
|
{
|
Source = selectAnnotation,
|
Mode = BindingMode.TwoWay,
|
NotifyOnSourceUpdated = true,
|
});
|
comboBox.SourceUpdated += (sender, e) =>
|
{
|
annotation.Write();
|
annotation.Read();
|
};
|
stack.Children.Add(comboBox);
|
return stack;
|
}
|
|
if (annotation.Get<FilePathAttribute>() is FilePathAttribute fpa)
|
{
|
FileSelector fs = new FileSelector();
|
stack.Children.Add(fs);
|
BindingOperations.SetBinding(fs, FileSelector.FilePathProperty,
|
new Binding(nameof(IStringValueAnnotation.Value))
|
{
|
Source = stringValueAnnotation,
|
Mode = BindingMode.TwoWay,
|
NotifyOnSourceUpdated = true
|
});
|
fs.SourceUpdated += (sender, e) =>
|
{
|
annotation.Write();
|
annotation.Read();
|
};
|
return stack;
|
}
|
|
|
if (annotation.Get<DynamicSelectAttribute>() is DynamicSelectAttribute dsa)
|
{
|
ComboBox combox = new ComboBox();
|
stack.Children.Add(combox);
|
|
BindingOperations.SetBinding(combox, ComboBox.ItemsSourceProperty,
|
new Binding(dsa.SourceName)
|
{
|
Source = dsa,
|
Mode = BindingMode.OneWay
|
});
|
|
BindingOperations.SetBinding(combox, ComboBox.TextProperty,
|
new Binding(nameof(stringValueAnnotation.Value))
|
{
|
Source = stringValueAnnotation,
|
Mode = BindingMode.TwoWay,
|
NotifyOnSourceUpdated = true
|
});
|
combox.SourceUpdated += (sender, e) =>
|
{
|
annotation.Write();
|
annotation.Read();
|
};
|
return stack;
|
}
|
|
TextBox tb = new TextBox();
|
stack.Children.Add(tb);
|
BindingOperations.SetBinding(tb, TextBox.TextProperty,
|
new Binding(nameof(IStringValueAnnotation.Value))
|
{
|
Source = stringValueAnnotation,
|
Mode = BindingMode.TwoWay,
|
NotifyOnSourceUpdated = true
|
});
|
tb.SourceUpdated += (sender, e) =>
|
{
|
annotation.Write();
|
annotation.Read();
|
};
|
|
//var update = annotation.Get<UpdateMonitor>(true);
|
//if (update != null)
|
//{
|
// update.RegisterSourceUpdated(tb, () => tb.GetBindingExpression(TextBox.TextProperty).UpdateTarget());
|
//}
|
return stack;
|
}
|
|
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 StackPanel GetControl(AnnotationCollection annotation)
|
{
|
var stack = new StackPanel()
|
{
|
|
};
|
|
var groupCache = new Dictionary<string, Expander>();
|
|
Expander GetExpander(List<string> groups)
|
{
|
Expander groupNode = null;
|
Expander lastGroup = null;
|
string groupKey = null;
|
foreach (var group in groups)
|
{
|
groupKey = string.IsNullOrEmpty(groupKey) ? group : (groupKey + "." + group);
|
groupNode = null;
|
if (groupCache.TryGetValue(groupKey, out groupNode) == false)
|
{
|
// add the group
|
groupNode = new Expander
|
{
|
IsExpanded = true,
|
Header = groupKey,
|
Content = new StackPanel()
|
};
|
groupCache[groupKey] = groupNode;
|
|
if (lastGroup != null)
|
{
|
((StackPanel)lastGroup.Content).Children.Add(groupNode);
|
}
|
else
|
{
|
stack.Children.Add(groupNode);
|
}
|
}
|
lastGroup = groupNode;
|
}
|
return groupNode;
|
}
|
|
var members = getMembers(annotation);
|
foreach (var ann in members)
|
{
|
var control = test(ann);
|
if (control == null) continue;
|
var _groups = ann?.Get<DisplayAttribute>().Group.ToList();
|
if (_groups == null || _groups.Count <= 0)
|
{
|
stack.Children.Add(control);
|
}
|
else
|
{
|
var expander = GetExpander(_groups);
|
((StackPanel)expander.Content).Children.Add(control);
|
}
|
}
|
return stack;
|
}
|
}
|
}
|