-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.LiveSignalSearch.cs
More file actions
313 lines (274 loc) · 11.9 KB
/
Copy pathMainWindow.LiveSignalSearch.cs
File metadata and controls
313 lines (274 loc) · 11.9 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ArIED61850Tester.Models;
namespace ArIED61850Tester;
/// <summary>
/// Lightweight, presentation-only search for the Explorer live-value workspace.
/// Filtering is performed over the existing ICollectionView; it never changes the
/// monitored point collection, acquisition lifecycle, or IEC 61850 network traffic.
/// </summary>
public partial class MainWindow
{
private static readonly bool LiveSignalSearchClassHandlerRegistered = RegisterLiveSignalSearchClassHandler();
private DataGrid? _liveSignalSearchGrid;
private TextBox? _liveSignalSearchBox;
private TextBlock? _liveSignalSearchCount;
private ICollectionView? _liveSignalSearchView;
private INotifyCollectionChanged? _liveSignalSearchCollection;
private DependencyPropertyDescriptor? _liveSignalItemsSourceDescriptor;
private bool _liveSignalSearchInstalled;
private static bool RegisterLiveSignalSearchClassHandler()
{
EventManager.RegisterClassHandler(
typeof(MainWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(LiveSignalSearch_MainWindowLoaded));
return true;
}
private static void LiveSignalSearch_MainWindowLoaded(object sender, RoutedEventArgs e)
{
if (sender is not MainWindow window || !ReferenceEquals(e.OriginalSource, window))
return;
window.Dispatcher.BeginInvoke(new Action(window.InstallLiveSignalSearch));
}
private void InstallLiveSignalSearch()
{
if (_liveSignalSearchInstalled)
return;
var dataGrid = FindLiveSignalVisualChildren<DataGrid>(this)
.FirstOrDefault(IsExplorerLiveSignalGrid);
if (dataGrid?.Parent is not Grid host)
return;
_liveSignalSearchInstalled = true;
_liveSignalSearchGrid = dataGrid;
// The existing host contains the DataGrid plus its empty-workspace overlay.
// Give both row 1 and reserve a compact row 0 for the search toolbar.
if (host.RowDefinitions.Count == 0)
{
host.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
host.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
foreach (UIElement child in host.Children.Cast<UIElement>().ToArray())
Grid.SetRow(child, 1);
}
var toolbar = BuildLiveSignalSearchToolbar();
Grid.SetRow(toolbar, 0);
host.Children.Add(toolbar);
_liveSignalItemsSourceDescriptor = DependencyPropertyDescriptor.FromProperty(
ItemsControl.ItemsSourceProperty,
typeof(DataGrid));
_liveSignalItemsSourceDescriptor?.AddValueChanged(dataGrid, LiveSignalSearch_ItemsSourceChanged);
PreviewKeyDown += LiveSignalSearch_WindowPreviewKeyDown;
Closed += LiveSignalSearch_WindowClosed;
AttachLiveSignalSearchSource();
}
private FrameworkElement BuildLiveSignalSearchToolbar()
{
var toolbar = new Grid
{
Margin = new Thickness(2, 0, 2, 9),
Height = 34
};
toolbar.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
toolbar.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
var titlePanel = new StackPanel
{
Orientation = Orientation.Horizontal,
VerticalAlignment = VerticalAlignment.Center
};
titlePanel.Children.Add(new Border
{
Width = 7,
Height = 7,
CornerRadius = new CornerRadius(3.5),
Background = new SolidColorBrush(Color.FromRgb(37, 99, 235)),
Margin = new Thickness(2, 0, 8, 0)
});
titlePanel.Children.Add(new TextBlock
{
Text = "LIVE SIGNAL VALUES",
FontSize = 11.2,
FontWeight = FontWeights.SemiBold,
Foreground = new SolidColorBrush(Color.FromRgb(70, 86, 109)),
VerticalAlignment = VerticalAlignment.Center
});
_liveSignalSearchCount = new TextBlock
{
Text = string.Empty,
FontSize = 10.8,
Foreground = new SolidColorBrush(Color.FromRgb(126, 142, 165)),
Margin = new Thickness(9, 0, 0, 0),
VerticalAlignment = VerticalAlignment.Center
};
titlePanel.Children.Add(_liveSignalSearchCount);
toolbar.Children.Add(titlePanel);
_liveSignalSearchBox = new TextBox
{
Width = 390,
Style = ResolveIndustrialSearchStyle(),
Tag = "Search signal, IEC reference, value, quality or acquisition",
ToolTip = "Filter the current live workspace. Ctrl+F focuses search; Esc clears it.",
VerticalAlignment = VerticalAlignment.Center
};
_liveSignalSearchBox.TextChanged += LiveSignalSearch_TextChanged;
_liveSignalSearchBox.PreviewKeyDown += LiveSignalSearch_BoxPreviewKeyDown;
Grid.SetColumn(_liveSignalSearchBox, 1);
toolbar.Children.Add(_liveSignalSearchBox);
return toolbar;
}
private Style? ResolveIndustrialSearchStyle()
{
var style = TryFindResource("IndustrialSearchTextBox") as Style
?? Application.Current?.TryFindResource("IndustrialSearchTextBox") as Style;
if (style != null)
return style;
// Defensive fallback for load ordering: use the same resource dictionary that
// P2IndustrialWorkstationUx uses for the IED Explorer search field.
var dictionary = new ResourceDictionary
{
Source = new Uri("/ARSAS;component/Resources/P2IndustrialControls.xaml", UriKind.Relative)
};
return dictionary["IndustrialSearchTextBox"] as Style;
}
private static bool IsExplorerLiveSignalGrid(DataGrid grid)
{
var headers = grid.Columns
.Select(column => column.Header?.ToString() ?? string.Empty)
.ToArray();
// This six-column signature is unique to the selected-IED live-value workspace.
// The global monitor has an additional IED column and the command grid has a
// different schema, so no visual-tree TabItem assumptions are required.
return headers.Length == 6 &&
headers[0].Equals("Signal", StringComparison.OrdinalIgnoreCase) &&
headers[1].Equals("IEC Telegram", StringComparison.OrdinalIgnoreCase) &&
headers[2].Equals("Value", StringComparison.OrdinalIgnoreCase) &&
headers[3].Equals("Quality", StringComparison.OrdinalIgnoreCase) &&
headers[4].Equals("IED Timestamp", StringComparison.OrdinalIgnoreCase) &&
headers[5].Equals("Acquisition", StringComparison.OrdinalIgnoreCase);
}
private void LiveSignalSearch_ItemsSourceChanged(object? sender, EventArgs e)
=> AttachLiveSignalSearchSource();
private void AttachLiveSignalSearchSource()
{
if (_liveSignalSearchCollection != null)
_liveSignalSearchCollection.CollectionChanged -= LiveSignalSearch_CollectionChanged;
if (_liveSignalSearchView != null)
_liveSignalSearchView.Filter = null;
var source = _liveSignalSearchGrid?.ItemsSource;
_liveSignalSearchCollection = source as INotifyCollectionChanged;
if (_liveSignalSearchCollection != null)
_liveSignalSearchCollection.CollectionChanged += LiveSignalSearch_CollectionChanged;
_liveSignalSearchView = source == null
? null
: CollectionViewSource.GetDefaultView(source);
ApplyLiveSignalSearch();
}
private void LiveSignalSearch_TextChanged(object sender, TextChangedEventArgs e)
=> ApplyLiveSignalSearch();
private void LiveSignalSearch_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
=> Dispatcher.BeginInvoke(new Action(UpdateLiveSignalSearchCount));
private void ApplyLiveSignalSearch()
{
if (_liveSignalSearchView == null)
{
UpdateLiveSignalSearchCount();
return;
}
_liveSignalSearchView.Filter = LiveSignalSearch_Matches;
_liveSignalSearchView.Refresh();
UpdateLiveSignalSearchCount();
}
private bool LiveSignalSearch_Matches(object item)
{
var query = (_liveSignalSearchBox?.Text ?? string.Empty).Trim();
if (query.Length == 0)
return true;
if (item is not Iec61850MonitorPoint point)
return true;
var searchable = string.Join('\n', new[]
{
point.DeviceName,
point.SignalName,
point.IecTelegram,
point.IecReference,
point.Value,
point.Quality,
point.DeviceTimestamp,
point.SourceMode
}.Where(value => !string.IsNullOrWhiteSpace(value)));
// Multiple terms use AND semantics: "MMXU instCVal" quickly narrows large workspaces.
var tokens = query.Split(
new[] { ' ', '\t', '\r', '\n' },
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return tokens.All(token => searchable.Contains(token, StringComparison.OrdinalIgnoreCase));
}
private void UpdateLiveSignalSearchCount()
{
if (_liveSignalSearchCount == null)
return;
var source = _liveSignalSearchGrid?.ItemsSource;
var total = source switch
{
ICollection collection => collection.Count,
IEnumerable<object> enumerable => enumerable.Count(),
_ => 0
};
var visible = _liveSignalSearchView?.Cast<object>().Count() ?? total;
var filtered = !string.IsNullOrWhiteSpace(_liveSignalSearchBox?.Text);
_liveSignalSearchCount.Text = filtered
? $"{visible:N0} of {total:N0} shown"
: $"{total:N0} signals";
}
private void LiveSignalSearch_WindowPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F && Keyboard.Modifiers.HasFlag(ModifierKeys.Control) &&
MainTabs?.SelectedIndex == 0 && _liveSignalSearchBox != null)
{
_liveSignalSearchBox.Focus();
_liveSignalSearchBox.SelectAll();
e.Handled = true;
}
}
private void LiveSignalSearch_BoxPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Escape)
return;
ClearLiveSignalSearch();
e.Handled = true;
}
private void ClearLiveSignalSearch()
{
if (_liveSignalSearchBox == null)
return;
_liveSignalSearchBox.Clear();
_liveSignalSearchBox.Focus();
}
private void LiveSignalSearch_WindowClosed(object? sender, EventArgs e)
{
PreviewKeyDown -= LiveSignalSearch_WindowPreviewKeyDown;
Closed -= LiveSignalSearch_WindowClosed;
if (_liveSignalSearchGrid != null)
_liveSignalItemsSourceDescriptor?.RemoveValueChanged(_liveSignalSearchGrid, LiveSignalSearch_ItemsSourceChanged);
if (_liveSignalSearchCollection != null)
_liveSignalSearchCollection.CollectionChanged -= LiveSignalSearch_CollectionChanged;
if (_liveSignalSearchView != null)
_liveSignalSearchView.Filter = null;
}
private static IEnumerable<T> FindLiveSignalVisualChildren<T>(DependencyObject root)
where T : DependencyObject
{
for (var index = 0; index < VisualTreeHelper.GetChildrenCount(root); index++)
{
var child = VisualTreeHelper.GetChild(root, index);
if (child is T typed)
yield return typed;
foreach (var descendant in FindLiveSignalVisualChildren<T>(child))
yield return descendant;
}
}
}