-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTagManagerForm.cs
More file actions
1020 lines (918 loc) · 42 KB
/
Copy pathTagManagerForm.cs
File metadata and controls
1020 lines (918 loc) · 42 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using ScintillaNET;
using StatTag.Controls;
using StatTag.Core.Exceptions;
using StatTag.Core.Models;
using StatTag.Core.Utility;
using StatTag.Models;
using Document = Microsoft.Office.Interop.Word.Document;
using Font = System.Drawing.Font;
using Rectangle = System.Drawing.Rectangle;
using View = System.Windows.Forms.View;
namespace StatTag
{
public sealed partial class TagManagerForm : Form
{
// These internal members are used to track if and when we have child forms
// open. This will allow us to manage closing them if a code file changes
private EditTag EditTagForm { get; set; }
private readonly List<Tag> FilteredTags = new List<Tag>();
public DocumentManager Manager { get; set; }
public StatsManager StatsManager { get; set; }
public Document ActiveDocument { get; set; }
/// <summary>
/// Holds state of the visibility of this form before an editing event occurs.
/// This allows us to properly restore the form visibility after (if needed)
/// </summary>
private bool? FormVisibilityBeforeEdit = null;
private static Bitmap WarningImage = new Bitmap(StatTag.Properties.Resources.warning, 24, 24);
private const int InnerPadding = 3;
private int ItemHeight = 60;
//private const int ItemHeight = 60;
private const int NumberRows = 3;
//private const int RowHeight = (int)(ItemHeight * (1.0 / NumberRows));
private int RowHeight = 60; //(int)(ItemHeight * (1.0 / NumberRows));
private const string DefaultFormat = "Default";
private const string DefaultPreviewText = "(Exactly as Generated)";
private const int TagTypeImageDimension = 32;
private const int TagAlertImageDimension = 20;
private const int CodePeekImageDimension = 16;
private const string BaseDialogTitle = "StatTag - Tag Manager";
private const string DuplicateTagIndicator = "StatTag|DuplicateTag";
private const string OverlappingTagIndicator = "StatTag|OverlappingTag";
private static readonly Brush AlternateBackgroundBrush = new SolidBrush(Color.FromArgb(255, 245, 245, 245));
private static readonly Brush HighlightBrush = new SolidBrush(Color.FromArgb(255, 17, 108, 214));
private static readonly Font NameFont = new Font("Segoe UI", 10.0F, FontStyle.Bold);
private static readonly Font NormalFont = new Font(NameFont, FontStyle.Regular);
private static readonly Font SubFont = new Font(NameFont.FontFamily, 8.0F, FontStyle.Regular);
private static readonly StringFormat TextFormat = new StringFormat
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Near,
Trimming = StringTrimming.EllipsisCharacter,
FormatFlags = StringFormatFlags.NoWrap
};
private static readonly Dictionary<string, Bitmap> StatPackageImages = new Dictionary<string, Bitmap>()
{
{ Constants.StatisticalPackages.R, new Bitmap(StatTag.Properties.Resources.stats_package_r) },
{ Constants.StatisticalPackages.SAS, new Bitmap(StatTag.Properties.Resources.stats_package_sas) },
{ Constants.StatisticalPackages.Stata, new Bitmap(StatTag.Properties.Resources.stats_package_stata) },
{ Constants.StatisticalPackages.RMarkdown, new Bitmap(StatTag.Properties.Resources.stats_package_rmd) },
{ Constants.StatisticalPackages.Python, new Bitmap(StatTag.Properties.Resources.stats_package_python) },
{ string.Empty, new Bitmap(StatTag.Properties.Resources.stats_package_unknown) }
};
private static readonly Dictionary<string, Bitmap> TagTypeImages = new Dictionary<string, Bitmap>()
{
{Constants.TagType.Figure, new Bitmap(StatTag.Properties.Resources.figure_preview)},
{Constants.TagType.Table, new Bitmap(StatTag.Properties.Resources.table_preview)},
{Constants.TagType.Value, new Bitmap(1,1)},
{Constants.TagType.Verbatim, new Bitmap(1,1)},
{string.Empty, new Bitmap(1,1)}
};
private static readonly Bitmap TagAlertImage = new Bitmap(StatTag.Properties.Resources.warning);
private static readonly Bitmap CodePeekImage = new Bitmap(StatTag.Properties.Resources.code_peek);
private readonly ScintillaEditorPopover scintillaPopOver = new ScintillaEditorPopover();
private readonly TagListViewColumnSorter ListViewSorter = new TagListViewColumnSorter();
private ExecutionProgressForm CurrentProgress;
public TagManagerForm(DocumentManager manager, StatsManager statsManager)
{
InitializeComponent();
// Calculate the height of the main row (with the name), and the height of the item with all included rows
// of text. This allows us to scale to the current DPI.
ItemHeight = TextRenderer.MeasureText("TEST TEXT", NameFont).Height;
RowHeight = ItemHeight;
ItemHeight += (TextRenderer.MeasureText("TEST TEXT", SubFont).Height*2) + (NumberRows * InnerPadding * 2);
this.MinimumSize = this.Size;
lvwTags.View = View.Details; // It must always be Details
lvwTags.ListViewItemSorter = ListViewSorter;
Manager = manager;
StatsManager = statsManager;
this.cmdCheckUnlinkedTags.Image = new Bitmap(StatTag.Properties.Resources.warning, 24, 24);
if (Manager != null)
{
SetDialogTitle();
LoadCodeFileList(this);
Manager.TagListChanged += ManagerOnTagListChanged;
Manager.CodeFileListChanged += ManagerOnCodeFileListChanged;
Manager.ActiveDocumentChanged += ManagerOnActiveDocumentChanged;
Manager.EditingTag += ManagerOnEditingTag;
Manager.EditedTag += ManagerOnEditedTag;
}
}
/// <summary>
/// Solution from: https://stackoverflow.com/a/8849636/
/// </summary>
/// <param name="lvwList"></param>
/// <returns></returns>
private delegate void SelectListItem(ListView lvwList, Tag tag);
private void SelectTagInList(ListView lvwList, Tag tag)
{
if (!lvwList.InvokeRequired)
{
var items = lvwList.Items.OfType<ListViewItem>();
var selectableItem = items.FirstOrDefault(x => x.Tag.Equals(tag));
if (selectableItem != null)
{
lvwList.SelectedItems.Clear();
selectableItem.Selected = true;
}
}
else
{
this.Invoke(new SelectListItem(SelectTagInList), new object[] { lvwList, tag });
}
}
private delegate void ManageVisibility(Form form, bool visible);
private void SetTagManagerVisibility(Form form, bool visible)
{
if (!form.InvokeRequired)
{
if (!form.IsDisposed)
{
form.Visible = visible;
}
}
else
{
this.Invoke(new ManageVisibility(SetTagManagerVisibility), new object[] { form, visible });
}
}
private void ManagerOnEditingTag(object sender, DocumentManager.TagEventArgs tagEventArgs)
{
// Save the state of the form's visibility
FormVisibilityBeforeEdit = Visible;
// Always attempt to hide the form
SetTagManagerVisibility(this, false);
}
private void ManagerOnEditedTag(object sender, DocumentManager.TagEventArgs tagEventArgs)
{
// If we tracked the form's visibility, and it was visible, then restore it.
// If this wasn't tracked for some reason, we err on the side of caution and
// will not attempt to show the form.
if (FormVisibilityBeforeEdit.HasValue && FormVisibilityBeforeEdit.Value)
{
SetTagManagerVisibility(this, true);
}
// Reset the value each time
FormVisibilityBeforeEdit = null;
// Select the tag that was just edited.
SelectTagInList(lvwTags, tagEventArgs.Tag);
}
/// <summary>
/// Event handler called when the active Word document changes. Our response is to update
/// the document title, code file list and tag list to reflect the new document's metadata.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void ManagerOnActiveDocumentChanged(object sender, EventArgs eventArgs)
{
SetDialogTitle();
LoadCodeFileList(this);
FilterTags(this);
}
/// <summary>
/// Update the title of the dialog to reflect the currently active document name
/// </summary>
private void SetDialogTitle()
{
var activeDocument = Manager.ActiveDocument;
Text = string.Format("{0}{1}", BaseDialogTitle,
(activeDocument != null) ? string.Format(" - {0}", activeDocument.FullName) : string.Empty);
}
/// <summary>
/// Load the list of code files (used for filtering tags) from our DocumentManager reference
/// </summary>
private delegate void CodeFileListDelegate(StatTag.TagManagerForm form);
private void LoadCodeFileList(StatTag.TagManagerForm form)
{
if (!form.InvokeRequired)
{
if (Manager != null)
{
var selectedItem = string.Empty;
if (cboCodeFiles.SelectedIndex >= 0 && cboCodeFiles.SelectedItem != null)
{
selectedItem = cboCodeFiles.SelectedItem.ToString();
}
var codeFiles = Manager.GetCodeFileList();
cboCodeFiles.Items.Clear();
cboCodeFiles.Items.Add("(Tags for all code files)");
cboCodeFiles.Items.AddRange(codeFiles.Select(path => Path.GetFileName(path.FilePath)).ToArray());
if (string.IsNullOrWhiteSpace(selectedItem))
{
cboCodeFiles.SelectedIndex = 0;
}
else
{
cboCodeFiles.SelectedItem = selectedItem;
// If the selection we tried is no longer valid, default to the "all tags" filter selection
if (cboCodeFiles.SelectedItem == null)
{
cboCodeFiles.SelectedIndex = 0;
}
}
}
}
else
{
this.Invoke(new CodeFileListDelegate(LoadCodeFileList), new object[] { form });
}
}
/// <summary>
/// Handle notifications from the DocumentManager when our list of code files has changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void ManagerOnCodeFileListChanged(object sender, EventArgs eventArgs)
{
LoadCodeFileList(this);
FilterTags(this);
}
/// <summary>
/// Handle notifications from the DocumentManager when the list and/or content of tags has changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void ManagerOnTagListChanged(object sender, EventArgs eventArgs)
{
FilterTags(this);
}
private void TagManager_Load(object sender, EventArgs e)
{
var imgList = new ImageList {ImageSize = new Size(1, ItemHeight)};
lvwTags.SmallImageList = imgList;
FilterTags(this);
}
/// <summary>
/// Filter the list of tags that is displayed based off of the code file filter, as well as the
/// search string filter.
/// </summary>
private delegate void FilterTagsDelegate(StatTag.TagManagerForm form);
private void FilterTags(StatTag.TagManagerForm form)
{
if (!form.InvokeRequired)
{
bool hasWarnings = false;
FilteredTags.Clear();
lvwTags.Items.Clear();
bool allCodeFiles = (cboCodeFiles.SelectedIndex == 0 || cboCodeFiles.SelectedItem == null);
bool noFilter = string.IsNullOrWhiteSpace(txtFilter.Text);
var filter = txtFilter.Text;
var overlappingTags = new List<Tag>();
if (Manager != null)
{
var tags = Manager.GetTags();
FilteredTags.AddRange(tags.Where(x =>
(allCodeFiles || x.CodeFilePath.EndsWith(cboCodeFiles.SelectedItem.ToString()))
&& (noFilter || x.Name.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0)).ToArray());
// If there are overlapping (colliding) tags, we want to get a list of all of them. This will
// provide us with a lookup list of tags we need to flag in the tag list.
var overlappingResults = Manager.TagManager.FindAllOverlappingTags();
if (overlappingResults != null)
{
overlappingTags.AddRange(overlappingResults.Values.SelectMany(x => x.SelectMany(y => y)));
}
}
foreach (var tag in FilteredTags)
{
var indicator = string.Empty;
var isDuplicate = (FilteredTags.Count(x => x.Id.Equals(tag.Id)) > 1);
if (!isDuplicate && overlappingTags.Any(x => x.Equals(tag)))
{
indicator = OverlappingTagIndicator;
hasWarnings = true;
}
else if (isDuplicate)
{
indicator = DuplicateTagIndicator;
hasWarnings = false;
}
lvwTags.Items.Add(new ListViewItem(new string[] { tag.CodeFile.StatisticalPackage, tag.Name, tag.Type, indicator }) { Tag = tag });
}
if (hasWarnings)
{
this.cmdCheckUnlinkedTags.Image = WarningImage;
this.cmdCheckUnlinkedTags.Text = " Check Tags";
}
else
{
this.cmdCheckUnlinkedTags.Image = null;
this.cmdCheckUnlinkedTags.Text = "Check Tags";
}
UpdateUIForSelection();
}
else
{
this.Invoke(new FilterTagsDelegate(FilterTags), new object[] { form });
}
}
/// <summary>
/// Helper to safely return a Bitmap to represent the statistical package used to run
/// the CodeFile file
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private Bitmap GetStatPackageImage(CodeFile file)
{
if (file != null && StatPackageImages.ContainsKey(file.StatisticalPackage))
{
return StatPackageImages[file.StatisticalPackage];
}
return StatPackageImages[string.Empty];
}
/// <summary>
/// For a given tag, return a string that describes how it will be formatted.
/// </summary>
/// <param name="tag"></param>
/// <returns></returns>
private string GenerateFormatDescriptionFromTag(Tag tag)
{
if (tag == null)
{
return DefaultFormat;
}
switch (tag.Type)
{
case Constants.TagType.Verbatim:
case Constants.TagType.Figure:
return DefaultFormat;
case Constants.TagType.Table:
case Constants.TagType.Value:
if (tag.ValueFormat == null)
{
return DefaultFormat;
}
return tag.ValueFormat.FormatType;
default:
return DefaultFormat;
}
}
/// <summary>
/// For a given tag, return a string that represents its preview - this accounts
/// for things like numeric formatting.
/// </summary>
/// <param name="tag"></param>
/// <returns></returns>
private string GeneratePreviewTextFromTag(Tag tag)
{
if (tag == null)
{
return string.Empty;
}
switch (tag.Type)
{
case Constants.TagType.Verbatim:
case Constants.TagType.Figure:
return DefaultPreviewText;
case Constants.TagType.Table:
case Constants.TagType.Value:
if (tag.ValueFormat == null)
{
return DefaultPreviewText;
}
switch (tag.ValueFormat.FormatType)
{
case Constants.ValueFormatType.DateTime:
return tag.ValueFormat.Format("January 24, 1984 19:30:50");
case Constants.ValueFormatType.Numeric:
return tag.ValueFormat.Format("100000");
case Constants.ValueFormatType.Percentage:
return tag.ValueFormat.Format("1");
default:
return DefaultPreviewText;
}
default:
return string.Empty;
}
}
private Rectangle[] GetSubItemRowBounds(Rectangle subItemBounds)
{
var topRow = subItemBounds;
topRow.Offset(InnerPadding, InnerPadding);
topRow.Height = RowHeight;
var row2 = new Rectangle(subItemBounds.X + InnerPadding, topRow.Y + topRow.Height, topRow.Width, RowHeight);
var row3 = new Rectangle(subItemBounds.X + InnerPadding, row2.Y + row2.Height, topRow.Width, RowHeight);
return new[] {topRow, row2, row3};
}
private void lvwTags_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ItemIndex < 0)
{
return;
}
// Have to look at Item.Selected status when HideSelection is false - see https://stackoverflow.com/a/2394142
var isSelected = e.Item.Selected;
if (isSelected)
{
e.Graphics.FillRectangle(HighlightBrush, e.Bounds);
}
else
{
e.Graphics.FillRectangle(e.ItemIndex%2 == 0 ? Brushes.White : AlternateBackgroundBrush, e.Bounds);
}
var rowBounds = GetSubItemRowBounds(e.Bounds);
//var topRow = e.Bounds;
//topRow.Offset(InnerPadding, InnerPadding);
//topRow.Height = RowHeight;
//var row2 = new Rectangle(e.Bounds.X + InnerPadding, topRow.Y + topRow.Height, topRow.Width, RowHeight);
//var row3 = new Rectangle(e.Bounds.X + InnerPadding, row2.Y + row2.Height, topRow.Width, RowHeight);
var tag = e.Item.Tag as Tag;
if (tag == null)
{
return;
}
switch (e.ColumnIndex)
{
case 0:
var image = GetStatPackageImage(tag.CodeFile);
var rect = e.Bounds;
rect.Offset((rect.Width - image.Width) / 2, (rect.Height - image.Height) / 2);
e.Graphics.DrawImageUnscaled(image, rect);
break;
case 1:
e.Graphics.DrawString(tag.Name, NameFont, (isSelected ? Brushes.White : Brushes.Black), rowBounds[0], TextFormat);
e.Graphics.DrawString(Path.GetFileName(tag.CodeFilePath), SubFont, (isSelected ? Brushes.White : Brushes.Gray), rowBounds[1], TextFormat);
var peekImageRect = rowBounds[2];
peekImageRect.Width = CodePeekImageDimension;
peekImageRect.Height = CodePeekImageDimension;
e.Graphics.DrawImage(CodePeekImage, peekImageRect);
break;
case 2:
var selectedSubBrush = (isSelected ? Brushes.White : Brushes.Gray);
e.Graphics.DrawString(tag.Type, NormalFont, (isSelected ? Brushes.White : Brushes.Black), rowBounds[0], TextFormat);
e.Graphics.DrawString(GenerateFormatDescriptionFromTag(tag), SubFont, selectedSubBrush, rowBounds[1], TextFormat);
e.Graphics.DrawString(GeneratePreviewTextFromTag(tag), SubFont, selectedSubBrush, rowBounds[2], TextFormat);
var typeImageRect = e.Bounds;
typeImageRect.Offset((e.Bounds.Width - TagTypeImageDimension - (2 * InnerPadding)),
(e.Bounds.Height - TagTypeImageDimension) / 2);
typeImageRect.Width = TagTypeImageDimension;
typeImageRect.Height = TagTypeImageDimension;
e.Graphics.DrawImage(TagTypeImages[tag.Type], typeImageRect);
break;
case 3:
// Our internal convention is that any non-blank text means something is up, and we should show the
// indicator icon.
if (!string.IsNullOrWhiteSpace(e.SubItem.Text))
{
var imageRect = e.SubItem.Bounds;
imageRect.Height = TagAlertImageDimension;
imageRect.Width = TagAlertImageDimension;
imageRect.Offset((e.SubItem.Bounds.Width - TagAlertImageDimension - (2 * InnerPadding)),
(e.SubItem.Bounds.Height - TagAlertImageDimension) / 2);
e.Graphics.DrawImage(TagAlertImage, imageRect);
}
break;
}
}
private void cboCodeFiles_SelectedIndexChanged(object sender, EventArgs e)
{
FilterTags(this);
}
private void lvwTags_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
// Code derived from https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawcolumnheader.aspx
using (var format = new StringFormat())
{
switch (e.Header.TextAlign)
{
case HorizontalAlignment.Center:
format.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
format.Alignment = StringAlignment.Far;
break;
}
format.LineAlignment = StringAlignment.Center;
e.DrawBackground();
e.Graphics.DrawString(e.Header.Text, NormalFont, Brushes.Black, e.Bounds, format);
}
}
private void lvwTags_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (lvwTags.SelectedItems.Count == 0)
{
return;
}
int firstSelectedIndex = lvwTags.SelectedItems[0].Index;
var existingTag = lvwTags.Items[firstSelectedIndex].Tag as Tag;
try
{
this.TopMost = false;
this.Visible = false;
var hit = lvwTags.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem.Text.Equals(DuplicateTagIndicator))
{
Manager.PerformDocumentCheck(Manager.ActiveDocument, false, CheckDocument.DefaultTab.DuplicateTags);
}
else if (hit.SubItem != null && hit.SubItem.Text.Equals(OverlappingTagIndicator))
{
Manager.PerformDocumentCheck(Manager.ActiveDocument, false, CheckDocument.DefaultTab.CollidingTags);
}
else if (Manager.EditTag(existingTag))
{
lvwTags.Refresh();
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
this.TopMost = true;
this.Visible = true;
}
}
private void lvwTags_MouseClick(object sender, MouseEventArgs e)
{
var hit = lvwTags.HitTest(e.Location);
if (hit.SubItem == null)
{
return;
}
var subItemIndex = hit.Item.SubItems.IndexOf(hit.SubItem);
if (subItemIndex != 1)
{
return;
}
var rowBounds = GetSubItemRowBounds(hit.SubItem.Bounds);
var peekImageBounds = rowBounds[2];
peekImageBounds.Width = CodePeekImageDimension;
peekImageBounds.Height = CodePeekImageDimension;
if (!peekImageBounds.Contains(e.Location))
{
return;
}
// At this point the user has clicked on the "code peek" icon, so we should display
// the popup for them.
var tag = hit.Item.Tag as Tag;
if (tag == null || !tag.LineStart.HasValue || !tag.LineEnd.HasValue)
{
return;
}
scintillaPopOver.ShowCodeFileLines(tag.CodeFile, tag.LineStart.Value, tag.LineEnd.Value);
scintillaPopOver.Show(this, e.Location);
}
private void lvwTags_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateUIForSelection();
}
private void UpdateUIForSelection()
{
int selectionCount = lvwTags.SelectedItems.Count;
bool hasSelection = (selectionCount > 0);
cmdInsert.Enabled = hasSelection;
cmdUpdate.Enabled = hasSelection;
cmdRemoveTags.Enabled = hasSelection;
cmdInsert.Text = string.Format("Insert {0} Tag Placeholder{1}", selectionCount, (selectionCount != 1 ? "s" : ""));
cmdUpdate.Text = string.Format("Update {0} Tag Result{1}", selectionCount, (selectionCount != 1 ? "s" : ""));
cmdRemoveTags.Text = string.Format("Remove {0} Tag{1}", selectionCount, (selectionCount != 1 ? "s" : ""));
}
private void lvwTags_ColumnClick(object sender, ColumnClickEventArgs e)
{
ListViewSorter.HandleSort(e.Column, lvwTags);
}
/// <summary>
/// Helper function to return the list of Tag objects that are represented by selections
/// within the ListView
/// </summary>
/// <returns></returns>
private List<Tag> GetSelectedTags()
{
// When tags get inserted into the document, the cursor ends up before the newly inserted field. I tried
// to move the cursor after the inserted field (I promise!), but to no avail. Our workaround so that the
// tags are inserted in the order they are in the list is to do it in reverse order (which is why it's
// OrderByDescending).
var selectedItems = lvwTags.SelectedItems;
return selectedItems.OfType<ListViewItem>()
.OrderByDescending(x => x.Index)
.Select(x => x.Tag as Tag)
.Where(x => x != null).ToList();
}
/// <summary>
/// Handler to insert selected tags at the current position within the document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmdInsert_Click(object sender, EventArgs e)
{
try
{
InitializeProgressDialog(insertBackgroundWorker);
var tags = GetSelectedTags();
insertBackgroundWorker.RunWorkerAsync(tags);
}
catch (StatTagUserException uex)
{
UIUtility.ReportException(uex, uex.Message, Manager.Logger);
}
catch (Exception exc)
{
UIUtility.ReportException(exc,
"There was an unexpected error when trying to insert the tag output into the document.",
Manager.Logger);
}
finally
{
Globals.ThisAddIn.Application.ScreenUpdating = true;
Cursor.Current = Cursors.Default;
}
}
private void InitializeProgressDialog(BackgroundWorker worker)
{
if (CurrentProgress != null && !CurrentProgress.IsDisposed)
{
CurrentProgress.Close();
CurrentProgress = null;
}
CurrentProgress = new ExecutionProgressForm(worker);
this.TopMost = false;
this.Visible = false;
CurrentProgress.TopMost = true;
CurrentProgress.Show();
}
private void cmdUpdate_Click(object sender, EventArgs e)
{
try
{
var tags = GetSelectedTags();
// Perform our pre-execution check on all relevant code files. If any of the checks fail, we will halt
// execution. We are assuming the PreExecutionCheck method handles all notifications, so we will also
// adjust our TopMost status to allow any dialogs to take focus.
var files = tags.Select(x => x.CodeFile).Distinct().ToArray();
this.TopMost = false;
if (files != null && files.Length > 0 && files.Any(x => !StatsManager.PreExecutionCheck(x)))
{
this.TopMost = true;
return;
}
this.TopMost = true;
InitializeProgressDialog(updateBackgroundWorker);
Cursor.Current = Cursors.WaitCursor;
Globals.ThisAddIn.Application.ScreenUpdating = false;
updateBackgroundWorker.RunWorkerAsync(tags);
}
catch (StatTagUserException uex)
{
CompletedBackgroundWorker(uex);
}
catch (Exception exc)
{
CompletedBackgroundWorker(new StatTagUserException("There was an unexpected error when trying to update values in your document.", exc));
}
}
private void txtFilter_FilterChanged(object sender, EventArgs e)
{
FilterTags(this);
}
private void cmdDefineTag_Click(object sender, EventArgs e)
{
try
{
this.TopMost = false;
this.Visible = false;
CodeFile lastSelectedCodeFile = null;
bool defineAnother = false;
int? defaultScrollLine = null;
do
{
EditTagForm = new EditTag(true, Manager, lastSelectedCodeFile, defaultScrollLine);
if (DialogResult.OK == EditTagForm.ShowDialog())
{
Manager.SaveEditedTag(EditTagForm);
defineAnother = EditTagForm.DefineAnother;
defaultScrollLine = EditTagForm.DefaultScrollLine;
lastSelectedCodeFile = EditTagForm.Tag.CodeFile;
}
else
{
defineAnother = false;
}
} while (defineAnother);
}
finally
{
this.Visible = true;
this.TopMost = true;
EditTagForm = null;
}
}
private void cmdRemoveTags_Click(object sender, EventArgs e)
{
var tagLabel = GeneralUtil.Pluralize("Tag", lvwTags.SelectedIndices.Count);
if (DialogResult.Yes != MessageBox.Show(
string.Format("Do you wish to remove the {0} selected {1} from your project?\r\n\r\nRemoving a tag will not materially change your code file. StatTag will remove references to the tag within the file, but leave the code itself intact.",
lvwTags.SelectedIndices.Count, tagLabel.ToLower()),
string.Format("{0} - Remove {1}?", UIUtility.GetAddInName(), tagLabel),
MessageBoxButtons.YesNo))
{
Manager.Logger.WriteMessage("Cancel removing selected tags");
return;
}
Manager.Logger.WriteMessage("Proceed with removing selected tags");
var removedTags = new List<Tag>();
var selectedIndices = lvwTags.SelectedIndices;
for (int index = (selectedIndices.Count - 1); index >= 0; index--)
{
removedTags.Add((Tag)lvwTags.Items[selectedIndices[index]].Tag);
lvwTags.Items.RemoveAt(selectedIndices[index]);
}
Manager.RemoveTags(removedTags);
}
/// <summary>
/// Called when we need to forcibly close any and all open dialogs, such as
/// for system shutdown, or when a linked code file has been changed.
/// </summary>
public void CloseAllChildDialogs()
{
UIUtility.ManageCloseDialog(EditTagForm);
Manager.CloseAllChildDialogs();
}
private void cmdCheckUnlinkedTags_Click(object sender, EventArgs e)
{
try
{
this.TopMost = false;
this.Visible = false;
Manager.PerformDocumentCheck(Manager.ActiveDocument);
}
finally
{
this.Visible = true;
this.TopMost = true;
EditTagForm = null;
}
}
private void TagManager_FormClosed(object sender, FormClosedEventArgs e)
{
if (Manager != null)
{
Manager.TagListChanged -= ManagerOnTagListChanged;
Manager.CodeFileListChanged -= ManagerOnCodeFileListChanged;
Manager.ActiveDocumentChanged -= ManagerOnActiveDocumentChanged;
Manager.EditingTag -= ManagerOnEditingTag;
Manager.EditedTag -= ManagerOnEditedTag;
}
}
/// <summary>
/// Needed to handle Ctrl+A as a select-all hotkey
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvwTags_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A && e.Control)
{
foreach (ListViewItem item in lvwTags.Items)
{
item.Selected = true;
}
}
}
/// <summary>
/// General event handler for all background processors to update the progress dialog (if it's shown)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if (CurrentProgress != null)
{
CurrentProgress.UpdateProgress(e.ProgressPercentage, e.UserState.ToString());
}
}
/// <summary>
/// Event handler started for the background worker that updates inserted tags within the Word document.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void updateBackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// First, go through and update all of the code files to ensure we have all
// refreshed tags.
var worker = (BackgroundWorker) sender;
var progressReporter = new BackgroundWorkerProgressReporter(worker);
var tags = (List<Tag>)e.Argument;
var refreshedFiles = new List<CodeFile>();
var files = tags.Select(x => x.CodeFile).Distinct().ToArray();
int length = files.Length;
var warnings = new List<string>();
for (int index = 0; index < length; index++)
{
// If the user cancels while running code files, exit right away so we don't do any of
// the later tag processing/updates.
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
var codeFile = files[index];
int progressBefore = (int)((((index * 1.0 + 1) / length) / 2.0) * 100);
worker.ReportProgress(progressBefore, string.Format("Running '{0}' (code file {1} of {2})", Path.GetFileName(codeFile.FilePath), (index + 1), length));
if (!refreshedFiles.Contains(codeFile))
{
var result = StatsManager.ExecuteStatPackage(codeFile, Constants.ParserFilterMode.TagList, tags);
if (!result.Success)
{
throw new StatTagUserException(result.ErrorMessage);
}
else if (!string.IsNullOrWhiteSpace(result.WarningMessage))
{
warnings.Add(string.Format("{0}:\r\n{1}",
Path.GetFileName(codeFile.FilePath),
result.WarningMessage));
}
refreshedFiles.Add(codeFile);
}
int progressAfter = (int)(((index * 1.0 + 1) / length) * 100);
worker.ReportProgress(progressAfter, string.Format("Completed code file {0} of {1}", (index + 1), length));
}
// Now we will refresh all of the tags that are fields. Since we most likely
// have more fields than tags, we are going to use the approach of looping
// through all fields and updating them (via the DocumentManager).
Manager.UpdateFields(tags, progressReporter);
e.Cancel = worker.CancellationPending;
var executionResult = new ExecutionResult();
if (warnings.Count > 0)
{
executionResult.WarningMessage = string.Join("\r\n", warnings);
}
e.Result = executionResult;
}
/// <summary>
/// General event handler called when a background worker has completed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
try
{
// In situations with exceptions, we may not be able to convert e.Result (or even reference it) without
// an exception being thrown. We will attempt to use e.Result, but if that throws a TargetInvocationException
// we will call to complete the background worker again, but with no attempt to use the result.
CompletedBackgroundWorker(e.Error, e.Cancelled, (ExecutionResult)e.Result);
}
catch (TargetInvocationException exc)
{
CompletedBackgroundWorker(e.Error, e.Cancelled, null);
}
}
/// <summary>
/// Helper function to manage cleanup & completion after a background worker is done.
/// This resets the state of dialogs/forms, and handles any exception reporting from the work.
/// </summary>
/// <param name="exception"></param>
private void CompletedBackgroundWorker(Exception exception = null, bool cancelled = false, ExecutionResult result = null)
{
if (CurrentProgress != null)
{
CurrentProgress.Close();
CurrentProgress = null;
}
if (exception != null)
{
UIUtility.ReportException(exception, exception.Message, Manager.Logger);
}
if (cancelled)
{
Manager.Logger.WriteMessage("The code execution was cancelled by the user");
UIUtility.WarningMessageBox("You have cancelled updates.\r\n\r\nThe values in your document may not be accurate until you update all tags.", Manager.Logger);
}
if (result != null && !string.IsNullOrWhiteSpace(result.WarningMessage))
{
UIUtility.WarningMessageBox(
string.Format("Your code file(s) ran, but the following warnings were reported:\r\n\r\n{0}", result.WarningMessage),
Manager.Logger);
}
this.TopMost = true;
this.Visible = true;