Add Configuration Interface

This commit is contained in:
Sergey Chebotar 2023-03-28 07:25:10 +03:00
parent 9846a3c35e
commit 15dd27fb68
7 changed files with 74 additions and 83 deletions

View File

@ -3,6 +3,7 @@ using ExcelDna.IntelliSense;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using RhSolutions.Services; using RhSolutions.Services;
using System;
using System.Net; using System.Net;
namespace RhSolutions.AddIn namespace RhSolutions.AddIn
@ -11,6 +12,7 @@ namespace RhSolutions.AddIn
{ {
public static Application Excel { get; private set; } public static Application Excel { get; private set; }
public static ServiceProvider ServiceProvider { get; set; } public static ServiceProvider ServiceProvider { get; set; }
public static IAddInConfiguration Configuration { get; set; }
public void AutoOpen() public void AutoOpen()
{ {
@ -18,12 +20,13 @@ namespace RhSolutions.AddIn
Excel = (Application)ExcelDnaUtil.Application; Excel = (Application)ExcelDnaUtil.Application;
Services.AddHttpClient() Services.AddHttpClient()
.AddSingleton<IDatabaseClient, RhDatabaseClient>(); .AddSingleton<IDatabaseClient, RhDatabaseClient>()
.AddSingleton<IAddInConfiguration, AddInConfiguration>();
ServiceProvider = Services.BuildServiceProvider(); ServiceProvider = Services.BuildServiceProvider();
Configuration = ServiceProvider.GetService<IAddInConfiguration>();
IntelliSenseServer.Install(); IntelliSenseServer.Install();
RegistryUtil.Initialize();
EventsUtil.Initialize(); EventsUtil.Initialize();
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol =
@ -33,7 +36,6 @@ namespace RhSolutions.AddIn
public void AutoClose() public void AutoClose()
{ {
IntelliSenseServer.Uninstall(); IntelliSenseServer.Uninstall();
RegistryUtil.Uninitialize();
EventsUtil.Uninitialize(); EventsUtil.Uninitialize();
} }
} }

View File

@ -1,4 +1,5 @@
using ExcelDna.Integration.CustomUI; using ExcelDna.Integration.CustomUI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn; using RhSolutions.AddIn;
using RhSolutions.Services; using RhSolutions.Services;
@ -51,13 +52,15 @@ namespace RhSolutions.Controllers
ribbonUi.InvalidateControl(id); ribbonUi.InvalidateControl(id);
} }
} }
public void OnSetPricePressed(IRibbonControl control) public void OnSetPricePressed(IRibbonControl control)
{ {
string path = Models.Dialog.GetFilePath(); string path = Models.Dialog.GetFilePath();
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(path))
{ {
RegistryUtil.PriceListPath = path; RhSolutionsAddIn.Configuration.SetPriceListPath(path);
RhSolutionsAddIn.Configuration.SaveSettings();
} }
} }
@ -131,7 +134,7 @@ namespace RhSolutions.Controllers
public string GetPriceListPathLabel(IRibbonControl control) public string GetPriceListPathLabel(IRibbonControl control)
{ {
string name = RegistryUtil.GetPriceListName(); string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
return string.IsNullOrEmpty(name) ? "Нет файла шаблона!" : name; return string.IsNullOrEmpty(name) ? "Нет файла шаблона!" : name;
} }
} }

View File

@ -12,22 +12,22 @@ namespace RhSolutions.Controllers
internal abstract class ToolBase internal abstract class ToolBase
{ {
protected Application ExcelApp = RhSolutionsAddIn.Excel; protected Application ExcelApp = RhSolutionsAddIn.Excel;
protected IAddInConfiguration Configuration = RhSolutionsAddIn.Configuration;
protected TargetPriceList TargetFile { get; set; } protected TargetPriceList TargetFile { get; set; }
protected ResultBar ResultBar { get; set; } protected ResultBar ResultBar { get; set; }
protected ProgressBar ProgressBar { get; set; } protected ProgressBar ProgressBar { get; set; }
public abstract void FillTarget(); public abstract void FillTarget();
public void OpenNewPrice() public void OpenNewPrice()
{ {
if (ExcelApp.Workbooks if (ExcelApp.Workbooks
.Cast<Workbook>() .Cast<Workbook>()
.FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) .FirstOrDefault(w => w.FullName == Configuration.GetPriceListPath()) != null)
{ {
throw new ArgumentException("Шаблонный файл редактируется в другом месте"); throw new ArgumentException("Шаблонный файл редактируется в другом месте");
} }
Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); Workbook wb = ExcelApp.Workbooks.Open(Configuration.GetPriceListPath(), null, true);
try try
{ {

View File

@ -0,0 +1,39 @@
using System;
using System.Configuration;
using System.IO;
namespace RhSolutions.Services
{
public class AddInConfiguration : ApplicationSettingsBase, IAddInConfiguration
{
[UserScopedSetting]
public string PriceListPath
{
get
{
return (string)this["PriceListPath"];
}
set
{
this["PriceListPath"] = value;
}
}
public event SettingChangingEventHandler OnSettingsChange
{
add
{
base.SettingChanging += value;
}
remove
{
base.SettingChanging -= value;
}
}
public string GetPriceListFileName() => Path.GetFileName(PriceListPath);
public string GetPriceListPath() => PriceListPath;
public void SetPriceListPath(string value) => PriceListPath = value;
public void SaveSettings() => base.Save();
}
}

View File

@ -1,6 +1,7 @@
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn; using RhSolutions.AddIn;
using RhSolutions.Controllers; using RhSolutions.Controllers;
using System.Configuration;
namespace RhSolutions.Services namespace RhSolutions.Services
{ {
@ -13,6 +14,7 @@ namespace RhSolutions.Services
Excel.SheetSelectionChange += RefreshExportButton; Excel.SheetSelectionChange += RefreshExportButton;
Excel.SheetActivate += RefreshConvertButton; Excel.SheetActivate += RefreshConvertButton;
Excel.WorkbookActivate += RefreshConvertButton; Excel.WorkbookActivate += RefreshConvertButton;
RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle;
} }
public static void Uninitialize() public static void Uninitialize()
@ -20,6 +22,7 @@ namespace RhSolutions.Services
Excel.SheetSelectionChange -= RefreshExportButton; Excel.SheetSelectionChange -= RefreshExportButton;
Excel.SheetActivate -= RefreshConvertButton; Excel.SheetActivate -= RefreshConvertButton;
Excel.WorkbookActivate -= RefreshConvertButton; Excel.WorkbookActivate -= RefreshConvertButton;
RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle;
} }
private static void RefreshConvertButton(object sh) private static void RefreshConvertButton(object sh)
@ -31,5 +34,10 @@ namespace RhSolutions.Services
{ {
RibbonController.RefreshControl("export"); RibbonController.RefreshControl("export");
} }
private static void RefreshSettingTitle(object sender, SettingChangingEventArgs e)
{
RibbonController.RefreshControl("setPriceList");
}
} }
} }

View File

@ -0,0 +1,13 @@
using System.Configuration;
namespace RhSolutions.Services
{
public interface IAddInConfiguration
{
public string GetPriceListPath();
public string GetPriceListFileName();
public event SettingChangingEventHandler OnSettingsChange;
public void SetPriceListPath(string value);
public void SaveSettings();
}
}

View File

@ -1,74 +0,0 @@
using Microsoft.Win32;
using RhSolutions.Controllers;
using RhSolutions.Models;
using System;
using System.IO;
using System.Windows.Forms;
namespace RhSolutions.Services
{
static class RegistryUtil
{
private static string priceListPath;
private static RegistryKey RootKey { get; set; }
public static void Initialize()
{
RootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\REHAU\SkuAssist");
priceListPath = RootKey.GetValue("PriceListPath") as string;
}
public static void Uninitialize()
{
RootKey.Close();
}
public static string PriceListPath
{
get
{
if (string.IsNullOrEmpty(priceListPath) || !File.Exists(priceListPath))
{
DialogResult result = MessageBox.Show("Прайс-лист отсутствует или неверный файл шаблона прайс-листа. " +
"Укажите файл шаблона прайс-листа.",
"Нет файла шаблона",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (result == DialogResult.OK)
{
string fileName = Dialog.GetFilePath();
if (string.IsNullOrEmpty(fileName))
{
throw new Exception("Нет файла шаблона");
}
priceListPath = fileName;
RootKey.SetValue("PriceListPath", fileName);
return priceListPath;
}
else
throw new Exception("Нет файла шаблона");
}
else
{
return priceListPath;
}
}
set
{
priceListPath = value;
RootKey.SetValue("PriceListPath", value);
RibbonController.RefreshControl("setPriceList");
}
}
public static string GetPriceListName()
{
return Path.GetFileName(priceListPath);
}
}
}