This repository has been archived by the owner on Feb 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
385 lines (326 loc) · 11.2 KB
/
MainWindow.xaml.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
namespace Com.Xenthrax.WindowsDataVisualizer
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.Exceptions = new System.Collections.ObjectModel.ObservableCollection<Exception>();
Utilities.Utilities.ExceptionHandler += this.Utilities_ExceptionHandler;
InitializeComponent();
}
#region Members
private readonly static DependencyProperty ExceptionsProperty = DependencyProperty.Register("Exceptions", typeof(ICollection<Exception>), typeof(MainWindow));
private readonly static DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(Data.Data), typeof(MainWindow));
#endregion
#region Properties
private ICollection<Exception> Exceptions
{
get
{
return (ICollection<Exception>)this.GetValue(MainWindow.ExceptionsProperty);
}
set
{
this.SetValue(MainWindow.ExceptionsProperty, value);
}
}
private Data.Data Data
{
get
{
return (Data.Data)this.GetValue(MainWindow.DataProperty);
}
set
{
this.SetValue(MainWindow.DataProperty, value);
}
}
#endregion
#region Methods
private void Utilities_ExceptionHandler(object sender, Utilities.ExceptionEventArgs e)
{
this.Dispatcher.Invoke(new Action<Exception>((ex) => this.Exceptions.Add(ex)), e.Exception);
e.Handled = true;
}
private void Window_Closed(object sender, EventArgs e)
{
Utilities.Utilities.CleanUpLog();
}
private void New_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
this.Data = new Data.Data().Initiate();
this.DataTree_SelectedItemChanged(this, new RoutedPropertyChangedEventArgs<object>(this.DataTree.SelectedItem, this.DataTree.SelectedItem));
}
catch (Exception ex)
{
Utilities.Utilities.Log(ex);
}
}
private void Open_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
InputDialog Input = new InputDialog();
if (Input.ShowDialog(this) != true)
return;
using (FileStream FS = new FileStream(Input.InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
this.Data = Serializer.DataSerializer.Deserialize(FS, Input.Certificate);
this.DataTree_SelectedItemChanged(this, new RoutedPropertyChangedEventArgs<object>(this.DataTree.SelectedItem, this.DataTree.SelectedItem));
}
}
catch (Exception ex)
{
Utilities.Utilities.Log(ex);
}
}
private void Save_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
SaveDialog Save = new SaveDialog();
if (Save.ShowDialog(this) != true)
return;
using (FileStream FS = new FileStream(Save.OutputFile, FileMode.Create, FileAccess.Write, FileShare.Read))
Serializer.DataSerializer.Serialize(this.Data, FS, Save.EncryptionType, Save.Certificate, Save.KeySize, Save.BlockSize, Save.FileFormat);
}
catch (Exception ex)
{
Utilities.Utilities.Log(ex);
}
}
private void Close_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
this.Close();
}
private void DataTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue == null)
return;
this.DataTextBox.Document = this.NewFlowDocument(new Run("Loading\u2026")
{
FontStyle = FontStyles.Italic
});
var worker = new System.ComponentModel.BackgroundWorker();
worker.DoWork += (paramater, workerargs) =>
{
var NewValue = e.NewValue;
if (NewValue is XAML.ComplexBindingGroup)
NewValue = XAML.ComplexBindingGroupConverter.Instance.ConvertBack(NewValue, null, null, null);
// Exception
var exception = NewValue as Exception;
if (exception != null)
{
StringBuilder ErrorString = new StringBuilder();
while (exception != null)
{
ErrorString.AppendFormat("{0}\r\n\r\n", exception);
exception = exception.InnerException;
}
this.Dispatcher.Invoke(new Action(() =>
{
this.DataTextBox.Document = this.NewFlowDocument(new Run(ErrorString.ToString().TrimEnd('\r', '\n')));
}));
return;
}
// IEnumerable<>
var item = NewValue as FrameworkElement;
IEnumerable<dynamic> enumerable = null;
if (item != null)
enumerable = this.Dispatcher.Invoke(new Func<object>(() => item.DataContext)) as IEnumerable<dynamic>;
else
enumerable = NewValue as IEnumerable<dynamic>;
if (enumerable != null)
{
this.Dispatcher.Invoke(new Action(() =>
{
this.DataTextBox.Document = this.NewFlowDocument(new Run(this.IEnumerableToString(enumerable, 1000)));
}));
return;
}
// Data.IData
Data.IData data = null;
if (item != null)
data = this.Dispatcher.Invoke(new Func<object>(() => item.DataContext)) as Data.IData;
else
data = NewValue as Data.IData;
if (data != null)
{
var Properties = new List<Inline>();
Uri navigateTo = null;
foreach (var prop in data.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public))
{
try
{
if (prop.GetCustomAttributes(typeof(Utilities.NavigateToAttribute), false).Length != 0)
navigateTo = prop.GetValue(data, null) as Uri;
}
catch (Exception ex)
{
Utilities.Utilities.Log(ex);
}
string name = null;
object value;
try
{
var dispAttr = (Utilities.DisplayConfigurationAttribute)prop.GetCustomAttributes(typeof(Utilities.DisplayConfigurationAttribute), false).LastOrDefault();
bool valueIsInline = false;
if (dispAttr == null)
{
name = Utilities.Utilities.FromCamelCase(prop.Name);
value = prop.GetValue(data, null);
}
else
{
if (dispAttr.Ignore)
continue;
value = prop.GetValue(data, null);
if (!dispAttr.EmitDefaultValue && value == prop.PropertyType.Default())
continue;
name = dispAttr.Name ?? (dispAttr.IgnoreCamelCase ? prop.Name : Utilities.Utilities.FromCamelCase(prop.Name));
if (dispAttr.Converter != null)
{
var converter = prop.DeclaringType.GetMethod(dispAttr.Converter, BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[1] { prop.PropertyType }, null);
if (converter == null)
throw new MissingMethodException(prop.DeclaringType.FullName, dispAttr.Converter);
else if (converter.ReturnType == typeof(Inline) || converter.ReturnType.IsSubclassOf(typeof(Inline)))
{
valueIsInline = true;
value = this.Dispatcher.Invoke(new Func<object, object[], object>(converter.Invoke), data, new object[1] { value });
}
else
value = converter.Invoke(data, new object[1] { value });
}
}
if (value != null && !valueIsInline && !(value is string))
{
if (value is DirectoryInfo)
value = ((DirectoryInfo)value).FullName;
else if (value is FileInfo)
value = ((FileInfo)value).FullName;
else if (value is IEnumerable<dynamic>)
value = this.IEnumerableToString((IEnumerable<dynamic>)value, 5);
else if (value is DateTime)
value = TimeZone.CurrentTimeZone.ToLocalTime((DateTime)value);
}
this.Dispatcher.Invoke(new Action(() =>
{
Properties.AddRange(
new Run(string.Format("{0}: ", name)),
valueIsInline
? (Inline)value
: new Run((value == null) ? string.Empty : value.ToString()),
new Run("\r\n"));
}));
}
catch (Exception ex)
{
Utilities.Utilities.Log(ex);
this.Dispatcher.Invoke(new Action(() =>
{
Properties.AddRange(
new Run(string.Format("{0}:\r\n", name ?? prop.Name)),
new Run(string.Format("\t{0}", ex))
{
FontStyle = FontStyles.Italic
},
new Run("\r\n"));
}));
}
}
if (Properties.Count > 0)
Properties.RemoveLast();
this.Dispatcher.Invoke(new Action(() =>
{
if (navigateTo != null)
this.DataTextBox.Document = this.NewFlowDocumentWithWebBrowser(navigateTo, Properties);
else
this.DataTextBox.Document = this.NewFlowDocument(Properties);
}));
return;
}
this.Dispatcher.Invoke(new Action(this.DataTextBox.Document.Blocks.Clear));
};
worker.RunWorkerAsync();
}
private string IEnumerableToString(IEnumerable<dynamic> value, int Limit = -1)
{
int Count = value.Count();
if (Limit > 0 && Count > Limit)
value = value.Take(Limit);
if (value is IEnumerable<Exception>)
value = value.Select(ex => string.Format("{0}: {1}", ex.GetType(), ex.Message));
if (Limit <= 0 || Count <= Limit)
return string.Format("{0}\r\n\t{1}", Count, string.Join("\r\n\t", value)).TrimEnd('\r', '\n', '\t');
else
return string.Format("{0}\r\n\t{1}\r\n\t\u2026", Count, string.Join("\r\n\t", value));
}
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
if (e.Uri != null)
System.Diagnostics.Process.Start(e.Uri.ToString());
}
private FlowDocument NewFlowDocument(params Inline[] Inlines)
{
return this.NewFlowDocument((IEnumerable<Inline>)Inlines);
}
private FlowDocument NewFlowDocument(IEnumerable<Inline> Inlines)
{
Paragraph Paragraph = new Paragraph();
Paragraph.Inlines.AddRange(Inlines);
return this.NewFlowDocument(Paragraph);
}
private FlowDocument NewFlowDocument(params Block[] Blocks)
{
FlowDocument Document = new FlowDocument();
Document.Blocks.AddRange(Blocks);
return Document;
}
private FlowDocument NewFlowDocumentWithWebBrowser(Uri WebBrowserUrl, params Inline[] Inlines)
{
return this.NewFlowDocumentWithWebBrowser(WebBrowserUrl, (IEnumerable<Inline>)Inlines);
}
private FlowDocument NewFlowDocumentWithWebBrowser(Uri WebBrowserUrl, IEnumerable<Inline> Inlines)
{
Paragraph Paragraph = new Paragraph();
Paragraph.Inlines.AddRange(Inlines);
return this.NewFlowDocumentWithWebBrowser(WebBrowserUrl, Paragraph);
}
private FlowDocument NewFlowDocumentWithWebBrowser(Uri WebBrowserUrl, params Block[] Blocks)
{
FlowDocument Document = this.NewFlowDocument(Blocks);
try
{
WebBrowser Browser = new WebBrowser()
{
MinHeight = 450
};
Browser.Navigate(WebBrowserUrl);
Document.Blocks.Add(new BlockUIContainer(Browser));
Document.Unloaded += new RoutedEventHandler((sender, e) =>
{
Browser.Dispose();
Browser = null;
});
}
catch (Exception ex)
{
Utilities.Utilities.Log(ex);
}
return Document;
}
#endregion
}
}