JD2022-TU1/main/tools/modules/JD.Rosetta.Module/RosettaModule.cs

89 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;
using JD.Localization.Core.Model;
using JD.LocalizationExport.Service.API;
using JD.LocalizationExport.Service.Model.Options;
using JD.Rosetta.Module.View;
using JD.Rosetta.Module.ViewModel;
using Plume.Core.Logging;
using Plume.Icons;
using Plume.Interfaces.Modules;
using Plume.Interfaces.WPF.Modules;
using Plume.Interfaces.WPF.Window;
using Plume.Perforce.Service.Api;
namespace JD.Rosetta.Module
{
public class RosettaModule : IWpfModule
{
private RosettaViewModel _rosettaViewModel;
private string _perforceData;
private string _perforceCode;
public event Func<IDictionary<ExportType, ExportOptionsModel>, IEnumerable<UafLanguage>, Task> LocalizationExportLaunched;
public void Dispose()
{
_rosettaViewModel.LocalizationExportLaunched -= OnLocalizationExportLaunched;
}
public void Setup(IModuleSetup setup)
{
if (!(setup is RosettaModuleSetup rosettaSetup))
{
throw new ArgumentException($"Expecting an {nameof(RosettaModuleSetup)} not a {setup.GetType()}", nameof(setup));
}
_perforceData = rosettaSetup.PerforceDataHost;
if (string.IsNullOrEmpty(_perforceData))
{
throw new ArgumentNullException("You must specify a valid Data Perforce server!");
}
_perforceCode = rosettaSetup.PerforceCodeHost;
if (string.IsNullOrEmpty(_perforceCode))
{
throw new ArgumentNullException("You must specify a valid Code Perforce server!");
}
}
public void Initialize(IWpfModuleContext context)
{
IWindowManager windowService = context.WindowManager ??
throw new ArgumentNullException(nameof(context.WindowManager));
ILocalizationExportService exportService = context.GetService<ILocalizationExportService>() ??
throw new ArgumentNullException(nameof(ILocalizationExportService));
IPerforceService perforceService = context.GetService<IPerforceService>() ??
throw new ArgumentNullException(nameof(IPerforceService));
ILogger logger = context.Logger ??
throw new ArgumentNullException(nameof(context.Logger));
_rosettaViewModel = new RosettaViewModel(exportService, perforceService, _perforceData, _perforceCode, logger);
RosettaToolWindow rosettaToolWindow = new RosettaToolWindow
{
DataContext = _rosettaViewModel
};
windowService.AddToolWindow("ExporterWindow",
"Exporter",
StandardIcon.TransferDownload,
rosettaToolWindow,
DockingSide.Left,
new KeyGesture(Key.E, ModifierKeys.Control));
_rosettaViewModel.LocalizationExportLaunched += OnLocalizationExportLaunched;
}
private async Task OnLocalizationExportLaunched(IDictionary<ExportType, ExportOptionsModel> exportTypesAndOptionsToExecute, IEnumerable<UafLanguage> languagesToExport)
{
if (LocalizationExportLaunched != null)
{
await LocalizationExportLaunched.Invoke(exportTypesAndOptionsToExecute, languagesToExport);
}
}
}
}