DepartmentProject/DepartmentPortal/DepartmentPortalDesctop/FormMain.cs

146 lines
3.8 KiB
C#

using DesktopTools.Controls;
using ModuleTools.BusinessLogics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DepartmentPortalDesctop
{
public partial class FormMain : Form
{
private Dictionary<Guid, ControlViewEntityList> _baseControls;
private Dictionary<Guid, ControlViewEntityList> _controls;
public FormMain()
{
InitializeComponent();
_baseControls = new Dictionary<Guid, ControlViewEntityList>();
_controls = new Dictionary<Guid, ControlViewEntityList>();
var extensions = ServiceProviderLoader.GetWindowDesktopExtensions();
foreach (var extens in extensions)
{
var list = extens?.GetListControlEntityList()?.OrderBy(x => x.Order).ToList();
if (list != null && list.Count > 0)
{
var menu = new ToolStripMenuItem { Text = list[0].Title };
for (int i = 0; i < list.Count; i++)
{
if (list[i].Control is ControlViewEntityList control)
{
if (_baseControls.ContainsKey(list[i].Id))
{
continue;
}
_baseControls.Add(list[i].Id, control);
var submenu = new ToolStripMenuItem { Text = list[i].Title, Tag = list[i].Id };
submenu.Click += (object sender, EventArgs e) =>
{
OpenControl(new Guid((sender as ToolStripMenuItem).Tag.ToString()));
};
menu.DropDownItems.Add(submenu);
}
}
menuMain.Items.Add(menu);
}
}
}
/// <summary>
/// Добавление нового контрола
/// </summary>
/// <param name="id"></param>
private void OpenControl(Guid id)
{
if (!_baseControls.ContainsKey(id))
{
return;
}
if (_baseControls[id] == null)
{
return;
}
var ctrl = _baseControls[id].Clone();
if (!_controls.ContainsKey(ctrl.Id))
{
ctrl.CloseEventAdd += CloseControl;
ctrl.Dock = DockStyle.Fill;
ctrl.Open();
_controls.Add(ctrl.Id, ctrl);
splitContainerMain.Panel1.Controls.Clear();
splitContainerMain.Panel1.Controls.Add(ctrl);
dataGridViewControls.Rows.Add(new object[] { ctrl.Id, ctrl.Title });
dataGridViewControls.Rows[dataGridViewControls.Rows.Count - 1].Selected = true;
}
}
/// <summary>
/// Закрытие контрола
/// </summary>
/// <param name="id"></param>
private void CloseControl(Guid id)
{
if (!_controls.ContainsKey(id))
{
return;
}
var ctrl = _controls[id];
if (ctrl == null)
{
return;
}
for (int i = 0; i < dataGridViewControls.Rows.Count; ++i)
{
if (dataGridViewControls.Rows[i].Cells[0].Value.ToString() == ctrl.Id.ToString())
{
dataGridViewControls.Rows.RemoveAt(i);
if (i < dataGridViewControls.Rows.Count - 1)
{
dataGridViewControls.Rows[i].Selected = true;
ShowControl(new Guid(dataGridViewControls.Rows[i].Cells[0].Value.ToString()));
}
else if (i > 0)
{
dataGridViewControls.Rows[i - 1].Selected = true;
ShowControl(new Guid(dataGridViewControls.Rows[i - 1].Cells[0].Value.ToString()));
}
break;
}
}
_controls.Remove(ctrl.Id);
}
/// <summary>
/// Отображение контрола
/// </summary>
/// <param name="id"></param>
private void ShowControl(Guid id)
{
if (!_controls.ContainsKey(id))
{
return;
}
var ctrl = _controls[id];
if (ctrl == null)
{
return;
}
splitContainerMain.Panel1.Controls.Clear();
splitContainerMain.Panel1.Controls.Add(ctrl);
}
private void ButtonShowHideControlList_Click(object sender, EventArgs e)
{
ButtonShowHideControlList.Text = panelControls.Visible ? ">" : "<";
panelControls.Visible = !panelControls.Visible;
}
private void DataGridViewControls_CellClick(object sender, DataGridViewCellEventArgs e) => ShowControl(new Guid(dataGridViewControls.Rows[e.RowIndex].Cells[0].Value.ToString()));
}
}