JobDocumentSetMileStoneConverter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Windows;
  6. using System.Windows.Data;
  7. using Comal.Classes;
  8. using InABox.Core;
  9. using InABox.WPF;
  10. namespace PRSDesktop
  11. {
  12. public class JobDocumentSetMileStoneConverter : IValueConverter
  13. {
  14. public static Dictionary<JobDocumentSetMileStoneStatus, System.Drawing.Color> StatusColors = new Dictionary<JobDocumentSetMileStoneStatus, Color>()
  15. {
  16. { JobDocumentSetMileStoneStatus.Unknown, System.Drawing.Color.Red },
  17. { JobDocumentSetMileStoneStatus.NotStarted, System.Drawing.Color.LightSalmon },
  18. { JobDocumentSetMileStoneStatus.InProgress, System.Drawing.Color.LightYellow },
  19. { JobDocumentSetMileStoneStatus.OnHold, System.Drawing.Color.Silver },
  20. { JobDocumentSetMileStoneStatus.InfoRequired, System.Drawing.Color.Silver },
  21. { JobDocumentSetMileStoneStatus.Submitted, System.Drawing.Color.Plum },
  22. { JobDocumentSetMileStoneStatus.Approved, System.Drawing.Color.LightGreen },
  23. { JobDocumentSetMileStoneStatus.Rejected, System.Drawing.Color.Gray },
  24. { JobDocumentSetMileStoneStatus.Cancelled, System.Drawing.Color.Gray },
  25. };
  26. public object Convert(object value, Type t, object parameter, CultureInfo culture)
  27. {
  28. try
  29. {
  30. switch (parameter)
  31. {
  32. case "ID":
  33. if (ValidJson(value))
  34. return GetBlock(value).ID;
  35. else
  36. return Guid.Empty;
  37. case "Revision":
  38. if (ValidJson(value))
  39. return String.IsNullOrWhiteSpace(GetBlock(value).Revision)
  40. ? "--"
  41. : String.Format("Rev {0}", GetBlock(value).Revision);
  42. else
  43. return "";
  44. case "Status":
  45. if (ValidJson(value))
  46. return GetBlock(value).Status.ToString().SplitCamelCase();
  47. else
  48. return "";
  49. case "Color":
  50. if (ValidJson(value))
  51. return ImageUtils.ColorToString(StatusColors[GetBlock(value).Status]);
  52. else
  53. return ImageUtils.ColorToString(Color.White);
  54. case "Date":
  55. if (ValidJson(value))
  56. return String.Format("{0:dd MMM yy}", GetBlock(value).Date);
  57. else
  58. return "";
  59. case "Notes":
  60. if (ValidJson(value))
  61. return GetBlock(value).Notes;
  62. else
  63. return "";
  64. case "NotesColor":
  65. if (ValidJson(value))
  66. return String.IsNullOrWhiteSpace(GetBlock(value).Notes)
  67. ? ImageUtils.ColorToString(Color.Transparent)
  68. : ImageUtils.ColorToString(Color.Orchid);
  69. else
  70. return ImageUtils.ColorToString(Color.Transparent);
  71. case "Attachments":
  72. if (ValidJson(value))
  73. return String.Format("{0} files attached", GetBlock(value).Attachments);
  74. else
  75. return "";
  76. case "AttachmeentsColor":
  77. if (ValidJson(value))
  78. return GetBlock(value).Attachments == 0
  79. ? ImageUtils.ColorToString(Color.Transparent)
  80. : ImageUtils.ColorToString(Color.CornflowerBlue);
  81. else
  82. return ImageUtils.ColorToString(Color.Transparent);
  83. }
  84. return parameter.ToString();
  85. }
  86. catch (Exception e)
  87. {
  88. return e.Message;
  89. }
  90. }
  91. private static JobDocumentSetMileStoneBlock? GetBlock(object value)
  92. {
  93. return Serialization.Deserialize<JobDocumentSetMileStoneBlock>(value.ToString());
  94. }
  95. private bool ValidJson(object value)
  96. {
  97. if (value == null)
  98. return false;
  99. if ("".Equals(value))
  100. return false;
  101. else
  102. return true;
  103. }
  104. public object ConvertBack(object value, Type t, object parameter, CultureInfo culture)
  105. {
  106. return value.Equals(false) ? DependencyProperty.UnsetValue : parameter;
  107. }
  108. }
  109. }