DepartmentProject/DepartmentPortal/DepartmentPortalDesctop/FormMain.cs

229 lines
6.1 KiB
C#

using DesktopTools.BusinessLogics;
using DesktopTools.Enums;
using DesktopTools.Interfaces;
using DesktopTools.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
namespace DepartmentPortalDesctop
{
public partial class FormMain : Form
{
private readonly Dictionary<Guid, IControl> _baseControls;
private readonly Dictionary<Guid, IControl> _controls;
private readonly string _fileForXmlConfig = "ControlsConfig.xml";
public FormMain()
{
InitializeComponent();
_baseControls = new Dictionary<Guid, IControl>();
_controls = new Dictionary<Guid, IControl>();
var extensions = DesktopLoader.GetWindowDesktopExtensions();
foreach (var extens in extensions)
{
ToolStripMenuItem menu = null;
var list = extens?.GetListControlEntityList()?.ToList();
if (list != null && list.Count > 0)
{
menu = new ToolStripMenuItem { Text = list[0].Title };
for (int i = 0; i < list.Count; i++)
{
if (list[i].Control is IControl 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);
}
}
}
list = extens?.GetListControlSpecialList()?.ToList();
if (list != null && list.Count > 0)
{
if (menu == null)
{
menu = new ToolStripMenuItem { Text = list[0].Title };
}
else if (menu.Text != list[0].Title)
{
menuMain.Items.Add(menu);
menu = new ToolStripMenuItem { Text = list[0].Title };
}
menu.DropDownItems.Add(new ToolStripSeparator());
for (int i = 0; i < list.Count; i++)
{
if (list[i].Control is IControl 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);
}
}
}
if (menu != null)
{
menuMain.Items.Add(menu);
}
}
if (File.Exists(_fileForXmlConfig))
{
var xml = XDocument.Load(_fileForXmlConfig).Element("Controls");
Guid? id = null;
if (xml != null)
{
foreach (XElement node in xml.Elements("Control"))
{
var type = node.Attribute("Type").Value.ToString();
var control = _baseControls.Values.FirstOrDefault(x => x.GetType().FullName == type);
if (control != null)
{
var ctrl = control.GetInstance();
ctrl.Open(new ControlOpenModel { OpenMode = ControlOpenMode.List, LazyLoading = true, CloseList = CloseControl });
ctrl.LoadFromXml(node.ToString());
_controls.Add(ctrl.ControlId, ctrl);
dataGridViewControls.Rows.Insert(0, new object[] { ctrl.ControlId, ctrl.Title });
id = ctrl.ControlId;
}
}
if (id.HasValue)
{
ShowControl(id.Value);
}
}
}
}
/// <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].GetInstance();
if (!_controls.ContainsKey(ctrl.ControlId))
{
ctrl.Open(new ControlOpenModel { OpenMode = ControlOpenMode.List, CloseList = CloseControl });
_controls.Add(ctrl.ControlId, ctrl);
panelControl.Controls.Clear();
panelControl.Controls.Add(ctrl as UserControl);
dataGridViewControls.Rows.Add(new object[] { ctrl.ControlId, ctrl.Title });
dataGridViewControls.Rows[^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.ControlId.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.ControlId);
}
/// <summary>
/// Отображение контрола
/// </summary>
/// <param name="id"></param>
private void ShowControl(Guid id)
{
if (!_controls.ContainsKey(id))
{
return;
}
var ctrl = _controls[id];
if (ctrl == null)
{
return;
}
panelControl.Controls.Clear();
panelControl.Controls.Add(ctrl as UserControl);
}
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()));
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
var xml = new XElement("Controls");
foreach (var cntrl in _controls)
{
xml.AddFirst(XElement.Parse(cntrl.Value.SaveToXml()));
}
if (File.Exists(_fileForXmlConfig))
{
File.Delete(_fileForXmlConfig);
}
xml.Save(_fileForXmlConfig);
}
}
}