JD2022-TU1/main/tools/framework/JD.Controls/CellGetters.cs

54 lines
2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using JD.Reflection;
using JD.Reflection.Attributes;
using TeaBox;
using TeaBox.Gui.PropertyGrid;
using UAFPropertyEditorAttribute = JD.Interfaces.Editors.PropertyEditorAttribute;
namespace JD.Controls
{
[Export(typeof(ICellGetters))]
public class CellGetters : ICellGetters
{
public ICellViewer GetViewer(IGridDataRow row)
{
return row.Attributes.OfType<UAFPropertyEditorAttribute>()
.Concat(row.ValueType.GetAttributes<UAFPropertyEditorAttribute>(true))
.Select(attribute => attribute.GetEditor<ICellViewer>())
.FirstOrDefault(viewer => viewer != null);
}
public CellEditor GetEditor(IGridDataRow row)
{
var cellEditor = GetTypeFromPropertyEditorAttribute<CellEditor>(row);
if (cellEditor != null)
{
return cellEditor;
}
var editorAttribute = GetTypeFromPropertyEditorAttribute<IEditorAttribute>(row) ??
row.GetAttributeByInterface<IEditorAttribute>() ??
row.ValueType.GetAttribute<IEditorAttribute>();
if (editorAttribute != null)
{
return editorAttribute.GetEditor(row);
}
return null;
}
public IEnumerable<IEditorExtensionProviderAttribute> GetEditorExtensionProviderAttributes(IAttributeProvider provider)
{
var editorAttributes = provider.Attributes.OfType<UAFPropertyEditorAttribute>();
return editorAttributes.SelectMany(attribute => attribute.GetEditors<IEditorExtensionProviderAttribute>());
}
private T GetTypeFromPropertyEditorAttribute<T>(IAttributeProvider attributeProvider) where T : class
{
var attribute = attributeProvider.GetAttributeByInterface<UAFPropertyEditorAttribute>();
return attribute != null ? attribute.GetEditor<T>() : null;
}
}
}