123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using InABox.Clients;
- using InABox.Core;
- using InABox.WPF;
- using RoslynPad.Editor;
- namespace InABox.DynamicGrid
- {
- public delegate void OnUpdateOtherEditorHandler(string columnname, object value);
- public delegate Dictionary<string, object?> EditorValueChangedHandler(object sender, string name, object value);
- /// <summary>
- /// Interaction logic for DynamicEditorGrid.xaml
- /// </summary>
- public partial class DynamicEditorGrid : UserControl
- {
- public delegate void EditorCreatedHandler(object sender, double height, double width);
- public delegate Document? FindDocumentEvent(string FileName);
- public delegate Document? GetDocumentEvent(Guid id);
- public delegate object? GetPropertyValueHandler(object sender, string name);
- public delegate void SaveDocumentEvent(Document document);
- public delegate void SetPropertyValueHandler(object sender, string name, object value);
- public delegate object?[] GetItemsEvent();
- // Column Definitions as defined by calling model
- private DynamicGridColumns _columns = new();
- private Type? LayoutType;
- private DynamicEditorGridLayout? Layout;
- public DynamicEditorGrid()
- {
- InitializeComponent();
- Loaded += DynamicEditorGrid_Loaded;
- }
- public DynamicEditorPages Pages { get; private set; } = new();
- public bool PreloadPages { get; set; }
- public Type UnderlyingType { get; set; }
- public OnLoadPage? OnLoadPage { get; set; }
- public OnSelectPage? OnSelectPage { get; set; }
- public OnUnloadPage? OnUnloadPage { get; set; }
- public DynamicGridColumns Columns => _columns;
- public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
- {
- foreach (var page in Pages)
- {
- if (page is DynamicEditPage editPage)
- {
- if (editPage.TryFindEditor(columnname, out editor))
- return true;
- }
- }
- editor = null;
- return false;
- }
- public IDynamicEditorControl? FindEditor(string columnname)
- {
- TryFindEditor(columnname, out var editor);
- return editor;
- }
- public virtual void ReconfigureEditors()
- {
- OnReconfigureEditors?.Invoke(this);
- }
- public object? GetPropertyValue(string columnname)
- {
- return OnGetPropertyValue?.Invoke(this, columnname);
- }
- public event EditorCreatedHandler? OnEditorCreated;
- public event OnCustomiseColumns? OnCustomiseColumns;
- public event OnGetEditor? OnGetEditor;
- public event OnGridCustomiseEditor? OnGridCustomiseEditor;
- public event OnGetEditorSequence? OnGetSequence;
- public event GetPropertyValueHandler? OnGetPropertyValue;
- public event SetPropertyValueHandler? OnSetPropertyValue;
- public event EditorValueChangedHandler? OnEditorValueChanged;
- public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
- public event OnReconfigureEditors? OnReconfigureEditors;
-
- public event OnDefineFilter? OnDefineFilter;
- public event OnDefineLookup? OnDefineLookups;
- public event OnLookupsDefined? OnLookupsDefined;
- public event GetDocumentEvent? OnGetDocument;
- public event FindDocumentEvent? OnFindDocument;
- public event SaveDocumentEvent? OnSaveDocument;
- public event GetItemsEvent? GetItems;
- private void DynamicEditorGrid_Loaded(object sender, RoutedEventArgs e)
- {
- //Reload();
- }
- public void Reload()
- {
- LoadPages();
- ReconfigureEditors();
- }
- #region Edit Page
- public class DynamicEditPage : ContentControl, IDynamicEditorPage
- {
- private Grid Grid;
- public DynamicEditorGrid EditorGrid { get; set; } = null!; // Set by DynamicEditorGrid
- public bool Ready { get; set; }
- private List<BaseDynamicEditorControl> Editors { get; set; }
- public PageType PageType => PageType.Editor;
- public int PageOrder { get; set; }
- public string Header { get; set; }
- private double GeneralHeight = 30;
- public DynamicEditPage(string header)
- {
- Header = header;
- Editors = new List<BaseDynamicEditorControl>();
- InitialiseContent();
- }
- public void AddEditor(string columnName, BaseEditor editor)
- {
- BaseDynamicEditorControl? element = editor switch
- {
- TextBoxEditor => new TextBoxEditorControl(),
- Core.RichTextEditor => new RichTextEditorControl(),
- URLEditor => new URLEditorControl(),
- CodeEditor or UniqueCodeEditor => new CodeEditorControl(),
- CheckBoxEditor => new CheckBoxEditorControl(),
- DateTimeEditor => new DateTimeEditorControl(),
- DateEditor dateEditor => new DateEditorControl { TodayVisible = dateEditor.TodayVisible },
- TimeOfDayEditor => new TimeOfDayEditorControl { NowButtonVisible = false },
- DurationEditor => new DurationEditorControl(),
- NotesEditor => new NotesEditorControl(),
- PINEditor => new PINEditorControl(),
- CheckListEditor => new CheckListBoxEditorControl(),
- MemoEditor => new MemoEditorControl(),
- JsonEditor => new JsonEditorControl(),
- LookupEditor => ClientFactory.IsSupported(((LookupEditor)editor).Type) ? new LookupEditorControl() : null,
- PopupEditor => ClientFactory.IsSupported(((PopupEditor)editor).Type) ? new PopupEditorControl() : null,
- CodePopupEditor => ClientFactory.IsSupported(((CodePopupEditor)editor).Type) ? new CodePopupEditorControl() : null,
- EnumLookupEditor or ComboLookupEditor => new LookupEditorControl(),
- ComboMultiLookupEditor => new MultiLookupEditorControl(),
- EmbeddedImageEditor imageEditor => new EmbeddedImageEditorControl
- {
- MaximumHeight = imageEditor.MaximumHeight,
- MaximumWidth = imageEditor.MaximumWidth,
- MaximumFileSize = imageEditor.MaximumFileSize
- },
- FileNameEditor fileNameEditor => new FileNameEditorControl
- {
- Filter = fileNameEditor.FileMask,
- AllowView = fileNameEditor.AllowView,
- RequireExisting = fileNameEditor.RequireExisting
- },
- FolderEditor folderEditor => new FolderEditorControl
- {
- InitialFolder = folderEditor.InitialFolder
- },
- MiscellaneousDocumentEditor => new DocumentEditorControl(),
- ImageDocumentEditor => new DocumentEditorControl(),
- VectorDocumentEditor => new DocumentEditorControl(),
- PDFDocumentEditor => new DocumentEditorControl(),
- PasswordEditor => new PasswordEditorControl(),
- CurrencyEditor => new CurrencyEditorControl(),
- DoubleEditor => new DoubleEditorControl(),
- IntegerEditor => new IntegerEditorControl(),
- Core.ScriptEditor scriptEditor => new ScriptEditorControl
- {
- SyntaxLanguage = scriptEditor.SyntaxLanguage
- },
- ButtonEditor buttonEditor => new ButtonEditorControl()
- {
- Label = buttonEditor.Label
- },
- BlobEditor blobEditor => new BlobEditorControl()
- {
- Label = blobEditor.Label
- },
- EmbeddedListEditor listEditor => new EmbeddedListEditorControl()
- {
- DataType = listEditor.DataType,
- Label = listEditor.Label,
- DirectEdit = listEditor.DirectEdit
- },
- TimestampEditor => new TimestampEditorControl(),
- ColorEditor => new ColorEditorControl(),
- FilterEditor filter => new FilterEditorControl { FilterType = filter.Type! },
- ExpressionEditor expression => new ExpressionEditorControl(expression),
- DimensionsEditor dimension => DimensionsEditorControl.Create(dimension),
- CoreTimeEditor dimension => new CoreTimeEditorControl(),
- _ => null,
- };
- if (element != null)
- {
- element.EditorDefinition = editor;
- element.IsEnabled = editor.Editable == Editable.Enabled;
- if (!string.IsNullOrWhiteSpace(editor.ToolTip))
- {
- element.ToolTip = new ToolTip() { Content = editor.ToolTip };
- }
- var label = new Label();
- label.Content = CoreUtils.Neatify(editor.Caption); // 2
- label.Margin = new Thickness(0F, 0F, 0F, 0F);
- label.HorizontalAlignment = HorizontalAlignment.Stretch;
- label.VerticalAlignment = VerticalAlignment.Stretch;
- label.HorizontalContentAlignment = HorizontalAlignment.Left;
- label.VerticalContentAlignment = VerticalAlignment.Center;
- label.SetValue(Grid.RowProperty, Grid.RowDefinitions.Count);
- label.SetValue(Grid.ColumnProperty, 0);
- label.Visibility = string.IsNullOrWhiteSpace(editor.Caption) ? Visibility.Collapsed : Visibility.Visible;
- Grid.Children.Add(label);
- element.ColumnName = columnName;
- element.Color = editor is UniqueCodeEditor ? Color.FromArgb(0xFF, 0xF6, 0xC9, 0xE8) : Colors.LightYellow;
- Editors.Add(element);
- element.Margin = new Thickness(5F, 2.5F, 5F, 2.5F);
- double iHeight = element.DesiredHeight();
- if (iHeight == int.MaxValue)
- {
- Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
- GeneralHeight += element.MinHeight + 5.0F;
- }
- else
- {
- Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(iHeight + 5.0F) });
- GeneralHeight += iHeight + 5.0F;
- }
- double iWidth = element.DesiredWidth();
- if (iWidth == int.MaxValue)
- {
- element.HorizontalAlignment = HorizontalAlignment.Stretch;
- }
- else
- {
- element.HorizontalAlignment = HorizontalAlignment.Left;
- element.Width = iWidth;
- }
- element.SetValue(Grid.RowProperty, Grid.RowDefinitions.Count - 1);
- element.SetValue(Grid.ColumnProperty, 1);
- Grid.Children.Add(element);
- }
- }
- [MemberNotNull(nameof(Grid))]
- private void InitialiseContent()
- {
- Grid = new Grid
- {
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch,
- Margin = new Thickness(0, 2.5, 0, 2.5)
- };
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- var scroll = new ScrollViewer
- {
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
- Padding = new Thickness(2),
- Content = Grid
- };
- var border = new Border
- {
- BorderBrush = new SolidColorBrush(Colors.Gray),
- Background = new SolidColorBrush(Colors.White),
- BorderThickness = new Thickness(0.75),
- Child = scroll
- };
- Content = border;
- }
- public void AfterSave(object item)
- {
- }
- public void BeforeSave(object item)
- {
- }
- public string Caption() => Header;
- public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
- {
- editor = Editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
- editor ??= Editors.FirstOrDefault(x => columnname.StartsWith(x.ColumnName));
- return editor is not null;
- }
- public IEnumerable<BaseDynamicEditorControl> FindEditors(DynamicGridColumn column)
- {
- return Editors.Where(x => string.Equals(x.ColumnName, column.ColumnName));
- }
- #region Configure Editors
- private void LoadLookupColumns(string column, Dictionary<string, string> othercolumns)
- {
- othercolumns.Clear();
- var comps = column.Split('.').ToList();
- comps.RemoveAt(comps.Count - 1);
- var prefix = string.Format("{0}.", string.Join(".", comps));
- var cols = EditorGrid.Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
- foreach (var col in cols)
- othercolumns[col.ColumnName.Replace(prefix, "")] = col.ColumnName;
- }
- private string GetCodeColumn(string column)
- {
- var comps = column.Split('.').ToList();
- comps.RemoveAt(comps.Count - 1);
- var prefix = string.Format("{0}.", string.Join(".", comps));
- var cols = EditorGrid.Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
- foreach (var col in cols)
- {
- var editor = EditorGrid.OnGetEditor?.Invoke(col);
- if (editor is CodeEditor || editor is UniqueCodeEditor)
- return col.ColumnName.Split('.').Last();
- }
- return "";
- }
- private void ConfigureExpressionEditor(ExpressionEditorControl control)
- {
- control.GetItems += () => EditorGrid.GetItems?.Invoke() ?? Array.Empty<object?>();
- }
- private void ConfigurePopupEditor(PopupEditorControl popup, string column, PopupEditor editor)
- {
- popup.ColumnName = column;
- LoadLookupColumns(column, popup.OtherColumns);
- if (popup.EditorDefinition is DataLookupEditor dataLookup)
- LoadLookupColumns(column, dataLookup.OtherColumns);
- popup.OnDefineFilter += (sender, type) => { return EditorGrid.OnDefineFilter?.Invoke(sender, type); };
- popup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
- }
- private void ConfigureCodePopupEditor(CodePopupEditorControl popup, string column, CodePopupEditor editor)
- {
- popup.ColumnName = column;
- LoadLookupColumns(column, popup.OtherColumns);
- if (popup.EditorDefinition is DataLookupEditor dataLookup)
- LoadLookupColumns(column, dataLookup.OtherColumns);
- popup.CodeColumn = !string.IsNullOrEmpty(editor.CodeColumn) ? editor.CodeColumn : GetCodeColumn(column);
- popup.OnDefineFilter += (sender, type) => { return EditorGrid.OnDefineFilter?.Invoke(sender, type); };
- popup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
- }
- private void ConfigureLookupEditor(LookupEditorControl lookup, string column, LookupEditor editor)
- {
- if (editor.LookupWidth != int.MaxValue)
- lookup.Width = editor.LookupWidth;
- lookup.ColumnName = column;
- LoadLookupColumns(column, lookup.OtherColumns);
- if (lookup.EditorDefinition is DataLookupEditor dataLookup)
- LoadLookupColumns(column, dataLookup.OtherColumns);
- lookup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
- lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
- lookup.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
- }
- private void ConfigureEnumEditor(LookupEditorControl lookup, string column, EnumLookupEditor editor)
- {
- if (editor.LookupWidth != int.MaxValue)
- lookup.Width = editor.LookupWidth;
- lookup.ColumnName = column;
- lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
- lookup.OnLookupsDefined += sender =>
- {
- //OnLookupsDefined?.Invoke(sender);
- };
- }
- private void ConfigureComboEditor(LookupEditorControl lookup, string column, ComboLookupEditor editor)
- {
- if (editor.LookupWidth != int.MaxValue)
- lookup.Width = editor.LookupWidth;
- lookup.ColumnName = column;
- lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
- lookup.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
- }
- private void ConfigureMultiLookupEditor(MultiLookupEditorControl lookup, string column, ComboMultiLookupEditor editor)
- {
- if (editor.LookupWidth != int.MaxValue)
- lookup.Width = editor.LookupWidth;
- lookup.ColumnName = column;
- lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
- lookup.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
- }
- private void ConfigureCheckListEditor(CheckListBoxEditorControl checks, string column, CheckListEditor editor)
- {
- checks.Width = editor.LookupWidth;
- checks.ColumnName = column;
- checks.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
- checks.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
- }
- private void ConfigureDocumentEditor(DocumentEditorControl document, string column, BaseDocumentEditor editor)
- {
- document.ColumnName = column;
- LoadLookupColumns(column, document.OtherColumns);
- if (document.EditorDefinition is DataLookupEditor dataLookup)
- LoadLookupColumns(column, dataLookup.OtherColumns);
- document.OnGetDocument += id => { return EditorGrid.OnGetDocument?.Invoke(id); };
- document.OnSaveDocument += doc => { EditorGrid.OnSaveDocument?.Invoke(doc); };
- document.OnFindDocument += file => { return EditorGrid.OnFindDocument?.Invoke(file); };
- document.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
- document.Filter = editor.FileMask;
- }
- private void Lookup_OnUpdateOtherEditor(string columnname, object value)
- {
- var editor = Editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
- if (editor != null)
- CoreUtils.SetPropertyValue(editor, "Value", value);
- }
- private void ConfigurePasswordEditor(PasswordEditorControl passwordEditorControl, PasswordEditor passwordEditor)
- {
- passwordEditorControl.ViewButtonVisible = passwordEditor.ViewButtonVisible;
- }
- private void ConfigureEditors()
- {
- foreach (var Editor in Editors)
- {
- var editor = Editor.EditorDefinition;
- var column = Editor.ColumnName;
- if (Editor is LookupEditorControl lookupControl)
- {
- if (editor is LookupEditor lookupEditor)
- ConfigureLookupEditor(lookupControl, column, lookupEditor);
- else if (editor is EnumLookupEditor enumEditor)
- ConfigureEnumEditor(lookupControl, column, enumEditor);
- else if (editor is ComboLookupEditor comboEditor)
- ConfigureComboEditor(lookupControl, column, comboEditor);
- }
- else if (Editor is MultiLookupEditorControl multiLookupEditor && editor is ComboMultiLookupEditor comboMultiLookup)
- {
- ConfigureMultiLookupEditor(multiLookupEditor, column, comboMultiLookup);
- }
- else if (Editor is CheckListBoxEditorControl checkBoxControl && editor is CheckListEditor checkListEditor)
- {
- ConfigureCheckListEditor(checkBoxControl, column, checkListEditor);
- }
- else if (Editor is PopupEditorControl popupControl && editor is PopupEditor popupEditor)
- {
- ConfigurePopupEditor(popupControl, column, popupEditor);
- }
- else if (Editor is CodePopupEditorControl codePopupControl && editor is CodePopupEditor codePopupEditor)
- {
- ConfigureCodePopupEditor(codePopupControl, column, codePopupEditor);
- }
- else if (Editor is DocumentEditorControl documentEditorControl && editor is BaseDocumentEditor baseDocumentEditor)
- {
- ConfigureDocumentEditor(documentEditorControl, column, baseDocumentEditor);
- }
- else if (Editor is PasswordEditorControl passwordEditorControl && editor is PasswordEditor passwordEditor)
- {
- ConfigurePasswordEditor(passwordEditorControl, passwordEditor);
- }
- else if (Editor is ExpressionEditorControl expressionEditorControl && editor is ExpressionEditor expressionEditor)
- {
- ConfigureExpressionEditor(expressionEditorControl);
- }
- else if (Editor is ButtonEditorControl buttonControl && editor is ButtonEditor buttonEditor)
- {
- ConfigureButtonControl(buttonControl, buttonEditor);
- }
- else if (Editor is BlobEditorControl blobControl && editor is BlobEditor blobEditor)
- {
- ConfigureBlobControl(blobControl, blobEditor);
- }
- Editor.Configure();
- if (!Editors.Any(x => x.ColumnName.Equals(Editor.ColumnName)))
- Editors.Add(Editor);
- Editor.Loaded = true;
- }
- }
- private void ConfigureButtonControl(ButtonEditorControl buttonControl, ButtonEditor buttonEditor)
- {
- buttonControl.OnClick += buttonEditor.OnClick;
- }
-
- private void ConfigureBlobControl(BlobEditorControl blobControl, BlobEditor blobEditor)
- {
- blobControl.OnClick += blobEditor.OnClick;
- }
- #endregion
- private void EditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
- {
- //Logger.Send(LogType.Information, "", string.Format("DynamicEditorGrid.EditorValueChanged({0})", values.Keys.Count));
- var changededitors = new Dictionary<string, object?>();
- void ExtractChanged(Dictionary<String, object?>? columns)
- {
- if (columns != null)
- foreach (var (change, value) in columns)
- if (!changededitors.ContainsKey(change) && !change.Equals(sender.ColumnName))
- changededitors[change] = value;
- }
- foreach (var key in values.Keys)
- {
- var changedcolumns = EditorGrid.OnEditorValueChanged?.Invoke(EditorGrid, key, values[key]);
- ExtractChanged(changedcolumns);
- }
- var afterchanged = EditorGrid.OnAfterEditorValueChanged?.Invoke(EditorGrid, sender.ColumnName);
- ExtractChanged(afterchanged);
- if (changededitors.Any())
- LoadEditorValues(changededitors);
- EditorGrid.ReconfigureEditors();
- }
- private void LoadEditorValues(Dictionary<string, object?>? changededitors = null)
- {
- var columnnames = changededitors != null ? changededitors.Keys.ToArray() : Editors.Select(x => x.ColumnName).ToArray();
- foreach (var columnname in columnnames)
- {
- if (!TryFindEditor(columnname, out var editor))
- continue;
- var bLoaded = editor.Loaded;
- editor.Loaded = false;
- if (changededitors != null && changededitors.ContainsKey(columnname))
- {
- editor.SetValue(columnname, changededitors[columnname]);
- }
- else
- {
- var curvalue = EditorGrid.GetPropertyValue(columnname);
- try
- {
- editor.SetValue(columnname, curvalue);
- }
- catch (Exception e)
- {
- MessageBox.Show($"Unable to set editor value for {columnname} -> {curvalue}: {CoreUtils.FormatException(e)}");
- }
- editor.Changed = false;
- }
- editor.Loaded = bLoaded;
- editor.OnEditorValueChanged += EditorValueChanged;
- }
- }
- public void Load(object item, Func<Type, CoreTable>? PageDataHandler)
- {
- ConfigureEditors();
- LoadEditorValues();
- foreach (var editor in Editors)
- {
- foreach(var (column, editorValue) in editor.GetValues())
- {
- var entityValue = EditorGrid.GetPropertyValue(column);
- if (!Equals(editorValue, entityValue))
- {
- editor.Loaded = false;
- editor.SetValue(column, entityValue);
- editor.Loaded = true;
- }
- }
- }
- Editors.FirstOrDefault()?.SetFocus();
- Ready = true;
- }
- public Size MinimumSize() => new Size(800, GeneralHeight);
- public int Order() => PageOrder;
- }
- #endregion
- #region Loading + Editing Layout
- private decimal GetSequence(DynamicGridColumn column)
- {
- if (OnGetSequence != null)
- return OnGetSequence.Invoke(column);
- return 999;
- }
- private DynamicEditPage GetEditPage(string name)
- {
- var page = Pages.Where(x => x is DynamicEditPage page && page.Header == name).FirstOrDefault() as DynamicEditPage;
- if(page is null)
- {
- page = new DynamicEditPage(name);
- if (name == "General")
- {
- page.PageOrder = -1;
- }
- else
- {
- page.PageOrder = 0;
- }
- Pages.Add(page);
- }
- return page;
- }
- public void SetLayoutType<T>() where T : DynamicEditorGridLayout
- {
- LayoutType = typeof(T);
- }
- private void InitialiseLayout()
- {
- Layout = (Activator.CreateInstance(LayoutType ?? typeof(DefaultDynamicEditorGridLayout)) as DynamicEditorGridLayout)!;
- Layout.OnSelectPage += Layout_SelectPage;
- Content = Layout;
- }
- private void CreateLayout()
- {
- if(Layout is null)
- {
- InitialiseLayout();
- }
- foreach (var column in _columns.OrderBy(x => GetSequence(x)))
- {
- var iProp = DatabaseSchema.Property(UnderlyingType, column.ColumnName);
- var editor = OnGetEditor?.Invoke(column);
- if (editor != null && iProp?.ShouldShowEditor() != true)
- {
- editor.Visible = Visible.Hidden;
- editor.Editable = Editable.Hidden;
- }
- if(editor is not null)
- {
- OnGridCustomiseEditor?.Invoke(this, column, editor);
- }
- if (editor != null && editor.Editable != Editable.Hidden)
- {
- var page = string.IsNullOrWhiteSpace(editor.Page) ? iProp is StandardProperty ? "General" : "Custom Fields" : editor.Page;
- var editPage = GetEditPage(page);
- editPage.AddEditor(column.ColumnName, editor);
- }
- else if (iProp?.HasParentEditor() == true)
- {
- var parent = iProp.GetParentWithEditor();
- if(parent is not null)
- {
- var parentEditor = parent.Editor;
- if(parentEditor is not null)
- {
- OnGridCustomiseEditor?.Invoke(this, new DynamicGridColumn { ColumnName = parent.Name }, parentEditor);
- }
- if(parentEditor is not null && parentEditor.Editable != Editable.Hidden)
- {
- var page = string.IsNullOrWhiteSpace(parentEditor.Page)
- ? parent is StandardProperty
- ? "General"
- : "Custom Fields"
- : parentEditor.Page;
- var editPage = GetEditPage(page);
- if (!editPage.TryFindEditor(parent.Name, out var editorControl))
- {
- editPage.AddEditor(parent.Name, parentEditor);
- }
- }
- }
- }
- }
-
- OnEditorCreated?.Invoke(this, 0, 800);
- }
- #endregion
- #region Pages
- private void Layout_SelectPage(IDynamicEditorPage page)
- {
- if (!page.Ready)
- using (new WaitCursor())
- {
- OnLoadPage?.Invoke(page);
- }
- OnSelectPage?.Invoke(this, null);
- }
- public void UnloadPages(bool saved)
- {
- if(Pages is not null)
- foreach (var page in Pages)
- if (page.Ready)
- OnUnloadPage?.Invoke(page, saved);
- }
- private void LoadPages()
- {
- if (Pages != null && Layout is not null)
- using (new WaitCursor())
- {
- foreach (var page in Pages)
- {
- page.Ready = false;
- page.EditorGrid = this;
- }
- Layout.LoadPages(Pages);
- if (PreloadPages)
- {
- foreach(var page in Pages)
- {
- OnLoadPage?.Invoke(page);
- }
- }
- }
- }
- public void Load(DynamicEditorPages pages)
- {
- Pages = pages;
- _columns = new DynamicGridColumns();
- OnCustomiseColumns?.Invoke(this, _columns);
- CreateLayout();
- Reload();
- }
- #endregion
- }
- }
|