196 lines
5.7 KiB
C#
196 lines
5.7 KiB
C#
using Plugins;
|
|
using System.Reflection;
|
|
|
|
namespace Lab4
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
private readonly Dictionary<string, IPluginsConvention> _plugins;
|
|
private string _selectedPlugin;
|
|
public Form1()
|
|
{
|
|
|
|
InitializeComponent();
|
|
_plugins = LoadPlugins();
|
|
_selectedPlugin = string.Empty;
|
|
}
|
|
|
|
private Dictionary<string, IPluginsConvention> LoadPlugins()
|
|
{
|
|
Dictionary<string, IPluginsConvention> plugins = new();
|
|
string currentDirectory = Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.Parent!.FullName + "\\plugin";
|
|
string[] dllFiles = Directory.GetFiles(currentDirectory, "*.dll", SearchOption.AllDirectories);
|
|
foreach (string dllFile in dllFiles)
|
|
{
|
|
try
|
|
{
|
|
Assembly assembly = Assembly.LoadFrom(dllFile);
|
|
|
|
Type[] types = assembly.GetTypes();
|
|
|
|
foreach (Type type in types)
|
|
{
|
|
if (typeof(IPluginsConvention).IsAssignableFrom(type) && !type.IsInterface)
|
|
{
|
|
var plugin = (IPluginsConvention)Activator.CreateInstance(type)!;
|
|
plugins.Add(plugin.PluginName, plugin);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Íå óäàëîñü çàãðóçèòü ïëàãèí {dllFile}: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
foreach (var plugin in plugins)
|
|
{
|
|
CreateMenuItem(plugin.Value.PluginName);
|
|
}
|
|
|
|
return plugins;
|
|
}
|
|
private void CreateMenuItem(string pluginName)
|
|
{
|
|
ToolStripMenuItem menuItem = new ToolStripMenuItem(pluginName);
|
|
menuItem.Click += (object? sender, EventArgs a) =>
|
|
{
|
|
_selectedPlugin = pluginName;
|
|
IPluginsConvention plugin = _plugins[pluginName];
|
|
UserControl userControl = plugin.GetControl;
|
|
if (userControl != null)
|
|
{
|
|
panelControl.Controls.Clear();
|
|
plugin.ReloadData();
|
|
userControl.Dock = DockStyle.Fill;
|
|
panelControl.Controls.Add(userControl);
|
|
}
|
|
};
|
|
ControlsStripMenuItem.DropDownItems.Add(menuItem);
|
|
}
|
|
private void ShowCatalog()
|
|
{
|
|
_plugins[_selectedPlugin].GetCatalog()?.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 CreateWordDoc()
|
|
{
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
|
saveFileDialog.Filter = "Word Documents (*.docx)|*.docx";
|
|
saveFileDialog.Title = "Ñîõðàíèòü ïðîñòîé äîêóìåíò";
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (_plugins[_selectedPlugin].CreateWordDocument(new
|
|
PluginsConventionSaveDocument()
|
|
{ FileName = saveFileDialog.FileName }))
|
|
{
|
|
MessageBox.Show("Äîêóìåíò ñîõðàíåí", "Ñîçäàíèå" +
|
|
" äîêóìåíòà", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Îøèáêà ïðè ñîçäàíèè äîêóìåíòà",
|
|
"Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
}
|
|
private void CreatePdfDoc()
|
|
{
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
|
saveFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
|
|
saveFileDialog.Title = "Ñîõðàíèòü òàáëèöó";
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (_plugins[_selectedPlugin].CreatePdfDocument(new
|
|
PluginsConventionSaveDocument()
|
|
{ FileName = saveFileDialog.FileName }))
|
|
{
|
|
MessageBox.Show("Äîêóìåíò ñîõðàíåí", "Ñîçäàíèå" +
|
|
" äîêóìåíòà", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Îøèáêà ïðè ñîçäàíèè äîêóìåíòà",
|
|
"Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
private void CreateExcelDoc()
|
|
{
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
|
saveFileDialog.Filter = "Excel Files (*.xlsx)|*.xlsx";
|
|
saveFileDialog.Title = "Ñîõðàíèòü äèàãðàììó";
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (_plugins[_selectedPlugin].CreateExcelDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }))
|
|
{
|
|
MessageBox.Show("Äîêóìåíò ñîõðàíåí", "Ñîçäàíèå" +
|
|
" äîêóìåíòà", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Îøèáêà ïðè ñîçäàíèè äîêóìåíòà",
|
|
"Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
}
|
|
}
|
|
private void ThesaurusToolStripMenuItem_Click(object sender,
|
|
EventArgs e) => ShowCatalog();
|
|
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) => CreateWordDoc();
|
|
private void ExcelDocToolStripMenuItem_Click(object sender, EventArgs
|
|
e) => CreateExcelDoc();
|
|
private void PdfDocToolStripMenuItem_Click(object sender, EventArgs
|
|
e) => CreatePdfDoc();
|
|
}
|
|
}
|