123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using InABox.Core;
- using static ICSharpCode.AvalonEdit.Document.TextDocumentWeakEventManager;
- namespace InABox.DynamicGrid
- {
- public delegate void EditorControlValueChangedHandler(IDynamicEditorControl sender, Dictionary<string, object> values);
- public interface IDynamicEditorControl
- {
- string ColumnName { get; set; }
- bool Loaded { get; set; }
- bool Changed { get; set; }
- BaseEditor EditorDefinition { get; set; }
- bool IsEnabled { get; set; }
- void SetFocus();
- int DesiredHeight();
- event EditorControlValueChangedHandler OnEditorValueChanged;
- void Configure();
- void SetEnabled(bool enabled);
- void SetVisible(bool enabled);
- void SetValue(string property, object? value);
- object? GetValue(string property);
- }
- public interface ILookupEditorControl : IDynamicEditorControl
- {
- Dictionary<string, string> OtherColumns { get; }
- event OnDefineLookup OnDefineLookups;
- event OnLookupsDefined OnLookupsDefined;
- void LoadLookups(CoreTable values);
- }
- public interface IPopupEditorControl
- {
- Dictionary<string, string> OtherColumns { get; }
- event OnDefineFilter OnDefineFilter;
- }
- public abstract class BaseDynamicEditorControl : ContentControl, IDynamicEditorControl
- {
- public static readonly DependencyProperty ColumnNameProperty =
- DependencyProperty.Register(nameof(ColumnName), typeof(string), typeof(BaseDynamicEditorControl));
- public static readonly DependencyProperty ColorProperty =
- DependencyProperty.Register("Color", typeof(Color), typeof(BaseDynamicEditorControl));
- public new static readonly DependencyProperty IsEnabledProperty =
- DependencyProperty.Register(nameof(IsEnabled), typeof(bool), typeof(BaseDynamicEditorControl));
- private BaseEditor _editordefinition;
- public BaseDynamicEditorControl()
- {
- Color = Colors.LightYellow;
- }
- public Color Color
- {
- get => (Color)GetValue(ColorProperty);
- set
- {
- SetValue(ColorProperty, value);
- if (EditorDefinition != null)
- SetColor(Color);
- }
- }
- public string ColumnName
- {
- get => (string)GetValue(ColumnNameProperty);
- set => SetValue(ColumnNameProperty, value);
- }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public BaseEditor EditorDefinition
- {
- get => _editordefinition;
- set
- {
- _editordefinition = value;
- Content = CreateEditor();
- SetColor(Color);
- }
- }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public bool Loaded { get; set; }
- public abstract bool Changed { get; set; }
- public virtual event EditorControlValueChangedHandler OnEditorValueChanged;
- public abstract int DesiredHeight();
- public abstract void SetFocus();
- public virtual void Configure()
- {
- }
- public abstract void SetEnabled(bool enabled);
- public abstract void SetVisible(bool visible);
- public abstract void SetValue(string property, object? value);
- /// <summary>
- ///
- /// </summary>
- /// <param name="property">The full property name of the entity being edited.</param>
- /// <returns></returns>
- public abstract object? GetValue(string property);
- public abstract Dictionary<string, object?> GetValues();
- public new bool IsEnabled
- {
- get => (bool)GetValue(IsEnabledProperty);
- set
- {
- SetValue(IsEnabledProperty, value);
- SetEnabled(value);
- }
- }
- protected abstract FrameworkElement CreateEditor();
- public abstract int DesiredWidth();
- public abstract void SetColor(Color color);
- protected List<Button> CreateButtons(out bool bDisableEditor)
- {
- var buttons = new List<Button>();
- bDisableEditor = false;
- if (EditorDefinition != null && EditorDefinition is IButtonEditor)
- {
- var ce = (IButtonEditor)EditorDefinition;
- if (ce.Buttons != null)
- foreach (var ceb in ce.Buttons.Reverse())
- {
- if (ceb.DisableEditor)
- bDisableEditor = true;
- var button = new Button
- {
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- Content = ceb.Caption,
- Width = ceb.Width,
- Margin = new Thickness(5, 0, 0, 0),
- Padding = new Thickness(5, 0, 5, 0),
- Tag = ceb,
- IsEnabled = ceb.IsEnabled
- };
- button.Click += (o, e) =>
- {
- var b = (Button)o;
- var eb = b.Tag as EditorButton;
- eb.Click(this);
- };
- ceb.OnEnabled += (enabled) =>
- {
- button.IsEnabled = enabled;
- };
- buttons.Add(button);
- }
- }
- return buttons;
- }
- }
- public abstract class DynamicEditorControl<T> : BaseDynamicEditorControl
- {
- //{
- // get { return (EditorControlValueChangedHandler)GetValue(OnEditorValueChangedProperty); }
- // set { SetValue(OnEditorValueChangedProperty, value) }
- //}
- protected bool Updating;
- public DynamicEditorControl()
- {
- Loaded = false;
- OtherValues = new();
- MinHeight = 25;
- Focusable = false;
- }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override bool Changed { get; set; }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public T Value
- {
- get => RetrieveValue();
- set => UpdateValue(value);
- }
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- protected virtual Dictionary<string, object?> OtherValues { get; }
- //public static readonly DependencyProperty OnEditorValueChangedProperty =
- // DependencyProperty.Register(nameof(OnEditorValueChanged), typeof(EditorControlValueChangedHandler), typeof(DynamicEditorControl<T>));
- public override event EditorControlValueChangedHandler OnEditorValueChanged;
- protected void EditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values) => OnEditorValueChanged?.Invoke(sender, values);
- protected virtual bool CheckChanged()
- {
- //Logger.Send(LogType.Information, "", string.Format("{0}({1}).CheckChanged()", GetType().EntityName().Split('.').Last(), ColumnName));
- if (Loaded && !Updating)
- {
- Updating = true;
- try
- {
- var values = new Dictionary<string, object>();
- var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
- values[sColumn] = RetrieveValue();
- foreach (var key in OtherValues.Keys)
- values[key] = OtherValues[key];
- OnEditorValueChanged?.Invoke(this, values);
- }
- finally
- {
- Changed = true;
- Updating = false;
- }
- }
- return Changed;
- }
- public override void SetValue(string property, object? value)
- {
- UpdateValue(value != null ? (T)value : default);
- }
- public override object? GetValue(string property)
- {
- return RetrieveValue();
- }
- public override Dictionary<string, object?> GetValues()
- {
- return new Dictionary<string, object?>
- {
- { ColumnName, RetrieveValue() }
- };
- }
- protected abstract T RetrieveValue();
- protected abstract void UpdateValue(T value);
- public override bool ShouldSerializeContent()
- {
- return false;
- }
- public override void SetEnabled(bool enabled)
- {
- if (Content is FrameworkElement element) element.IsEnabled = enabled;
- SetColor(enabled ? Color : Colors.WhiteSmoke);
- }
- public override void SetVisible(bool visible)
- {
- Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
- }
- }
- public abstract class DynamicEnclosedEditorControl<T> : BaseDynamicEditorControl
- where T : IEnclosedEntity
- {
- protected bool Updating;
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override bool Changed { get; set; }
- public override event EditorControlValueChangedHandler? OnEditorValueChanged;
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- protected virtual Dictionary<string, object?> OtherValues { get; }
- public DynamicEnclosedEditorControl()
- {
- Loaded = false;
- OtherValues = new();
- MinHeight = 25;
- Focusable = false;
- }
- protected virtual bool CheckChanged()
- {
- //Logger.Send(LogType.Information, "", string.Format("{0}({1}).CheckChanged()", GetType().EntityName().Split('.').Last(), ColumnName));
- if (Loaded && !Updating)
- {
- Updating = true;
- try
- {
- var values = new Dictionary<string, object>();
- var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
- foreach(var (k, v) in GetValues())
- {
- values[k] = v;
- }
- foreach (var key in OtherValues.Keys)
- values[key] = OtherValues[key];
- OnEditorValueChanged?.Invoke(this, values);
- }
- finally
- {
- Changed = true;
- Updating = false;
- }
- }
- return Changed;
- }
- public override void SetEnabled(bool enabled)
- {
- if(Content is FrameworkElement element) element.IsEnabled = enabled;
- SetColor(enabled ? Color : Colors.WhiteSmoke);
- }
- public override void SetVisible(bool visible)
- {
- Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
- }
- }
- }
|