-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIoListTestingWindow.HeaderSelection.cs
More file actions
208 lines (175 loc) · 6.89 KB
/
Copy pathIoListTestingWindow.HeaderSelection.cs
File metadata and controls
208 lines (175 loc) · 6.89 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System.ComponentModel;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using ArIED61850Tester.Models.IoTesting;
namespace ArIED61850Tester;
/// <summary>
/// Adds a compact tri-state header checkbox to the FAT TEST column without changing
/// the grid item source or the per-row TestEnabled contract. The header uses the same
/// editability gate as the row checkboxes and persists one bulk plan change.
/// </summary>
public partial class IoListTestingWindow
{
private static readonly bool IoTestHeaderSelectionClassHandlerRegistered =
RegisterIoTestHeaderSelectionClassHandler();
private bool _ioTestHeaderSelectionInstallScheduled;
private bool _ioTestHeaderSelectionInstalled;
private CheckBox? _ioTestHeaderSelectionCheckBox;
private IoTestIedPlan? _ioTestHeaderObservedIed;
private static bool RegisterIoTestHeaderSelectionClassHandler()
{
EventManager.RegisterClassHandler(
typeof(IoListTestingWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnIoTestHeaderSelectionWindowLoaded),
handledEventsToo: true);
return true;
}
private static void OnIoTestHeaderSelectionWindowLoaded(object sender, RoutedEventArgs e)
{
if (sender is not IoListTestingWindow window ||
window._ioTestHeaderSelectionInstalled ||
window._ioTestHeaderSelectionInstallScheduled)
{
return;
}
window._ioTestHeaderSelectionInstallScheduled = true;
window.Dispatcher.BeginInvoke(
DispatcherPriority.ContextIdle,
new Action(() =>
{
window._ioTestHeaderSelectionInstallScheduled = false;
window.EnsureIoTestHeaderSelection();
}));
}
private void EnsureIoTestHeaderSelection()
{
if (_ioTestHeaderSelectionInstalled)
{
RefreshIoTestHeaderSelection();
return;
}
var grid = FindIoTestSelectionGrid(this);
if (grid == null)
return;
var column = grid.Columns.FirstOrDefault(candidate =>
string.Equals(candidate.Header?.ToString(), "TEST", StringComparison.OrdinalIgnoreCase));
if (column == null)
return;
var headerCheckBox = new CheckBox
{
Width = 16,
Height = 16,
IsThreeState = true,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Cursor = Cursors.Hand,
Focusable = true,
ToolTip = "Check / uncheck all TEST rows for the selected IED"
};
AutomationProperties.SetName(headerCheckBox, "Toggle all FAT test rows");
headerCheckBox.Click += IoTestHeaderSelectionCheckBox_Click;
column.Header = headerCheckBox;
_ioTestHeaderSelectionCheckBox = headerCheckBox;
_ioTestHeaderSelectionInstalled = true;
PropertyChanged += IoTestHeaderSelectionWindow_PropertyChanged;
Closed += IoTestHeaderSelectionWindow_Closed;
RewireIoTestHeaderObservedIed();
RefreshIoTestHeaderSelection();
}
private void IoTestHeaderSelectionCheckBox_Click(object sender, RoutedEventArgs e)
{
var ied = SelectedIed;
if (ied == null || !CanEditPlan)
{
RefreshIoTestHeaderSelection();
return;
}
var target = GetIoTestHeaderSelectionState(ied) != true;
foreach (var point in ied.TestPoints)
point.TestEnabled = target;
Storage?.ScheduleSave();
Raise(nameof(SelectedIedSummary));
RaiseSelectedIedContextProperties();
RefreshIoTestHeaderSelection();
}
private void IoTestHeaderSelectionWindow_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SelectedIed))
RewireIoTestHeaderObservedIed();
RefreshIoTestHeaderSelection();
}
private void IoTestHeaderObservedIed_PropertyChanged(object? sender, PropertyChangedEventArgs e)
=> RefreshIoTestHeaderSelection();
private void RewireIoTestHeaderObservedIed()
{
if (ReferenceEquals(_ioTestHeaderObservedIed, SelectedIed))
return;
if (_ioTestHeaderObservedIed != null)
_ioTestHeaderObservedIed.PropertyChanged -= IoTestHeaderObservedIed_PropertyChanged;
_ioTestHeaderObservedIed = SelectedIed;
if (_ioTestHeaderObservedIed != null)
_ioTestHeaderObservedIed.PropertyChanged += IoTestHeaderObservedIed_PropertyChanged;
}
private void RefreshIoTestHeaderSelection()
{
var header = _ioTestHeaderSelectionCheckBox;
if (header == null)
return;
var ied = SelectedIed;
header.IsEnabled = CanEditPlan && ied?.TestPoints.Count > 0;
header.IsChecked = ied == null ? false : GetIoTestHeaderSelectionState(ied);
}
private static bool? GetIoTestHeaderSelectionState(IoTestIedPlan ied)
{
if (ied.TestPoints.Count == 0)
return false;
var enabled = ied.TestPoints.Count(point => point.TestEnabled);
if (enabled == 0)
return false;
if (enabled == ied.TestPoints.Count)
return true;
return null;
}
private void IoTestHeaderSelectionWindow_Closed(object? sender, EventArgs e)
{
PropertyChanged -= IoTestHeaderSelectionWindow_PropertyChanged;
Closed -= IoTestHeaderSelectionWindow_Closed;
if (_ioTestHeaderObservedIed != null)
_ioTestHeaderObservedIed.PropertyChanged -= IoTestHeaderObservedIed_PropertyChanged;
_ioTestHeaderObservedIed = null;
if (_ioTestHeaderSelectionCheckBox != null)
_ioTestHeaderSelectionCheckBox.Click -= IoTestHeaderSelectionCheckBox_Click;
}
private static DataGrid? FindIoTestSelectionGrid(DependencyObject root)
{
foreach (var grid in FindIoTestHeaderVisualChildren<DataGrid>(root))
{
if (grid.Columns.Any(column =>
string.Equals(column.Header?.ToString(), "TEST", StringComparison.OrdinalIgnoreCase)))
{
return grid;
}
}
return null;
}
private static IEnumerable<T> FindIoTestHeaderVisualChildren<T>(DependencyObject root)
where T : DependencyObject
{
if (root == null)
yield break;
var count = VisualTreeHelper.GetChildrenCount(root);
for (var index = 0; index < count; index++)
{
var child = VisualTreeHelper.GetChild(root, index);
if (child is T match)
yield return match;
foreach (var descendant in FindIoTestHeaderVisualChildren<T>(child))
yield return descendant;
}
}
}