123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf.Dashboard.Editor;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace InABox.Wpf.Dashboard;
- public class DynamicDashboardGridPresenterProperties : BaseObject
- {
- /// <summary>
- /// The key of the query that will provide the data for this presenter.
- /// </summary>
- public string Table { get; set; } = "";
- public DynamicGridColumns? Columns { get; set; }
- }
- [Caption("Grid")]
- public class DynamicDashboardGridPresenter : IDynamicDashboardDataPresenter<DynamicDashboardGridPresenterProperties>
- {
- public DynamicDashboardGridPresenterProperties Properties { get; set; } = null!;
- private DynamicDashboardDataComponent _dataComponent = null!;
- public DynamicDashboardDataComponent DataComponent
- {
- get => _dataComponent;
- set
- {
- _dataComponent = value;
- UpdateData();
- }
- }
- public int PreviewHeight => 300;
- public bool IsPreview { get; set; }
- private CoreTableGrid? Grid;
- private readonly ContentControl Content = new();
- private DynamicDashboardData? _data;
- public FrameworkElement? Setup()
- {
- UpdateData();
- return Content;
- }
- private void UpdateData()
- {
- if (Properties.Table.IsNullOrWhiteSpace() || !DataComponent.TryGetTable(Properties.Table, out var table))
- {
- Properties.Table = DataComponent.Tables.FirstOrDefault()?.Key ?? "";
- }
- if (!DataComponent.TryGetTable(Properties.Table, out table))
- {
- var border = new Border
- {
- Background = Colors.DimGray.ToBrush()
- };
- border.ContextMenuOpening += Border_ContextMenuOpening;
- Content.Content = border;
- return;
- }
- Grid = new DataGrid(this)
- {
- ColumnSchema = new DataGridColumnSchema(this)
- };
- Grid.Refresh(true, false);
- Content.Content = Grid as FrameworkElement;
- }
- private void CustomiseMenu(ContextMenu menu)
- {
- if (!IsPreview) return;
- var tableItem = menu.AddItem("Select Table", null, null);
- foreach(var table in DataComponent.Tables)
- {
- tableItem.AddCheckMenuItem(table.Key, table.Key, SelectTable_Click, isChecked: Properties.Table == table.Key);
- }
- if(tableItem.Items.Count <= 1)
- {
- menu.Items.Remove(tableItem);
- }
- menu.AddItem("Select Data", null, SelectData_Click);
- }
- private void SelectData_Click()
- {
- if (DynamicDashboardDataEditor.Execute(DataComponent))
- {
- UpdateData();
- DataComponent.RunQueryAsync(100).ContinueWith(data =>
- {
- if(data.Exception is not null)
- {
- MessageWindow.ShowError("Error loading data.", data.Exception);
- }
- else
- {
- Refresh(data.Result);
- }
- }, TaskScheduler.FromCurrentSynchronizationContext());
- }
- }
- private void SelectTable_Click(string key, bool isChecked)
- {
- if (!isChecked) return;
- Properties.Table = key;
- Properties.Columns = null;
- UpdateData();
- if(_data is not null)
- {
- Refresh(_data);
- }
- }
- private void Border_ContextMenuOpening(object sender, ContextMenuEventArgs e)
- {
- var menu = new ContextMenu();
- CustomiseMenu(menu);
- menu.IsOpen = true;
- e.Handled = true;
- }
- public void Refresh(DynamicDashboardData data)
- {
- _data = data;
- if (!DataComponent.TryGetTable(Properties.Table, out var _table) || Grid is null)
- {
- return;
- }
- if(data.TryGetData(Properties.Table, out var table))
- {
- Grid.Table = table;
- }
- else
- {
- Grid.Table.Rows.Clear();
- }
- Grid.Refresh(false, true);
- }
- public void Shutdown(CancelEventArgs? cancel)
- {
- }
- private class DataGridColumnSchema(DynamicDashboardGridPresenter presenter) : IDynamicGridColumnSchema
- {
- public IEnumerable<string> ColumnNames
- {
- get
- {
- if (presenter.DataComponent.TryGetTable(presenter.Properties.Table, out var table))
- {
- return table.GetColumns(presenter.DataComponent).Where(x => EditorUtils.GetEditor(x.DataType) is not null).Select(x => x.ColumnName);
- }
- else
- {
- return [];
- }
- }
- }
- public DynamicGridColumn GetColumn(string column)
- {
- var table = presenter.DataComponent.GetTable(presenter.Properties.Table);
- var coreCol = table.GetColumns(presenter.DataComponent).First(x => x.ColumnName == column);
- return DynamicGridColumn.FromCoreColumn(coreCol)!;
- }
- public string? GetComment(string column)
- {
- return null;
- }
- public bool IsVisible(string column)
- {
- return true;
- }
- }
- private class DataGrid(DynamicDashboardGridPresenter presenter) : CoreTableGrid
- {
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.SelectColumns = true;
- }
- public override DynamicGridColumns GenerateColumns()
- {
- if(presenter.Properties.Columns is not null)
- {
- var columns = new DynamicGridColumns();
- columns.AddRange(presenter.Properties.Columns.Select(x => x.Copy()));
- return columns;
- }
- else
- {
- return base.GenerateColumns();
- }
- }
- protected override void LoadColumnsMenu(ContextMenu menu)
- {
- base.LoadColumnsMenu(menu);
- menu.AddSeparatorIfNeeded();
- presenter.CustomiseMenu(menu);
- menu.RemoveUnnecessarySeparators();
- }
- protected override void SaveColumns(DynamicGridColumns columns)
- {
- base.SaveColumns(columns);
- presenter.Properties.Columns = columns.Count > 0 ? columns : null;
- }
- protected override DynamicGridColumns LoadColumns()
- {
- return presenter.Properties.Columns ?? base.LoadColumns();
- }
- }
- }
|