FogBugz » FogBugz for Visual Studio Read More
Clone URL:  
CaseListConnector.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
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
using System; using System.Collections.Generic; using System.Drawing; using System.Resources; using System.Windows.Forms; using FogBugzForVisualStudio.Api; namespace FogBugzForVisualStudio { public class CaseListConnector { private Dictionary<int, bool> ixBugInFilter; private Dictionary<int, CaseListRow> allRows; private List<CaseListRow> visibleRows; private List<CaseListRow> rootRows; private bool loaded; private string sortField; private SortOrder sortOrder; public string SortField { get { return sortField; } set { this.Sort(value, sortOrder); } } public SortOrder SortOrder { get { return sortOrder; } set { this.Sort(sortField, value); } } internal FogBugzClient FogBugzClient { get; private set; } /// <summary> /// Construct a CaseListConnector. /// </summary> /// <param name="fb">A logged-on FogBugzClient.</param> /// <param name="casesInFilter">Cases that are in the user's current filter, as returned from FogBugzClient.ListCases</param> /// <param name="supplementaryCases">A list of cases that are not in the filter, but are parent cases or subcases to cases that are.</param> /// <param name="sortField"></param> /// <param name="sortOrder"></param> /// <param name="expandedCases">Optional: a dictionary matching cases to whether they are expanded or collapsed. Pass the value from a previous CaseListConnector's GetCasesExpanded() to save view state.</param> /// <param name="casesShowingSubcases">Optional: a dictionary matching cases to whether they show subcases not in the filter. Pass the value from a previous CaseListConnector's GetCasesShowingSubcases() to save view state.</param> public CaseListConnector(FogBugzClient fb, List<Case> casesInFilter, List<Case> supplementaryCases, string sortField, SortOrder sortOrder, Dictionary<int, bool> expandedCases = null, Dictionary<int, bool> casesShowingSubcases = null) { FogBugzClient = fb; this.sortField = sortField; this.sortOrder = sortOrder; ixBugInFilter = new Dictionary<int, bool>(); allRows = new Dictionary<int, CaseListRow>(); // Compute a list of root cases, which have no parent subcase, from casesInFilter. var allCases = new Dictionary<int, Case>(); foreach (var c in casesInFilter) { allCases[c.ixBug] = c; ixBugInFilter[c.ixBug] = true; } foreach (var c in supplementaryCases) { allCases[c.ixBug] = c; ixBugInFilter[c.ixBug] = false; } var dictRootCases = new Dictionary<int, bool>(); foreach (var c in casesInFilter) { var i = c.ixBug; while (allCases[i].ixBugParent.HasValue) { i = allCases[i].ixBugParent.Value; } dictRootCases[i] = true; } rootRows = new List<CaseListRow>(); foreach (var ixBug in dictRootCases.Keys) { rootRows.Add(createCaseRow(allCases[ixBug], 0, allCases, expandedCases, casesShowingSubcases)); } PopulateVisibleRows(); loaded = true; } public Dictionary<int, bool> GetCasesExpanded() { var expanded = new Dictionary<int, bool>(); foreach (var c in allRows.Values) { expanded[c.Bug.ixBug] = c.Expanded; } return expanded; } public Dictionary<int, bool> GetCasesShowingSubcases() { var showing = new Dictionary<int, bool>(); foreach (var c in allRows.Values) { showing[c.Bug.ixBug] = c.ShowsSubcasesNotInFilter; } return showing; } private CaseListRow createCaseRow(Case c, int i, Dictionary<int, Case> allCases, Dictionary<int, bool> expandedCases, Dictionary<int, bool> casesShowingSubcases) { var row = new CaseListRow(c, i, this); if (row.Expandable) { row.Expanded = (expandedCases != null && expandedCases.ContainsKey(c.ixBug)) ? expandedCases[c.ixBug] : containsSubcaseInFilter(c, allCases); } if (casesShowingSubcases != null && casesShowingSubcases.ContainsKey(c.ixBug)) { row.ShowsSubcasesNotInFilter = casesShowingSubcases[c.ixBug]; } allRows[c.ixBug] = row; foreach (var ixBugChild in c.ixBugChildren) { createCaseRow(allCases[ixBugChild], i + 1, allCases, expandedCases, casesShowingSubcases); } return row; } private bool containsSubcaseInFilter(Case c, Dictionary<int, Case> allCases) { foreach (var ixBug in c.ixBugChildren) { if (ixBugInFilter[ixBug]) return true; if (containsSubcaseInFilter(allCases[ixBug], allCases)) return true; } return false; } private void PopulateVisibleRows() { visibleRows = new List<CaseListRow>(); if (sortField != null) rootRows.Sort(new CaseListRowComparer(sortField, sortOrder)); foreach (var row in rootRows) { visibleRows.Add(row); if (row.Expanded) ExpandRow(row); } } public int RowCount { get { return visibleRows.Count; } } public CaseListRow Row(int index) { return visibleRows[index]; } public void Sort(string sortField, SortOrder order) { this.sortField = sortField; this.sortOrder = order; PopulateVisibleRows(); } private List<CaseListRow> getSubrows(CaseListRow row) { var subrows = new List<CaseListRow>(); var hiddenSubrows = new List<CaseListRow>(); foreach (var ixBug in row.Bug.ixBugChildren) { // Rows that are in the filter are always shown. Otherwise we're dependent on ShowsSubcasesNotInFilter. if (ixBugInFilter[ixBug] || row.ShowsSubcasesNotInFilter) { subrows.Add(allRows[ixBug]); } else { hiddenSubrows.Add(allRows[ixBug]); } } if (subrows.Count == 0 && hiddenSubrows.Count > 0) { // In this case, override and don't show a "X other cases hidden" row. return hiddenSubrows; } if (hiddenSubrows.Count > 0) { // Add a row to display "[ x cases were hidden because they don't match your filter]" subrows.Add(row.HiddenCasesRow); } return subrows; } private int ExpandRow(CaseListRow row) { var subrows = getSubrows(row); if (sortField != null) subrows.Sort(new CaseListRowComparer(sortField, sortOrder)); var ixRowParent = visibleRows.IndexOf(row); var c = 0; foreach (var subrow in subrows) { visibleRows.Insert(ixRowParent + 1 + c++, subrow); if (subrow.Expanded) { c += ExpandRow(subrow); } } return c; } private void CollapseRow(CaseListRow row) { var subrows = getSubrows(row); foreach (var subrow in subrows) { visibleRows.Remove(subrow); if (subrow.Expanded) CollapseRow(subrow); } if (visibleRows.Contains(row.HiddenCasesRow)) { visibleRows.Remove(row.HiddenCasesRow); } } internal void RowExpandedChanged(CaseListRow row) { if (!loaded) return; if (row.Expanded) { ExpandRow(row); } else { CollapseRow(row); } OnRowCountChanged(this, new EventArgs()); OnViewStateChanged(this, new EventArgs()); } internal void RowShowsSubcasesNotInFilterChanged(CaseListRow caseListRow) { if (!loaded) return; CollapseRow(caseListRow); ExpandRow(caseListRow); OnRowCountChanged(this, new EventArgs()); OnViewStateChanged(this, new EventArgs()); } internal void ShowHiddenSubcases(CaseListHiddenCasesRow caseListHiddenCasesRow, int ixBugParent) { allRows[ixBugParent].ShowsSubcasesNotInFilter = true; // Will raise OnRowCountChanged } internal bool BugInFilter(int ixBug) { return ixBugInFilter[ixBug]; } public event EventHandler OnRowCountChanged; public event EventHandler OnViewStateChanged; public event FogBugzForVisualStudio.FogBugzCtl.ShowUrlHandler OnShowUrl; internal void ShowUrl(string p) { this.OnShowUrl(p); } } public class CaseListRow { public CaseListRow(Case c, int i, CaseListConnector parent) { Bug = c; Indent = i; _parentConnector = new WeakReference(parent); } private WeakReference _parentConnector; protected CaseListConnector parentConnector { get { if (_parentConnector == null || !_parentConnector.IsAlive) { return null; } return (CaseListConnector)_parentConnector.Target; } } public virtual Case Bug { get; private set; } public int Indent { get; private set; } public virtual bool Expandable { get { return Bug.ixBugChildren.Count > 0; } } private bool _expanded; public virtual bool Expanded { get { return _expanded; } set { if (Bug.ixBugChildren.Count == 0 && value) { throw new InvalidOperationException(); } _expanded = value; if (parentConnector != null) { parentConnector.RowExpandedChanged(this); } } } private bool _showsSubcasesNotInFilter; public virtual bool ShowsSubcasesNotInFilter { get { return _showsSubcasesNotInFilter; } set { if (Bug.ixBugChildren.Count == 0 && value) { throw new InvalidOperationException(); } _showsSubcasesNotInFilter = value; if (parentConnector != null) { parentConnector.RowShowsSubcasesNotInFilterChanged(this); } } } public virtual bool InFilter { get { if (parentConnector != null) return parentConnector.BugInFilter(Bug.ixBug); return true; } } public virtual void OnClicked(DataGridView grid, DataGridViewCellEventArgs e) { if (parentConnector == null) return; switch (grid.Columns[e.ColumnIndex].Name) { case "sTitle": case "ixBug": parentConnector.ShowUrl(parentConnector.FogBugzClient.GetDefaultUrl() + Bug.ixBug); Bug.Unread = false; grid.InvalidateRow(e.RowIndex); break; case "hrsCurrEst": var estimateForm = new frmSetEstimate(); estimateForm.lblEstimate.Text = "Enter an estimate for Case " + Bug.ixBug + ", " + Bug.sTitle + "."; estimateForm.fldEstimate.Text = Bug.hrsCurrEst.ToString(); if (estimateForm.ShowDialog() == DialogResult.OK) { try { parentConnector.FogBugzClient.SetCaseEstimate(Bug, estimateForm.fldEstimate.Text); grid.InvalidateRow(e.RowIndex); // invalidate all cells as remaining time might change } catch (FogBugzErrorException exp) { BugScout.ReportException(exp); MessageBox.Show(String.IsNullOrEmpty(exp.Message) ? ("FogBugz returned an unknown error (" + exp.Code + ")") : (exp.Message + " (" + exp.Code + ")")); } } estimateForm.Dispose(); break; } } public virtual object GetCellValue(string columnName) { return typeof(Case).GetProperty(columnName).GetValue(Bug, null); } public virtual void CellFormatting(DataGridViewCellFormattingEventArgs e, DataGridView grid) { if (grid.Columns[e.ColumnIndex].Name == "sTitle") { e.CellStyle.Padding = new Padding(32 * Indent + (Expandable ? 14 : 0), 0, 0, 0); } if (grid.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewLinkCell)) { bool selected = grid.Rows[e.RowIndex].Selected; DataGridViewLinkCell linkCell = (DataGridViewLinkCell)grid.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (selected) { linkCell.LinkColor = System.Drawing.SystemColors.HighlightText; } else { if (this.InFilter) { bool Unread = (grid.Columns[e.ColumnIndex].Name == "hrsCurrEst") ? (Bug.hrsElapsed.hrs == Decimal.Zero) : Bug.Unread; linkCell.LinkColor = Unread ? Color.Blue : Color.Purple; } else { linkCell.LinkColor = Color.Gray; } } } if (!this.InFilter) { e.CellStyle.ForeColor = Color.Gray; } } private CaseListHiddenCasesRow hiddenCasesRow; public CaseListHiddenCasesRow HiddenCasesRow { get { if (hiddenCasesRow == null) { var hide = 0; foreach (var ixBugChild in Bug.ixBugChildren) { if (!parentConnector.BugInFilter(ixBugChild)) hide++; } hiddenCasesRow = new CaseListHiddenCasesRow(Bug.ixBug, hide, Indent + 1, parentConnector); } return hiddenCasesRow; } } } public class CaseListHiddenCasesRow : CaseListRow { private int ixBugParent; private int cHiddenCases; public CaseListHiddenCasesRow(int ixBugParent, int cHiddenCases, int i, CaseListConnector parent) : base(null, i, parent) { this.ixBugParent = ixBugParent; this.cHiddenCases = cHiddenCases; } public override Case Bug { get { return null; } } public override bool Expandable { get { return false; } } public override bool Expanded { get { return false; } set { throw new InvalidOperationException(); } } public override bool ShowsSubcasesNotInFilter { get { return false; } set { throw new InvalidOperationException(); } } public override void OnClicked(DataGridView grid, DataGridViewCellEventArgs e) { if (parentConnector != null) { parentConnector.ShowHiddenSubcases(this, ixBugParent); } } public override object GetCellValue(string columnName) { if (columnName == "sTitle") { return "[ " + cHiddenCases + " subcases not matching filter ]"; } else if (columnName == "categoryImage") { var manager = new ResourceManager("FogBugzForVisualStudio.Resource1", GetType().Assembly); return (Bitmap)manager.GetObject("icon_none"); } return null; } public override bool InFilter { get { return false; } } } internal class CaseListRowComparer : IComparer<CaseListRow> { private string field; private SortOrder sortOrder; public CaseListRowComparer(string field, SortOrder sortOrder) { this.field = field; this.sortOrder = sortOrder; } public int Compare(CaseListRow x, CaseListRow y) { if (x is CaseListHiddenCasesRow) return 1; if (y is CaseListHiddenCasesRow) return -1; var xValue = x.GetCellValue(field); var yValue = y.GetCellValue(field); var result = 0; if (xValue == null && yValue == null) { result = 0; } else if (xValue == null) { result = -1; } else if (yValue == null) { result = 1; } else { if (xValue is IComparable && yValue is IComparable) { result = ((IComparable)xValue).CompareTo(yValue); } // If values don't implement IComparer but are equivalent else if (xValue.Equals(yValue)) { result = 0; } // Values don't implement IComparer and are not equivalent, so compare as string values else result = xValue.ToString().CompareTo(yValue.ToString()); } // ixBug as a secondary sort so that the order is consistent if (result == 0) { var ixX = x.Bug.ixBug; var ixY = y.Bug.ixBug; result = ixX.CompareTo(ixY); } if (sortOrder == SortOrder.Descending) { result = -result; } return result; } } }