CheckBoxTableSourceWrapperByIndex.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Terminal.Gui {
  4. /// <summary>
  5. /// Implementation of <see cref="CheckBoxTableSourceWrapperBase"/> which records toggled rows
  6. /// by their row number.
  7. /// </summary>
  8. public class CheckBoxTableSourceWrapperByIndex : CheckBoxTableSourceWrapperBase {
  9. /// <inheritdoc/>
  10. public CheckBoxTableSourceWrapperByIndex (TableView tableView, ITableSource toWrap) : base (tableView, toWrap)
  11. {
  12. }
  13. /// <summary>
  14. /// Gets the collection of all the checked rows in the
  15. /// <see cref="CheckBoxTableSourceWrapperBase.Wrapping"/> <see cref="ITableSource"/>.
  16. /// </summary>
  17. public HashSet<int> CheckedRows { get; private set; } = new HashSet<int> ();
  18. /// <inheritdoc/>
  19. protected override bool IsChecked (int row)
  20. {
  21. return CheckedRows.Contains (row);
  22. }
  23. /// <inheritdoc/>
  24. protected override void ToggleRows (int [] range)
  25. {
  26. // if all are ticked untick them
  27. if (range.All (CheckedRows.Contains)) {
  28. // select none
  29. foreach (var r in range) {
  30. CheckedRows.Remove (r);
  31. }
  32. } else {
  33. // otherwise tick all
  34. foreach (var r in range) {
  35. CheckedRows.Add (r);
  36. }
  37. }
  38. }
  39. /// <inheritdoc/>
  40. protected override void ToggleRow (int row)
  41. {
  42. if (CheckedRows.Contains (row)) {
  43. CheckedRows.Remove (row);
  44. } else {
  45. CheckedRows.Add (row);
  46. }
  47. }
  48. /// <inheritdoc/>
  49. protected override void ToggleAllRows ()
  50. {
  51. if (CheckedRows.Count == Rows) {
  52. // select none
  53. ClearAllToggles ();
  54. } else {
  55. // select all
  56. CheckedRows = new HashSet<int> (Enumerable.Range (0, Rows));
  57. }
  58. }
  59. /// <inheritdoc/>
  60. protected override void ClearAllToggles ()
  61. {
  62. CheckedRows.Clear ();
  63. }
  64. }
  65. }