/
Extensions.cs
76 lines (66 loc) · 3.44 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MoreLinq;
using Sdl.DesktopEditor.EditorApi;
using Sdl.ProjectAutomation.Core;
using Sdl.TranslationStudioAutomation.IntegrationApi;
namespace Sdl.Community.YourProductivity.Util
{
public static class Extensions
{
public static ProjectFile TryGetActiveFile(this Document document)
{
if (document.ActiveFile != null) return document.ActiveFile;
//at the time of implementation using ActiveFile property is not working always for the virtual merged files - for some reasons in the editor
//there more file nodes for one file. This is a workaround to try and resolve it.
var segmentPair = document.ActiveSegmentPair;
if (segmentPair == null) return null;
var type = document.GetType();
var method = type.GetMethod("GetTargetSegmentContainerNodeById",
BindingFlags.NonPublic | BindingFlags.Instance);
var result = method.Invoke(document, new object[] {segmentPair.Target.Properties.Id.Id});
var targetSegmentContainer = result as ISegmentContainerNode;
if (targetSegmentContainer == null) return null;
IAbstractContainerNode node = targetSegmentContainer;
while (node != null && !(node is IFileContainerNode))
{
node = node.Parent;
}
var fileContainerNode = node as IFileContainerNode;
if (fileContainerNode == null) return null;
var assembly = Assembly.LoadFrom("Sdl.TranslationStudio.Common.dll");
var filterFramweworkUtilitiesType =
assembly.GetType("Sdl.TranslationStudio.Common.Editing.FilterFrameworkUtilities");
var internalDocument =
type.GetProperty("InternalDocument", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(document, null);
var nativeBillingualDocument =
internalDocument.GetType()
.GetProperty("NativeBilingualDocument")
.GetValue(internalDocument, null);
var target = nativeBillingualDocument.GetType()
.GetProperty("Target")
.GetValue(nativeBillingualDocument, null);
var rootContainer = target.GetType()
.GetProperty("RootContainer")
.GetValue(target, null);
var getFileContainerNodesMethod = filterFramweworkUtilitiesType.GetMethod("GetFileContainerNodes",
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
var fileContainers = getFileContainerNodesMethod.Invoke(null, new[] {rootContainer, null});
var fileContainerNodes = fileContainers as List<IFileContainerNode>;
if (fileContainerNodes == null) return null;
var distinctFileContainerNodes = fileContainerNodes.DistinctBy(x => x.FileProperties.FileConversionProperties.FileId).ToList();
var index =
distinctFileContainerNodes.FindIndex(
f =>
f.FileProperties.FileConversionProperties.FileId.Equals(
fileContainerNode.FileProperties.FileConversionProperties.FileId));
if (0 <= index && index < document.Files.Count())
{
return document.Files.ElementAt(index);
}
return null;
}
}
}