using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using InABox.Clients; using InABox.Core; namespace InABox.DynamicGrid { public class PopupEditorControl : DynamicEditorControl, IPopupEditorControl { static PopupEditorControl() { //DynamicEditorControlFactory.Register(); } private Type? _type; private Guid _value = Guid.Empty; private TextBox Editor; //public event DefineFilter OnDefineFilter; public PopupEditorControl() { OtherColumns = new Dictionary(); } public Dictionary OtherColumns { get; } public event OnDefineFilter? OnDefineFilter; public override void Configure() { _type = (EditorDefinition as PopupEditor)?.Type; Host.LoadColumns(ColumnName, OtherColumns); if (EditorDefinition is DataLookupEditor dataLookup) Host.LoadColumns(ColumnName, dataLookup.OtherColumns); } protected override FrameworkElement CreateEditor() { var Grid = new Grid { VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch }; Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Pixel) }); Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) }); Editor = new TextBox { VerticalAlignment = VerticalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, IsReadOnly = true }; Editor.SetValue(Grid.ColumnProperty, 0); Editor.SetValue(Grid.RowProperty, 0); Grid.Children.Add(Editor); var Select = new Button { VerticalAlignment = VerticalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Content = "..", Margin = new Thickness(5, 0, 0, 0), Focusable = false }; Select.SetValue(Grid.ColumnProperty, 1); Select.SetValue(Grid.RowProperty, 0); Select.Click += Select_Click; Grid.Children.Add(Select); var Clear = new Button { VerticalAlignment = VerticalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Content = "Clear", Margin = new Thickness(5, 0, 0, 0), Focusable = false }; Clear.SetValue(Grid.ColumnProperty, 2); Clear.SetValue(Grid.RowProperty, 0); Clear.Click += Clear_Click; Grid.Children.Add(Clear); return Grid; } private void Select_Click(object sender, RoutedEventArgs e) { if (_type != null) { var list = new PopupList(_type, _value, OtherColumns.Keys.ToArray()); list.OnDefineFilter += t => { return Host.DefineFilter(t); }; if (list.ShowDialog() == true) { _value = list.ID; //Dictionary fieldmap = new Dictionary(); foreach (var key in OtherColumns.Keys) //String[] comps = othercolumn.Split(new String[] { "->" }, StringSplitOptions.None); //String field = comps.Last(); //fieldmap[comps.First()] = field; OtherValues[OtherColumns[key]] = list.OtherValues[key]; //fieldmap[OtherColumns[key]] = list.OtherValues[othercolumn] CheckChanged(); var display = new List(); //var columns = Entity.DefaultLookupColumns(_type) as IColumns; var columns = LookupFactory.DefineColumns(_type); foreach (var column in columns.ColumnNames()) if (list.OtherValues.ContainsKey(column)) display.Add(list.OtherValues[column]?.ToString()); Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString()))); } } } private void Clear_Click(object sender, RoutedEventArgs e) { if (_type != null) { _value = Guid.Empty; foreach (var otherfield in OtherColumns.Keys) OtherValues[OtherColumns[otherfield]] = null; CheckChanged(); Editor.Text = ""; } } public override int DesiredHeight() { return 25; } public override int DesiredWidth() { return int.MaxValue; } protected override Guid RetrieveValue() { return _value; } protected override void UpdateValue(Guid value) { _value = value; if (_type is null) { Logger.Send(LogType.Error, "", "PopupEditorControl.UpdateValue(): _type is null!"); return; } var client = ClientFactory.CreateClient(_type); var columns = LookupFactory.DefineColumns(_type); var sort = LookupFactory.DefineSort(_type); var filter = Filter.Create(_type); filter.Expression = CoreUtils.GetMemberExpression(_type, "ID"); filter.Operator = Operator.IsEqualTo; filter.Value = value; var lookup = client.Query(filter, columns, sort); var display = new List(); if (lookup.Rows.Any()) foreach (var col in lookup.Columns.Where(x => !x.ColumnName.Equals("ID"))) display.Add(lookup.Rows.First()[col.ColumnName]); Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString()))); } public override void SetFocus() { Editor.Focus(); } public override void SetColor(Color color) { Editor.Background = new SolidColorBrush(color); } } }