using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; namespace PRSDesktop.Configuration { internal class CustomPropertyGrid : DynamicDataGrid { public CustomPropertyGrid() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns); HiddenColumns.Add(x => x.Class); HiddenColumns.Add(x => x.Type); } protected override void DoValidate(CustomProperty[] items, List errors) { base.DoValidate(items, errors); if (items.Any(x => string.IsNullOrWhiteSpace(x.Class))) errors.Add("[Class] must not be blank!"); if (items.Any(x => string.IsNullOrWhiteSpace(x.Name))) errors.Add("[Name] must not be blank!"); if (items.Any(x => string.IsNullOrWhiteSpace(x.Type))) errors.Add("[Type] must not be blank!"); } private IEnumerable FindTypes(Func predicate) { if (predicate == null) throw new ArgumentNullException(nameof(predicate)); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) if (!assembly.IsDynamic) { Type[] exportedTypes = null; try { exportedTypes = assembly.GetExportedTypes(); } catch (ReflectionTypeLoadException e) { exportedTypes = e.Types; } if (exportedTypes != null) foreach (var type in exportedTypes) if (predicate(type)) yield return type; } } public override void ConfigureColumns(DynamicGridColumns columns /*, bool dolookups = true */) { base.ConfigureColumns(columns /*, dolookups */); var column = columns.FirstOrDefault(x => x.ColumnName.Equals("Class")); if (column != null) { column.Lookups.Clear(); var classes = CoreUtils.TypeList( new[] { typeof(Setout).Assembly }, myType => myType.IsClass && !myType.IsAbstract && !myType.IsGenericType && myType.IsSubclassOf(typeof(Entity)) && myType.GetInterfaces().Contains(typeof(IPersistent)) ).OrderBy(x => x.EntityName()).ToArray(); foreach (var entity in classes) if (ClientFactory.IsSupported(entity)) column.Lookups[entity.EntityName()] = entity; } column = columns.FirstOrDefault(x => x.ColumnName.Equals("Type")); if (column != null) { column.Lookups.Clear(); column.Lookups[typeof(string).EntityName()] = typeof(string).EntityName(); column.Lookups[typeof(int).EntityName()] = typeof(int).EntityName(); column.Lookups[typeof(double).EntityName()] = typeof(double).EntityName(); column.Lookups[typeof(bool).EntityName()] = typeof(bool).EntityName(); column.Lookups[typeof(DateTime).EntityName()] = typeof(DateTime).EntityName(); } } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { base.Reload(criteria, columns, ref sort, action); } } }