Created
February 25, 2018 11:56
-
-
Save cromica/6bc54468bfe6b15f493164c3783a485a to your computer and use it in GitHub Desktop.
This is a sample on how to consume project creation, open and publish events in Trados Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using NotificationsSample.EventAggregator; | |
using Newtonsoft.Json; | |
using Sdl.Desktop.IntegrationApi; | |
using Sdl.Desktop.IntegrationApi.Extensions; | |
using Sdl.Desktop.IntegrationApi.Interfaces; | |
using Sdl.ProjectAutomation.FileBased.Events; | |
using Sdl.TranslationStudioAutomation.IntegrationApi; | |
namespace NotificationsSample.EventAggregator | |
{ | |
[ViewPart( | |
Id = "MyProjectNotificationsViewPart", | |
Name = "Project Notifications", | |
Description = "Project Notifications")] | |
[ViewPartLayout(Dock = DockType.Bottom, LocationByType = typeof(ProjectsController))] | |
class NotificationSampleViewPart : AbstractViewPartController | |
{ | |
protected override Control GetContentControl() | |
{ | |
return _control.Value; | |
} | |
protected override void Initialize() | |
{ | |
//Obtain the event aggregator instance from Trados Studio which will facilitate | |
//event registration | |
IStudioEventAggregator ea = SdlTradosStudio.Application.GetService<IStudioEventAggregator>(); | |
//Event registration is just one line of code and it requires 2 pieces of information | |
//the event to register which is specified in the GetEvent method and | |
//the susbcribe method which expects a method that will be called | |
//when the specified event is triggered | |
ea.GetEvent<ProjectCreatedEvent>().Subscribe(ShowInfo); | |
ea.GetEvent<ProjectOpenedEvent>().Subscribe(ShowInfo); | |
ea.GetEvent<ProjectPublishedEvent>().Subscribe(ShowInfo); | |
} | |
private void ShowInfo(object eventData) | |
{ | |
string json = JsonConvert.SerializeObject(eventData, Formatting.Indented); | |
StringBuilder info = new StringBuilder(); | |
info.Append(Environment.NewLine); | |
//Cast the method argument to ProjectEvent type which is the base class | |
//for project events. This exposed the ProjectInfor property which will provide | |
//details about the project for which the event was generated | |
ProjectEvent projectEvent = eventData as ProjectEvent; | |
if(projectEvent == null) return; | |
if (projectEvent is ProjectCreatedEvent) | |
{ | |
info.AppendLine("Project " + project.ProjectInfo.Name + " created"); | |
} | |
if (projectEvent is ProjectOpenedEvent) | |
{ | |
info.AppendLine("Project " + project.ProjectInfo.Name + " opened"); | |
} | |
if (projectEvent is ProjectPublishedEvent) | |
{ | |
info.AppendLine("Project " + project.ProjectInfo.Name + " published"); | |
} | |
_control.Value.UpdateText2(info); | |
} | |
private static readonly Lazy<NotificationSampleViewPartControl> _control = new Lazy<NotificationSampleViewPartControl>(() => new NotificationSampleViewPartControl()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment