using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using InABox.Clients; using InABox.Core; namespace InABox.DynamicGrid { internal class PopupEditorControl : DynamicEditorControl { private string _description = ""; 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 OnUpdateOtherEditorHandler OnUpdateOtherEditor; 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 += (o, t) => { return OnDefineFilter?.Invoke(o, 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; var client = ClientFactory.CreateClient(_type); var columns = LookupFactory.DefineColumns(_type); var sort = LookupFactory.DefineSort(_type); var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(_type)); CoreUtils.SetPropertyValue(filter, "Expression", CoreUtils.GetMemberExpression(_type, "ID")); CoreUtils.SetPropertyValue(filter, "Operator", Operator.IsEqualTo); CoreUtils.SetPropertyValue(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 Configure() { base.Configure(); _type = (EditorDefinition as PopupEditor)?.Type; } public override void SetFocus() { Editor.Focus(); } public override void SetColor(Color color) { Editor.Background = new SolidColorBrush(color); } } }