231 lines
7.8 KiB
C#
231 lines
7.8 KiB
C#
using Data.Models;
|
|
using Library14Petrushin;
|
|
using Library15Gerimovich.OfficePackage.HelperModels;
|
|
using Library15Gerimovich;
|
|
using WinFormsLibrary1;
|
|
using WinFormsLibrary1.Models;
|
|
using View.ViewModels;
|
|
using PluginsConventionLibrary.Plugins;
|
|
using System.Reflection;
|
|
|
|
namespace View
|
|
{
|
|
public partial class FormMain : Form
|
|
{
|
|
private readonly Dictionary<string, IPluginsConvention> _plugins;
|
|
private string _selectedPlugin;
|
|
private ContextMenuStrip contextMenu = new ContextMenuStrip();
|
|
|
|
public FormMain()
|
|
{
|
|
InitializeComponent();
|
|
|
|
_plugins = LoadPlugins();
|
|
_selectedPlugin = string.Empty;
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<string, IPluginsConvention> LoadPlugins()
|
|
{
|
|
var plugins = new Dictionary<string, IPluginsConvention>();
|
|
|
|
string pluginsDir = Directory.GetParent(Directory.GetCurrentDirectory())!.Parent!.Parent!.Parent!.FullName + "\\Plugins";
|
|
|
|
string[] dllFiles = Directory.GetFiles(pluginsDir, "*.dll", SearchOption.AllDirectories);
|
|
|
|
foreach (string dllFile in dllFiles)
|
|
{
|
|
try
|
|
{
|
|
|
|
Assembly assembly = Assembly.LoadFrom(dllFile);
|
|
Type[] types = assembly.GetTypes();
|
|
|
|
foreach (var type in types)
|
|
{
|
|
|
|
if (typeof(IPluginsConvention).IsAssignableFrom(type) && !type.IsInterface)
|
|
{
|
|
var plugin = (IPluginsConvention)Activator.CreateInstance(type)!;
|
|
plugins.Add(plugin.PluginName, plugin);
|
|
CreateMenuItem(plugin.PluginName);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Ошибка при загрузке сборки {dllFile}: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
return plugins;
|
|
}
|
|
|
|
private void CreateMenuItem(string pluginName)
|
|
{
|
|
ToolStripMenuItem menuItem = new(pluginName);
|
|
menuItem.Click += (object? sender, EventArgs e) =>
|
|
{
|
|
UserControl userControl = _plugins[pluginName].GetControl;
|
|
if (userControl != null)
|
|
{
|
|
panelControl.Controls.Clear();
|
|
userControl.Dock = DockStyle.Fill;
|
|
_plugins[pluginName].ReloadData();
|
|
_selectedPlugin = pluginName;
|
|
panelControl.Controls.Add(userControl);
|
|
}
|
|
};
|
|
ControlsStripMenuItem.DropDownItems.Add(menuItem);
|
|
}
|
|
|
|
private void FormMain_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (!e.Control)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (e.KeyCode)
|
|
{
|
|
case Keys.A:
|
|
AddNewElement();
|
|
break;
|
|
case Keys.U:
|
|
UpdateElement();
|
|
break;
|
|
case Keys.D:
|
|
DeleteElement();
|
|
break;
|
|
case Keys.S:
|
|
CreateWord();
|
|
break;
|
|
case Keys.T:
|
|
CreatePdf();
|
|
break;
|
|
case Keys.C:
|
|
CreateExcel();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void AddNewElement()
|
|
{
|
|
var form = _plugins[_selectedPlugin].GetForm(null);
|
|
if (form != null && form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
_plugins[_selectedPlugin].ReloadData();
|
|
}
|
|
}
|
|
|
|
private void UpdateElement()
|
|
{
|
|
var element = _plugins[_selectedPlugin].GetElement;
|
|
if (element == null)
|
|
{
|
|
MessageBox.Show("Нет выбранного элемента", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
var form = _plugins[_selectedPlugin].GetForm(element);
|
|
if (form != null && form.ShowDialog() == DialogResult.OK)
|
|
{
|
|
_plugins[_selectedPlugin].ReloadData();
|
|
}
|
|
}
|
|
|
|
private void DeleteElement()
|
|
{
|
|
if (MessageBox.Show("Удалить выбранный элемент", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; }
|
|
var element = _plugins[_selectedPlugin].GetElement;
|
|
if (element == null)
|
|
{
|
|
MessageBox.Show("Нет выбранного элемента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (_plugins[_selectedPlugin].DeleteElement(element))
|
|
{
|
|
_plugins[_selectedPlugin].ReloadData();
|
|
}
|
|
}
|
|
|
|
private void CreateWord()
|
|
{
|
|
using (var dialog = new SaveFileDialog { Filter = "docx|*.docx" })
|
|
{
|
|
if (_plugins[_selectedPlugin].CreateWord(new PluginsConventionSaveDocument { FileName = dialog.FileName }))
|
|
{
|
|
//MessageBox.Show("Документ сохранен", "Создание документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void CreateExcel()
|
|
{
|
|
using (var dialog = new SaveFileDialog { Filter = "xlsx|*.xlsx" })
|
|
{
|
|
if (_plugins[_selectedPlugin].CreateExcel(new PluginsConventionSaveDocument { FileName = dialog.FileName }))
|
|
{
|
|
//MessageBox.Show("Документ сохранен", "Создание документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void CreatePdf()
|
|
{
|
|
using (var dialog = new SaveFileDialog { Filter = "pdf|*.pdf" })
|
|
{
|
|
if (_plugins[_selectedPlugin].CreatePdf(new PluginsConventionSaveDocument { FileName = dialog.FileName }))
|
|
{
|
|
//MessageBox.Show("Документ сохранен", "Создание документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddElementToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
AddNewElement();
|
|
}
|
|
|
|
private void UpdElementToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
UpdateElement();
|
|
}
|
|
|
|
private void DelElementToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
DeleteElement();
|
|
}
|
|
|
|
private void WordDocToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
CreateWord();
|
|
}
|
|
|
|
private void TableDocToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
CreateExcel();
|
|
}
|
|
|
|
private void ChartDocToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
CreatePdf();
|
|
}
|
|
}
|
|
}
|