Skip to content

Instantly share code, notes, and snippets.

@cromica
Created February 25, 2018 11:16
Show Gist options
  • Save cromica/4a9434a544c22f4bf91ab599a943042b to your computer and use it in GitHub Desktop.
Save cromica/4a9434a544c22f4bf91ab599a943042b to your computer and use it in GitHub Desktop.
This is a sample action that shows how to extract additional info for the content selection when working with Trados Studio Integarion API.
using Sdl.Desktop.IntegrationApi;
using Sdl.Desktop.IntegrationApi.Extensions;
using Sdl.TranslationStudioAutomation.IntegrationApi;
using System;
using System.Text;
using System.Windows.Forms;
namespace CurrentSelectionSample.RibbonAction
{
[Action(Id = "SelectionSample", Name = "Test Me", Icon = "CursorIcon")]
[ActionLayout(typeof(SelectionSample), 10, DisplayType.Large)]
public class Selection : AbstractAction
{
protected override void Execute()
{
//Obtain the editor controll together with the current active document. The editor might
//have multiple documents opened but only one is active.
var editorController = SdlTradosStudio.Application.GetController<EditorController>();
var activeDocument = editorController.ActiveDocument;
//pass the current selection to the methods that will extract all the new
//selection information
ShowSelectionInfo(activeDocument.Selection);
}
private void ShowSelectionInfo(DocumentSelection documentSelection)
{
//Selection can be done in both source and target and this sample
//is demonstrating that this is working in the same way for both
var details = new StringBuilder();
details.AppendLine("Source selection details");
var sourceSelection = documentSelection.Source;
details.AppendLine(ShowContentSelection(sourceSelection));
details.AppendLine("Target selection details");
var targetSelection = documentSelection.Target;
details.AppendLine(ShowContentSelection(targetSelection));
MessageBox.Show(details.ToString());
}
private string ShowContentSelection(AbstractContentSelection selection)
{
var details = new StringBuilder();
//Information like IsEmpty, IsReversed and IsValid are available general
//details available for the text selection
details.AppendLine(" Selection empty: " + (selection.IsEmpty ? "Yes" : "No"));
details.AppendLine(" Selection reversed: " + (selection.IsReversed ? "Yes" : "No"));
details.AppendLine(" Selection valid: " + (selection.IsValid ? "Yes" : "No"));
//Besides the above general information each content/text selection can have has
//a starting (From) and ending (UpTo) point which are marked with more details and this required
//because a selection can span on multiple segments. This is why From has a row number, cursor position,
//and segment id which can be different from the details in the UpTo
details.AppendLine(" From Details: ");
var fromDetails = selection.From;
details.Append(ShowSelectionInfoDetails(fromDetails));
details.AppendLine(" Upto Details: ");
var upToDetails = selection.UpTo;
details.Append(ShowSelectionInfoDetails(upToDetails));
return details.ToString();
}
private string ShowSelectionInfoDetails(ContentSelectionInfo fromDetails)
{
var details = new StringBuilder();
if (fromDetails != null)
{
details.AppendLine(" Row Number: " + fromDetails.RowNumber);
details.AppendLine(" Cursor Position: " + fromDetails.CursorPosition);
details.AppendLine(" Segment Id: " + fromDetails.SegmentId);
}
return details.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment