Using Trados Studio FilesController
The TranslationStudioAutomation API enables you to access the Trados Studio files view and perform file operations.
Create a custom file list in a viewpart
This sample demonstrates how to create a custom file list using the FilesController and add it to the files view as a custom viewpart.
The custom list includes:
- Two columns: the file name and the total word count from the file
- Selection synchronization between the custom list and the files view
Step 1: Implement the user control
Create a Windows Forms user control to display the file list:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sdl.Desktop.IntegrationApi.Interfaces;
using Sdl.ProjectAutomation.Core;
using Sdl.TranslationStudioAutomation.IntegrationApi;
namespace FilesOperations.Sample
{
public partial class MyFilesViewPartControl : UserControl, IUIControl
{
public MyFilesViewPartControl()
{
InitializeComponent();
GetFilesController().SelectedFilesChanged += OnSelectedFilesChanged;
}
private FilesController GetFilesController()
{
return SdlTradosStudio.Application.GetController<FilesController>();
}
private void OnSelectedFilesChanged(object sender, EventArgs eventArgs)
{
RepopulateFilesList();
OpenFileButton.Enabled = false;
}
private void RepopulateFilesList()
{
FilesListView.Items.Clear();
FilesController filesController = GetFilesController();
foreach (ProjectFile file in filesController.SelectedFiles)
{
var item = new ListViewItem(file.Name)
{
Tag = file
};
item.SubItems.Add(file.AnalysisStatistics.Total.Words.ToString());
FilesListView.Items.Add(item);
}
}
private void FilesListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
OpenFileButton.Enabled = FilesListView.SelectedItems.Count > 0;
}
private void FilesListView_ItemActivate(object sender, EventArgs e)
{
if (FilesListView.SelectedItems.Count == 0)
{
return;
}
SdlTradosStudio.Application.GetController<EditorController>().Open((ProjectFile)FilesListView.SelectedItems[0].Tag, EditingMode.Translation);
}
private void OpenFileButton_Click(object sender, EventArgs e)
{
var selectedProjectFiles = new List<ProjectFile>();
for (int i = 0; i < FilesListView.SelectedItems.Count; i++)
{
selectedProjectFiles.Add((ProjectFile)FilesListView.SelectedItems[i].Tag);
}
if (selectedProjectFiles.Count == 0)
{
return;
}
try
{
SdlTradosStudio.Application.GetController<EditorController>()
.Open(selectedProjectFiles, EditingMode.Translation);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Cannot open the project file: \n{0}", ex.Message));
}
}
private void AddFileButton_Click(object sender, EventArgs e)
{
GetFilesController().AddFiles();
}
}
}
Step 2: Integrate the viewpart into the files view
Register the viewpart with the files view: