236 lines
7.2 KiB
C#
236 lines
7.2 KiB
C#
using PluginsConventionLibrary;
|
||
using System.Reflection;
|
||
|
||
namespace WindowsFormAppPlugin
|
||
{
|
||
public partial class FormMain : Form
|
||
{
|
||
private readonly Dictionary<string, IPluginsConvention> _plugins;
|
||
private string _selectedPlugin;
|
||
public FormMain()
|
||
{
|
||
InitializeComponent();
|
||
_plugins = LoadPlugins();
|
||
_selectedPlugin = string.Empty;
|
||
}
|
||
private Dictionary<string, IPluginsConvention> LoadPlugins()
|
||
{
|
||
var plugins = new Dictionary<string, IPluginsConvention>();
|
||
string pluginsDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory())!.Parent!.Parent!.FullName, "Plugins");
|
||
if (!Directory.Exists(pluginsDir))
|
||
{
|
||
MessageBox.Show($"Каталог {pluginsDir} не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return plugins;
|
||
}
|
||
foreach (string dllFile in Directory.EnumerateFiles(pluginsDir, "*.dll", SearchOption.AllDirectories))
|
||
{
|
||
try
|
||
{
|
||
Assembly assembly = Assembly.LoadFrom(dllFile);
|
||
Type[] types = assembly.GetTypes();
|
||
foreach (var type in types)
|
||
{
|
||
if (typeof(IPluginsConvention).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
|
||
{
|
||
var plugin = (IPluginsConvention)Activator.CreateInstance(type)!;
|
||
plugins.Add(plugin.PluginName, plugin);
|
||
CreateToolStripMenuItem(plugin.PluginName);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка при загрузке сборки {dllFile}: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
return plugins;
|
||
|
||
}
|
||
private void CreateToolStripMenuItem(string pluginName)
|
||
{
|
||
var menuItem = new ToolStripMenuItem(pluginName);
|
||
menuItem.Click += (object? sender, EventArgs e) =>
|
||
{
|
||
_selectedPlugin = pluginName;
|
||
IPluginsConvention plugin = _plugins![pluginName];
|
||
UserControl userControl = plugin?.GetControl ?? throw new Exception("Проблема с загрузкой элемента для вывода данных");
|
||
panelControl.Controls.Clear();
|
||
plugin.ReloadData();
|
||
userControl.Dock = DockStyle.Fill;
|
||
panelControl.Controls.Add(userControl);
|
||
};
|
||
ControlsStripMenuItem.DropDownItems.Add(menuItem);
|
||
}
|
||
|
||
private void FormMain_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(_selectedPlugin) ||
|
||
!_plugins.ContainsKey(_selectedPlugin))
|
||
{
|
||
return;
|
||
}
|
||
if (!e.Control)
|
||
{
|
||
return;
|
||
}
|
||
switch (e.KeyCode)
|
||
{
|
||
case Keys.I:
|
||
ShowThesaurus();
|
||
break;
|
||
case Keys.A:
|
||
AddNewElement();
|
||
break;
|
||
case Keys.U:
|
||
UpdateElement();
|
||
break;
|
||
case Keys.D:
|
||
DeleteElement();
|
||
break;
|
||
case Keys.S:
|
||
CreateSimpleDoc();
|
||
break;
|
||
case Keys.T:
|
||
CreateTableDoc();
|
||
break;
|
||
case Keys.C:
|
||
CreateChartDoc();
|
||
break;
|
||
}
|
||
}
|
||
private void ShowThesaurus()
|
||
{
|
||
_plugins[_selectedPlugin].GetThesaurus()?.Show();
|
||
}
|
||
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 CreateSimpleDoc()
|
||
{
|
||
using var dialog = new SaveFileDialog
|
||
{
|
||
Filter = "docx|*.docx"
|
||
};
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
|
||
if (_plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument() { FileName = dialog.FileName}))
|
||
{
|
||
MessageBox.Show("Документ сохранен", "Создание документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
|
||
}
|
||
}
|
||
private void CreateTableDoc()
|
||
{
|
||
using var dialog = new SaveFileDialog
|
||
{
|
||
Filter = "PDF Files|*.pdf"
|
||
};
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
|
||
if (_plugins[_selectedPlugin].CreateTableDocument(new PluginsConventionSaveDocument() { FileName = dialog.FileName }))
|
||
{
|
||
MessageBox.Show("Документ сохранен", "Создание документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Ошибка при создании документа",
|
||
"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void CreateChartDoc()
|
||
{
|
||
using var dialog = new SaveFileDialog
|
||
{
|
||
Filter = "Excel Files|*.xlsx"
|
||
};
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
|
||
if (_plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument() { FileName = dialog.FileName }))
|
||
{
|
||
MessageBox.Show("Документ сохранен", "Создание документа", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Произошла ошибка: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void ThesaurusToolStripMenuItem_Click(object sender, EventArgs e) => ShowThesaurus();
|
||
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 SimpleDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateSimpleDoc();
|
||
private void TableDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateTableDoc();
|
||
private void ChartDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateChartDoc();
|
||
|
||
}
|
||
}
|