-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathGraphNodeManagerView.xaml.cs
More file actions
319 lines (274 loc) · 11.1 KB
/
Copy pathGraphNodeManagerView.xaml.cs
File metadata and controls
319 lines (274 loc) · 11.1 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
314
315
316
317
318
319
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Dynamo.GraphNodeManager.ViewModels;
namespace Dynamo.GraphNodeManager
{
/// <summary>
/// Interaction logic for GraphNodeManagerView.xaml
/// </summary>
public partial class GraphNodeManagerView : UserControl, IDisposable
{
/// <summary>
/// A persistent handle of the currently selected row
/// </summary>
private DataGridRow selectedRow;
/// <summary>
/// Allows to handle the selection of the DataGrid element
/// </summary>
private bool allowSelection = false;
/// <summary>
/// Helps to stop bubbling of the event when mouse click on a button inside a datagrid row
/// </summary>
private bool mouseHandled = false;
private GraphNodeManagerViewModel viewModel;
private bool disposedValue;
/// <summary>
/// Store the current sort direction for each sortable column:
/// </summary>
private ListSortDirection? nameSortDirection = null;
private ListSortDirection? typeSortDirection = null;
private ListSortDirection? stateSortDirection = null;
private ListSortDirection? issueSortDirection = null;
private ListSortDirection? outputSortDirection = null;
/// <summary>
/// Constructor
/// </summary>
/// <param name="viewModel"></param>
public GraphNodeManagerView(GraphNodeManagerViewModel viewModel)
{
InitializeComponent();
this.viewModel = viewModel;
this.DataContext = viewModel;
viewModel.PropertyChanged += ViewModel_OnPropertyChanged;
viewModel.RequestExportGraph += ViewModel_RequestExportGraph;
NodesInfoDataGrid.Sorting += NodesInfoDataGrid_Sorting;
}
private void ViewModel_RequestExportGraph(object parameter)
{
if (parameter == null) return;
var type = parameter.ToString();
var workspaceFileName = viewModel.CurrentWorkspace?.FileName;
var rawPromptName = !string.IsNullOrWhiteSpace(workspaceFileName)
? System.IO.Path.GetFileNameWithoutExtension(workspaceFileName)
: viewModel.CurrentWorkspace?.Name;
var promptName = SanitizePromptNameForFile(rawPromptName);
var filteredNodes = NodesInfoDataGrid.ItemsSource.Cast<GridNodeViewModel>().ToArray();
switch (type)
{
case "CSV":
Utilities.Utilities.ExportToCSV(filteredNodes, promptName);
break;
case "JSON":
Utilities.Utilities.ExportToJson(filteredNodes, promptName);
break;
}
}
private static string SanitizePromptNameForFile(string promptName)
{
const string fallbackName = "GraphNodes";
if (string.IsNullOrWhiteSpace(promptName))
{
return fallbackName;
}
var invalidChars = System.IO.Path.GetInvalidFileNameChars();
var sanitized = new string(promptName
.Select(ch => invalidChars.Contains(ch) ? '_' : ch)
.ToArray())
.Trim()
.TrimEnd('.');
if (string.IsNullOrWhiteSpace(sanitized))
{
return fallbackName;
}
var windowsReservedNames = new[]
{
"CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
};
if (windowsReservedNames.Contains(sanitized, StringComparer.OrdinalIgnoreCase))
{
return fallbackName;
}
return sanitized;
}
private void ViewModel_OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(GraphNodeManagerViewModel.IsAnyFilterOn))
{
CollectionViewSource.GetDefaultView(NodesInfoDataGrid.ItemsSource).Refresh();
}
}
/// <summary>
/// Handles DataGrid row click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Row_PreviewClickHandler(object sender, MouseButtonEventArgs e)
{
FocusNode(sender); // Always zoom around the node
if (mouseHandled)
{
mouseHandled = false;
return;
}
DataGridRow row = sender as DataGridRow;
if (row == null) return;
if (selectedRow != null && row != selectedRow)
{
ResetRows();
}
row.DetailsVisibility = row.DetailsVisibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
allowSelection = true;
if (row.DetailsVisibility == Visibility.Collapsed) selectedRow = null;
else selectedRow = row;
}
private void FocusNode(object sender)
{
var vm = this.DataContext as GraphNodeManagerViewModel;
if ((sender as DataGridRow) == null) return;
vm.NodeSelect((sender as DataGridRow).Item);
}
/// <summary>
/// Resets the selected row details visibility
/// </summary>
private void ResetRows()
{
if (selectedRow != null) selectedRow.DetailsVisibility = Visibility.Collapsed;
selectedRow = null;
}
/// <summary>
/// Handles Clipboard Button Click event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnClipboardButtonClick(object sender, RoutedEventArgs e)
{
mouseHandled = true;
var node = (GridNodeViewModel)((Button)sender).Tag;
var info = ((Button)sender).DataContext as NodeInfo;
var vm = this.DataContext as GraphNodeManagerViewModel;
var dynamoVersion = vm.DynamoVersion;
var hostProgram = vm.HostName; // How do we get the Host Program? Wouldn't that be the same as Dynamo?
// Package - package manager
if (node == null || info == null) return;
var message = $"Node Name: {node.Name}\nOriginal Node Name: {node.OriginalName}\nPackage: {node.Package}\nDynamo Version: {dynamoVersion}\nHost: {hostProgram}\nMessages: {info.Message}\nState: {info.State}";
Clipboard.SetText(message);
}
/// <summary>
/// Handles export button click function
/// </summary>
private void ExportButton_OnClick(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
ContextMenu contextMenu = button.ContextMenu;
contextMenu.PlacementTarget = button;
contextMenu.IsOpen = true;
e.Handled = true;
}
#region Sort
/// <summary>
/// Handles the sorting logic for the DataGrid.
/// </summary>
private void NodesInfoDataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{
e.Handled = true;
ResetSortDirections(e.Column.SortMemberPath);
if (e.Column.SortMemberPath == "Name")
{
ToggleSortDirection(ref nameSortDirection);
ApplySort(e.Column, nameSortDirection);
}
else if (e.Column.SortMemberPath == "TypeSortKey")
{
ToggleSortDirection(ref typeSortDirection);
ApplySort(e.Column, typeSortDirection);
}
else if (e.Column.SortMemberPath == "StateSortKey")
{
ToggleSortDirection(ref stateSortDirection);
ApplySort(e.Column, stateSortDirection);
}
else if (e.Column.SortMemberPath == "IssueSortKey")
{
ToggleSortDirection(ref issueSortDirection);
ApplySort(e.Column, issueSortDirection);
}
else if (e.Column.SortMemberPath == "OutputSortKey")
{
ToggleSortDirection(ref outputSortDirection);
ApplySort(e.Column, outputSortDirection);
}
}
/// <summary>
/// Toggles the sort direction between ascending, descending, and unsorted.
/// </summary>
private void ToggleSortDirection(ref ListSortDirection? currentSortDirection)
{
if (currentSortDirection == ListSortDirection.Ascending)
{
currentSortDirection = ListSortDirection.Descending;
}
else if (currentSortDirection == ListSortDirection.Descending)
{
// Reset to natural (unsorted) order
currentSortDirection = null;
}
else
{
currentSortDirection = ListSortDirection.Ascending;
}
}
/// <summary>
/// Resets all sort directions except for the currently clicked column.
/// </summary>
/// <param name="currentColumn"></param>
private void ResetSortDirections(string currentColumn)
{
if (currentColumn != "Name") nameSortDirection = null;
if (currentColumn != "TypeSortKey") typeSortDirection = null;
if (currentColumn != "StateSortKey") stateSortDirection = null;
if (currentColumn != "IssueSortKey") issueSortDirection = null;
if (currentColumn != "OutputSortKey") outputSortDirection = null;
}
/// <summary>
/// Applies the specified sort direction to the given DataGrid column.
/// </summary>
private void ApplySort(DataGridColumn column, ListSortDirection? direction)
{
ICollectionView view = CollectionViewSource.GetDefaultView(NodesInfoDataGrid.ItemsSource);
view.SortDescriptions.Clear();
if (direction.HasValue)
{
view.SortDescriptions.Add(new SortDescription(column.SortMemberPath, direction.Value));
column.SortDirection = direction;
}
// Natural order, no sort direction
else
{
column.SortDirection = null;
}
}
#endregion
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
viewModel.PropertyChanged -= ViewModel_OnPropertyChanged;
viewModel.RequestExportGraph -= ViewModel_RequestExportGraph;
disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}