From 8289e0f2a624509ae9ea2ff17725460dc0b59af7 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Fri, 1 Dec 2023 03:02:22 +0400 Subject: [PATCH] Lab 4 done --- .../AccountsView/PluginsConvention.cs | 21 +- .../AppByPlugins/AppByPlugins.csproj | 21 ++ .../AppByPlugins/FormMain.Designer.cs | 176 +++++++++++++ NevaevaLibrary/AppByPlugins/FormMain.cs | 236 ++++++++++++++++++ NevaevaLibrary/AppByPlugins/FormMain.resx | 63 +++++ NevaevaLibrary/AppByPlugins/Program.cs | 17 ++ NevaevaLibrary/NevaevaLibrary.sln | 6 + 7 files changed, 536 insertions(+), 4 deletions(-) create mode 100644 NevaevaLibrary/AppByPlugins/AppByPlugins.csproj create mode 100644 NevaevaLibrary/AppByPlugins/FormMain.Designer.cs create mode 100644 NevaevaLibrary/AppByPlugins/FormMain.cs create mode 100644 NevaevaLibrary/AppByPlugins/FormMain.resx create mode 100644 NevaevaLibrary/AppByPlugins/Program.cs diff --git a/NevaevaLibrary/AccountsView/PluginsConvention.cs b/NevaevaLibrary/AccountsView/PluginsConvention.cs index 7dc7ad7..8c16ed9 100644 --- a/NevaevaLibrary/AccountsView/PluginsConvention.cs +++ b/NevaevaLibrary/AccountsView/PluginsConvention.cs @@ -4,6 +4,7 @@ using AccountBusinessLogic.BusinessLogic; using AccountContracts.BusinessLogicsContracts; using AccountContracts.ViewModels; using AccountDatabaseImplement.Implements; +using AccountDatabaseImplement.Models; using ComponentsLibraryNet60.DocumentWithChart; using ComponentsLibraryNet60.DocumentWithTable; using ComponentsLibraryNet60.Models; @@ -20,7 +21,7 @@ using System.Windows.Forms; namespace AccountsView { - public class PluginsConvention + public class PluginsConvention : IPluginsConvention { private readonly IAccountLogic _accountLogic; private readonly IRoleLogic _roleLogic; @@ -88,14 +89,26 @@ namespace AccountsView { try { - var account = _accountLogic.ReadList(null); - if (account != null) + var accounts = _accountLogic.ReadList(null); + if (accounts != null) { _abazovTreeView.clear(); _abazovTreeView.setHierarchy(new List<(string, bool)> { ("RoleName", false), ("ViewRating", false), ("Id", true), ("Login", true) }); - _abazovTreeView.addItems(account); + foreach (var account in accounts) + { + if (!account.Rating.HasValue) + { + account.ViewRating = 0; + } + else + { + account.ViewRating = account.Rating.Value; + } + } + + _abazovTreeView.addItems(accounts); } } catch (Exception ex) diff --git a/NevaevaLibrary/AppByPlugins/AppByPlugins.csproj b/NevaevaLibrary/AppByPlugins/AppByPlugins.csproj new file mode 100644 index 0000000..177d96d --- /dev/null +++ b/NevaevaLibrary/AppByPlugins/AppByPlugins.csproj @@ -0,0 +1,21 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + + + + + + + + + \ No newline at end of file diff --git a/NevaevaLibrary/AppByPlugins/FormMain.Designer.cs b/NevaevaLibrary/AppByPlugins/FormMain.Designer.cs new file mode 100644 index 0000000..85a2f10 --- /dev/null +++ b/NevaevaLibrary/AppByPlugins/FormMain.Designer.cs @@ -0,0 +1,176 @@ +using static System.Net.Mime.MediaTypeNames; +using System.Windows.Forms; + +namespace AppByPlugins +{ + partial class FormMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + menuStrip = new MenuStrip(); + ControlsStripMenuItem = new ToolStripMenuItem(); + ActionsToolStripMenuItem = new ToolStripMenuItem(); + ThesaurusToolStripMenuItem = new ToolStripMenuItem(); + AddElementToolStripMenuItem = new ToolStripMenuItem(); + UpdElementToolStripMenuItem = new ToolStripMenuItem(); + DelElementToolStripMenuItem = new ToolStripMenuItem(); + DocsToolStripMenuItem = new ToolStripMenuItem(); + SimpleDocToolStripMenuItem = new ToolStripMenuItem(); + TableDocToolStripMenuItem = new ToolStripMenuItem(); + ChartDocToolStripMenuItem = new ToolStripMenuItem(); + panelControl = new Panel(); + menuStrip.SuspendLayout(); + SuspendLayout(); + // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { ControlsStripMenuItem, ActionsToolStripMenuItem, DocsToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(800, 24); + menuStrip.TabIndex = 0; + menuStrip.Text = "Меню"; + // + // ControlsStripMenuItem + // + ControlsStripMenuItem.Name = "ControlsStripMenuItem"; + ControlsStripMenuItem.Size = new Size(90, 20); + ControlsStripMenuItem.Text = "Компоненты"; + // + // ActionsToolStripMenuItem + // + ActionsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { ThesaurusToolStripMenuItem, AddElementToolStripMenuItem, UpdElementToolStripMenuItem, DelElementToolStripMenuItem }); + ActionsToolStripMenuItem.Name = "ActionsToolStripMenuItem"; + ActionsToolStripMenuItem.Size = new Size(70, 20); + ActionsToolStripMenuItem.Text = "Действия"; + // + // ThesaurusToolStripMenuItem + // + ThesaurusToolStripMenuItem.Name = "ThesaurusToolStripMenuItem"; + ThesaurusToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.I; + ThesaurusToolStripMenuItem.Size = new Size(180, 22); + ThesaurusToolStripMenuItem.Text = "Справочник"; + ThesaurusToolStripMenuItem.Click += ThesaurusToolStripMenuItem_Click; + // + // AddElementToolStripMenuItem + // + AddElementToolStripMenuItem.Name = "AddElementToolStripMenuItem"; + AddElementToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.A; + AddElementToolStripMenuItem.Size = new Size(180, 22); + AddElementToolStripMenuItem.Text = "Добавить"; + AddElementToolStripMenuItem.Click += AddElementToolStripMenuItem_Click; + // + // UpdElementToolStripMenuItem + // + UpdElementToolStripMenuItem.Name = "UpdElementToolStripMenuItem"; + UpdElementToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.U; + UpdElementToolStripMenuItem.Size = new Size(180, 22); + UpdElementToolStripMenuItem.Text = "Изменить"; + UpdElementToolStripMenuItem.Click += UpdElementToolStripMenuItem_Click; + // + // DelElementToolStripMenuItem + // + DelElementToolStripMenuItem.Name = "DelElementToolStripMenuItem"; + DelElementToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.D; + DelElementToolStripMenuItem.Size = new Size(180, 22); + DelElementToolStripMenuItem.Text = "Удалить"; + DelElementToolStripMenuItem.Click += DelElementToolStripMenuItem_Click; + // + // DocsToolStripMenuItem + // + DocsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SimpleDocToolStripMenuItem, TableDocToolStripMenuItem, ChartDocToolStripMenuItem }); + DocsToolStripMenuItem.Name = "DocsToolStripMenuItem"; + DocsToolStripMenuItem.Size = new Size(82, 20); + DocsToolStripMenuItem.Text = "Документы"; + // + // SimpleDocToolStripMenuItem + // + SimpleDocToolStripMenuItem.Name = "SimpleDocToolStripMenuItem"; + SimpleDocToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + SimpleDocToolStripMenuItem.Size = new Size(233, 22); + SimpleDocToolStripMenuItem.Text = "Простой документ"; + SimpleDocToolStripMenuItem.Click += SimpleDocToolStripMenuItem_Click; + // + // TableDocToolStripMenuItem + // + TableDocToolStripMenuItem.Name = "TableDocToolStripMenuItem"; + TableDocToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.T; + TableDocToolStripMenuItem.Size = new Size(233, 22); + TableDocToolStripMenuItem.Text = "Документ с таблицой"; + TableDocToolStripMenuItem.Click += TableDocToolStripMenuItem_Click; + // + // ChartDocToolStripMenuItem + // + ChartDocToolStripMenuItem.Name = "ChartDocToolStripMenuItem"; + ChartDocToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.C; + ChartDocToolStripMenuItem.Size = new Size(233, 22); + ChartDocToolStripMenuItem.Text = "Документ с диаграммой"; + ChartDocToolStripMenuItem.Click += ChartDocToolStripMenuItem_Click; + // + // panelControl + // + panelControl.Dock = DockStyle.Fill; + panelControl.Location = new Point(0, 24); + panelControl.Name = "panelControl"; + panelControl.Size = new Size(800, 426); + panelControl.TabIndex = 1; + // + // FormMain + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(panelControl); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; + Name = "FormMain"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Главная форма"; + WindowState = FormWindowState.Maximized; + KeyDown += FormMain_KeyDown; + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private System.Windows.Forms.MenuStrip menuStrip; + private System.Windows.Forms.ToolStripMenuItem ControlsStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem DocsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem SimpleDocToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem TableDocToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem ChartDocToolStripMenuItem; + private System.Windows.Forms.Panel panelControl; + private System.Windows.Forms.ToolStripMenuItem ActionsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem ThesaurusToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem AddElementToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem UpdElementToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem DelElementToolStripMenuItem; + } +} \ No newline at end of file diff --git a/NevaevaLibrary/AppByPlugins/FormMain.cs b/NevaevaLibrary/AppByPlugins/FormMain.cs new file mode 100644 index 0000000..6565442 --- /dev/null +++ b/NevaevaLibrary/AppByPlugins/FormMain.cs @@ -0,0 +1,236 @@ +using PluginsConventionLibrary; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AppByPlugins +{ + public partial class FormMain : Form + { + private readonly Dictionary _plugins; + private string _selectedPlugin; + + public FormMain() + { + InitializeComponent(); + _plugins = new(); + LoadPlugins(); + _selectedPlugin = string.Empty; + } + + private void LoadPlugins() + { + List pluginsList = GetPlugins(); + + foreach (var plugin in pluginsList) + { + _plugins[plugin.PluginName] = plugin; + CreateMenuItem(plugin.PluginName); + } + } + + private List GetPlugins() + { + string currentDir = Environment.CurrentDirectory; + string pluginsDir = Directory.GetParent(currentDir).Parent.Parent.Parent.FullName + "\\Plugins"; + string[] dllFiles = Directory.GetFiles( + pluginsDir, + "*.dll", + SearchOption.AllDirectories + ); + List plugins = new(); + 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) + { + if (Activator.CreateInstance(type) is IPluginsConvention plugin) + { + plugins.Add(plugin); + } + } + } + } + catch (Exception ex) + { + MessageBox.Show( + 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 (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() + { + SaveFileDialog saveFileDialog = new() + { + Filter = "Word Files|*.docx" + }; + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }); + + } + } + private void CreateTableDoc() + { + SaveFileDialog saveFileDialog = new() + { + Filter = "PDF Files|*.pdf" + }; + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].CreateTableDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }); + + } + } + private void CreateChartDoc() + { + SaveFileDialog saveFileDialog = new() + { + Filter = "Excel Files|*.xlsx" + }; + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + _plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument() { FileName = saveFileDialog.FileName }); + } + } + + 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(); + } +} diff --git a/NevaevaLibrary/AppByPlugins/FormMain.resx b/NevaevaLibrary/AppByPlugins/FormMain.resx new file mode 100644 index 0000000..81a9e3d --- /dev/null +++ b/NevaevaLibrary/AppByPlugins/FormMain.resx @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/NevaevaLibrary/AppByPlugins/Program.cs b/NevaevaLibrary/AppByPlugins/Program.cs new file mode 100644 index 0000000..a783345 --- /dev/null +++ b/NevaevaLibrary/AppByPlugins/Program.cs @@ -0,0 +1,17 @@ +namespace AppByPlugins +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new FormMain()); + } + } +} \ No newline at end of file diff --git a/NevaevaLibrary/NevaevaLibrary.sln b/NevaevaLibrary/NevaevaLibrary.sln index 50e81c9..d51d422 100644 --- a/NevaevaLibrary/NevaevaLibrary.sln +++ b/NevaevaLibrary/NevaevaLibrary.sln @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccountDatabaseImplement", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluginsConventionLibrary", "PluginsConventionLibrary\PluginsConventionLibrary.csproj", "{230C6E4F-CB10-47D7-B273-260BE1F30340}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppByPlugins", "AppByPlugins\AppByPlugins.csproj", "{A18192D3-2BCD-4C62-8C49-30D2A000CA39}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -57,6 +59,10 @@ Global {230C6E4F-CB10-47D7-B273-260BE1F30340}.Debug|Any CPU.Build.0 = Debug|Any CPU {230C6E4F-CB10-47D7-B273-260BE1F30340}.Release|Any CPU.ActiveCfg = Release|Any CPU {230C6E4F-CB10-47D7-B273-260BE1F30340}.Release|Any CPU.Build.0 = Release|Any CPU + {A18192D3-2BCD-4C62-8C49-30D2A000CA39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A18192D3-2BCD-4C62-8C49-30D2A000CA39}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A18192D3-2BCD-4C62-8C49-30D2A000CA39}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A18192D3-2BCD-4C62-8C49-30D2A000CA39}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE