2021-03-27 23:50:29 +04:00
|
|
|
|
using DesktopTools.Models;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using ModuleTools.Attributes;
|
|
|
|
|
using ModuleTools.BindingModels;
|
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Extensions;
|
|
|
|
|
using ModuleTools.ViewModels;
|
2021-03-27 23:50:29 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-03-29 23:16:11 +04:00
|
|
|
|
using System.Linq;
|
2021-03-27 23:50:29 +04:00
|
|
|
|
using System.Reflection;
|
2021-03-29 23:16:11 +04:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-27 23:50:29 +04:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace DesktopTools.Controls
|
|
|
|
|
{
|
2021-03-30 22:34:31 +04:00
|
|
|
|
public abstract partial class AbstractGenericControlEntityList<G, S, L, E, BL> : AbstractControlViewEntityList
|
2021-03-29 23:16:11 +04:00
|
|
|
|
where G : GetBindingModel, new()
|
|
|
|
|
where S : SetBindingModel, new()
|
|
|
|
|
where L : ListViewModel<E>
|
2021-03-27 23:50:29 +04:00
|
|
|
|
where E : ElementViewModel
|
2021-03-29 12:13:47 +04:00
|
|
|
|
where BL : GenericBusinessLogic<G, S, L, E>
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Объект бизнес-логики для получения данных
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected BL _businessLogic;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Констркутор
|
|
|
|
|
/// </summary>
|
2021-03-30 22:34:31 +04:00
|
|
|
|
public AbstractGenericControlEntityList()
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
InitEvents();
|
2021-03-28 19:48:15 +04:00
|
|
|
|
_businessLogic = DependencyManager.Instance.Resolve<BL>();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 23:16:11 +04:00
|
|
|
|
public override async void Open()
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (dataGridViewList.Columns.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Configurate(GetConfig());
|
|
|
|
|
}
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await LoadListAsync();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
public override string GetTitleFromId(Guid id) => _businessLogic.GetElement(new G { Id = id })?.ToString();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конфигуратор контрола
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model">Настройки</param>
|
|
|
|
|
private void Configurate(ControlViewEntityListConfiguration config)
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
if (config == null)
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// формирование таблицы на основе модели
|
|
|
|
|
dataGridViewList.Columns.Clear();
|
|
|
|
|
var properties = typeof(E).GetProperties();
|
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
{
|
|
|
|
|
var attr = property.GetCustomAttribute<ViewModelOnListPropertyAttribute>();
|
|
|
|
|
if (attr != null)
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
var colIndex = attr.DisplayName == "Идентификатор" ? 0 : dataGridViewList.Columns.Count;
|
|
|
|
|
dataGridViewList.Columns.Insert(colIndex, new DataGridViewTextBoxColumn
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
HeaderText = attr.DisplayName,
|
|
|
|
|
Name = string.Format("Column{0}", property.Name),
|
|
|
|
|
ReadOnly = true,
|
|
|
|
|
Visible = !attr.IsHide,
|
|
|
|
|
Width = attr.ColumnWidth ?? 0,
|
|
|
|
|
AutoSizeMode = attr.ColumnWidth.HasValue ? DataGridViewAutoSizeColumnMode.None : DataGridViewAutoSizeColumnMode.Fill
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// настройка отображения основных кнопок
|
|
|
|
|
if (config.ShowToolStripButton != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (ToolStripItem button in toolStripMenu.Items)
|
|
|
|
|
{
|
|
|
|
|
if (config.ShowToolStripButton.Contains(button.Name))
|
|
|
|
|
{
|
|
|
|
|
button.Visible = false;
|
|
|
|
|
switch (button.Name)
|
|
|
|
|
{
|
|
|
|
|
case "toolStripButtonUpd":
|
|
|
|
|
toolStripSeparator1.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case "toolStripButtonDel":
|
|
|
|
|
toolStripSeparator2.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case "toolStripButtonRef":
|
|
|
|
|
toolStripSeparator3.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case "toolStripButtonSearch":
|
|
|
|
|
toolStripSeparator4.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Загрузка подпунктов в контекстное меню и в пункт меню "Действие"
|
|
|
|
|
if (config.ControlOnMoveElem != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var elem in config.ControlOnMoveElem)
|
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem item = new() { Text = elem.Value.Title, Name = elem.Key };
|
|
|
|
|
item.Click += elem.Value.Event;
|
|
|
|
|
toolStripSplitButtonActions.DropDownItems.Add(item);
|
|
|
|
|
contextMenuStripDataGrid.Items.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// либо скрытие пункта, если не предусмотренно подпунктов
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
toolStripSplitButtonActions.Visible = false;
|
|
|
|
|
toolStripSeparator3.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Пагинация
|
|
|
|
|
if (config.PaginationOn)
|
|
|
|
|
{
|
|
|
|
|
if (config.CountElementsOnPage.HasValue)
|
|
|
|
|
{
|
|
|
|
|
toolStripTextBoxPage.Tag = config.CountElementsOnPage.Value;
|
|
|
|
|
}
|
|
|
|
|
// пагинация по названиям
|
|
|
|
|
if (config.PageNamesForPagination != null)
|
|
|
|
|
{
|
|
|
|
|
toolStripButtonPrev.Visible = toolStripLabelPage.Visible = toolStripTextBoxPage.Visible = toolStripLabelCountPages.Visible =
|
|
|
|
|
toolStripButtonNext.Visible = false;
|
|
|
|
|
toolStripComboBoxPageNames.Items.AddRange(config.PageNamesForPagination.ToArray());
|
|
|
|
|
toolStripComboBoxPageNames.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
// пагинация по страницам
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
toolStripLabelPageName.Visible = toolStripComboBoxPageNames.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// нет пагинации
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
toolStripFooter.Visible = false;
|
|
|
|
|
}
|
2021-03-30 22:34:31 +04:00
|
|
|
|
|
|
|
|
|
// при вызове контрола для выбора элемента, делаем возможность выбора только одной записи
|
|
|
|
|
dataGridViewList.MultiSelect = FormForSelected == null;
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Инициализация событий к контролам
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void InitEvents()
|
|
|
|
|
{
|
|
|
|
|
toolStripButtonAdd.Click += (object sender, EventArgs e) => { CallAddElementEvent(); };
|
|
|
|
|
toolStripButtonUpd.Click += (object sender, EventArgs e) => { CallUpdElementEvent(); };
|
|
|
|
|
toolStripButtonDel.Click += (object sender, EventArgs e) => { CallDelElementEvent(); };
|
|
|
|
|
toolStripButtonSearch.Click += (object sender, EventArgs e) => { panelSearch.Visible = !panelSearch.Visible; };
|
2021-03-29 23:16:11 +04:00
|
|
|
|
toolStripButtonRef.Click += async (object sender, EventArgs e) => { await LoadListAsync(); };
|
2021-03-27 23:50:29 +04:00
|
|
|
|
toolStripButtonClose.Click += (object sender, EventArgs e) => { Close(); };
|
|
|
|
|
|
2021-03-29 23:16:11 +04:00
|
|
|
|
buttonSearch.Click += async (object sender, EventArgs e) => { await LoadListAsync(); };
|
2021-03-27 23:50:29 +04:00
|
|
|
|
buttonCancelSearch.Click += (object sender, EventArgs e) => { panelSearch.Visible = false; };
|
|
|
|
|
|
|
|
|
|
dataGridViewList.KeyDown += (object sender, KeyEventArgs e) =>
|
|
|
|
|
{
|
|
|
|
|
switch (e.KeyCode)
|
|
|
|
|
{
|
|
|
|
|
case Keys.Insert:
|
|
|
|
|
CallAddElementEvent();
|
|
|
|
|
break;
|
|
|
|
|
case Keys.Enter:
|
|
|
|
|
CallUpdElementEvent();
|
|
|
|
|
break;
|
|
|
|
|
case Keys.Delete:
|
|
|
|
|
CallDelElementEvent();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
dataGridViewList.CellDoubleClick += (object sender, DataGridViewCellEventArgs e) =>
|
|
|
|
|
{
|
2021-03-30 22:34:31 +04:00
|
|
|
|
if (FormForSelected != null && dataGridViewList.SelectedRows.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
SelectedId = new Guid(dataGridViewList.SelectedRows[0].Cells[0].Value.ToString());
|
|
|
|
|
SelectedText = _businessLogic.GetElement(new G { Id = SelectedId })?.ToString();
|
|
|
|
|
FormForSelected.DialogResult = DialogResult.OK;
|
|
|
|
|
FormForSelected.Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-27 23:50:29 +04:00
|
|
|
|
CallUpdElementEvent();
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-29 23:16:11 +04:00
|
|
|
|
toolStripButtonPrev.Click += async (object sender, EventArgs e) =>
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(toolStripTextBoxPage.Text, out int page))
|
|
|
|
|
{
|
|
|
|
|
toolStripTextBoxPage.Text = (page - 1).ToString();
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await LoadListAsync();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-03-29 23:16:11 +04:00
|
|
|
|
toolStripButtonNext.Click += async (object sender, EventArgs e) =>
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(toolStripTextBoxPage.Text, out int page))
|
|
|
|
|
{
|
|
|
|
|
toolStripTextBoxPage.Text = (page + 1).ToString();
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await LoadListAsync();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-03-29 23:16:11 +04:00
|
|
|
|
toolStripTextBoxPage.KeyDown += async (object sender, KeyEventArgs e) =>
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (e.KeyData == Keys.Enter)
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await LoadListAsync();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-29 23:16:11 +04:00
|
|
|
|
toolStripComboBoxPageNames.SelectedIndexChanged += async (object sender, EventArgs e) => { await LoadListAsync(); };
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события загрузки данных на datagrid
|
|
|
|
|
/// </summary>
|
2021-03-29 23:16:11 +04:00
|
|
|
|
private async Task LoadListAsync()
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
var cursor = Cursor.Current;
|
|
|
|
|
L data = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
|
|
|
// если включена пагинация
|
|
|
|
|
if (toolStripFooter.Visible)
|
|
|
|
|
{
|
|
|
|
|
// постраничная
|
|
|
|
|
if (toolStripTextBoxPage.Visible)
|
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(toolStripTextBoxPage.Text, out int page) && int.TryParse(toolStripTextBoxPage.Tag.ToString(), out int count))
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await Task.Run(() => data = GetDataWithPageNumber(page, count));
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// поименная
|
|
|
|
|
else if (toolStripComboBoxPageNames.Visible)
|
|
|
|
|
{
|
|
|
|
|
var key = toolStripComboBoxPageNames.Text;
|
|
|
|
|
if (key.IsNotEmpty())
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await Task.Run(() => data = GetDataWithPageName(key));
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
await Task.Run(() => data = GetData());
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data == null)
|
|
|
|
|
{
|
|
|
|
|
// TODO вывод сообщения об ощибок
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toolStripLabelCountPages.Text = $"из {data.MaxCount}";
|
2021-03-29 23:16:11 +04:00
|
|
|
|
FillDataOnGridAsync(data.List);
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = cursor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Заполнение таблицы
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
2021-03-29 23:16:11 +04:00
|
|
|
|
private void FillDataOnGridAsync(List<E> data)
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (data == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dataGridViewList.Rows.Clear();
|
|
|
|
|
foreach (var elem in data)
|
|
|
|
|
{
|
|
|
|
|
var mas = new List<object>();
|
|
|
|
|
foreach (DataGridViewColumn column in dataGridViewList.Columns)
|
|
|
|
|
{
|
|
|
|
|
mas.Add(elem.GetType().GetProperty(column.Name["Column".Length..])?.GetValue(elem));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataGridViewList.Rows.Add(mas.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при добавлении элемента
|
|
|
|
|
/// </summary>
|
2021-03-29 23:16:11 +04:00
|
|
|
|
private void CallAddElementEvent() => LaunchControl(null);
|
2021-03-27 23:50:29 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при изменении элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void CallUpdElementEvent()
|
|
|
|
|
{
|
|
|
|
|
foreach (DataGridViewRow selected in dataGridViewList.SelectedRows)
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
LaunchControl(new Guid(selected.Cells[0].Value.ToString()));
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при удалении элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void CallDelElementEvent()
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("Удалить выбранные записи?", "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
var cursor = Cursor.Current;
|
|
|
|
|
try
|
2021-03-27 23:50:29 +04:00
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
|
|
|
foreach (DataGridViewRow selected in dataGridViewList.SelectedRows)
|
|
|
|
|
{
|
|
|
|
|
_businessLogic.Delete(new G { Id = new Guid(selected.Cells[0].Value.ToString()) });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (_businessLogic.Errors.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(_businessLogic.Errors.LastOrDefault().Message ?? ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = cursor;
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 23:16:11 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание формы с контролом для работы с элементом
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
private void LaunchControl(Guid? id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-30 22:34:31 +04:00
|
|
|
|
var control = new GenericControlEntityElement<G, S, L, E, BL>();
|
|
|
|
|
control.Open(id);
|
2021-03-29 23:16:11 +04:00
|
|
|
|
var form = new Form
|
|
|
|
|
{
|
|
|
|
|
Height = control.Height,
|
|
|
|
|
Width = control.Width,
|
|
|
|
|
Text = $"{Title}. Добавление",
|
|
|
|
|
StartPosition = FormStartPosition.CenterParent,
|
|
|
|
|
ControlBox = false
|
|
|
|
|
};
|
|
|
|
|
form.Controls.Add(control);
|
|
|
|
|
control.Dock = DockStyle.Fill;
|
|
|
|
|
control.Form = form;
|
|
|
|
|
form.Show();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-27 23:50:29 +04:00
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
protected abstract ControlViewEntityListConfiguration GetConfig();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
protected abstract L GetData();
|
2021-03-27 23:50:29 +04:00
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
protected abstract L GetDataWithPageName(string key);
|
2021-03-27 23:50:29 +04:00
|
|
|
|
|
2021-03-30 22:34:31 +04:00
|
|
|
|
protected abstract L GetDataWithPageNumber(int page, int count);
|
2021-03-27 23:50:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|