-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaultRecordWindow.Diagnostics.cs
More file actions
151 lines (132 loc) · 4.84 KB
/
Copy pathFaultRecordWindow.Diagnostics.cs
File metadata and controls
151 lines (132 loc) · 4.84 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
namespace ArIED61850Tester;
public partial class FaultRecordWindow
{
private bool _diagnosticHooksInstalled;
private string _selectedTransferDiagnostic =
"No transfer failure has been recorded. Run a download, then select a failed row.";
static FaultRecordWindow()
{
EventManager.RegisterClassHandler(
typeof(DataGridRow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnFaultRecordRowLoaded),
handledEventsToo: true);
}
public string SelectedTransferDiagnostic
{
get => _selectedTransferDiagnostic;
private set => Set(ref _selectedTransferDiagnostic, value ?? string.Empty);
}
private static void OnFaultRecordRowLoaded(object sender, RoutedEventArgs e)
{
if (sender is not DataGridRow row ||
Window.GetWindow(row) is not FaultRecordWindow)
{
return;
}
BindingOperations.SetBinding(
row,
FrameworkElement.ToolTipProperty,
new Binding(nameof(FaultRecordRow.Detail))
{
Mode = BindingMode.OneWay,
FallbackValue = string.Empty,
TargetNullValue = string.Empty
});
ToolTipService.SetShowDuration(row, 30_000);
}
private void Diagnostics_ContentRendered(object? sender, EventArgs e)
{
if (_diagnosticHooksInstalled)
return;
_diagnosticHooksInstalled = true;
Records.CollectionChanged += DiagnosticRecords_CollectionChanged;
foreach (var row in Records)
AttachDiagnosticRow(row);
}
private void DiagnosticRecords_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var item in e.NewItems.OfType<FaultRecordRow>())
AttachDiagnosticRow(item);
}
if (e.OldItems != null)
{
foreach (var item in e.OldItems.OfType<FaultRecordRow>())
item.PropertyChanged -= DiagnosticRow_PropertyChanged;
}
}
private void AttachDiagnosticRow(FaultRecordRow row)
{
row.PropertyChanged -= DiagnosticRow_PropertyChanged;
row.PropertyChanged += DiagnosticRow_PropertyChanged;
}
private void DiagnosticRow_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not FaultRecordRow row ||
e.PropertyName != nameof(FaultRecordRow.Detail) ||
string.IsNullOrWhiteSpace(row.Detail) ||
!row.Status.Equals("Failed", StringComparison.OrdinalIgnoreCase))
{
return;
}
ShowTransferDiagnostic(row, expand: true);
}
private void FaultRecordsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (FaultRecordsGrid.SelectedItem is FaultRecordRow row &&
!string.IsNullOrWhiteSpace(row.Detail))
{
ShowTransferDiagnostic(
row,
expand: row.Status.Equals("Failed", StringComparison.OrdinalIgnoreCase));
}
}
private void ShowTransferDiagnostic(FaultRecordRow row, bool expand)
{
void Apply()
{
SelectedTransferDiagnostic = BuildVisibleDiagnostic(row);
if (!ReferenceEquals(FaultRecordsGrid.SelectedItem, row))
FaultRecordsGrid.SelectedItem = row;
FaultRecordsGrid.ScrollIntoView(row);
if (expand)
TransferDiagnosticsExpander.IsExpanded = true;
}
if (Dispatcher.CheckAccess())
Apply();
else
Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new Action(Apply));
}
private string BuildVisibleDiagnostic(FaultRecordRow row)
=> $"Device : {DeviceName}\r\n" +
$"Endpoint : {EndpointText}\r\n" +
$"Record : {row.RecordName}\r\n" +
$"Remote directory : {row.RemoteDirectory}\r\n" +
$"Files : {row.FilesText}\r\n" +
$"Status : {row.Status}\r\n" +
$"Captured local : {DateTimeOffset.Now:O}\r\n" +
new string('=', 76) + "\r\n" +
row.Detail.Trim();
private void CopyDiagnostic_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(SelectedTransferDiagnostic))
return;
try
{
Clipboard.SetText(SelectedTransferDiagnostic);
StatusText = "Transfer diagnostic copied to the clipboard.";
}
catch (Exception ex)
{
StatusText = $"Could not copy the transfer diagnostic: {ex.Message}";
}
}
}